c++三子棋游戏求大佬加上推演环节
  • 板块灌水区
  • 楼主ywtank
  • 当前回复7
  • 已保存回复7
  • 发布时间2024/8/27 15:16
  • 上次更新2024/8/27 17:25:02
查看原帖
c++三子棋游戏求大佬加上推演环节
1070431
ywtank楼主2024/8/27 15:16
#include <bits/stdc++.h>
using namespace std;
int a[10][10];
//1玩家获胜2电脑获胜3棋子已满4接着下! 
int p(){
	bool flag=false;
	for(int i=1;i<=3;i++){
		for(int j=1;j<=3;j++){
			if(a[i][j]==0){
				flag=true;
				continue;
			}
			if(a[i][j]==1 && a[i+1][j]==1 && a[i+2][j]==1){
				return 1;
			}
			if(a[i][j]==1 && a[i][j+1]==1 && a[i][j+2]==1){
				return 1;
			}
			if(a[i][j]==1 && a[i+1][j+1]==1 && a[i+2][j+2]==1)return 1;
			if(a[i][j]==2 && a[i+1][j]==2 && a[i+2][j]==2){
				return 2;
			}
			if(a[i][j]==2 && a[i][j+1]==2 && a[i][j+2]==2){
				return 2;
			}
			if(a[i][j]==2 && a[i+1][j+1]==2 && a[i+2][j+2]==2)return 2;
		}
	}
	if(!flag){
		return 3;
	}
	return 4;
}
//int dfs(int x,int y,int z){
//	int l=p();
//	if(l==1)return -1;
//	if(l==2)return 10;
//	if(i==3)return 0;
//	if(z==0){
//		int z=INT_MAX;
//		for(int i=1;i<=3;i++){
//			for(int j=1;j<=3;j++){
//				if(a[i][j]==0){
//					a[i][j]=1;
//					int h=INT_MIN;
//					for(int i=1;i<=3;i++){
//						for(int j=1;j<=3;j++){
//							if(a[i][j]==0){
//								h=max(dfs(i,j,0),h);
//							}
//						}		
//					}
//					z=
//				}
//			}
//		}
//	}
//	for(int i=1;i<=3;i++){
//		for(int j=1;j<=3;j++){
//			if(a[i][j]==0)
//		}
//	}
//}
int main(){
	cout << "欢迎来到三子棋游戏\n";
	system("pause");
	system("cls");
	cout << ".表示空格,X表示你的棋子,O表示我的\n";
	system("pause");
	system("cls");
	int i=0;
	while(true){
		int z=p();
		if(z==1){
			cout << "你赢了";
			return 0;
		}else if(z==2){
			cout << "你输了\n";
			for(int i=1;i<=3;i++){
						for(int j=1;j<=3;j++){
							if(a[i][j]==0)cout << '.';
							if(a[i][j]==1)cout << 'X';//1玩家棋子 
							if(a[i][j]==2)cout << 'O';//2电脑棋子 
						}
						cout << '\n';
					}
			return 0;
		}
		else if(z==3){
			cout << "平局";
			return 0; 
		}
		for(int i=1;i<=3;i++){
			for(int j=1;j<=3;j++){
				if(a[i][j]==0)cout << '.';
				if(a[i][j]==1)cout << 'X';//1玩家棋子 
				if(a[i][j]==2)cout << 'O';//2电脑棋子 
			}
			cout << '\n';
		}
		if((i&1)==0){
			cout << "你要在哪里落子 格式:行 列:";
			int x,y;
			F:
			cin >> x >> y;
			if(a[x][y] || x<=0 || x>3 || y<=0 || y>3){
				cout << "请重新输入:";
				goto F;
			}
			a[x][y]=1;
		}else{
			int f,g;
			random_device rd;
			srand(rd());
			C:
			f=rand()%3+1;
			g=rand()%3+1;
			if(a[f][g]!=0)goto C;
			a[f][g]=2;
		}
		i++;
		system("pause");
		system("cls");
	}
	return 0;
} 
2024/8/27 15:16
加载中...