欢迎来到天天文库
浏览记录
ID:11943743
大小:78.50 KB
页数:0页
时间:2018-07-15
《c语言编写的井字棋》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、井字旗C语言程序:运行环境:TurboC/C++forWindows集成实验与学习环境或VC++6.0#defineMAX3#defineStatusint#defineHUMAN_WIN0//人取得了胜利#defineDRAW1//平局#definePLAYING2//没有决出胜负,正在进行游戏#defineCOMPUTER_WIN3//电脑取得了胜利#defineHUMAN0//人#defineCOMPUTER1//机器#defineEMPTY2//空#defineFALSE0//假#defineTRUE1//真#include#
2、include"malloc.h"//记录一步棋所需的所有信息:行数,列数,判断值typedefstruct{intcolumn;introw;intval;}Nodes;intboard[MAX][MAX];//InitBoard初始化棋盘StatusInitBoard(){introw,column;for(row=0;row3、tatusPositionIsEmpty(introw,intcolumn){if(board[row][column]==2)returnTRUE;elsereturnFALSE;}//Place在指定的地方落子StatusPlace(introw,intcolumn,intpiece){board[row][column]=piece;returnTRUE;}//BoardIsFull判断棋盘是否己满StatusBoardIsFull(){inti=0,j=0;for(i=0;i4、d[i][j]==2)returnFALSE;}returnTRUE;}//IsWin判断是否有一方己经胜利StatusIsWin(intside){introw,column;//判断一行for(row=0;row=MAX)returnTRUE;}//判断一列for(column=0;column5、f(board[row][column]!=side)break;if(row>=MAX)returnTRUE;}//判断主对角线if(board[1][1]==side&&board[2][2]==side&&board[0][0]==side)returnTRUE;//判断副对角线if(board[0][2]==side&&board[1][1]==side&&board[2][0]==side)returnTRUE;returnFALSE;}//PositonValue返回落子后的状态有四种状态在ConstNum.h中定义COMPUTER_WIN6、,HUMAN_WIN,DRAW,PLAYINGStatusPostionValue(){returnIsWin(COMPUTER)?COMPUTER_WIN:(IsWin(HUMAN)?HUMAN_WIN:(BoardIsFull()?DRAW:PLAYING));}//BestMovement判断最佳落子位置,采用递归,求出最佳位置NodesBestMovement(intside){intopp;//对手Nodesnodes,node2;//nodes记录当前最佳位置,node2返回最佳位置intsimpleEval;//记当中间结果intbest7、Row=0,row;intbestColumn=0,column;intvalue;//判断是否游戏己经结束if((simpleEval=PostionValue())!=PLAYING){node2.row=0;node2.column=0;node2.val=simpleEval;returnnode2;}if(side==COMPUTER){opp=HUMAN;value=HUMAN_WIN;}else{opp=COMPUTER;value=COMPUTER_WIN;}for(row=0;row8、lumn
3、tatusPositionIsEmpty(introw,intcolumn){if(board[row][column]==2)returnTRUE;elsereturnFALSE;}//Place在指定的地方落子StatusPlace(introw,intcolumn,intpiece){board[row][column]=piece;returnTRUE;}//BoardIsFull判断棋盘是否己满StatusBoardIsFull(){inti=0,j=0;for(i=0;i4、d[i][j]==2)returnFALSE;}returnTRUE;}//IsWin判断是否有一方己经胜利StatusIsWin(intside){introw,column;//判断一行for(row=0;row=MAX)returnTRUE;}//判断一列for(column=0;column5、f(board[row][column]!=side)break;if(row>=MAX)returnTRUE;}//判断主对角线if(board[1][1]==side&&board[2][2]==side&&board[0][0]==side)returnTRUE;//判断副对角线if(board[0][2]==side&&board[1][1]==side&&board[2][0]==side)returnTRUE;returnFALSE;}//PositonValue返回落子后的状态有四种状态在ConstNum.h中定义COMPUTER_WIN6、,HUMAN_WIN,DRAW,PLAYINGStatusPostionValue(){returnIsWin(COMPUTER)?COMPUTER_WIN:(IsWin(HUMAN)?HUMAN_WIN:(BoardIsFull()?DRAW:PLAYING));}//BestMovement判断最佳落子位置,采用递归,求出最佳位置NodesBestMovement(intside){intopp;//对手Nodesnodes,node2;//nodes记录当前最佳位置,node2返回最佳位置intsimpleEval;//记当中间结果intbest7、Row=0,row;intbestColumn=0,column;intvalue;//判断是否游戏己经结束if((simpleEval=PostionValue())!=PLAYING){node2.row=0;node2.column=0;node2.val=simpleEval;returnnode2;}if(side==COMPUTER){opp=HUMAN;value=HUMAN_WIN;}else{opp=COMPUTER;value=COMPUTER_WIN;}for(row=0;row8、lumn
4、d[i][j]==2)returnFALSE;}returnTRUE;}//IsWin判断是否有一方己经胜利StatusIsWin(intside){introw,column;//判断一行for(row=0;row=MAX)returnTRUE;}//判断一列for(column=0;column5、f(board[row][column]!=side)break;if(row>=MAX)returnTRUE;}//判断主对角线if(board[1][1]==side&&board[2][2]==side&&board[0][0]==side)returnTRUE;//判断副对角线if(board[0][2]==side&&board[1][1]==side&&board[2][0]==side)returnTRUE;returnFALSE;}//PositonValue返回落子后的状态有四种状态在ConstNum.h中定义COMPUTER_WIN6、,HUMAN_WIN,DRAW,PLAYINGStatusPostionValue(){returnIsWin(COMPUTER)?COMPUTER_WIN:(IsWin(HUMAN)?HUMAN_WIN:(BoardIsFull()?DRAW:PLAYING));}//BestMovement判断最佳落子位置,采用递归,求出最佳位置NodesBestMovement(intside){intopp;//对手Nodesnodes,node2;//nodes记录当前最佳位置,node2返回最佳位置intsimpleEval;//记当中间结果intbest7、Row=0,row;intbestColumn=0,column;intvalue;//判断是否游戏己经结束if((simpleEval=PostionValue())!=PLAYING){node2.row=0;node2.column=0;node2.val=simpleEval;returnnode2;}if(side==COMPUTER){opp=HUMAN;value=HUMAN_WIN;}else{opp=COMPUTER;value=COMPUTER_WIN;}for(row=0;row8、lumn
5、f(board[row][column]!=side)break;if(row>=MAX)returnTRUE;}//判断主对角线if(board[1][1]==side&&board[2][2]==side&&board[0][0]==side)returnTRUE;//判断副对角线if(board[0][2]==side&&board[1][1]==side&&board[2][0]==side)returnTRUE;returnFALSE;}//PositonValue返回落子后的状态有四种状态在ConstNum.h中定义COMPUTER_WIN
6、,HUMAN_WIN,DRAW,PLAYINGStatusPostionValue(){returnIsWin(COMPUTER)?COMPUTER_WIN:(IsWin(HUMAN)?HUMAN_WIN:(BoardIsFull()?DRAW:PLAYING));}//BestMovement判断最佳落子位置,采用递归,求出最佳位置NodesBestMovement(intside){intopp;//对手Nodesnodes,node2;//nodes记录当前最佳位置,node2返回最佳位置intsimpleEval;//记当中间结果intbest
7、Row=0,row;intbestColumn=0,column;intvalue;//判断是否游戏己经结束if((simpleEval=PostionValue())!=PLAYING){node2.row=0;node2.column=0;node2.val=simpleEval;returnnode2;}if(side==COMPUTER){opp=HUMAN;value=HUMAN_WIN;}else{opp=COMPUTER;value=COMPUTER_WIN;}for(row=0;row8、lumn
8、lumn
此文档下载收益归作者所有