版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 <stdio.h>#include "malloc.h"/记录一步棋所需的所有信息:行数,列数,判断值typedef struct int column; int row; int val;Nodes;int boardMAXMAX;/InitBoard初始化棋盘Status InitBoard() int row,column; for(row=0; row<MAX; row+) for(column=0; column<MAX; column+) boardrowcolumn=EMPTY; return TRUE;/PostionIs
3、Empty判断在棋盘上在给定的置是否为空Status 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;i<MAX;i+) for(j=0;j<MAX
4、;j+) if(boardij =2) return FALSE; return TRUE;/IsWin判断是否有一方己经胜利Status IsWin( int side ) int row, column; /判断一行 for( row = 0; row < MAX; row+ ) for( column = 0; column < MAX; column+ ) if( board row column != side ) break; if( column >= MAX ) return TRUE; /判断一列 for( column = 0; column < M
5、AX; column+ ) for( row = 0; row < MAX; row+ ) if( board row column != side ) break; if( row >= MAX ) return TRUE; /判断主对角线 if( board 1 1 = side && board 2 2 = side && board 0 0 = side ) return TRUE; /判断副对角线 if( board 0 2 = side && board 1 1 = side && board 2 0 =
6、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判断最佳落子位置,采用递归 ,求出最佳位置Nodes BestMovement(int side) int opp;/对手 N
7、odes 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=COMPUTER) opp=HUMAN; value=HUMAN_WIN; else op
8、p=COMPUTER; value=COMPUTER_WIN; for(row=0; row<MAX; row+) for(column=0; column<MAX; column+) if(PositionIsEmpty(row, column) ) Place(row, column, side); nodes = BestMovement(opp); Place(row,column,EMPTY); / 到更好的位置,更新信息 if( (side =COMPUTER && nodes.val > value) | (side=HUMAN &&am
9、p; nodes.val < value) ) value=nodes.val; bestRow=row; bestColumn=column; node2.row=bestRow; node2.column=bestColumn; node2.val=value; return node2;/Print打印出当前棋盘状态Status Print() int row,column; for(row=0; row<MAX; row+) for(column=0; column<MAX; column+) if(boardrowcolumn=2) printf(" &q
10、uot;); else if(boardrowcolumn=1) printf("X "); else printf("O "); if(column!=0) && (column%2 =0) printf("n"); return TRUE;int main(void) Nodes playNode; int first,a, b, result,opera; /first决定谁先下第一步棋。result记录每下一步棋后的结果 while(TRUE) while(TRUE) printf("请选择你要进行的
11、操作: n"); printf("1:开局n"); printf("2: 退出n"); scanf("%d",&opera); if(opera=1) break; if(opera=2) return TRUE; printf("你的输入有误,请重新输入n"); InitBoard(); while(TRUE) printf("请决定人机对战时谁先走第一步?0:人 1:电脑"); scanf("%d",&first); if(first=0 | f
12、irst =1) break; printf("输入错误,请重新选择nn"); printf("人的棋子为O,电脑的棋子X,空位用表示n"); if(first=0) while(TRUE) while(TRUE) printf("请输入你落子所在的行数,列数(格式:a,b(a,b在02之间):"); scanf("%d,%d",&a,&b); if(a>=0 && a<MAX && b>=0 && b<=MAX &&a
13、mp; PositionIsEmpty(a,b) break; printf("你输入的位置不合法,请重新输入:nn"); Place(a,b,HUMAN); Print(); /下一步棋后打印出棋盘状态,并判断是否结束,如结束,则跳出 if( (result=PostionValue() != PLAYING) break; playNode=BestMovement(COMPUTER); Place( playNode.row, playNode.column, COMPUTER); printf("n电脑落子后的棋盘为:n"); Print();
14、/下一步棋后打印出棋盘状态,并判断是否结束,如结束,则跳出 if( (result=PostionValue() != PLAYING) break; else if(first=1) while(TRUE) printf("n电脑落子后棋盘状态为n"); playNode = BestMovement(COMPUTER); Place( playNode.row, playNode.column,COMPUTER); Print(); /下一步棋后打印出棋盘状态,并判断是否结束,如结束,则跳出 if( (result=PostionValue() != PLAYING)
15、break; while(TRUE) printf("请输入你落子所在的行数,列数(格式:a,b(a,b在02之间):"); scanf("%d,%d",&a,&b); if(a>=0 && a<MAX && b>=0 && b<=MAX && PositionIsEmpty(a,b) break; printf("你输入的位置不合法,请重新输入:nn"); Place(a,b, HUMAN); Print(); /下一步棋后打印出
16、棋盘状态,并判断是否结束,如结束,则跳出 if( (result=PostionValue() != PLAYING) 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英文版等。#incl
17、ude "stdio.h"#include "malloc.h"#define SIZE 3#ifndef FALSE #define FALSE 0#endif#ifndef TRUE #define TRUE 1#endif#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
18、;/*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,ret=0; int row,col,inc; int bNONE=FALSE; /*check the row*/ for(i=0;i<SIZE;i+) row=SIZE; bNONE=FALSE; for(j=0;j<SIZE;j+) /*if there is a competitor's chess
19、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) bNONE=TRUE; /*computer : one empty and others are competitor's chess man, oh my god, danger, you may lose the game*/ if(row=1&&bNONE=TRUE) re
20、turn WARNNING; /*computer : no competitor's 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;i<SIZE;i+) col=SIZE; bNONE=FALSE; for(j=0;j<SIZE;j+) if(chessboardji=player) col-; if(chessboardji=NONE) bNONE=TRUE; /*computer : w
21、arnning : 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=FALSE; for(i=0,j=0;i<SIZE;i+,j+) if(chessboardij=player) inc-; if(chessboardij=NONE) bNONE=TRUE; /*computer : i won
22、't 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;i<SIZE;i+,j-) if(chessboardij=player) inc-; if(chessboardij=NONE) bNONE=TRUE; /*be careful*/ if(inc=1&&bNONE=TRUE) return WARN
23、NING; /*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;i<SIZE*4+1;i+) printf("-"); printf("n"); /*print the contect*/ for(i=0;i<SIZE;i+) printf("|"); for(j=0;j<SIZE;
24、j+) if(chessboardij=PLAYER_A) printf(" o |"); else if(chessboardij=PLAYER_B) printf(" x |"); else printf(" |"); printf("n"); /*print the floor*/ for(j=0;j<SIZE*4+1;j+) printf("-"); printf("n"); return;/*init the chess board*/void init_ch
25、ess_board(void) int i,j; for(i=0;i<SIZE;i+) for(j=0;j<SIZE;j+) chessboardij=NONE; return;int enter_chess_man(int row, int col, int player) /*out of size*/ if(row>=SIZE|col>=SIZE) return FALSE; /*the pionted location is not empty*/ if(chessboardrowcol!=NONE) return FALSE; /*okay, put down
26、 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 the player's chess men in the same row*/ for(i=0;i<SIZE;i+) row=TRUE; for(j=0;j<SIZE;j+) if(chessboardij!=player) row=FALSE; if
27、(row=TRUE) return TRUE; /*are there all the player's chess men in the same col*/ for(i=0;i<SIZE;i+) col=FALSE; for(j=0;j<SIZE;j+) if(chessboardji!=player) col=FALSE; if(col=TRUE) return TRUE; /*what about the inc*/ inc=TRUE; j=0; for(i=0;i<SIZE;i+) if(chessboardii+j!=player) inc=FALSE;
28、if(inc=TRUE) return TRUE; /*and this?*/ inc=TRUE; j=SIZE-1; for(i=0;i<SIZE;i+) if(chessboardij-i!=player) inc=FALSE; if(inc=TRUE) return TRUE; /*sorry, 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
29、other) int tat_num=SIZE*SIZE; int chess_value9; struct CHESS_MAN chess9; int i,j,cur=0; /*init chess*/ for(i=0;i<SIZE;i+) for(j=0;j<SIZE;j+) chesscur.row=i; chesscur+.col=j; /*when i take one of the chess man, what's the chess_value of my competitor i will choose the min value, because it
30、means that's the worst case for him*/ for(i=0;i<tat_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, mea
31、ns that chess_board has layed my cpmpetitor's chess_man*/ chess_valuei=COMPETITOR; /*choose the lowest chess_value*/ cur=0; for(i=0;i<tat_num;i+) if(chess_valuecur>chess_valuei) cur=i; /*my best is my competitor's worst*/ best_chess->row=chesscur.row; best_chess->col=chesscur.col
32、; return chess_valuecur;int chk_full(void) int i,j; for(i=0;i<SIZE;i+) for(j=0;j<SIZE;j+) if(chessboardij=NONE) return FALSE; return TRUE;int main() struct CHESS_MAN best_chess; int player=PLAYER_A; int competitor=PLAYER_B; int bEND=FALSE; /*whetch need end of the program*/ int row,col; /*user's input location*/ /best_chess=(struct CHESS_MAN*)malloc(sizeof(struct CHESS_MAN); init_chess_board(); disp_chess_board(); while(bEND=FALSE) if(player=PLAYER_A) /*user's turn*/ do printf(" Input your chess location : n"); printf(" loca
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年基金从业资格考试法律法规重点解析及答案
- 高中语文新课程教学心得分享
- 餐饮场所食品安全管理体系搭建
- 医院医共体建设项目汇报
- 高校体育赛事组织与管理实务指南
- 美容院开业典礼主持词范文
- 桥梁工程质量检验常用技术表格模板合集
- 长途通信布线工程施工规范
- 电子商务网站客户服务标准操作流程
- 企业市场调研报告撰写模板与范文
- 2025年大学公共管理(公共管理学)试题及答案
- 证件租借协议书
- 雨课堂学堂在线学堂云《药物信息学(山东大学 )》单元测试考核答案
- 2026版九上英语人教专题02 完形填空(期末真题必练)(解析版)
- 长春财经学院《大学英语》2023-2024学年第一学期期末试卷
- 钢结构波形梁护栏技术说明书
- 2026届新高考历史冲刺备考复习第一次世界大战与战后国际秩序
- 消防设施操作员基础知识
- T-CAQ 10201-2024《质量管理小组活动准则》解读与实践指南
- 新能源车电池性能检测报告范本
- 2025年春新沪粤版物理八年级下册全册教案
评论
0/150
提交评论