《C语言编写的井字棋(共17页).doc》由会员分享,可在线阅读,更多相关《C语言编写的井字棋(共17页).doc(17页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
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 /真 #in
2、clude #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 Position
3、IsEmpty(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 TR
4、UE;/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 ) retu
5、rn 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, PLAYINGStatus PostionValue() return IsWin(COMPUTER)?COMPUTER_WIN:(IsWin(HUMAN)?HUMAN_WIN:(BoardIsFull()?DRAW:PLAYING);/Best
6、Movement判断最佳落子位置,采用递归 ,求出最佳位置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=simpleEv
7、al; 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;
8、 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() != PLAYING) break; if(r
9、esult=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#en
10、dif#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
11、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 bNONE as TRUE*/ if(chessboardij=NON
12、E) 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=SIZE) ret+; /*check the col*/ for(i=0;
13、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) ret+; /*check inc*/ inc=SIZE; bNON
14、E=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;iSIZE;i+,j-) if(chessboardij=player
15、) 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(-); printf(n); /*print the contect*/ fo
16、r(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 init_chess_board(void) int i,j; for(i=0;iS
17、IZE;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
18、 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(chessboardji!=player) col=FALSE; if(col=T
19、RUE) 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
20、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.row=i; chesscur+.col=j; /*when i take one of
21、 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_value(other); /*/ if(chk_winner(player)=TR
22、UE) 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; be