900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > c语言大作业井字棋程序 C语言编写的井字棋

c语言大作业井字棋程序 C语言编写的井字棋

时间:2020-08-11 00:43:58

相关推荐

c语言大作业井字棋程序 C语言编写的井字棋

《C语言编写的井字棋》由会员分享,可在线阅读,更多相关《C语言编写的井字棋(18页珍藏版)》请在人人文库网上搜索。

1、井字旗C语言程序:运行环境:Turbo C/C+for Windows集成实验与学习环境或VC+6.0#define MAX 3#define Status int#define HUMAN_WIN 0 /人取得了胜利#define DRAW 1 /平局#define PLAYING 2 /没有决出胜负,正在进行游戏#define COMPUTER_WIN 3 /电脑取得了胜利#define HUMAN 0 /人#define COMPUTER 1 /机器#define EMPTY 2 /空#define FALSE 0 /假#define TRUE 1 /真#include #include。

2、 malloc.h/记录一步棋所需的所有信息:行数,列数,判断值typedef structint column;int row;int val;Nodes;int boardMAXMAX;/InitBoard初始化棋盘Status InitBoard()int row,column;for(row=0; row= MAX )return TRUE;/判断一列for( column = 0; column = MAX )return TRUE;/判断主对角线if( board 1 1 = side & board 2 2 = side& board 0 0 = side )return TRU。

3、E;/判断副对角线if( board 0 2 = side & board 1 1 = side& board 2 0 = side )return TRUE;return FALSE;/PositonValue返回落子后的状态 有四种状态在ConstNum.h中定义 COMPUTER_WIN, HUMAN_WIN, DRAW, PLAYINGStatus PostionValue()return IsWin(COMPUTER)?COMPUTER_WIN:(IsWin(HUMAN)?HUMAN_WIN:(BoardIsFull()?DRAW:PLAYING);/BestMovement判断最佳。

4、落子位置,采用递归 ,求出最佳位置Nodes BestMovement(int side)int opp;/对手Nodes nodes, node2; /nodes记录当前最佳位置,node2返回最佳位置int simpleEval; /记当中间结果int bestRow=0, row;int bestColumn=0, column;int value;/判断是否游戏己经结束if( (simpleEval=PostionValue() != PLAYING)node2.row=0;node2.column=0;node2.val=simpleEval;return node2;if(side。

5、=COMPUTER)opp=HUMAN;value=HUMAN_WIN;elseopp=COMPUTER;value=COMPUTER_WIN;for(row=0; row value) | (side=HUMAN & nodes.val =0 & a=0 & b=0 & a=0 & b=SIZE|col=SIZE)return FALSE;/*the pionted location is not empty*/if(chessboardrowcol!=NONE)return FALSE;/*okay, put down the chess man*/chessboardrowcol=pla。

6、yer;return TRUE;/*check whetch the player win the game*/int chk_winner(int player)int i,j;int col,row,inc;/*are there all the players chess men in the same row*/for(i=0;ichess_valuei)cur=i;/*my best is my competitors worst*/best_chess-row=chesscur.row;best_chess-col=chesscur.col;return chess_valuecu。

7、r;int chk_full(void)int i,j;for(i=0;i row : );scanf(%d,&row);printf( location col : );scanf(%d,&col);if(enter_chess_man(row-1,col-1,player)=TRUE)printf( You have take chess man at %d%dn,row,col);break;elseprintf( Error : You put the chess to a wrong locationn);while(TRUE);else/*computer says : it is。

8、 my turn.*/get_best_chess(&best_chess,player,competitor);enter_chess_man(best_chess.row,best_chess.col,player);printf( Player %d put chess at %d%dn,player,best_chess.row+1,best_chess.col+1);/*display the current chess board*/disp_chess_board();/*anybody win?!*/bEND=TRUE;if(chk_winner(player)printf( 。

9、Player %d Win the Game.n,player);else if(chk_winner(competitor)printf( Player %d Win the Game.n,competitor);else if(chk_full()printf( No One Win the Game.n);elsebEND=FALSE;/*change the turn of the players*/competitor=player;if(player=PLAYER_A)player=PLAYER_B;elseplayer=PLAYER_A;printf(nnthe command completed successfully.nn);return 0。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。