C语言编写的井字棋.docx
《C语言编写的井字棋.docx》由会员分享,可在线阅读,更多相关《C语言编写的井字棋.docx(23页珍藏版)》请在冰豆网上搜索。
C语言编写的井字棋
井字旗C语言程序:
运行环境:
TurboC/C++forWindows集成实验与学习环境或VC++6.0
#defineMAX3
#defineStatusint
#defineHUMAN_WIN0//人取得了胜利
#defineDRAW1//平局
#definePLAYING2//没有决出胜负,正在进行游戏
#defineCOMPUTER_WIN3//电脑取得了胜利
#defineHUMAN0//人
#defineCOMPUTER1//机器
#defineEMPTY2//空
#defineFALSE0//假
#defineTRUE1//真
#include
#include"malloc.h"
//记录一步棋所需的所有信息:
行数,列数,判断值
typedefstruct
{
intcolumn;
introw;
intval;
}Nodes;
intboard[MAX][MAX];
//InitBoard初始化棋盘
StatusInitBoard()
{
introw,column;
for(row=0;rowfor(column=0;columnboard[row][column]=EMPTY;
returnTRUE;
}
//PostionIsEmpty判断在棋盘上在给定的置是否为空
StatusPositionIsEmpty(introw,intcolumn)
{
if(board[row][column]==2)
returnTRUE;
else
returnFALSE;
}
//Place在指定的地方落子
StatusPlace(introw,intcolumn,intpiece)
{
board[row][column]=piece;
returnTRUE;
}
//BoardIsFull判断棋盘是否己满
StatusBoardIsFull()
{
inti=0,j=0;
for(i=0;ifor(j=0;j{
if(board[i][j]==2)
returnFALSE;
}
returnTRUE;
}
//IsWin判断是否有一方己经胜利
StatusIsWin(intside)
{
introw,column;
//判断一行
for(row=0;row{
for(column=0;columnif(board[row][column]!
=side)
break;
if(column>=MAX)
returnTRUE;
}
//判断一列
for(column=0;column{
for(row=0;rowif(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,HUMAN_WIN,DRAW,PLAYING
StatusPostionValue()
{
returnIsWin(COMPUTER)?
COMPUTER_WIN:
(IsWin(HUMAN)?
HUMAN_WIN:
(BoardIsFull()?
DRAW:
PLAYING));
}
//BestMovement判断最佳落子位置,采用递归,求出最佳位置
NodesBestMovement(intside)
{
intopp;//对手
Nodesnodes,node2;//nodes记录当前最佳位置,node2返回最佳位置
intsimpleEval;//记当中间结果
intbestRow=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;rowfor(column=0;columnif(PositionIsEmpty(row,column))
{
Place(row,column,side);
nodes=BestMovement(opp);
Place(row,column,EMPTY);
//到更好的位置,更新信息
if((side==COMPUTER&&nodes.val>value)||(side==HUMAN&&nodes.val{
value=nodes.val;
bestRow=row;
bestColumn=column;
}
}
node2.row=bestRow;
node2.column=bestColumn;
node2.val=value;
returnnode2;
}
//Print打印出当前棋盘状态
StatusPrint()
{
introw,column;
for(row=0;rowfor(column=0;column{
if(board[row][column]==2)
printf("^");
elseif(board[row][column]==1)
printf("X");
else
printf("O");
if((column!
=0)&&(column%2==0))
printf("\n");
}
returnTRUE;
}
intmain(void)
{
NodesplayNode;
intfirst,a,b,result,opera;//first决定谁先下第一步棋。
result记录每下一步棋后的结果
while(TRUE)
{
while(TRUE)
{
printf("请选择你要进行的操作:
\n");
printf("1:
开局\n");
printf("2:
退出\n");
scanf("%d",&opera);
if(opera==1)
break;
if(opera==2)
returnTRUE;
printf("你的输入有误,请重新输入\n");
}
InitBoard();
while(TRUE)
{
printf("请决定人机对战时谁先走第一步?
0:
人1:
电脑");
scanf("%d",&first);
if(first==0||first==1)
{
break;
}
printf("输入错误,请重新选择\n\n");
}
printf("人的棋子为O,电脑的棋子X,空位用^表示\n");
if(first==0)
{
while(TRUE)
{
while(TRUE)
{
printf("请输入你落子所在的行数,列数(格式:
a,b(a,b在0~2之间)):
");
scanf("%d,%d",&a,&b);
if(a>=0&&a=0&&b<=MAX&&PositionIsEmpty(a,b))
break;
printf("你输入的位置不合法,请重新输入:
\n\n");
}
Place(a,b,HUMAN);
Print();//下一步棋后打印出棋盘状态,并判断是否结束,如结束,则跳出
if((result=PostionValue())!
=PLAYING)
break;
playNode=BestMovement(COMPUTER);
Place(playNode.row,playNode.column,COMPUTER);
printf("\n电脑落子后的棋盘为:
\n");
Print();////下一步棋后打印出棋盘状态,并判断是否结束,如结束,则跳出
if((result=PostionValue())!
=PLAYING)
break;
}
}
elseif(first==1)
{
while(TRUE)
{
printf("\n电脑落子后棋盘状态为\n");
playNode=BestMovement(COMPUTER);
Place(playNode.row,playNode.column,COMPUTER);
Print();//下一步棋后打印出棋盘状态,并判断是否结束,如结束,则跳出
if((result=PostionValue())!
=PLAYING)
break;
while(TRUE)
{
printf("请输入你落子所在的行数,列数(格式:
a,b(a,b在0~2之间)):
");
scanf("%d,%d",&a,&b);
if(a>=0&&a=0&&b<=MAX&&PositionIsEmpty(a,b))
break;
printf("你输入的位置不合法,请重新输入:
\n\n");
}
Place(a,b,HUMAN);
Print();//下一步棋后打印出棋盘状态,并判断是否结束,如结束,则跳出
if((result=PostionValue())!
=PLAYING)
break;
}
}
if(result==COMPUTER_WIN)
printf("哈哈,你输了!
\n\n");
elseif(result==HUMAN_WIN)
printf("恭喜,你赢了!
\n\n");
else
printf("平局!
\n\n");
}
return0;
}
英文版本
运行环境:
TurboC或TurboC/C++forWindows集成实验与学习环境或VC++6.0或TurboC2.0英文版等。
。
。
。
。
。
#include"stdio.h"
#include"malloc.h"
#defineSIZE3
#ifndefFALSE
#defineFALSE0
#endif
#ifndefTRUE
#defineTRUE1
#endif
#defineNONE0
#definePLAYER_A1
#definePLAYER_B2
#defineWARNNING255
#defineCOMPETITOR200
#defineWINNER-1
charchessboard[SIZE][SIZE];
structCHESS_MAN
{
introw;
intcol;
};
/*getthevalueofcurrentchessboard:
countandretrunhowmanywaystheplayercanwinthegame*/
intget_value(intplayer)
{
inti,j,ret=0;
introw,col,inc;
intbNONE=FALSE;
/*checktherow*/
for(i=0;i{
row=SIZE;
bNONE=FALSE;
for(j=0;j{
/*ifthereisacompetitor'schessmanatthelocation
subrow*/
if(chessboard[i][j]==player)
row--;
/*ifthereisanyemptylocationintherow,
setbNONEasTRUE*/
if(chessboard[i][j]==NONE)
bNONE=TRUE;
}
/*computer:
oneemptyandothersarecompetitor'schessman,
ohmygod,danger,youmaylosethegame*/
if(row==1&&bNONE==TRUE)
returnWARNNING;
/*computer:
nocompetitor'schessmanintherow,
thereisonewaytomakemewinthegame*/
elseif(row==SIZE)
ret++;
}
/*checkthecol*/
for(i=0;i{
col=SIZE;
bNONE=FALSE;
for(j=0;j{
if(chessboard[j][i]==player)
col--;
if(chessboard[j][i]==NONE)
bNONE=TRUE;
}
/*computer:
warnning:
thecompetitormaybewinthegame*/
if(col==1&&bNONE==TRUE)
returnWARNNING;
/*computer:
thisismychance.*/
elseif(col==SIZE)
ret++;
}
/*checkinc*/
inc=SIZE;
bNONE=FALSE;
for(i=0,j=0;i{
if(chessboard[i][j]==player)
inc--;
if(chessboard[i][j]==NONE)
bNONE=TRUE;
}
/*computer:
iwon'tlosethegame*/
if(inc==1&&bNONE==TRUE)
returnWARNNING;
/*mychance?
*/
elseif(inc==SIZE)
ret++;
/*checkinc*/
inc=SIZE;
bNONE=FALSE;
for(i=0,j=SIZE-1;i{
if(chessboard[i][j]==player)
inc--;
if(chessboard[i][j]==NONE)
bNONE=TRUE;
}
/*becareful*/
if(inc==1&&bNONE==TRUE)
returnWARNNING;
/*anotherchance*/
elseif(inc==SIZE)
ret++;
returnret;
}
/*displaythechessboard*/
voiddisp_chess_board(void)
{
inti,j;
/*printthehead*/
for(i=0;iprintf("-");
printf("\n");
/*printthecontect*/
for(i=0;i{
printf("|");
for(j=0;j{
if(chessboard[i][j]==PLAYER_A)
printf("o|");
elseif(chessboard[i][j]==PLAYER_B)
printf("x|");
else
printf("|");
}
printf("\n");
/*printthefloor*/
for(j=0;jprintf("-");
printf("\n");
}
return;
}
/*initthechessboard*/
voidinit_chess_board(void)
{
inti,j;
for(i=0;ifor(j=0;jchessboard[i][j]=NONE;
return;
}
intenter_chess_man(introw,intcol,intplayer)
{
/*outofsize*/
if(row>=SIZE||col>=SIZE)
returnFALSE;
/*thepiontedlocationisnotempty*/
if(chessboard[row][col]!
=NONE)
returnFALSE;
/*okay,putdownthechessman*/
chessboard[row][col]=player;
returnTRUE;
}
/*checkwhetchtheplayerwinthegame*/
intchk_winner(intplayer)
{
inti,j;
intcol,row,inc;
/*aretherealltheplayer'schessmeninthesamerow*/
for(i=0;i{
row=TRUE;
for(j=0;j{
if(chessboard[i][j]!
=player)
row=FALSE;
}
if(row==TRUE)
returnTRUE;
}
/*aretherealltheplayer'schessmeninthesamecol*/
for(i=0;i{
col=FALSE;
for(j=0;j{
if(chessboard[j][i]!
=player)
col=FALSE;
}
if(col==TRUE)
returnTRUE;
}
/*whatabouttheinc*/
inc=TRUE;
j=0;
for(i=0;iif(chessboard[i][i+j]!
=player)
inc=FALSE;
if(inc==TRUE)
returnTRUE;
/*andthis?
*/
inc=TRUE;
j=SIZE-1;
for(i=0;iif(chessboard[i][j-i]!
=player)
inc=FALSE;
if(inc==TRUE)
returnTRUE;
/*sorry,theplayerhasnotwonyet.*/
returnFALSE;
}
/*getthebestchessmanforplayer*/
intget_best_chess(structCHESS_MAN*best_chess,intplayer,intother)
{
inttat_num=SIZE*SIZE;
intchess_value[9];
structCHESS_MANchess[9];
inti,j,cur=0;
/*initchess[]*/
for(i=0;i{
for(j=0;j{
chess[cur].row=i;
chess[cur++].col=j;
}
}
/*whenitakeoneofthechessman,
what'sthechess_valueofmycompetitor
iwillchoosetheminvalue,
becauseitmeansthat'stheworstcaseforhim*/
for(i=0;i{
/*itrytotakethischess_man*/
if(enter_chess_man(chess[i].row,chess[i].col,player)==TRUE)
{
chess_value[i]=get_value(other);
/**/
if(chk_winner(player)==TRUE)
chess_value[i]=WINNER;
chessboard[chess[i].row][chess[i].col]=NONE;
}
else
/*cannottake,meansthat
chess_boardhaslayedmycpmpetitor'schess_man*/
chess_value[i]=COMPETITOR;
}
/*choosethelowestchess_value*/
cur=0;
for(i=0;i{
if(chess_value[cur]>c