ImageVerifierCode 换一换
格式:DOCX , 页数:23 ,大小:19.51KB ,
资源ID:13413895      下载积分:5 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-13413895.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C语言编写的井字棋.docx)为本站会员(b****1)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

C语言编写的井字棋.docx

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 1 /真 #includ

2、e #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判断在棋盘上在给定的置是否为空Status PositionIsEm

3、pty(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 FALSE; return TRUE;/

4、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 0 0 = side ) return T

5、RUE; /判断副对角线 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);/BestMove

6、ment判断最佳落子位置,采用递归 ,求出最佳位置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;

7、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; node2.val=value; ret

8、urn 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() != PLAYING) break; if(resul

9、t=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 TRUE #define TRUE 1#endif#

10、define NONE 0#define PLAYER_A 1#define PLAYER_B 2#define WARNNING 255#define COMPETITOR 200#define WINNER -1char 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 game*/int get_value(int player) int i,j,

11、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 bNONE as TRUE*/ if(chessboardij=NONE) b

12、NONE=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=SIZE) ret+; /*check the col*/ for(i=0;iSIZ

13、E;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) ret+; /*check inc*/ inc=SIZE; bNONE=FA

14、LSE; 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;iSIZE;i+,j-) if(chessboardij=player) in

15、c-; 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(-); printf(n); /*print the contect*/ for(i=

16、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 init_chess_board(void) int i,j; for(i=0;iSIZE;

17、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 player) int i,j; int col,row,inc; /*are there all

18、 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(chessboardji!=player) col=FALSE; if(col=TRUE)

19、 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; /*sorry, the player has not won yet.*/ return FALS

20、E;/*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.row=i; chesscur+.col=j; /*when i take one of the

21、 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_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;ic

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2