简单易懂的可使用dev编译的c++五子棋程序

这是一个小白编写的简单易懂的五子棋程序

本代码采用了二维数组的虚拟棋盘与if条件输出的完美结合,大佬们一眼就可以看出来
欢迎喷子与大佬指点,你们的使我进步
上代码:

#include 
#include 
#include 
using namespace std;
int main(){
	while(true){
		//打印棋盘 
		int n = 10;
		long long s;
		cout << "输入您的验证码加enter开始." << endl; 
		cin >> s;
		int arr[10][10];
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				arr[i][j] = 0;
			}
		}
		bool setin = true;//至关重要!!!! 
		if(s == 10086){
			int winner;
			//永久运行
			bool win = true;
			while(true){
				cout << "    ";
			for(int i = 0 ; i < n; i++){
				cout << i + 1 << " ";
			}
			cout << endl;
			cout << endl;
			for(int i = 0; i < n; i++){//正式打印 
				if(i >= 9){
					cout << i + 1 << "  ";
				}else{
					cout << i + 1 << "   ";
				}
				for(int j = 0; j < n; j++){
					if(arr[i][j] == 0){
						cout << "O" << " ";
					}else if(arr[i][j] == 1){
						cout << "*" << " ";
					}else{
						cout << "@" << " ";
					}
				}
				cout << endl;
			}
			//最最最最最重要的部分!!!!!!!!! 
			//判断胜负
			int winner;//!!!
			for(int i = 0; i < n; i++){
				for(int j = 0; j < n; j++){
					if(arr[i][j] == 1){
						if(arr[i + 1][j] == 1 && arr[i + 2][j] == 1 && arr[i + 3][j] == 1 && arr[i + 4][j] == 1){
							win = false;
							winner = 1;
							break;
						}else if(arr[i][j + 1] == 1 && arr[i][j + 2] == 1 && arr[i][j + 3] == 1 && arr[i][j + 4] == 1){
							win = false;
							winner = 1;
							break;
						}else if(arr[i + 1][j + 1] == 1 && arr[i + 2][j + 2] == 1 && arr[i + 3][j + 3] == 1 && arr[i + 4][j + 4] == 1){
							win = false;
							winner = 1;
							break;
						}else if(arr[i - 1][j - 1] == 1 && arr[i - 2][j - 2] == 1 && arr[i - 3][j - 3] == 1 && arr[i - 4][j - 4] == 1){
							win = false;
							winner = 1;
							break;
						}
					}
					if(arr[i][j] == 2){
						if(arr[i + 1][j] == 2 && arr[i + 2][j] == 2 && arr[i + 3][j] == 2 && arr[i + 4][j] == 2){
							win = false;
							winner = 2;
							break;
						}else if(arr[i][j + 1] == 2 && arr[i][j + 2] == 2 && arr[i][j + 3] == 2 && arr[i][j + 4] == 2){
							win = false;
							winner = 2;
							break;
						}else if(arr[i + 1][j + 1] == 2 && arr[i + 2][j + 2] == 2 && arr[i + 3][j + 3] == 2 && arr[i + 4][j + 4] == 2){
							win = false;
							winner = 2;
							break;
						}else if(arr[i - 1][j - 1] == 2 && arr[i - 2][j - 2] == 2 && arr[i - 3][j - 3] == 2 && arr[i - 4][j - 4] == 2){
							win = false;
							winner = 2;
							break;
						}
					}
				}
			}
			//请输入合法坐标号
			//_____________________________________________________________
			//分界线 
			//_____________________________________________________________ 
			if(setin){
				cout << "*下棋" << endl;
			}else{
				cout << "@下棋" << endl;
			}
			cout << "请输入合法坐标号,否则爆栈处罚";
			int n1, m1;
			cin >> n1 >> m1;
			if(arr[n1][m1] != 0){
				cout << "?---试图吞子? 没门!";
			}
			if(setin == true && arr[n1][m1] == 0){
				arr[n1 - 1][m1 - 1] = 1;
			}else{
				arr[n1 - 1][m1 - 1] = 2;
			}
			for(int i = 0; i < n; i++){
				for(int j = 0; j < n; j++){
					if(arr[i][j] == 1){
						if(arr[i + 1][j] == 1 && arr[i + 2][j] == 1 && arr[i + 3][j] == 1 && arr[i + 4][j] == 1){
							win = false;
							winner = 1;
							break;
						}else if(arr[i][j + 1] == 1 && arr[i][j + 2] == 1 && arr[i][j + 3] == 1 && arr[i][j + 4] == 1){
							win = false;
							winner = 1;
							break;
						}else if(arr[i + 1][j + 1] == 1 && arr[i + 2][j + 2] == 1 && arr[i + 3][j + 3] == 1 && arr[i + 4][j + 4] == 1){
							win = false;
							winner = 1;
							break;
						}else if(arr[i - 1][j - 1] == 1 && arr[i - 2][j - 2] == 1 && arr[i - 3][j - 3] == 1 && arr[i - 4][j - 4] == 1){
							win = false;
							winner = 1;
							break;
						}
					}
					if(arr[i][j] == 2){
						if(arr[i + 1][j] == 2 && arr[i + 2][j] == 2 && arr[i + 3][j] == 2 && arr[i + 4][j] == 2){
							win = false;
							winner = 2;
							break;
						}else if(arr[i][j + 1] == 2 && arr[i][j + 2] == 2 && arr[i][j + 3] == 2 && arr[i][j + 4] == 2){
							win = false;
							winner = 2;
							break;
						}else if(arr[i + 1][j + 1] == 2 && arr[i + 2][j + 2] == 2 && arr[i + 3][j + 3] == 2 && arr[i + 4][j + 4] == 2){
							win = false;
							winner = 2;
							break;
						}else if(arr[i - 1][j - 1] == 2 && arr[i - 2][j - 2] == 2 && arr[i - 3][j - 3] == 2 && arr[i - 4][j - 4] == 2){
							win = false;
							winner = 2;
							break;
						}
					}
				}
			}
			//换人下棋
			if(setin){
				setin = false; 
			}else{
				setin = true;
			}
			cout << "成功操作棋盘" << endl;
			if(win == false){
				break;
			}
			//视觉优化
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			cout << endl;
			}
			cout << "本局结束,";
			cout << "胜利者:";
			if(winner == 1){
				cout << "@";
			}else{
				cout << "*";
			}
			cout << "  若要再来一局,请打S并enter" << endl;
		}else{
			cout << "你是怎么得到我的?" << endl;
			
		}
	}
	return 0;
}
/*喷子使我进步

喜欢的话,请多多为我点赞吧

你可能感兴趣的:(c++,小程序,小游戏)