tic tac toe (井子棋)
#include <stdio.h> int main() { int winer=-1; //-1(平局) 0(圈圈赢) 1(叉叉赢) const int size=3; //定义3*3的二维数组读入棋盘 int a[size][size]={0}; int i,t; printf("请输入棋盘(圈圈==0 叉叉==1):\n"); for(i=0; i<size; i++){ for(t=0; t<size; t++){ scanf("%d",&a[i][t]); } } //二维数组行、列同时遍历,a[i][t]浮标i,t交换 //遍历行时,浮标i定t动,遍历列时,浮标t定,i动 int sumrowoo,sumrowxx,sumlineoo,sumlinexx; //行圈圈,行叉叉,列圈圈,列叉叉 for(i=0; i<size && winer==-1; i++){ sumrowoo=sumrowxx=sumlineoo=sumlinexx=0; for(t=0; t<size; t++){ if( a[i][t]==0 ){ //列 sumrowoo++; }else{ sumrowxx++; } if( a[t][i]==0 ){ //行 sumlineoo++; }else{ sumlinexx++; } } if(sumrowoo == 3 || sumlineoo == 3){ winer=0; } if(sumrowxx == 3 || sumlinexx == 3){ winer=1; } } int sumoo,sumxx; if( winer == -1 ){ sumoo=sumxx=0; //遍历对角线 \00 11 22 for(i=0; i<size; i++){ if(a[i][i] == 0){ sumoo+=1; }else{ sumxx+=1; } } if(sumoo==3){ winer=0; } else if(sumxx==3){ winer=1; } } if( winer == -1 ){ sumoo=sumxx=0; //遍历对角线 \02 11 20 t=2; for(i=0; i<size;i++){ if(a[i][t--] == 0){ sumoo+=1; }else{ sumxx+=1; } } if(sumoo==3){ winer=0; } else if(sumxx==3){ winer=1; } } // 输出游戏结果 printf("你输入的棋盘为:\n"); printf(" ______________\n"); printf("| %d | %d | %d |\n",a[0][0],a[0][1],a[0][2]); printf(" ______________\n"); printf("| %d | %d | %d |\n",a[1][0],a[1][1],a[1][2]); printf(" ______________\n"); printf("| %d | %d | %d |\n",a[2][0],a[2][1],a[2][2]); if(winer == 0){ printf("圈圈赢\n"); } else if(winer == 1){ printf("叉叉赢\n"); } else{ printf("平局\n"); } return 0; }
编译