游戏(DEV C++)
2026-05-01 14:49:12
发布于:天津
#include <bits/stdc++.h>
#define clear() printf("\033c")
#include <windows.h>
using namespace std;
char c[4][4];
int cnt;
void on() {
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= 3; j++) {
cout << c[i][j] << " ";
}
cout << endl;
}
}
bool check() {
for(int i = 1; i <= 3; i++) {
if(c[i][1] == 'o' && c[i][2] == 'o' && c[i][3] == 'o') return true;
}
for(int i = 1; i <= 3; i++) {
if(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') return true;
}
for(int i = 1; i <= 3; i++) {
if(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;
}
bool ss() {
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= 3; j++) {
if(c[i][j] == '-') return false;
}
}
return true;
}
int main() {
cnt = 1;
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= 3; j++) {
c[i][j] = '-';
}
}
while(true) {
int x, y;
clear();
if(cnt % 2 == 1) {
on();
cout << "1号玩家选择" << endl << "行:";
cin >> x;
cout << "列:";
cin >> y;
if(c[x][y] == '-') {
c[x][y] = 'o';
cnt++;
}
else {
clear();
cout << "选择无效" << endl;
Sleep(2000); // 暂停 2000 毫秒(即 2 秒)
continue;
}
clear();
if(check()) {
clear();
on();
cout << endl;
cout << "1号玩家胜利" << endl;
return 0;
}
} else {
on();
cout << "2号玩家选择" << endl << "行:";
cin >> x;
cout << "列:";
cin >> y;
cout << endl;
if(c[x][y] == '-') {
c[x][y] = 'x';
cnt++;
}
else {
clear();
cout << "选择无效" << endl;
Sleep(2000); // 暂停 2000 毫秒(即 2 秒)
}
clear();
if(check()) {
clear();
on();
cout << endl;
cout << "2号玩家胜利" << endl;
return 0;
}
}
if(ss()) {
cout << endl;
on();
cout << "平局!";
return 0;
}
}
return 0;
}
这里空空如也




















有帮助,赞一个