井字棋(最终版)
2026-09-25 12:44:27
发布于:天津
#include <bits/stdc++.h>
#define clear() printf("\033c")
#include <windows.h>
using namespace std;
//c是当前棋盘状态
char c[15][15] = {{' '},
{' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' '}};
string x, y;
void cou_t(){
cout << " 1 2 3" << endl;
cout << " +---+---+---+" << endl;
cout << "1 | " << c[1][1] << " | " << c[1][2] << " | " << c[1][3] << " |" << endl;
cout << " |---+---+---+" << endl;
cout << "2 | " << c[2][1] << " | " << c[2][2] << " | " << c[2][3] << " |" << endl;
cout << " |---+---+---+" << endl;
cout << "3 | " << c[3][1] << " | " << c[3][2] << " | " << c[3][3] << " |" << endl;
cout << " +---+---+---+" << endl;
//布置棋盘
}
bool i_f() {
for(int i = 1; i <= 3; i++) {
if(c[i][1] == 'o' && c[i][2] == 'o' && c[i][3] == 'o'
|| c[i][1] == 'x' && c[i][2] == 'x' && c[i][3] == 'x') return true;
}
//行
for(int i = 1; i <= 3; i++) {
if(c[1][i] == 'o' && c[2][i] == 'o' && c[3][i] == 'o'
|| c[1][i] == 'x' && c[2][i] == 'x' && c[3][i] == 'x') return true;
}
//列
if(c[1][1] == 'o' && c[2][2] == 'o' && c[3][3] == 'o') return true;
if(c[1][1] == 'x' && c[2][2] == 'x' && c[3][3] == 'x') return true;
if(c[1][3] == 'o' && c[2][2] == 'o' && c[3][1] == 'o') return true;
if(c[1][3] == 'x' && c[2][2] == 'x' && c[3][1] == 'x') return true;
//对角线
return false;
}
void ci_n1() {
cout << "1号玩家要下的位置:" << endl;
cout << "行:";
cin >> x;
if(x == "999") return ;
while(x != "1" && x != "2" && x != "3") {
clear();
cou_t();
cout << "1号玩家要下的位置:" << endl;
cout << "行:";
cin >> x;
if(x == "999") return ;
}
cout << "列:";
cin >> y;
if(y == "999") return ;
while(y != "1" && y != "2" && y != "3") {
clear();
cou_t();
cout << "1号玩家要下的位置:" << endl;
cout << "行:" << x << endl;
cout << "列:";
cin >> y;
if(y == "999") return ;
}
}
void ci_n2() {
cout << "2号玩家要下的位置:" << endl;
cout << "行:";
cin >> x;
if(x == "999") return ;
while(x != "1" && x != "2" && x != "3") {
clear();
cou_t();
cout << "2号玩家要下的位置:" << endl;
cout << "行:";
cin >> x;
if(x == "999") return ;
}
cout << "列:";
cin >> y;
if(y == "999") return ;
while(y != "1" && y != "2" && y != "3") {
clear();
cou_t();
cout << "2号玩家要下的位置:" << endl;
cout << "行:" << x << endl;
cout << "列:";
cin >> y;
if(y == "999") return ;
}
}
int main() {
cout << endl << endl << endl << endl << endl << endl << endl << endl << endl;
cout << " 井字棋(退出按999)";
Sleep(2000);
clear();
int round = 0;
while(round <= 9) {
clear();
cou_t();
round++;
while(true) {
clear();
cou_t();
if(round % 2 == 1) ci_n1();
else ci_n2();
if(x == "999" || y == "999") {
clear();
cout << endl << endl << endl << endl << endl << endl << endl << endl << endl;
cout << " 结束";
return 0;
}
int x1, y1;
x1 = stoi(x);
y1 = stoi(y);
if(c[x1][y1] == ' '){
if(round % 2 == 1) c[x1][y1] = 'x';
else c[x1][y1] = 'o';
break;
}
}
if (i_f() && round % 2 == 1){
clear();
cou_t();
cout << "恭喜1号玩家获胜";
break;
} else if (i_f()) {
clear();
cou_t();
cout << "恭喜2号玩家获胜";
break;
}
//判断胜负
if(round == 9) {
cout << "平局" << endl;
break;
}
}
return 0;
}
点个赞,谢谢
全部评论 2
有建议的欢迎发言
1周前 来自 天津
0作者编写,非AI
1周前 来自 天津
0





















有帮助,赞一个