求教!!! 关于三连棋游戏编程问题

书帆 2013-10-19 05:31:03
以下是我的code, 我的intermove 不能正确工作,求大神指教, 在intermove function中, 我的for loop 中的gamecheck 不能正确的测试我board1和board2, 但是当我加入printboard(board1or2)时他会打印出我想要的board。 求帮助!万分感谢!




















void playGame(int first,int xlevel,int olevel){
int board[9];
int i;
for (i=0;i<9;i++) board[i]=-1; /*initialize the tic table */
printBoard(board);
while (gameCheck(board)==-1){
if (first==1){printf("X's turn\n");
if (xlevel==0){noviceMove(board,1);}
else if (xlevel==1){interMove(board,1);}
else if (xlevel==2){advMove(board,1);}
else if (xlevel==3){humanMove(board,1);}
first=0;
}
else {printf("O's turn\n");
if (olevel==0){noviceMove(board,0);}
else if (olevel==1){interMove(board,0);}
else if (olevel==2){advMove(board,0);}
else if (olevel==3){humanMove(board,0);}
first=1;
}
printBoard(board);
}
if (gameCheck(board)==1){printf("X win!");}
else if (gameCheck(board)==0){printf("O win!");}
else {printf("TIE!");}
}
int gameCheck(const int board[]){
int R=2;/*assume this game do not have any winer if R will not change*/
int i;
if ((board[0]==board[4]&&board[4]==board[8])||(board[2]==board[4]&&board[4]==board[6])||(board[1]==board[4]&&board[4]==board[7])||(board[3]==board[4]&&board[4]==board[5])) {R=board[4];}
/*check all lines have no.4*/
else if ((board[0]==board[1]&&board[1]==board[2])||(board[0]==board[3]&&board[3]==board[6])){R=board[0];}
/*check all lines have no.0*/
else if ((board[8]==board[5]&&board[5]==board[2])||(board[8]==board[7]&&board[7]==board[6])) {R=board[8];}
/*check all lines have no.8*/
if (R==2){for(i=0;i<9;i++){if (board[i]==-1){R=-1;break;} }/*if the board have any -1, and there is no winer,the game is not over set returen value to -1*/
}
return R;
}
void noviceMove(int board[],int player){
int i;
i=ranf_next(9);/* get a random number*/
while (board[i]!=-1){
i=ranf_next(9);}
board[i]=player;/*check if this position empty*/
}
void interMove(int board[],int player){
int i,k=0,g=0;
int board1[9];
int board2[9];
for (k=0;k<9;k++){for (i=0;i<9;i++){board1[i]=board[i];} /* reset test1 in every loop*/
board1[k]=player;
if (gameCheck(board1)==player){
break;
}
}/* try to find a position that can win this game*/
for (g=0;g<9;g++){for (i=0;i<9;i++){board2[i]=board[i];} /* reset test1 in every loop*/
board2[g]=1-player;
if (gameCheck(board2)==(1-player)){
break;
}

}/*try to find a position that can block others*/
if (gameCheck(board1)==player) {board[k]=player;}/*check if this move can win the game, do it*/
else if (gameCheck(board2)==(1-player)) {board[g]=player;}/*check if this move can block*/
else {noviceMove(board,player);}
}
void advMove(int board[],int player){
int i;
int k=1;
for (i=0;i<9;i++){if(board[i]==player){k=0;}}/*check if player is fist move*/

if (k){if(board[4]==-1){board[4]=player;}
else board[2]=player;}

else interMove(board,player);

}
void humanMove(int board[],int player){
int i;
printf("Your move?");
scanf("%d",&i);
while (board[i]!=-1){
printf("Your move?");
scanf("%d",&i);
}
board[i]=player;
}
void printBoard(const int board[]){
int i;
for (i=0;i<2;i++){
if (board[i]==-1){printf("-|");}
else if (board[i]==0){printf("O|");}
else if (board[i]==1){printf("X|");}
}
if (board[2]==-1){printf("-\n----------\n");}
else if (board[2]==0){printf("O\n----------\n");}
else if (board[2]==1){printf("X\n----------\n");}

for (i=3;i<5;i++){
if (board[i]==-1){printf("-|");}
else if (board[i]==0){printf("O|");}
else if (board[i]==1){printf("X|");}
}
if (board[5]==-1){printf("-\n----------\n");}
else if (board[5]==0){printf("O\n----------\n");}
else if (board[5]==1){printf("X\n----------\n");}
for (i=6;i<8;i++){
if (board[i]==-1){printf("-|");}
else if (board[i]==0){printf("O|");}
else if (board[i]==1){printf("X|");}
}
if (board[8]==-1){printf("-\n");}
else if (board[8]==0){printf("O\n");}
else if (board[8]==1){printf("X\n");}
...全文
93 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
做或不做 2013-10-21
  • 打赏
  • 举报
回复
你只给出程序的一个处理部分 注释是英语写的 没人知道你游戏的规则是什么 估计没人会回答你这个问题 因为它既复杂 又不是太好解决 只能说明你描述问题的方式 和准确程度不是很好

69,372

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