C语言编写的井字棋.docx

上传人:牧羊曲112 文档编号:3155380 上传时间:2023-03-11 格式:DOCX 页数:19 大小:41.17KB
返回 下载 相关 举报
C语言编写的井字棋.docx_第1页
第1页 / 共19页
C语言编写的井字棋.docx_第2页
第2页 / 共19页
C语言编写的井字棋.docx_第3页
第3页 / 共19页
C语言编写的井字棋.docx_第4页
第4页 / 共19页
C语言编写的井字棋.docx_第5页
第5页 / 共19页
亲,该文档总共19页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《C语言编写的井字棋.docx》由会员分享,可在线阅读,更多相关《C语言编写的井字棋.docx(19页珍藏版)》请在三一办公上搜索。

1、C语言编写的井字棋井字旗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

2、1 /真 #include #include malloc.h /记彔一步棋所需的所有信息:行数,列数,判断值 typedef struct int column; int row; int val; Nodes; int boardMAXMAX; /InitBoard初始化棋盘 Status InitBoard int row,column; for(row=0; rowMAX; row+) for(column=0; columnMAX; column+) boardrowcolumn=EMPTY; return TRUE; /PostionIsEmpty判断在棋盘上在给定的置是否为空 S

3、tatus PositionIsEmpty(int row , int column) if(boardrowcolumn=2) return TRUE; else return FALSE; /Place在指定的地方落子 Status Place(int row,int column, int piece) boardrowcolumn=piece; return TRUE; /BoardIsFull判断棋盘是否己满 Status BoardIsFull int i=0,j=0; for(i=0;iMAX;i+) for(j=0;jMAX;j+) if(boardij =2) return

4、FALSE; return TRUE; /IsWin判断是否有一方己经胜利 Status IsWin( int side ) int row, column; /判断一行 for( row = 0; row MAX; row+ ) for( column = 0; column = MAX ) return TRUE; /判断一列 for( column = 0; column MAX; column+ ) for( row = 0; row = MAX ) return TRUE; /判断主对角线 if( board 1 1 = side & board 2 2 = side & board

5、 0 0 = side ) return TRUE; /判断副对角线 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, PLAYING Status PostionValue return IsWin(COMPUTER)?COMPUTER_WIN:(IsWin(HUMAN)?HUMAN_WIN:(BoardIsFull?DRA

6、W:PLAYING); /BestMovement判断最佳落子位置,采用递归 ,求出最佳位置 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; n

7、ode2.val=simpleEval; return node2; if(side=COMPUTER) opp=HUMAN; value=HUMAN_WIN; else opp=COMPUTER; value=COMPUTER_WIN; for(row=0; rowMAX; row+) for(column=0; column value) | (side=HUMAN & nodes.val value) ) value=nodes.val; bestRow=row; bestColumn=column; node2.row=bestRow; node2.column=bestColumn;

