您的位置:首页 > 财经 > 产业 > C++五子棋(未做完,但能玩,而且还不错)

C++五子棋(未做完,但能玩,而且还不错)

2024/12/23 4:47:29 来源:https://blog.csdn.net/LiXinLong_LXL_13/article/details/140648019  浏览:    关键词:C++五子棋(未做完,但能玩,而且还不错)

代码放下面了,关于步骤介绍的我以后再完善一下。

#include<bits/stdc++.h>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
#define random(x) (rand()%x)
using namespace std;
//--------------------------------五子棋------------------------------------//
//player_1:o
//player_2:x
int Game_mode;
char qizi[25][25];
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
//主要模块
void print_checkerboard(){SetConsoleTextAttribute(handle,  FOREGROUND_GREEN | FOREGROUND_INTENSITY);cout<<"   ";for(int i=1;i<=20;i++){cout<<i<<" ";if(i<10) cout<<" ";}cout<<endl;for(int i=1;i<=20;i++){if(i<10) cout<<" ";cout<<i<<" ";for(int j=1;j<=20;j++){if(qizi[i][j]=='o'){SetConsoleTextAttribute(handle,  FOREGROUND_BLUE | FOREGROUND_INTENSITY);cout<<qizi[i][j]<<"  ";SetConsoleTextAttribute(handle,  FOREGROUND_GREEN | FOREGROUND_INTENSITY);}else if(qizi[i][j]=='x'){SetConsoleTextAttribute(handle,  FOREGROUND_RED | FOREGROUND_INTENSITY);cout<<qizi[i][j]<<"  ";SetConsoleTextAttribute(handle,  FOREGROUND_GREEN | FOREGROUND_INTENSITY);}else{SetConsoleTextAttribute(handle,  FOREGROUND_GREEN | FOREGROUND_INTENSITY);cout<<qizi[i][j]<<"  ";}}cout<<endl;}
}
bool inspect_1(){for(int i=1;i<=20;i++){for(int j=1;j<=20;j++){if(qizi[i][j]=='o'){if((qizi[i+1][j]=='o'&&qizi[i+2][j]=='o'&&qizi[i+3][j]=='o'&&qizi[i+4][j]=='o') or(qizi[i-1][j]=='o'&&qizi[i-2][j]=='o'&&qizi[i-3][j]=='o'&&qizi[i-4][j]=='o') or(qizi[i][j+1]=='o'&&qizi[i][j+2]=='o'&&qizi[i][j+3]=='o'&&qizi[i][j+4]=='o') or(qizi[i][j-1]=='o'&&qizi[i][j-2]=='o'&&qizi[i][j-3]=='o'&&qizi[i][j-4]=='o') or(qizi[i+1][j+1]=='o'&&qizi[i+2][j+2]=='o'&&qizi[i+3][j+3]=='o'&&qizi[i+4][j+4]=='o') or(qizi[i+1][j-1]=='o'&&qizi[i+2][j-2]=='o'&&qizi[i+3][j-3]=='o'&&qizi[i+4][j-4]=='o') or(qizi[i-1][j+1]=='o'&&qizi[i-2][j+2]=='o'&&qizi[i-3][j+3]=='o'&&qizi[i-4][j+4]=='o') or(qizi[i-1][j-1]=='o'&&qizi[i-2][j-2]=='o'&&qizi[i-3][j-3]=='o'&&qizi[i-4][j-4]=='o') ){return true;}}}}return false;
}bool inspect_2(){for(int i=1;i<=20;i++){for(int j=1;j<=20;j++){if(qizi[i][j]=='x'){if((qizi[i+1][j]=='x'&&qizi[i+2][j]=='x'&&qizi[i+3][j]=='x'&&qizi[i+4][j]=='x') or(qizi[i-1][j]=='x'&&qizi[i-2][j]=='x'&&qizi[i-3][j]=='x'&&qizi[i-4][j]=='x') or(qizi[i][j+1]=='x'&&qizi[i][j+2]=='x'&&qizi[i][j+3]=='x'&&qizi[i][j+4]=='x') or(qizi[i][j-1]=='x'&&qizi[i][j-2]=='x'&&qizi[i][j-3]=='x'&&qizi[i][j-4]=='x') or(qizi[i+1][j+1]=='x'&&qizi[i+2][j+2]=='x'&&qizi[i+3][j+3]=='x'&&qizi[i+4][j+4]=='x') or(qizi[i+1][j-1]=='x'&&qizi[i+2][j-2]=='x'&&qizi[i+3][j-3]=='x'&&qizi[i+4][j-4]=='x') or(qizi[i-1][j+1]=='x'&&qizi[i-2][j+2]=='x'&&qizi[i-3][j+3]=='x'&&qizi[i-4][j+4]=='x') or(qizi[i-1][j-1]=='x'&&qizi[i-2][j-2]=='x'&&qizi[i-3][j-3]=='x'&&qizi[i-4][j-4]=='x') ){return true;}}}}return false;
}//电脑模块
void reflection(){system("cls");for(int i=1;i<=2;i++){print_checkerboard();cout<<"电脑正在思考中";for(int i=1;i<=3;i++){cout<<".";Sleep(500);}system("cls");}
}void lianzi_3_o(){for(int i=1;i<=20;i++){for(int j=1;j<=20;j++){if(qizi[i][j]=='o'){if(qizi[i+1][j]=='o' and qizi[i+2][j]=='o'){}}}}
}int main(){memset(qizi, 42 , sizeof(qizi));HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleTextAttribute(handle,  BACKGROUND_BLUE | BACKGROUND_GREEN);SetConsoleTextAttribute(handle,  FOREGROUND_BLUE );cout<<"请先确定游戏模式:"<<endl;cout<<"1.(玩家VS电脑)"<<endl;cout<<"2.(玩家VS玩家)"<<endl;SetConsoleTextAttribute(handle,  FOREGROUND_RED );cin>>Game_mode;system("cls");if(Game_mode==1){
//		print_checkerboard();
//		int cmp=0,player=0;
//		int player_x=0,player_y=0;
//		int cmp_x=0,cmp_y=0;
//		int F_PER=0,F_CMP=0;
//		srand(( int )time(0));
//        int order=random(2);
//		if(order==1){
//			player=1,cmp=0;
//			F_PER=1;
//			cout<<"你先下。。。"<<endl;
//			Sleep(3000);
//			system("cls");
//		}else{
//			player=0,cmp=1;
//			F_CMP=1;
//			cout<<"电脑先下。。。"<<endl;
//			Sleep(3000);
//			system("cls");
//		}
//		while(inspect_1()==false and inspect_2()==false){
//			if(player==1){
//				int a=0,b=0;
//				while(a>20 or b>20 or qizi[a][b]=='o'||qizi[a][b]=='x'){
//					a=0,b=0;
//					cin>>a>>b;
//					if(a>20 or b>20){
//						system("cls");
//						print_checkerboard();
//						SetConsoleTextAttribute(handle,  FOREGROUND_RED | FOREGROUND_INTENSITY);
//						cout<<"超出棋盘范围,请重新输入!"<<endl;
//						SetConsoleTextAttribute(handle,  FOREGROUND_GREEN | FOREGROUND_INTENSITY);
//					}
//					if(a<=20 and b<=20) {
//						if(qizi[a][b]=='o'||qizi[a][b]=='x'){
//							system("cls");
//							print_checkerboard();
//							SetConsoleTextAttribute(handle,  FOREGROUND_RED | FOREGROUND_INTENSITY);
//							cout<<"此坐标已有棋子,请重新输入!"<<endl;
//							SetConsoleTextAttribute(handle,  FOREGROUND_GREEN | FOREGROUND_INTENSITY);
//						}else{
//							player_x=a,player_y=b;
//							break;
//						}
//
//					}
//				}
//				if(F_PER==1){
//					qizi[player_x][player_y]='o';
//					system("cls");
//					print_checkerboard();
//					if(inspect_1()){
//						cout<<"哈哈,你赢了!"<<endl;
//						system("pause");
//					}
//					system("cls");
//				}else{
//					qizi[player_x][player_y]='x';
//					system("cls");
//					print_checkerboard();
//					if(inspect_2()){
//						cout<<"哈哈,你赢了!"<<endl;
//						system("pause");
//					}
//				}
//			}else{
//				int a=0,b=0;
//				print_checkerboard();
//				reflection();
//				
//			}
//		}}else if(Game_mode==2){bool player1=0,player2=0;int player1_x,player1_y;int player2_x,player2_y;print_checkerboard();while(inspect_1()==false and inspect_2()==false){if(player1==0) {int a=0,b=0;while(a>20 or b>20 or qizi[a][b]=='o'||qizi[a][b]=='x'){a=0,b=0;cin>>a>>b;if(a>20 or b>20){system("cls");print_checkerboard();SetConsoleTextAttribute(handle,  FOREGROUND_RED | FOREGROUND_INTENSITY);cout<<"超出棋盘范围,请重新输入!"<<endl;SetConsoleTextAttribute(handle,  FOREGROUND_GREEN | FOREGROUND_INTENSITY);		}if(a<=20 and b<=20) {if(qizi[a][b]=='o'||qizi[a][b]=='x'){system("cls");print_checkerboard();SetConsoleTextAttribute(handle,  FOREGROUND_RED | FOREGROUND_INTENSITY);cout<<"此坐标已有棋子,请重新输入!"<<endl;SetConsoleTextAttribute(handle,  FOREGROUND_GREEN | FOREGROUND_INTENSITY);	}else{player1_x=a,player1_y=b;break;}}}qizi[player1_x][player1_y]='o';system("cls");print_checkerboard();if(inspect_1()==true){cout<<"哈哈,玩家1,赢了!"<<endl;system("pause"); return 0; }player1=1;player2=0;}if(player2==0) {int a=0,b=0;while(a>20 or b>20 or qizi[a][b]=='o'or qizi[a][b]=='x'){a=0,b=0;cin>>a>>b;if(a>20 or b>20){system("cls");print_checkerboard();SetConsoleTextAttribute(handle,  FOREGROUND_RED);cout<<"超出棋盘范围,请重新输入!"<<endl;SetConsoleTextAttribute(handle,  FOREGROUND_GREEN);		}if(a<=20 and b<=20) {if(qizi[a][b]=='o'||qizi[a][b]=='x'){system("cls");print_checkerboard();SetConsoleTextAttribute(handle,  FOREGROUND_RED);cout<<"此坐标已有棋子,请重新输入!"<<endl;SetConsoleTextAttribute(handle,  FOREGROUND_GREEN);	}else{player2_x=a,player2_y=b;break;}}}qizi[player2_x][player2_y]='x';system("cls");print_checkerboard();if(inspect_2()==true){cout<<"哈哈,玩家2,赢了!"<<endl;system("pause"); return 0; }player1=0;player2=1;}}}else{cout<<"输入错误,结束。"<<endl;}return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com