TIC-TAC-TOE游戏-两层循环实现行与列的遍历(笔记整理)

#include
int main() {
const int size = 3;
int bost[size][size];
int numberO;
int numberX;
int result = -1;
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
scanf("%d", &bost[i][j]);
}
}
printf("--------------------\n");
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
if(j%3 == 0)
printf("\n");
printf("%d", bost[i][j]);
}
}
printf("\n");
if(result = -1) {
for(int i = 0; i < size; i++) {
numberX = numberO = 0;
for(int j = 0; j < size; j++) {
if(bost[i][j] == 1 || bost[j][i] == 1)
numberX++;
else if(bost[i][j] == 0 || bost[j][i] == 0)
numberO++;
}
}
if(numberX == size)
result = 1;
else
result = 0;
}
if(result = -1) {
numberO = numberX = 0;
for(int i = 0; i < size; i++) {
if(bost[i][i] == 1 || bost[i][size-i-1] == 1)
numberX++;
else if(bost[i][i] == 0 || bost[i][size-i-1] == 0)
numberO++;
}
if(numberX == size)
result = 1;
else
result = 0;
}
if(result == 1)
printf(“X赢”);
else if(result == 0)
printf(“O赢”);
else
printf(“平局”);
}

TIC-TAC-TOE游戏
1.读入一个3*3矩阵,数字1表明该位置上有一个X,0表示O
2.程序判断这个矩阵是否有获胜的一方,当对角线或者每行每列有三个连续的X或者O时,相应的一方获胜
(矩阵运算,利用循环对行和列进行遍历,还有对脚线的遍历)

你可能感兴趣的:(C学习笔记整理,c++)