8、 node2.val=value; return node2; /Print打印出当前棋盘状态 Status Print int row,column; for(row=0; rowMAX; row+) for(column=0; column=0 & a=0 & b=0 & a=0 & b=MAX & PositionIsEmpty(a,b) break; printf(你输入的位置不合法,请重新输入:nn); Place(a,b, HUMAN); Print; /下一步棋后打印出棋盘状态,并判断是否结束,如结束,则跳出 if( (result=PostionValue) != PLAYIN

9、G) break; if(result=COMPUTER_WIN) printf(哈哈,你输了!nn); else if(result = HUMAN_WIN) printf(恭喜,你赢了!nn); else printf(平局!nn); return 0; 英文版本 运行环境:Turbo C 或Turbo C/C+for Windows集成实验与学习环境 或VC+6.0 或Turbo C2.0英文版等。 #include stdio.h #include malloc.h #define SIZE 3 #ifndef FALSE #define FALSE 0 #endif #ifndef

10、TRUE #define TRUE 1 #endif #define NONE 0 #define PLAYER_A 1 #define PLAYER_B 2 #define WARNNING 255 #define COMPETITOR 200 #define WINNER -1 char chessboardSIZESIZE; struct CHESS_MAN int row; int col; ; /*get the value of current chess board: count and retrun how many ways the player can win the ga

11、me*/ int get_value(int player) int i,j,ret=0; int row,col,inc; int bNONE=FALSE; /*check the row*/ for(i=0;iSIZE;i+) row=SIZE; bNONE=FALSE; for(j=0;jSIZE;j+) /*if there is a competitors chess man at the location sub row*/ if(chessboardij=player) row-; /*if there is any empty location in the row, set

12、bNONE as TRUE*/ if(chessboardij=NONE) bNONE=TRUE; /*computer : one empty and others are competitors chess man, oh my god, danger, you may lose the game*/ if(row=1&bNONE=TRUE) return WARNNING; /*computer : no competitors chess man in the row, there is one way to make me win the game*/ else if(row=SIZ

13、E) ret+; /*check the col*/ for(i=0;iSIZE;i+) col=SIZE; bNONE=FALSE; for(j=0;jSIZE;j+) if(chessboardji=player) col-; if(chessboardji=NONE) bNONE=TRUE; /*computer : warnning : the competitor may be win the game*/ if(col=1&bNONE=TRUE) return WARNNING; /*computer : this is my chance.*/ else if(col=SIZE)

14、 ret+; /*check inc*/ inc=SIZE; bNONE=FALSE; for(i=0,j=0;iSIZE;i+,j+) if(chessboardij=player) inc-; if(chessboardij=NONE) bNONE=TRUE; /*computer : i wont lose the game*/ if(inc=1&bNONE=TRUE) return WARNNING; /*my chance?*/ else if(inc=SIZE) ret+; /*check inc*/ inc=SIZE; bNONE=FALSE; for(i=0,j=SIZE-1;

15、iSIZE;i+,j-) if(chessboardij=player) inc-; if(chessboardij=NONE) bNONE=TRUE; /*be careful*/ if(inc=1&bNONE=TRUE) return WARNNING; /*another chance*/ else if(inc=SIZE) ret+; return ret; /*display the chess board*/ void disp_chess_board(void) int i,j; /*print the head*/ for(i=0;iSIZE*4+1;i+) printf(-)

16、; printf(n); /*print the contect*/ for(i=0;iSIZE;i+) printf(|); for(j=0;jSIZE;j+) if(chessboardij=PLAYER_A) printf( o |); else if(chessboardij=PLAYER_B) printf( x |); else printf( |); printf(n); /*print the floor*/ for(j=0;jSIZE*4+1;j+) printf(-); printf(n); return; /*init the chess board*/ void ini

17、t_chess_board(void) int i,j; for(i=0;iSIZE;i+) for(j=0;j=SIZE|col=SIZE) return FALSE; /*the pionted location is not empty*/ if(chessboardrowcol!=NONE) return FALSE; /*okay, put down the chess man*/ chessboardrowcol=player; return TRUE; /*check whetch the player win the game*/ int chk_winner(int play

18、er) int i,j; int col,row,inc; /*are there all the players chess men in the same row*/ for(i=0;iSIZE;i+) row=TRUE; for(j=0;jSIZE;j+) if(chessboardij!=player) row=FALSE; if(row=TRUE) return TRUE; /*are there all the players chess men in the same col*/ for(i=0;iSIZE;i+) col=FALSE; for(j=0;jSIZE;j+) if(

19、chessboardji!=player) col=FALSE; if(col=TRUE) return TRUE; /*what about the inc*/ inc=TRUE; j=0; for(i=0;iSIZE;i+) if(chessboardii+j!=player) inc=FALSE; if(inc=TRUE) return TRUE; /*and this?*/ inc=TRUE; j=SIZE-1; for(i=0;iSIZE;i+) if(chessboardij-i!=player) inc=FALSE; if(inc=TRUE) return TRUE; /*sor

20、ry, the player has not won yet.*/ return FALSE; /*get the best chess man for player*/ int get_best_chess(struct CHESS_MAN *best_chess, int player, int other) int tat_num=SIZE*SIZE; int chess_value9; struct CHESS_MAN chess9; int i,j,cur=0; /*init chess*/ for(i=0;iSIZE;i+) for(j=0;jSIZE;j+) chesscur.r

21、ow=i; chesscur+.col=j; /*when i take one of the chess man, whats the chess_value of my competitor i will choose the min value, because it means thats the worst case for him*/ for(i=0;itat_num;i+) /*i try to take this chess_man*/ if(enter_chess_man(chessi.row,chessi.col,player)=TRUE) chess_valuei=get

22、_value(other); /*/ if(chk_winner(player)=TRUE) chess_valuei=WINNER; chessboardchessi.rowchessi.col=NONE; else /*can not take, means that chess_board has layed my cpmpetitors chess_man*/ chess_valuei=COMPETITOR; /*choose the lowest chess_value*/ cur=0; 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_valuecur; int chk_full(void) int i,j; for(i=0;iSIZE;i+) for(j=0;jSIZE;j+) if(chessboardij=NONE) return FALSE; return TRUE; int main

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号