/* ma_ba.c Function: 人間VS人間の○×ゲーム Author: Ken Date: 02/08/03 URL: http://blue.ribbon.to/~kenweb/ mail: kenweb@s8.xrea.com */ #include <stdio.h> void print_bord(int box[3][3]); int main() { int bord[3][3]={ /*変数宣言・初期設定*/ {0,0,0}, {0,0,0}, {0,0,0}, }; int tate,yoko,kakunin=0,turn,turn0,a,b,p,k=0; char moji[3],maru[3]="○",batu[3]="×"; fprintf(stdout,"○×ゲーム\n\n"); print_bord(bord); for (turn=1;turn<=1000;turn++) { if (turn%2==1) {turn0=1; for(p=0;p<=1;p++) moji[p]=maru[p];} /*ターン変更*/ else if (turn%2==0) {turn0=2; for(p=0;p<=1;p++) moji[p]=batu[p];} while(1){ kakunin=0; fprintf(stdout,"%sを置く縦の列のキーを入力してください(1-3)\n",moji); /*○×の配置*/ fscanf(stdin,"%d",&tate); fprintf(stdout,"%sを置く横の列のキーを入力してください(1-3)\n",moji); fscanf(stdin,"%d",&yoko); if (((yoko>=1)&&(3>=yoko))&&((tate>=1)&&(3>=tate))){ while((kakunin<1)||(kakunin>2)){ fprintf(stdout,"縦%d番目、横%d番目に%sを置きますか?(yes:1 no:2)\n",tate,yoko,moji); fscanf(stdin,"%d",&kakunin); } if ((kakunin==1)&&(bord[tate-1][yoko-1]==0)) break; if ((kakunin==1)&&((bord[tate-1][yoko-1]==1)||(bord[tate-1][yoko-1]==2))) { system("cls") ; print_bord(bord); fprintf(stderr,"そこには既に置かれています\n"); } else if (kakunin==2) { system("cls") ; print_bord(bord); } } else if (((yoko<1)||(3<yoko))||((tate<1)||(3<tate))) { system("cls") ; print_bord(bord); fprintf(stderr,"そこにはマスがありません\n"); } } system("cls") ; bord[tate-1][yoko-1]=turn0; print_bord(bord); /*盤面出力*/ k=0; /*試合終了時の判定*/ for(a=0;a<=2;a++) if ((bord[a][0]==turn0)&&(bord[a][1]==turn0)&&(bord[a][2]==turn0)) { fprintf (stdout,"%sの勝ち\n",moji); exit(0); } for(b=0;b<=2;b++) if ((bord[0][b]==turn0)&&(bord[1][b]==turn0)&&(bord[2][b]==turn0)) { fprintf (stdout,"%sの勝ち\n",moji); exit(0); } if ((bord[0][0]==turn0)&&(bord[1][1]==turn0)&&(bord[2][2]==turn0)) { fprintf (stdout,"%sの勝ち\n",moji); exit(0); } if ((bord[0][2]==turn0)&&(bord[1][1]==turn0)&&(bord[2][0]==turn0)) { fprintf (stdout,"%sの勝ち\n",moji); exit(0); } for (a=0;a<=2;a++) { for(b=0;b<=2;b++) if(bord[a][b]==0) k++; } if (k==0) { fprintf (stdout,"引き分け\n"); exit(0); } } } void print_bord(int box[3][3]) /*盤面表示設定*/ { int a,b; fprintf(stdout," 123\n"); for (a=0;a<=2;a++) { fprintf(stdout,"%d ",a+1); for (b=0;b<=2;b++) if (box[a][b]==0) fprintf(stdout,"−"); else if (box[a][b]==1) fprintf(stdout,"○"); else if (box[a][b]==2) fprintf(stdout,"×"); fprintf(stdout,"\n"); } }