#define_CRT_SECURE_NO_WARNINGS1#include<>#include""voidinit_board(chararr[ROW][COL]){inti=0;intj=0;for(i=0;i<ROW;i++){for(j=0;j<COL;j++){arr[i][j]='';}}}voiddisplay_board(chararr[ROW][COL]){inti=0;printf("____________\n");for(i=0;i<ROW;i++){printf("%c|%c|%c|\n",arr[i][0],arr[i][1],arr[i][2]);if(i<2){printf("---|---|---|");}printf("\n");}}voidplayer_chess(chararr[ROW][COL]){intx=0;inty=0;while(1){printf("请输入你所要下棋的方位:");scanf("%d%d",&x,&y);x--;y--;if(arr[x][y]==''){arr[x][y]='*';break;}else{printf("这个位置已经被占,请重新选择方位。\n");continue;}}}puter_chess(chararr[ROW][COL]){srand((unsigned)time(NULL));while(1){intx=0;inty=0;srand((unsigned)time(NULL));x=rand()%3;y=rand()%3;if(arr[x][y]==''){arr[x][y]='O';break;}else{if(check_full(arr)==0){continue;}else{break;}}}}intcheck_full(chararr[ROW][COL]){inti=0;intj=0;for(i=0;i<ROW;i++){for(j=0;j<COL;j++){if(arr[i][j]==''){return0;}}}return1;}charcheck_win(chararr[ROW][COL]){inti=0;intret=0;for(i=0;i<ROW;i++){if((arr[i][0]==arr[i][1])&&(arr[i][2]==arr[i][1])){returnarr[i][0];}}for(i=0;i<COL;i++){if((arr[0][i]==arr[1][i])&&(arr[2][i]==arr[1][i])){returnarr[0][i];}}if((arr[0][0]==arr[1][1])&&(arr[1][1]==arr[2][2])){returnarr[0][0];}if((arr[0][2]==arr[1][1])&&(arr[1][1]==arr[2][0])){returnarr[2][0];}ret=check_full(arr);if(ret==1){return'q';}return'';}建立testc源文件#define_CRT_SECURE_NO_WARNINGS1#include<>#include<>#include<>#include""intmain(){chararr[ROW][COL]={0};intinput=1;while(input){printf("请选择是否开始游戏:\n");printf("\n");printf("\n");scanf("%d",&input);switch(input){case1:{charret=0;init_board(arr);do{display_board(arr);player_chess(arr);display_board(arr);if(check_win(arr)!=''){ret=check_win(arr);break;}computer_chess(arr);ret=check_win(arr);}while(ret=='');
三子棋小游戏 来自淘豆网m.daumloan.com转载请标明出处.