C语言版井字棋
C语言版井字棋

十五、井字棋【问题描述】设计一个井字棋的游戏程序【基本要求】游戏规则是:从一个空的棋盘开始,人为x一方,计算机为o一方,人机双方轮流放置棋子,人下的时候,字符x将被放入棋盘中指定的位置,轮到计算机下时,程序将扫描棋盘,并将字符o放入扫描到的第一个空格,某一方有3枚棋子占据了一横行、一竖行或一对角线则获胜,若整个棋盘被占满还没有一方获胜则为和局。
截图:代码://@author GXU-pengcheng #include<stdio.h>#include<windows.h>#include<conio.h>void Help();//帮助int Play(char player,int choice);//对弈void Init();//初始化棋盘void Display();//打印棋盘void Black(int choice);//黑棋void White(int choice);//白棋void Block(int choice);//添加选择框void Clear(int choice);//清空之前的选择框int Judge(int choice,char symbol);//判断输赢返回值0为无结果,1为获胜,2为平局int Funcx(int choice);//将标号转换为行号int Funcy(int choice);//将标号转换为列号void End(char symbol);//结束int Found();//返回第一个没有棋子的位置的编号char a[31][64];//用数组存放棋盘和棋子,a[行][列]int b=0;//棋子个数int main(){char player;int choice;system("title 井字棋");//设置标题system("mode con cols=64 lines=35");//设置窗口大小system("color 70");//设置颜色while(1){printf("\n\n\n\n\n\n\t\t\t井\t字\t棋");printf("\n\n\n\n\n\n\n\n\t\t\t1. 玩家先手\n\n\t\t\t2. 电脑先手\n\n\t\t\t3. 帮助\n\n\t\t\t4. 退出\n\n\n\n\n\t\t请输入: ");player=getch();if(player=='1'){Init();Block(5);choice=5;Play(player,choice);}else if(player=='2'){Init();Play(player,choice);}else if(player=='3'){Help();getch();system("cls");continue;}else if(player=='4'){return 0;}else{printf("\n\n\t\t 输入错误请重新输入!");Sleep(1000);system("cls");continue;}}return 0;}void Help(){system("cls");printf("\n\n\n\n\n\n\n\n\n\t\t\t 帮助\n\n\n");printf("\t 'W'上移, 'S'下移, 'A'左移, 'D'右移 \n\n");printf("\t\t 游戏中按'4'退出");printf("\n\n\n\n\t\t 按任意键退出");}int Play(char player,int choice){//对弈char get;char symbol;int c=0;//Judge得出的结果while(1){system("cls");//每次循环清屏一次Display();if(player=='1'){//玩家下棋while(1){//确定要下的位置if((get=getch())!=' '){if(get=='4'){system("cls");return 0;}else if( (get=='w'||get=='W') &&choice-3>=1){Clear(choice);choice =choice-3;}else if((get=='s'||get=='S')&&choice+3<=9){Clear(choice);choice = choice+3;}else if((get=='a'||get=='A')&&(choice+2)/3==(choice+1)/3){ Clear(choice);choice -= 1;}else if((get=='d'||get=='D')&&(choice-1)/3==choice/3){ Clear(choice);choice += 1;}else{continue;}Block(choice);system("cls");Display();}else{if(a[((choice-1)/3)*10+2][((choice-1)%3)*21+4]!=' '){ printf("该位置已有其他棋子!");continue;}else{Clear(choice);break;}}}//while结束b++;Black(choice);player='0';symbol='#';}//if结束else{//电脑下棋choice=Found();Block(choice);b++;White(choice);player='1';symbol='*';}if((c=Judge(choice,symbol))!=0){//判断输赢if(c==2)symbol='=';c=0;End(symbol);break;}return 0;}void Init(){//初始化棋盘int x=0,y=0;int i='1';for(x=0;x<31;x++){for(y=0;y<64;y++){if((y==Funcy(1)||y==Funcy(2)||y==Funcy(3)||y==Funcy(2)+Funcy(3))){if(x==Funcx(1)||x==Funcx(4)||x==Funcx(7)||x==Funcx(4)+Funcx(7))a[x][y]='+';elsea[x][y]='|';}elseif(x==Funcx(1)||x==Funcx(4)||x==Funcx(7)||x==Funcx(4)+Funcx(7))a[x][y]='-';elsea[x][y]=' ';}//for(x=1;x<=7;x=x+3)//在每个格子的左上角添加序号,范围1~9 //{// for(y=1;y<=3;y++)// {// a[(Funcx(x)+1)][(Funcy(y)+1)]=i;// i++;// }//}}void Display(){//打印棋盘int x=0,y=0;for(x=0;x<31;x++){for(y=0;y<64;y++){printf("%c",a[x][y]);}}}void Block(int choice){//添加选择框int x,y;for(x=Funcx(choice)+1;x<Funcx(choice)+10;x++){for(y=Funcy(choice)+2;y<Funcy(choice)+20;y++)if(x==Funcx(choice)+1||x==Funcx(choice)+9){if(y==Funcy(choice)+2||y==Funcy(choice)+19)a[x][y]='+';else if(y<Funcy(choice)+7||y>Funcy(choice)+14)a[x][y]='-';}else if(x<Funcx(choice)+4||x>Funcx(choice)+6)if(y==Funcy(choice)+2||y==Funcy(choice)+19)a[x][y]='|';}}void Clear(int choice){//清空选择框int x,y,i;for(i=1;i<=9;i++){if(a[Funcx(choice)+1][Funcy(choice)+2]=='+'&&i!=choice){ for(x=Funcx(choice)+1;x<Funcx(choice)+10;x++){for(y=Funcy(choice)+2;y<Funcy(choice)+20;y++)if(x==Funcx(choice)+1||x==Funcx(choice)+9){if(y==Funcy(choice)+2||y==Funcy(choice)+19)a[x][y]=' ';else if(y<Funcy(choice)+7||y>Funcy(choice)+14)a[x][y]=' ';}else if(x<Funcx(choice)+4||x>Funcx(choice)+6)if(y==Funcy(choice)+2||y==Funcy(choice)+19)a[x][y]=' ';}}}}void Black(choice){//添加黑棋int x,y;for(x=Funcx(choice)+2;x<Funcx(choice)+9;x++){for(y=Funcy(choice)+4;y<Funcy(choice)+18;y++)if(x==Funcx(choice)+2||x==Funcx(choice)+8)a[x][y]='#';else if(y==Funcy(choice)+4||y==Funcy(choice)+17)a[x][y]='#';}}void White(choice){//添加白棋int x;int d=4;for(x=Funcx(choice)+2;x<=Funcx(choice)+8;x++){a[x][Funcy(choice)+d]='*';a[x][Funcy(choice)+20-d]='*';d=d+2;}}int Judge(int choice,char symbol){//判断输赢返回值0为无结果,1为获胜,2为平局if(((a[Funcx(choice)+2][Funcy(choice)+4]==a[Funcx(choice+3)+2][Funcy(choice)+4]&&a[Funcx(choice+3)+2][Funcy(choice)+4]==a[Funcx(choice+6)+2][Funcy(choice) +4])||(a[Funcx(choice)+2][Funcy(choice)+4]==a[Funcx(choice)+2][Funcy(choice+1)+4]&&a[Funcx(choice)+2][Funcy(choice+1)+4]==a[Funcx(choice)+2][Funcy(choice+2) +4]))||a[Funcx(5)+2][Funcy(5)+4]==symbol&&((a[Funcx(1)+2][Funcy(1)+4]==a[Funcx(5)+2][Funcy(5)+4]&&a[Funcx(5)+2][Funcy(5)+4]==a[Funcx(9)+2][Funcy(9)+4])||(a[Funcx(3)+2][Funcy(3)+4]==a[Funcx(5)+2][Funcy(5)+4]&&a[Funcx(5)+2][Funcy(5)+4]==a[Funcx(7)+2][Funcy(7)+4])))return 1;if(b==9)return 2;return 0;}int Funcx(int choice){//将标号转换为行号return (((choice-1)/3)%3)*10;}int Funcy(int choice){//将标号转换为列号return ((choice-1)%3)*21;void End(char symbol){//结束system("cls");Display();if(symbol=='*')printf("\t\t\t 电脑胜利!\n\n");else if(symbol=='#')printf("\t\t\t 玩家胜利!\n\n");elseprintf("\t\t\t 平局!\n\n");b=0;printf("\t\t\t按任意键返回菜单");getch();system("cls");}int Found(){//返回第一个没有棋子的位置的编号int i;for(i=1;i<=9;i++){if(a[Funcx(i)+2][Funcy(i)+4]==' ')return i;}}如有侵权请联系告知删除,感谢你们的配合!。
C语言程序设计 指针(9.1.24)--课外选作3:编程实现井字棋的自动判断获胜

#include <stdio.h> #include <string.h> void judge(char a[][3]); int main() { int i,j; char phase[3][3]; for(i=0;i<3;i++) {
for(j=0;j<3;j++) scanf("%c",&phase[i][j]);
getchar(); } judge(phase); return 0; } void judge(char a[][3]) { int i; if((a[0][1]==a[0][2]&&a[0][2]==a[0][0] &&a[0][0]=='X')&&(a[2][1]==a[2][2]&&a[2][2]==a[2][0] &&a[2][0]=='O')) {
C语言实现井字棋游戏(人机对弈)

C语⾔实现井字棋游戏(⼈机对弈)井字棋游戏:即三⼦棋,英⽂名叫Tic-Tac-Tic,是⼀种在3*3格⼦上进⾏的连珠游戏,和五⼦棋⽐较类似,由于棋盘⼀般不画边线框,格线排成井字故得名。
题⽬分析:要完成该游戏的编写,我们需要先分析出完成整个游戏过程都需要⼲什么?1.⾸先,需要定义出⼀个3*3的棋盘,根据相关知识,我们可以以⼆维数组的⽅式将棋盘表⽰出来;2.棋盘定义出来后,需要将棋盘初始化,将3*3⼆维数组的每⼀个位置初始化为‘ ’(空格);3.有了棋盘,我们就可以开始进⾏下棋了,⾸先要确定是玩家先下还是电脑先下。
在这⾥我们规定玩家先下且玩家的下棋⽅式为‘x’,电脑下棋⽅式为‘o’;4.每⼀次下完棋后需要进⾏检测,判断该下棋位置是否合法、判断是否胜利等等。
根据上述分析,可以⼤致定义出以下函数窗⼝:void InitGame();//初始化游戏(棋盘)void PrintChess();//输出棋盘void PlayerMove();//玩家下棋void ComputerMove();//电脑下棋char CheckGameOver();//判断游戏是否结束(玩家胜/电脑胜/和棋)初始化棋盘:将3*3的⼆维数组棋盘的每个位置初始化为‘ ’void InitGame(){for (int i = 0; i < ROW; i++){for (int j = 0; j < COL; j++)chess_board[i][j] = ' ';}}输出棋盘:输出棋盘时,棋盘的风格可以根据⾃⼰的喜好来设计void PrintfChess()//输出棋盘,棋盘的设计可以根据⾃⼰的喜好设计{for (int i = 0; i < ROW; i++){printf("| %c | %c | %c |\n", chess_board[i][0], chess_board[i][1], chess_board[i][2]);if (i < ROW - 1)printf("|---|---|---|\n");}}玩家下棋:玩家输⼊下棋位置后,需要判断该位置是否合法、输⼊位置是否已被占⽤void PlayerMove()//玩家下棋{printf("玩家落⼦.\n");int row, col;while (1){printf("请输⼊⼀组坐标(下棋位置):>");scanf("%d %d", &row, &col);//检查坐标的有效性if (row < 0 || row > ROW || col < 0 || col > COL){printf("输⼊⾮法,请重新输⼊...");continue;}if (chess_board[row][col] != ' '){printf("输⼊的位置已被占⽤,请重新输⼊...");continue;}chess_board[row][col] = 'x';//x代表玩家下的棋break;}}电脑下棋:电脑下棋时,下棋的位置利⽤srand函数随机产⽣void ComputerMove()//电脑下棋{srand(time(0));while (1){int row = rand() % ROW;int col = rand() % COL;if (chess_board[row][col] != ' '){continue;}chess_board[row][col] = 'o';//o代表电脑下的棋break;}}检查棋盘:在检测棋盘时,分别判断⾏、列、对⾓线,在这⾥我规定:'x'代表玩家赢 'o'代表电脑赢 'h'代表和棋 'c'代表继续char CheckGameOver()//检测游戏是否结束{//检查⾏for (int i = 0; i < ROW; i++){if (chess_board[i][0] != ' '&& chess_board[i][0] == chess_board[i][1]&& chess_board[i][0] == chess_board[i][2])return chess_board[i][0];}//检查列for (int j = 0; j < COL; j++){if (chess_board[0][j] != ' '&& chess_board[0][j] == chess_board[1][j]&& chess_board[0][j] == chess_board[2][j])return chess_board[0][j];}//检查对⾓线if (chess_board[0][0] != ' '&& chess_board[0][0] == chess_board[1][1]&& chess_board[0][0] == chess_board[2][2])return chess_board[0][0];if (chess_board[0][2] != ' '&& chess_board[0][2] == chess_board[1][1]&& chess_board[0][2] == chess_board[2][0])return chess_board[0][2];//判断是否和棋if (ChessFull())return 'h';return 'c';}⾄此,主要的功能函数均已编写完毕,整个程序的流程如下所⽰:1.初始化棋盘;2.输出棋盘;3.玩家下棋;4.检测棋盘;5.电脑下棋;6.检测棋盘#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <time.h>#include <stdbool.h>#include <stdlib.h>#define START 1#define QUIT 0#define ROW 3#define COL 3static char chess_board[ROW][COL];//定义棋盘void StartGame();void InitGame();void PrintfChess();void PlayerMove();void ComputerMove();char CheckGameOver();bool ChessFull();int main(int argc, char* argv[]){int select = 1;while (select){printf("*********************\n");printf("* [1] Start Game *\n");printf("* [2] Over Game *\n");printf("*********************\n");printf("请选择:>");scanf_s("%d", &select);if (select == QUIT)break;if (select != START){printf("输⼊有错,请重新输⼊.....\n"); continue;}StartGame();}printf("GoodBye.....");return 0;}void StartGame(){char winner;//1 初始化游戏(棋盘)InitGame();//2 进⼊游戏while (1){//3 输出棋盘PrintfChess();//4玩家下棋PlayerMove();//5检查结果winner = CheckGameOver();if (winner != 'c')break;//6电脑下棋ComputerMove();//7检查结果CheckGameOver();winner = CheckGameOver();if (winner != 'c')break;}if (winner == 'x')printf("玩家赢.\n");printf("电脑赢.\n");if (winner == 'h')printf("和棋.\n");}void InitGame(){for (int i = 0; i < ROW; i++){for (int j = 0; j < COL; j++)chess_board[i][j] = ' ';}}void PrintfChess()//输出棋盘,棋盘的设计可以根据⾃⼰的喜好设计{for (int i = 0; i < ROW; i++){printf("| %c | %c | %c |\n", chess_board[i][0], chess_board[i][1], chess_board[i][2]); if (i < ROW - 1)printf("|---|---|---|\n");}}void PlayerMove()//玩家下棋{printf("玩家落⼦.\n");int row, col;while (1){printf("请输⼊⼀组坐标(下棋位置):>");scanf("%d %d", &row, &col);//检查坐标的有效性if (row < 0 || row > ROW || col < 0 || col > COL){printf("输⼊⾮法,请重新输⼊...");continue;}if (chess_board[row][col] != ' '){printf("输⼊的位置已被占⽤,请重新输⼊...");continue;}chess_board[row][col] = 'x';//x代表玩家下的棋break;}}void ComputerMove()//电脑下棋{srand(time(0));while (1){int row = rand() % ROW;int col = rand() % COL;if (chess_board[row][col] != ' '){continue;}chess_board[row][col] = 'o';//o代表电脑下的棋break;}}/** 'x'代表玩家赢* 'o'代表电脑赢* 'h'代表和棋* 'c'代表继续*/char CheckGameOver()//检测游戏是否结束{//检查⾏for (int i = 0; i < ROW; i++){if (chess_board[i][0] != ' '&& chess_board[i][0] == chess_board[i][1]&& chess_board[i][0] == chess_board[i][2])return chess_board[i][0];}//检查列for (int j = 0; j < COL; j++){if (chess_board[0][j] != ' '&& chess_board[0][j] == chess_board[1][j]&& chess_board[0][j] == chess_board[2][j])return chess_board[0][j];}//检查对⾓线if (chess_board[0][0] != ' '&& chess_board[0][0] == chess_board[1][1]&& chess_board[0][0] == chess_board[2][2])return chess_board[0][0];if (chess_board[0][2] != ' '&& chess_board[0][2] == chess_board[1][1]&& chess_board[0][2] == chess_board[2][0])return chess_board[0][2];//判断是否和棋if (ChessFull())return 'h';return 'c';}bool ChessFull(){for (int i = 0; i < ROW; i++){for (int j = 0; j < COL; j++){if (chess_board[i][j] == ' ')return false;}}return true;}运⾏测试图:程序的运⾏界⾯我们还可以利⽤system("cls")对界⾯进⾏优化,这样可以使界⾯更加美观。
c语言井字游戏

c语言井字游戏第一篇:c语言井字游戏#include void main(){ int hang,lie,count=0;int player,ying=0;char weizhi[3][3]={ {'-','-','-'}, {'-','-','-'}, {'-','-','-'} };for(int i=0;i<=9&&ying==0;i++){printf(“nn”);printf(“§0 1 2n”);printf(“0 %c %c %cn”,weizhi[0][0],weizhi[0][1],weizhi[0][2]);printf(“1 %c %c %cn”,weizhi[1][0],weizhi[1][1],weizhi[1][2]);printf(“2 %c %c %cn”,weizhi[2][0],weizhi[2][1],weizhi[2][2]);player=i%2+1;do{printf(“n 玩家%d:请输入您的棋子位置(%c)”,player,(player==1)? '1':'2');scanf(“%d%d”,&hang,&lie);if(weizhi[hang][lie]=='1'||weizhi[hang][lie]=='2')printf(“n对不起!这个位置已经有棋子了,请您再输入棋子的位置:n”);}:while(hang<0||hang>2||lie<0||lie>2||weizhi[hang][lie]=='1'|| weizhi[hang][lie]=='2');weizhi[hang][lie]=(player==1)? '1':'2';count++;if(count==9)break;if((weizhi[0][0]==weizhi[1][1]&&weizhi[0][0]==weizhi[2][2] &&weizhi[2][2]!='-')||(weizhi[0][2]==weizhi[1][1]&&weizhi[0][2]==weizhi[2][0]& &weizhi[2][0]!='-')||(weizhi[0][0]==weizhi[1][0]&&weizhi[2][0]==weizhi[1][0]&&weizhi[1][0]!='-')||(weizhi[0][1]==weizhi[1][1]&&weizhi[2][1]==weizhi[1][1]& &weizhi[1][1]!='-')||(weizhi[0][2]==weizhi[1][2]&&weizhi[2][2]==weizhi[0][2]& &weizhi[0][2]!='-')||(weizhi[0][0]==weizhi[0][1]&&weizhi[0][0]==weizhi[0][2]& &weizhi[0][2]!='-')||(weizhi[1][0]==weizhi[1][1]&&weizhi[1][2]==weizhi[1][1]& &weizhi[1][1]!='-')||(weizhi[2][0]==weizhi[2][1]&&weizhi[2][2]==weizhi[2][1]& &weizhi[2][1]!='-')){ying=player;break;}elseif(count==9)break;}printf(“nn”);printf(“%c %c %cn”,weizhi[0][0],weizhi[0][1],weizhi[0][2]);printf(“%c %c %cn”,weizhi[1][0],weizhi[1][1],weizhi[1][2]);printf(“%c %c %cn”,weizhi[2][0],weizhi[2][1],weizhi[2][2]);if(ying==0)printf(“n实力相当!此局平局!n”);elseprintf(“n恭喜玩家%d!了!nn”,ying);}您赢补充:第二篇:猜字游戏横向:1、世乒赛中与王励勤一起拿下混双金牌的女选手2、即APEC3、我国一种民间工艺4、欧阳修的《生查子》中“月上柳梢头”的下句5、中德合资的汽车有限公司,车标是“Volks Wagenwerk”的两个首字母6、《倚天屠龙记》中男主人公的义父7、著名国画大师徐悲鸿的一幅名作8、由用餐者自取菜肴、主食的用餐方式9、自愿服兵役的士兵,我国专指服满一定年限的兵役后自愿继续服役的士兵10、家庭内部代代相传的有价值之物11、塞黑国家足球队球员,现效力于国际米兰12、在病人自愿的前提下,由医务人员实施的加速其死亡的一种行为13、无机化合物,成分是碳酸氢钠(NaHCO3),可以灭火或制焙粉纵向:一、《西游记》主角之一二、形容文学作品叙述描写真实生动三、美国一所著名的私立大学四、魏源提出“师夷长技以制夷”的一本书五、美国乔治亚州的首府,1996年奥运会的举办城市六、一部网络小说,在Google和百度的2004年十大中文搜索关键词中分别位列第三和第十七、为买卖双方撮合或代他人进行买卖而取得佣金的人八、曾因兴奋剂而冠军旁落的短跑名将九、由阿里巴巴公司投资创办的个人交易网上平台十、一个棒球术语十一、“雷锋精神”的一个方面十二、达·芬奇的一幅名画横列1.由商业银行发行,专供消费者购买商品和支付费用的凭证。
c井字棋课课程设计

c 井字棋课课程设计一、教学目标本节课旨在通过学习井字棋游戏的设计与实现,让学生掌握以下知识目标:理解井字棋的规则;掌握Python编程基础,如条件语句、循环语句等。
在技能目标方面,学生能够运用Python编程实现井字棋游戏,提高编程能力。
在情感态度价值观目标方面,学生通过小组合作完成游戏设计,培养团队协作精神和问题解决能力。
二、教学内容本节课的教学内容主要包括井字棋游戏的规则介绍、Python编程基础以及井字棋游戏的实现。
首先,介绍井字棋的规则,让学生了解游戏的基本玩法。
其次,教授Python编程基础,包括条件语句、循环语句等,为学生实现井字棋游戏打下基础。
最后,指导学生运用所学的Python编程知识,分组设计并实现井字棋游戏。
三、教学方法为了提高学生的学习兴趣和主动性,本节课采用讲授法、讨论法、案例分析法和实验法等多种教学方法。
在讲授井字棋规则时,采用生动的语言和实例进行讲解,让学生更容易理解和接受。
在教授Python编程基础时,通过案例分析和实验操作,让学生在实践中掌握编程知识。
在设计井字棋游戏环节,鼓励学生分组讨论、合作解决问题,培养团队协作精神。
四、教学资源为了支持教学内容和教学方法的实施,丰富学生的学习体验,本节课准备以下教学资源:教材《Python编程:从入门到实践》、参考书《Python核心编程》、多媒体资料(井字棋游戏教学视频、编程实例演示等)、实验设备(计算机、投影仪等)。
这些教学资源将有助于学生更好地掌握井字棋游戏的设计与实现,提高编程能力。
五、教学评估本节课的教学评估将采用多元化评价方式,全面客观地评价学生的学习成果。
评估主要包括以下几个方面:1.平时表现:观察学生在课堂上的参与程度、提问回答、小组讨论等,以了解学生的学习态度和实际操作能力。
2.作业:布置相关的编程练习,要求学生在规定时间内完成,以检验学生对井字棋游戏设计和Python编程知识的掌握程度。
3.考试:设置期末考试,包括选择题、填空题和编程题,全面测试学生对本节课知识点的理解和应用能力。
c语言井字棋课程设计

c语言井字棋课程设计一、课程目标知识目标:1. 理解C语言基本语法结构,掌握变量定义、数据类型、运算符和表达式等基础知识。
2. 学习使用C语言实现数组操作,掌握二维数组的使用方法。
3. 掌握C语言控制结构,如顺序结构、选择结构(if-else)、循环结构(for、while)等,并能运用到实际编程中。
技能目标:1. 培养学生运用C语言解决问题的能力,学会分析问题、设计算法、编写代码、调试程序。
2. 培养学生团队合作意识,学会在团队中分工协作,共同完成项目。
3. 提高学生逻辑思维能力,学会通过编程解决实际问题。
情感态度价值观目标:1. 培养学生积极的学习态度,激发学生对编程的兴趣和热情。
2. 培养学生面对问题敢于挑战、勇于克服困难的精神,增强自信心。
3. 培养学生遵守编程规范,养成良好的编程习惯。
本课程针对初中年级学生,结合C语言学科特点,注重培养学生的编程兴趣和实际动手能力。
课程内容以井字棋游戏为载体,引导学生学习C语言基础知识,掌握编程技巧,同时强调团队合作和逻辑思维能力的培养。
通过本课程的学习,使学生能够在实践中巩固所学知识,提高编程技能,培养积极向上的情感态度。
二、教学内容1. C语言基础知识回顾:变量定义、数据类型、运算符和表达式。
2. 数组概念及使用:一维数组、二维数组,重点掌握二维数组在井字棋游戏中的应用。
3. 控制结构:顺序结构、选择结构(if-else)、循环结构(for、while),结合井字棋游戏逻辑进行讲解。
4. 函数定义与调用:介绍函数的概念,编写并调用函数完成井字棋游戏功能模块。
5. 井字棋游戏设计:a. 游戏界面设计:使用二维数组表示棋盘,设计美观易用的游戏界面。
b. 玩家输入与输出:实现玩家输入落子位置,并在棋盘上显示落子。
c. 判断胜负:编写函数判断当前棋局是否分出胜负,并输出结果。
d. 人工智能对手:引入简单的算法,实现电脑自动落子,与玩家对弈。
6. 项目实践:分组进行项目实践,每组完成一个完整的井字棋游戏。
C语言程序设计 指针(9.1.23)--课外选作2:编程实现井字棋自动下棋

#include <stdio.h> #include <stdlib.h> #include <time.h>
int main() {
int i,j,flag=0,a[9],b[9],k=0,n; char x[3][3],o; for(i=0;i<2;i++) {
for(j=0;j<3;j++) {
1.题目:
继续上一题。这是个挑战性问题。 给定任意布局,如果作为其中某一方(X 方或O 方),判断下一手应走在什么位置? (注:井字棋规则描述:初始为空棋盘,双方每次下一个棋子,直到一方胜出或棋盘填满(类似于五子棋)) 将程序按照逻辑功能分解为多个函数。二维数组可以作为函数参数传递过去,参考的函数原型: void DisplayPhase(int M[3][3]); void GotoNextPhase(int M[3][3]); 提示:此问题可以实现得很复杂(如仔细分析未来双方可能的走法,寻找对己方最有利的走法),也可以实现得很简单(如简单地从 那些可走的地方中随机选择一个,这里称那些可走位置的总体为可行位置集)。能实现从可行位置集中随机选择位置的功能就算完成 本题。
scanf("%c%c",&x[i][j],&o); } scanf("-+-+-%c",&o); } for(j=0;j<3;j++) {
scanf("%c%c",&x[i][j],&o); } for(i=0;i<3;i++) { for(j=0;j<3;j++) {
C语言编写的井字棋

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 //真#include#include"malloc.h"//记彔⼀步棋所需的所有信息:⾏数,列数,判断值typedef struct{int column;int row;int val;}Nodes;int board[MAX][MAX];//InitBoard初始化棋盘Status InitBoard(){int row,column;for(row=0; rowfor(column=0; columnboard[row][column]=EMPTY;return TRUE;}if(board[row][column]==2)return TRUE;elsereturn FALSE;}//Place在指定的地⽅落⼦Status Place(int row,int column, int piece) { board[row][column]=piece;return TRUE;}//BoardIsFull判断棋盘是否⼰满Status BoardIsFull(){int i=0,j=0;for(i=0;ifor(j=0;j{if(board[i][j] ==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 )//判断⼀列for( column = 0; column < MAX; 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 ] == side )return TRUE;return FALSE;}//PositonValue返回落⼦后的状态有四种状态在ConstNum.h中定义COMPUTER_WIN, HUMAN_WIN, DRAW, PLAYING Status PostionValue(){returnIsWin(COMPUTER)?COMPUTER_WIN:(IsWin(HUMAN)?HUMAN_WIN:(BoardIsF ull()?DRAW: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.val=simpleEval;return node2;}if(side==COMPUTER){opp=HUMAN;value=HUMAN_WIN;}else{opp=COMPUTER;value=COMPUTER_WIN;}for(row=0; rowfor(column=0; column{Place(row, column, side);nodes = BestMovement(opp);Place(row,column,EMPTY);// 到更好的位置,更新信息if( (side ==COMPUTER && nodes.val >value) || (side==HUMAN && nodes.val {value=nodes.val;bestRow=row;bestColumn=column;}}node2.row=bestRow;node2.column=bestColumn;node2.val=value;return node2;}//Print打印出当前棋盘状态int row,column;for(row=0; rowfor(column=0; column{if(board[row][column]==2)printf("^ ");else if(board[row][column]==1)printf("X ");elseprintf("O ");if((column!=0) && (column%2 ==0))printf("\n");}return TRUE;}int main(void){Nodes playNode;int first,a, b, result,opera; //first决定谁先下第⼀步棋。
基于C语言实现井字棋游戏

基于C语⾔实现井字棋游戏井字棋游戏要求在3乘3棋盘上,每⾏都相同或者每列都相同再或者对⾓线相同,则胜出.因此我们可以使⽤⼀个⼆维数组来表⽰棋盘,判断胜负只需要判断数组元素是否相同即可.具体我们可以分为以下⼏步来做:1.创建维数组并进⾏初始化,如果仅仅是⼀个⼆维数组来表⽰棋盘,看起来不是很清楚,因此我们可以对棋盘边框⽤符号打印出来进⾏优化⼀下://初始化棋盘void init(char board[max_row][max_col]) {for (int row = 0; row < max_row; row++) {for (int col = 0; col < max_col; col++) {board[row][col] =' ';}}srand((unsigned int)time(0));}//打印棋盘void print(char board[max_row][max_col]) {system("cls");//每次打印之前清除掉上⼀次的结果.for (int a = 0; a < max_col; a++) {printf("+---+---+---+\n");for (int b = 0; b < max_row; b++) {printf("| %c ", board[a][b]);}printf("| \n");}printf("+---+---+---+\n");}2.进⾏玩家落⼦,同样是使⽤数组操作,让玩家输⼊相对应的坐标,在⼆维数组的该位置打印'x'来表⽰玩家下的棋⼦void play(char board[max_row][max_col]) {printf("玩家落⼦阶段!\n");int a;int b;while (1) {printf("请输⼊想要落⼦的坐标: ");scanf("%d %d", &a, &b);//判定玩家落⼦坐标是否在有效范围内if (a < 0 || b < 0 || a >= max_row || b >= max_col) {printf("输⼊坐标有误请重新输⼊!\n");continue;}//判定落⼦处是否已经存在了棋⼦if (board[b][a] != ' ') {printf("此处已被落⼦⽆法再落⼦,请重新输⼊!\n");continue;}board[b][a] = 'x'; //⽤'x'表⽰玩家落⼦break;}}3.玩家落⼦结束后让电脑在⼆维数组中随机落⼦,使⽤rand函数产⽣0到2的随机数进⾏赋值操作,进⽽实现电脑随机落⼦void computer(char board[max_row][max_col]) {printf("电脑落⼦阶段!\n");while (1) {int a = rand() % max_row; //产⽣0到2的随机数int b = rand() % max_col;if (board[a][b] != ' ') { //判断所下位置是否已经有⼦continue;}board[a][b] = 'o';break;}4.判断胜负,当玩家落⼦结束,或者电脑落⼦结束后,判断是否存在某⾏或某列或者对⾓线有相同的元素,如果有则直接判定结果,如果没有则轮到下家落⼦char check(char board[max_row][max_col]) {//检测所有⾏是否相等for (int cow = 0; cow < max_row; cow++) {if (board[cow][0] != ' ' && board[cow][0] == board[cow][1] && board[cow][0]==board[cow][2] ) {return board[cow][0];}}//检测所有列是否相等for (int col = 0; col < max_col; col++) {if (board[0][col] != ' ' && board[0][col] == board[1][col] && board[0][col]==board[2][col] ) {return board[0][col];}}//检测对⾓线是否相等if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[0][0] == board[2][2]) {return board[0][0];}if (board[0][2] != ' ' && board[0][2] == board[1][1] && board[0][2] == board[2][0]) {return board[0][2];}if (pingju(board)) {return 'q'; //棋⼦已满和棋}return 'a'; //棋盘未满}//判断棋盘是否棋⼦已满,如果满了返回1,未满返回0.int pingju(char board[max_row][max_col]) {for (int row = 0; row < max_row; row++) {for (int col = 0; col < max_col; col++) {if (board[row][col] == ' ') {return 0;}}}return 1;}5.在主函数中调⽤以上函数,进⾏操作:int main() {char board[max_row][max_col] = {0};//数组初始化为0char winner = 'n';init(board); //将棋盘中元素初始化为空格while (1) {print(board); //打印棋盘play(board); //玩家落⼦winner = check(board); //判断胜负if (winner != 'a') {break;}computer(board); //电脑落⼦winner = check(board); //判断胜负if (winner != 'a') {break;}}if (winner == 'x') {print(board);printf("你赢了!");}else if (winner == 'o') {print(board);printf("你输了!");}else if (winner == 'q') {print(board);printf("和棋!");}return 0;在主函数中利⽤while循环来实现玩家与电脑的轮流落⼦,如果当⼀⽅落⼦之后胜负已出,则跳出循环直接打印最终结果.完整代码如下:#include <stdio.h>#include <stdlib.h>#include <time.h>#include <windows.h>#define max_row 3#define max_col 3//初始化棋盘void init(char board[max_row][max_col]) {for (int row = 0; row < max_row; row++) {for (int col = 0; col < max_col; col++) {board[row][col] =' ';}}srand((unsigned int)time(0));}//打印棋盘void print(char board[max_row][max_col]) {system("cls");//每次打印之前清除掉上⼀次的结果.for (int a = 0; a < max_col; a++) {printf("+---+---+---+\n");for (int b = 0; b < max_row; b++) {printf("| %c ", board[a][b]);}printf("| \n");}printf("+---+---+---+\n");}//玩家落⼦void play(char board[max_row][max_col]) {printf("玩家落⼦阶段!\n");int a;int b;while (1) {printf("请输⼊想要落⼦的坐标: ");scanf("%d %d", &a, &b);//判定玩家落⼦坐标是否在有效范围内if (a < 0 || b < 0 || a >= max_row || b >= max_col) {printf("输⼊坐标有误请重新输⼊!\n");continue;}//判定落⼦处是否已经存在了棋⼦if (board[a][b] != ' ') {printf("此处已被落⼦⽆法再落⼦,请重新输⼊!\n");continue;}board[a][b] = 'x'; //⽤'x'表⽰玩家落⼦break;}}//void computer(char board[max_row][max_col]) {printf("电脑落⼦阶段!\n");while (1) {int a = rand() % max_row; //产⽣0到2的随机数int b = rand() % max_col;if (board[a][b] != ' ') { //判断所下位置是否已经有⼦continue;}board[a][b] = 'o';break;}//}//char check(char board[max_row][max_col]) {//检测所有⾏是否相等for (int cow = 0; cow < max_row; cow++) {if (board[cow][0] != ' ' && board[cow][0] == board[cow][1] && board[cow][0]==board[cow][2] ) { return board[cow][0];}}//检测所有列是否相等for (int col = 0; col < max_col; col++) {if (board[0][col] != ' ' && board[0][col] == board[1][col] && board[0][col]==board[2][col] ) {return board[0][col];}}//检测对⾓线是否相等if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[0][0] == board[2][2]) {return board[0][0];}if (board[0][2] != ' ' && board[0][2] == board[1][1] && board[0][2] == board[2][0]) {return board[0][2];}if (pingju(board)) {return 'q';}return 'a';}//判断棋盘是否棋⼦已满,如果满了返回1,未满返回0.int pingju(char board[max_row][max_col]) {for (int row = 0; row < max_row; row++) {for (int col = 0; col < max_col; col++) {if (board[row][col] == ' ') {return 0;}}}return 1;}int main() {char board[max_row][max_col] = {0};//数组初始化为0char winner = 'n';init(board); //将棋盘中元素初始化为空格while (1) {print(board); //打印棋盘play(board); //玩家落⼦winner = check(board); //判断胜负if (winner != 'a') {break;}computer(board); //电脑落⼦winner = check(board); //判断胜负if (winner != 'a') {break;}}if (winner == 'x') {print(board);printf("你赢了!");}else if (winner == 'o') {print(board);printf("你输了!");}else if (winner == 'q') {print(board);printf("和棋!");}return 0;}以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
井字棋游戏(C语言)

说明:该游戏的源代码在后面!//源代码如下:#include <stdio.h>#include <string.h>#include <stdlib.h>#include <conio.h>//这里少了人机对弈的模块,所以要增加个搜索算。
//已经有的是双人对弈的模块,有时间再去弄弄!char chess[8][8], _isexit = '0', A = '*', B = '#', role = '*', Do = '1'; int flag[8][8], Round = 0, result = 3, count = 0;#include "Initilize.h"#include "Print_chess.h"#include "Choose.h"#include "Operation.h"#include "Judge.h"int main() {//getch();chooseRole();getchar(); //缓冲下!return 0;}//#include <stdlib.h>void print_chess() {int i, j;system("cls\n"); //清屏操作;printf(" 我现在持%c 棋:\n", role);printf(" 第%d 回合:\n\n", Round);for(i = 1;i <= 6;i++){if(2 == i || 4 == i) {printf(" __"); chess[i][6] = '_';}else printf(" ");for(j = 1;j <= 6;j++)printf("%c",chess[i][j]);if(2 == i || 4 == i) printf("_");printf("\n");}//测试:for(i = 1;i < 6;i++){ if(2 == i || 4 == i) continue;for(j = 1;j < 6;j++){if(2 == j || 4 == j) continue;printf("%d ",flag[i][j]);}printf("\n");}return ;}//#include <stdio.h>//#include <string.h>void theResult();int judge();void operation();void choose();void chooseRole();void delay();void delay2();void chooseRole() {printf(" 请选择先手:(1 / 0) \n");// 1 代表先手,0 代表后手;initilize();while(1) {printf(" ");role = getch();printf("%c\n",role);if('0' == role || '1' == role) break;role = ' ';print_chess();printf(" 输入错误,请重新输入!\n");delay2();}delay2();if('0' == role) role = A;else role = B;while(1) {print_chess();judge();if(result == 0 || result == 1) {choose(); break;}operation();}return ;}void choose() {theResult();while(1) {printf(" 你想要退出吗?(1 / 0)\n");printf(" ");_isexit = getch();printf("%c\n", _isexit);if('0' == _isexit) {delay2();break;}else if('1' == _isexit) {printf(" 欢迎下次再来!\n ");delay();exit(0);}else { printf(" 抱歉!输入错误,请重新输入!\n");delay2(); delay2();print_chess(); theResult();continue;}}//end of while() ;_isexit = '0', A = '*', B = '#', role = '*', Do = '1';result = 3, count = 0, Round = 0;initilize();chooseRole();return ;}void theResult() {if(result) printf(" 恭喜您!你获得最后的胜利!\n");else printf(" 很遗憾!您输掉了这一局!\n 请再接再厉!\n");return ;}char A0;void operation2();void operation() {int i, j;count ++;if(role == A0) Round ++; //回合数:第几回合。
井字棋游戏源代码

井字棋游戏源代码井字棋C语言源代码#include<graphics.h> #include<math.h>#include<dos.h>#include<bios.h>#include<conio.h> #include<string.h> #define ENTER 0x1c0d #define esc 0x11b #define up 0x4800 #define down 0x5000 #define left 0x4b00 #define right 0x4d00 int i,j,x1,y1,v,n,m,key,d,e,p,q,h,o,x,y,k;int a[3][3]={0,0,0,0,0,0,0,0,0};int score[3][3]={0,0,0,0,0,0,0,0,0};char s [30];int main(){void drawone();void drawtwo();void drawthree(); void drawfour();void drawfive();void drawsix();void xianka();int renzouqi();int xuanze();int py( int a[][3]); int jisuanji();int xuanzeyouxi(); int bioskey(int cmd); xianka();drawone();if((key=bioskey(0))==ENTER) drawtwo();key=bioskey(0);if(key==ENTER)drawfour();v=1;while(v==1){drawthree();n=xuanze();x1=220; y1=230;circle(220,230,20);o=pan_y(a);while(o==2) {if(n==1)n=renzouqi();if(n==-1)n=jisuanji();o=pan_y(a);}drawsix();getch();drawfive();v=xuanzeyouxi();}closegraph();}/*显卡驱动*/void xianka(){int graphdriver,graphmode;int driver=DETECT,mode;initgraph(&driver,&mode,""); graphdriver=DETECT;initgraph(&graphdriver,&graphmode,""); }/*定义判赢函数*/int pan_y(int a[][3]){int n,b,c,i,j,k=2;for(i=0;i<3;i++){n=a[i][0]; b=a[i][1]; c=a[i][2];if(n+b+c==3) k=1;if(n+b+c==-3) k=-1;}for(j=0;j<3;j++){n=a[0][j];b=a[1][j];c=a[2][j];if(n+b+c==3) k=1;if(n+b+c==-3) k=-1;}n=a[0][0]; b=a[1][1]; c=a[2][2];if(n+b+c==3) k=1;if(n+b+c==-3) k=-1;n=a[0][2]; b=a[1][1]; c=a[2][0];if(n+b+c==3) k=1;if(n+b+c==-3) k=-1;if(a[0][0]!=0&&a[0][1]!=0&&a[0][2]!=0&&a[1][0]!=0&&a[1][1]!=0&&a[1][ 2]!=0&&a[2][0]!=0&&a[2][1]!=0&&a[2][2]!=0)k=0;return k;}/*选择哪一方先下*/int xuanze(){key=bioskey(0);n=-1;while(key!=ENTER){if(key==up){setfillstyle(1,3);floodfill(471,150,4);setfillstyle(1,14);floodfill(471,200,4);n=-1; }if(key==down){setfillstyle(1,14);floodfill(471,150,4);setfillstyle(1,3);floodfill(550,200,4);n=1; }key=bioskey(0);}return n;}/*界面1*/void drawone() {for(i=1;i<6;i++){setcolor(i);settextstyle(0,HORIZ_DIR,i); outtextxy(250-43*i,80*i,"Welcome To You");}setcolor(10);settextstyle(0,VERT_DIR,2); outtextxy(20,80,"*** How Do You Do ***");settextstyle(0,VERT_DIR,2); outtextxy(620,80,"*** How Do You Do***");setcolor(13);line(20,0,20,479);line(22,0,22,479);line(600,0,600,479);line(602,0,602,479);}/*界面2*/void drawtwo() {cleardevice();setbkcolor(2);setviewport(0,0,639,479,0); settextstyle(1,0,6);setfillstyle(1,12);rectangle(230,10,370,280); floodfill(150,350,13); settextstyle(1,0,5);setbkcolor(0);outtextxy(250,10,"Jing"); outtextxy(280,100,"Zi"); outtextxy(280,200,"Qi"); settextstyle(1,0,3);setcolor(0);outtextxy(250,300,"Designed by: Zhang Kai Xuan");outtextxy(402,350,"Yang Shao Hua");outtextxy(402,400,"Ren Jian"); }/*界面3*/void drawthree() {cleardevice();setbkcolor(14);setviewport(0,0,639,479,1); setfillstyle(1,2);setcolor(4);line(70,180,370,180);line(70,280,370,280);line(170,80,170,380);line(270,80,270,380);line(470,180,570,180); rectangle(470,130,570,230); setfillstyle(1,3);floodfill(500,150,4);settextstyle(1,0,2);outtextxy(475,190,"person"); outtextxy(475,140,"computer");settextstyle(3,0,1);outtextxy(468,260,"person");circle(557,274,10);setfillstyle(1,13);floodfill(555,274,4);outtextxy(468,300,"computer"); circle(557,308,10);setfillstyle(1,3);floodfill(555,300,4);}/*界面4*/void drawfour() {int i,x5=300,y5=200,r=100,x6=350,y6=200;cleardevice();setbkcolor(8);setcolor(14);settextstyle(1,0,5);setviewport(0,0,639,479,0); for(i=0;i<800;i++){x6=x5+r*cos(i*2*3.1415926/50); y6=y5+r*sin(i*2*3.1415926/50); setcolor(i%15);line(x5,y5,x6,y6);outtextxy(222,350,"Loading.....");}}/*界面5*/void drawfive() {cleardevice();setviewport(0,0,639,479,1);circle(120,240,30);setfillstyle(1,3);circle(115,225,4);circle(135,225,4);floodfill(115,225,2);floodfill(135,225,2);setfillstyle(1,7);floodfill(120,240,2);line(115,251,133,247);settextstyle(1,0,1);outtextxy(400,400,"choose quit or new game");outtextxy(381,325,"NEW");outtextxy(480,325,"QUIT");settextstyle(1,0,5);if(o==-1)outtextxy(200,230,"You lost! Come on!");if(o==1)outtextxy(200,230,"You win!");if(o==0)outtextxy(200,230,"DRAW!"); circle(400,330,25); circle(500,330,25); setfillstyle(1,12); floodfill(400,330,2); }/*界面6*/void drawsix() {settextstyle(1,0,5); setcolor(2);outtextxy(110,410,"GAME OVER!");}/*人下棋*/int renzouqi(){key=bioskey(0);while(key!=ENTER){if(key==up){x=x1; y=y1-100;if(y>80){ setcolor(14);circle(x1,y1,20);setcolor(4);circle(x,y,20);x1=x; y1=y; }}if(key==down){x=x1; y=y1+100;if(y<380){ setcolor(14); circle(x1,y1,20); setcolor(4);circle(x,y,20);x1=x; y1=y; }}if(key==left){x=x1-100; y=y1;if(x>70){ setcolor(14); circle(x1,y1,20); setcolor(4);circle(x,y,20);x1=x; y1=y; }}if(key==right){x=x1+100; y=y1;if(x<370){ setcolor(14); circle(x1,y1,20); setcolor(4);circle(x,y,20);x1=x; y1=y; }}key=bioskey(0);}if(a[(x1-120)/100][(y1-130)/100]==0){ setfillstyle(1,13);floodfill(x1,y1,4);d=(x1-120)/100;e=(y1-130)/100;a[d][e]=1;n=-1;}else n=1;return n; }/*计算机下棋*/int jisuanji() {for(i=0;i<3;i++){for(j=0;j<3;j++){if(a[i][j]!=0) score[i][j]=-2;}}for(i=0;i<3;i++){m=a[i][0]; p=a[i][1]; q=a[i][2];if(m+p+q==2){ if(m==0) score[i][0]=25; if(p==0) score[i][1]=25;if(q==0) score[i][2]=25;}if(m+p+q==-2){ if(m==0) score[i][0]=50; if(p==0) score[i][1]=50;if(q==0) score[i][2]=50;}if(m+p+q==1){ if(p==0 && q==0)score[i][1]=score[i][2]=8;if(m==0 && q==0)score[i][0]=score[i][2]=8;if(p==0 && p==0)score[i][1]=score[i][0]=8;}if(m+p+q==-1){ if(p==0 && q==0)score[i][1]=score[i][2]=10;if(m==0 && q==0)score[i][0]=score[i][2]=10;if(p==0 && m==0)score[i][1]=score[i][0]=10;}if(m+p+q==0){ if(m==0 && p==0 && q==0) score[i][0]=score[i][1]=score[i][2]=4; }}/*end横*/for(i=0;i<3;i++){m=a[0][i]; p=a[1][i]; q=a[2][i];if(m+p+q==2){ if(m==0) {if(score[0][i]<25) score[0][i]=25;}if(p==0) {if(score[1][i]<25) score[1][i]=25;}if(q==0) {if(score[2][i]<25) score[2][i]=25;}}if(m+p+q==-2){ if(m==0) {if(score[0][i]<50) score[0][i]=50;}if(p==0) {if(score[0][i]<50) score[1][i]=50;}if(q==0) {if(score[0][i]<50) score[2][i]=50;}}if(m+p+q==1){ if(p==0 && q==0) { if(score[1][i]<8) score[1][i]=8;if(score[2][i]<8) score[2][i]=8;}if(m==0 && q==0) { if(score[0][i]<8) score[0][i]=8;if(score[2][i]<8) score[2][i]=8;}if(p==0 && m==0) { if(score[1][i]<8) score[1][i]=8;if(score[0][i]<8) score[0][i]=8;}}if(m+p+q==-1){ if(p==0 && q==0) { if(score[1][i]<10) score[1][i]=10; if(score[2][i]<10) score[2][i]=10;}if(m==0 && q==0) { if(score[0][i]<10) score[0][i]=10;if(score[2][i]<10) score[2][i]=10;}if(p==0 && m==0) { if(score[1][i]<10) score[1][i]=10;if(score[0][i]<10) score[0][i]=10;}if(m+p+q==0){ if(m==0 && p==0 && q==0) {if(score[0][i]<4) score[0][i]=4;if(score[1][i]<4) score[1][i]=4;if(score[2][i]<4) score[2][i]=4;}}}/*end竖*/m=a[0][0]; p=a[1][1]; q=a[2][2];if(m+p+q==2){ if(m==0) {if(score[0][0]<25) score[0][0]=25;}if(p==0) {if(score[1][1]<25) score[1][1]=25;}if(q==0) {if(score[2][2]<25) score[2][2]=25;}}if(m+p+q==-2){ if(m==0) {if(score[0][0]<50) score[0][0]=50;}if(p==0) {if(score[1][1]<50) score[1][1]=50;}if(q==0) {if(score[2][2]<50) score[2][2]=50;}}if(m+p+q==1){ if(p==0 && q==0) { if(score[1][1]<8) score[1][1]=8;if(score[2][2]<8) score[2][2]=8;}if(m==0 && q==0) { if(score[0][0]<8) score[0][0]=8;if(score[2][2]<8) score[2][2]=8;}if(p==0 && m==0) { if(score[1][1]<8) score[1][1]=8;if(score[0][0]<8) score[0][0]=8;}}if(m+p+q==-1){ if(p==0 && q==0) { if(score[1][1]<10) score[1][1]=10; if(score[2][2]<10) score[2][2]=10;}if(m==0 && q==0) { if(score[0][0]<10) score[0][0]=10;if(score[2][2]<10) score[2][2]=10;}if(p==0 && m==0) { if(score[1][1]<10) score[1][1]=10;if(score[0][0]<10) score[0][0]=10;}}if(m+p+q==0){ if(m==0 && p==0 && q==0) {if(score[0][0]<4) score[0][0]=4;if(score[1][1]<4) score[1][1]=4;if(score[2][2]<4) score[2][2]=4;}}}/*斜扫描*/m=a[0][2]; p=a[1][1]; q=a[2][0];if(m+p+q==2){ if(m==0) {if(score[0][2]<25) score[0][2]=25;}if(p==0) {if(score[1][1]<25) score[1][1]=25;}if(q==0) {if(score[2][0]<25) score[2][0]=25;}}if(m+p+q==-2){ if(m==0) {if(score[0][2]<50) score[0][2]=50;}if(p==0) {if(score[1][1]<50) score[1][1]=50;}if(q==0) {if(score[2][0]<50) score[2][0]=50;}}if(m+p+q==1){ if(p==0 && q==0) { if(score[1][1]<8) score[1][1]=8; if(score[2][0]<8) score[2][0]=8;}if(m==0 && q==0) { if(score[2][0]<8) score[2][0]=8;if(score[0][2]<8) score[0][2]=8;}if(p==0 && m==0) { if(score[1][1]<8) score[1][1]=8;if(score[0][2]<8) score[0][2]=8;}}if(m+p+q==-1){if(p==0 && q==0) { if(score[1][1]<10) score[1][1]=10; if(score[2][0]<10) score[2][0]=10;}if(m==0 && q==0) { if(score[0][2]<10) score[0][2]=10; if(score[2][0]<10) score[2][0]=10;}if(p==0 && m==0) { if(score[1][1]<10) score[1][1]=10; if(score[0][2]<10) score[0][2]=10;}}if(m+p+q==0){ if(m==0 && p==0 && q==0) {if(score[0][2]<4) score[0][2]=4;if(score[1][1]<4) score[1][1]=4;if(score[2][0]<4) score[2][0]=4;}}/*斜扫描*/h=k=1;for(i=0;i<3;i++) {for(j=0;j<3;j++) {if(score[i][j]>score[h][k]){h=i; k=j; }}}x1=120+100*h;y1=130+100*k;circle(x1,y1,20);setfillstyle(1,3);floodfill(x1,y1,4);a[h][k]=-1;n=1;for(i=0;i<3;i++){for(j=0;j<3;j++)score[i][j]=0;}return n;}/*重新开始游戏*/int xuanzeyouxi() {key=bioskey(0);while(key!=ENTER){if(key==left){ setfillstyle(1,12); floodfill(400,330,2); setfillstyle(1,14); floodfill(500,330,2); settextstyle(1,0,2); v=1;}if(key==right){ setfillstyle(1,12); floodfill(500,330,2); setfillstyle(1,14); floodfill(400,330,2); v=0;}key=bioskey(0);}for(i=0;i<3;i++){ for(j=0;j<3;j++){ a[i][j]=0;}}return v;}。
C语言实现简单的井字棋游戏

C语⾔实现简单的井字棋游戏本⽂实例为⼤家分享了C语⾔实现简单井字棋游戏的具体代码,供⼤家参考,具体内容如下1.什么是井字棋井字棋相信⼤部分⼈都玩过规则:双⽅轮流放⼦,当某⼀⽅的三个⼦连成⼀线(⾏,列,对⾓)时,该⽅获胜。
2.游戏前的准备1. 菜单游戏正式开始前我们可以为⽤户提供⼀个选择菜单,笔者做了⼀个最简单的游戏开始菜单:代码:/*菜单*/int menu(){printf("###########################\n");printf("## 1.Play 2.Exit ##\n");printf("###########################\n");return 0;}2. 打印棋盘要玩游戏我们⾸先要有⼀个3*3的棋盘,棋⼦信息则⽤⼀个⼆维数组来表⽰,并进⾏初始化:char chess[ROW][COL] = { '\0' }; //ROW=3,COL=3显⽰棋盘我们可以写⼀个函数:/*显⽰棋盘*/int ShowBroad(char chess[][COL]){system("cls"); //刷新界⾯printf(" %c | %c |%c Player: X\n",chess[0][0],chess[0][1],chess[0][2]);printf(" ---------- Computer: O\n");printf(" %c | %c |%c \n", chess[1][0], chess[1][1], chess[1][2]);printf(" ----------\n");printf(" %c | %c |%c \n", chess[2][0], chess[2][1], chess[2][2]);}将棋盘打印出来的同时在旁边显⽰出玩家和电脑所⽤的棋⼦。
C语言实现井字棋(三子棋)

C 语⾔实现井字棋(三⼦棋)本⽂实例为⼤家分享了C 语⾔实现井字棋的具体代码,供⼤家参考,具体内容如下⼀、实现思路三⼦棋是九宫格的格式,所以⽤⼆维数组接收数据。
⽤‘O'代表电脑下的⼦,‘X'代表玩家下的⼦。
未下⼦的时候初始化 '‘(space)。
则⼆维数组为“char”类型,⼤⼩为char board[3][3]。
打印出井字的棋盘,同时为了将数据显⽰在每格的中间,⽤空格隔开(“ %c |”)的格式设置棋盘“|”⽤来形成竖,接着打印“- - -|”⽤来形成⾏。
将两部⽤for 循环按照逻辑链接起来,即可打印出“井”。
同时“%c”处初始化为‘ '(space )<1> 玩家下的⼦⽤数组的坐标表⽰,输⼊提⽰:(请输⼊坐标:),输⼊格式为(1 1),范围为1~3。
<2> 玩家下⼦的时候,如果遇到已经下过⼦的坐标,返回并提⽰错误,重新输⼊。
<3> 如果输⼊的坐标超过范围,提⽰错误,重新输⼊。
<4> 打印棋盘,将玩家下⼦的坐标处⽤'X'替换。
<1> 电脑下⼦,利⽤范围为1~3,随机产⽣条件下的坐标,如果遇到已经下过⼦的坐标,就重新产⽣条件下的坐标,这⾥利⽤循环进⾏调试。
<2> 有⼀个电脑下⼦优先规则:a 、电脑下⼦的第⼀个是随机产⽣,在电脑先⼿的时候,第⼆个也是随机产⽣。
b 、判断是否有两个 ‘O”O' 在⾏、列或者斜对⾓,如果有,就将第三个⼦下在可以连成⼀直线的空⽩处(即三点成线,赢得⽐赛)。
如果有连成线但是没⽤空⽩处,进⾏c 步骤。
c 、判断是不是有两个 ‘X”X' 在⾏、列或者斜对⾓练成线,并且第三个空为空⽩。
如果有就将⼦下在该空⽩处(即对玩家进⾏堵截)。
如果没⽤,进⾏d 步骤。
d 、在范围内随机下⼦。
<3> 打印棋盘,将电脑下⼦的坐标处⽤'O'代替。
C语言写的简单井字过三关(初学者入门版)

#include<stdio.h>#include<stdlib.h>#include<time.h>#include<conio.h>void prinf(void);/*这里负责输出与清屏*/int judge_chess(void);/*判断棋局输赢,同时为电脑服务*/int Computer_strategy(void);/*作为电脑攻防优先级*/int compurter_think(void);/*电脑思考,实质是发生随机数*/int a[9]={49,50,51,52,53,54,55,56,57};/*棋盘*/int b_j=0,who=0; /*who为限制使用权限*/struct computer_construe/*作为电脑判断用*/{int n;int attack;}b[8];int main(void){int scan,i,j=5;char compurter_one;prinf();puts("电脑先下,请按y或Y,否则按其它键,默认电脑为X,用户黙认为O");compurter_one=getchar();if(compurter_one=='y'||compurter_one=='Y')/*电脑先下,按y,否则按其它键*/ {compurter_think();prinf();j=4;}for(i=0;i<j;i++){printf("\t\t\t\t\t\t输入时按数字:");do{scanf("%d",&scan);if(scan<0||scan>9)printf("请新重生输入");}while(scan<1||scan>9);if(who==0)who=1;switch(scan){case 1:scan=i;if(a[0]>='1'&&a[0]<='9')a[0]=79;else{--i;printf("己使用\n");}break;case 2:scan=i;if(a[1]>='1'&&a[1]<='9')a[1]=79;else{--i;printf("己使用\n");}break;case 3:scan=i;if(a[2]>='1'&&a[2]<='9')a[2]=79;else{--i;printf("己使用\n");}break;case 4:scan=i;if(a[3]>='1'&&a[3]<='9')a[3]=79;else{--i;printf("己使用\n");}break;case 5:scan=i;if(a[4]>='1'&&a[4]<='9')a[4]=79;else{--i;printf("己使用\n");}break;case 6:scan=i;if(a[5]>='1'&&a[5]<='9')a[5]=79;else{--i;printf("己使用\n");}break;case 7:scan=i;if(a[6]>='1'&&a[6]<='9')a[6]=79;else{--i;printf("己使用\n");}break;case 8:scan=i;if(a[7]>='1'&&a[7]<='9')a[7]=79;else{--i;printf("己使用\n");}break;case 9:scan=i;if(a[8]>='1'&&a[8]<='9')a[8]=79;else{--i;printf("己使用\n");}break;}if(scan==i){if(!(judge_chess()))if(Computer_strategy())compurter_think();who=0;prinf();if(judge_chess()){if(judge_chess()==3)puts("YOU WIN");elseputs("COM WIN");break; }}scan=j;}if(scan==j)puts("a draw in chess");/*和局*/getch();return 0;}void prinf(void){int i;system("cls");for(i=0;i<7;i++)puts("");for(i=1;i<4;i++){printf("\t\t\t %c | %c | %c \n",a[3*(i-1)],a[3*(i-1)+1],a[3*(i-1)+2]);if(i!=3)printf("\t\t\t_________________\n");}}int judge_chess(void){int i,j,k,m,n,X,O;for(i=1;i<4;i++){k=0;X=0;O=0;for(j=0;j<9;j++){switch(i){case 1:m=a[j];break;case 2:m=a[j%3*3+k];break;case 3:if(j<6) m=a[2*(2-k)*(j%3+k)];break;}switch(m){case 'O':++O;break;case 'X':--X;break;default:switch(i){case 1:n=j;break;case 2:n=j%3*3+k;break;case 3:if(j<6) n=2*(2-k)*(j%3+k);break;}}if(2==j%3){m=O+X;switch(m){case 2:if(who){b[b_j].n=n;b[b_j].attack=-1;++b_j;}break;case 3:return 3;break;case -2:if(who){b[b_j].n=n;b[b_j].attack=1;++b_j;}break;case -3:return -3;break;}++k;X=0;O=0;}}if(i==3&&j>5)return 0;}}int Computer_strategy(void)/*电脑战略*/{int i;for(i=0;i<b_j;i++)if(b[i].attack==1&&a[b[i].n]!=88&&a[b[i].n]!=79){a[b[i].n]=88;return 0;}for(i=0;i<b_j;i++)if(b[i].attack!=1&&a[b[i].n]!=88&&a[b[i].n]!=79){a[b[i].n]=88;return 0;}return 1;}int compurter_think(void){int i,j,k=1,m=0;srand(time(NULL));do{m=rand()%10;if(m==0&&a[4]!=88&&a[4]!=79){a[4]=88;return 0;} }while(m==0);for(j=0;j<9;j++)for(i=0;i<9;i++)/*选译功能*/if(a[i]>='1'&&a[i]<='9'){if(k==m){a[i]=88;return 0;}k++;}}。
C语言代码实现井字棋游戏

C语⾔代码实现井字棋游戏井字棋是⼀个很常见的⼩游戏。
要求对战双⽅在⼀个“井”形的棋盘⾥分别下“*”棋⼦或者“#”棋⼦,谁先连成3个,谁就赢。
本次使⽤C语⾔来实现这个⼩游戏。
由于使⽤函数较多,所以采⽤多⽂件的⽅式⽅便梳理代码思路。
本次⼀共有三个⽂件:**game.h:包含了game.c⽂件中函数的声明和库函数的声明。
**#pragma once#define ROW 3#define COL 3#include<stdio.h>#include<stdlib.h>#include<time.h>void InitBoard(char board [ROW][COL], int row, int col); //初始化棋盘void DisplayBoard(char board [ROW][COL], int row, int col); //打印棋盘void PlayerBoard(char board [ROW][COL], int row, int col); //玩家⾛void ComputerBoard(char board [ROW][COL], int row, int col);//电脑⾛char IsWin(char board [ROW][COL], int row, int col); //结果判断game.c:包含游戏过程中关键函数的函数主体。
#define _CRT_SECURE_NO_WARNINGS#include"game.h"void InitBoard(char board[ROW][COL], int row, int col){//初始化棋盘int i = 0;for (i = 0; i < row; i++){int j = 0;for (j = 0; j < col; j++)board [i][j] = ' ';}}void DisplayBoard(char board [ROW][COL], int row, int col){//打印棋盘int i = 0;for (i = 0; i < row; i++){int j = 0;for (j = 0; j < col; j++){printf(" %c ", board [i][j]);if (j < col - 1)printf("|");}printf("\n");if (i < row - 1){for (j = 0; j < col; j++){printf("---");if (j < col - 1)printf("|");}printf("\n");}}}void PlayerBoard(char board [ROW][COL], int row, int col){int x = 0, y = 0;while (1){printf("输⼊棋⼦坐标->");scanf("%d%d", &x, &y);if (x > 0 && x <= row && y > 0 && y <= col){if (board [x - 1][y - 1] == ' '){board [x - 1][y - 1] = '*';break;}else{printf("此位置已有棋⼦!请重新输⼊。
C语言版井字棋

十五、井字棋ﻫ【问题描述】ﻫ设计一个井字棋得游戏程序【基本要求】ﻫ游戏规则就是:从一个空得棋盘开始,人为x一方,计算机为o一方,人机双方轮流放置棋子,人下得时候,字符x将被放入棋盘中指定得位置,轮到计算机下时,程序将扫描棋盘,并将字符o放入扫描到得第一个空格,某一方有3枚棋子占据了一横行、一竖行或一对角线则获胜,若整个棋盘被占满还没有一方获胜则为与局、截图:代码://author GXU-pengcheng#include〈stdio、h>#include〈windows。
h>#include<conio、h>void Help();//帮助intPlay(char player,int choice);//对弈voidInit();//初始化棋盘void Display();//打印棋盘void Black(int choice);//黑棋voidWhite(intchoice);//白棋voidBlock(intchoice);//添加选择框voidClear(int choice);//清空之前得选择框int Judge(int choice,char symbol);//判断输赢返回值0为无结果,1为获胜,2为平局int Funcx(intchoice);//将标号转换为行号int Funcy(int choice);//将标号转换为列号voidEnd(char symbol);//结束int Found();//返回第一个没有棋子得位置得编号char a[31][64];//用数组存放棋盘与棋子,a[行][列]int b=0;//棋子个数int main(){char player;ﻩint choice;system("title 井字棋");//设置标题system("mode con cols=64 lines=35”);//设置窗口大小system("color 70”);//设置颜色while(1){printf(”\n\n\n\n\n\n\t\t\t井\t字\t棋”);printf("\n\n\n\n\n\n\n\n\t\t\t1、玩家先手\n\n\t\t\t2。
c语言井字棋双人版

#include<stdio.h>#include<stdlib.h>#include<string.h>int main(void){system("mode con:cols=30 lines=10");system("color 0b");system("title 迷你双人版井字棋");for(;;){system("cls");char qipan[3][3]={{'+','+','+'},{'+','+','+'},{'+','+','+'}};int i=0,a=0,b=0,k=0,time=0,c=0,s=0;for(i=0;i<=2;i++){for(k=0;k<=2;k++){printf(" %c ",qipan[i][k]);}printf("\n\n");}for(time=1;time<=9;time++){printf("请玩家一输入坐标:");start:scanf("%d,%d",&b,&a);a-=1;b-=1;if(qipan[a][b]!='+'){printf("输入错误,请在此输入:");goto start;}else{qipan[a][b]='@';system("cls");for(i=0;i<=2;i++){for(k=0;k<=2;k++){printf(" %c ",qipan[i][k]);}printf("\n\n");}}if(qipan[0][0]!='+'&qipan[0][1]!='+'&qipan[0][2]!='+'&qipan[1][0]!='+'&qipan[1][1]!='+'&qipan[1][ 2]!='+'&qipan[2][0]!='+'&qipan[2][1]!='+'&qipan[2][2]!='+'){printf("和棋!!!!\n");goto end;}if(qipan[0][0]=='@'&qipan[0][0]==qipan[1][0]&qipan[1][0]==qipan[2][0]|qipan[0][1]=='@'&qipan [0][1]==qipan[1][1]&qipan[1][1]==qipan[2][1]|qipan[0][2]=='@'&qipan[0][2]==qipan[1][2]&qipan[0][2]==qipan[2][2]|qipan[0][0]=='@'&qipan[0][0]==qipan[0][1]&qipan[0][0]==qipan[0][2]|qipan[1][0]=='@'&qipan[1][0]==qipan[1][1]&qipan[1][0]==qipan[1][2]|qipan[2][0]=='@'&qipan[2][0]== qipan[2][1]&qipan[2][0]==qipan[2][2]|qipan[0][0]=='@'&qipan[0][0]==qipan[1][1]&qipan[0][0]== qipan[2][2]|qipan[0][2]=='@'&qipan[0][2]==qipan[1][1]&qipan[1][1]==qipan[2][0]){printf("玩家一获胜!\n");goto end;}printf("请玩家二输入坐标:");second:scanf("%d,%d",&b,&a);a-=1;b-=1;if(qipan[a][b]!='+'){printf("输入错误,请在此输入:");goto second;}else{qipan[a][b]='#';system("cls");for(i=0;i<=2;i++){for(k=0;k<=2;k++){printf(" %c ",qipan[i][k]);}printf("\n\n");}}if(qipan[0][0]=='#'&qipan[0][0]==qipan[1][0]&qipan[1][0]==qipan[2][0]|qipan[0][1]=='#'&qipan[0 ][1]==qipan[1][1]&qipan[1][1]==qipan[2][1]|qipan[0][2]=='#'&qipan[0][2]==qipan[1][2]&qipan[0] [2]==qipan[2][2]|qipan[0][0]=='#'&qipan[0][0]==qipan[0][1]&qipan[0][0]==qipan[0][2]|qipan[1][ 0]=='#'&qipan[1][0]==qipan[1][1]&qipan[1][0]==qipan[1][2]|qipan[2][0]=='#'&qipan[2][0]==qipa n[2][1]&qipan[2][0]==qipan[2][2]|qipan[0][0]=='#'&qipan[0][0]==qipan[1][1]&qipan[0][0]==qipa n[2][2]|qipan[0][2]=='#'&qipan[0][2]==qipan[1][1]&qipan[1][1]==qipan[2][0]){printf("玩家二获胜!\n");goto end;}}end:printf("请按任意键重新开始:");system("pause>nul");system("cls");}}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
代码:
//@author GXU-pengcheng
#include<stdio.h>
#include<windows.h>
#include<conio.h>
void Help();//帮助
int Play(char player,int choice);//对弈
void Init();//初始化棋盘
}
}
}
void Block(int choice){//添加选择框
int x,y;
for(x=Funcx(choice)+1;x<Funcx(choice)+10;x++)
{
for(y=Funcy(choice)+2;y<Funcy(choice)+20;y++)
if(x==Funcx(choice)+1||x==Funcx(choice)+9)
&&a[Funcx(choice)+2][Funcy(choice+1)+4]==a[Funcx(choice)+2][Funcy(choice+2)+4])
)
||
a[Funcx(5)+2][Funcy(5)+4]==symbol
&&
(
(a[Funcx(1)+2][Funcy(1)+4]==a[Funcx(5)+2][Funcy(5)+4]
for(x=Funcx(choice)+1;x<Funcx(choice)+10;x++)
{
for(y=Funcy(choice)+2;y<Funcy(choice)+20;y++)
if(x==Funcx(choice)+1||x==Funcx(choice)+9)
{
if(y==Funcy(choice)+2||y==Funcy(choice)+19)
else
a[x][y]='|';
}
else if(x==Funcx(1)||x==Funcx(4)||x==Funcx(7)||x==Funcx(4)+Funcx(7))
a[x][y]='-';
else
a[x][y]=' ';
}
}
//for(x=1;x<=7;x=x+3)//在每个格子的左上角添加序号,范围1~9
a[x][y]=' ';
else if(y<Funcy(choice)+7||y>Funcy(choice)+14)
a[x][y]=' ';
}
else if(x<Funcx(choice)+4||x>Funcx(choice)+6)
if(y==Funcy(choice)+2||y==Funcy(choice)+19)
}else if((get=='a'||get=='A')&&(choice+2)/3==(choice+1)/3){
Clear(choice);
choice -= 1;
}else if((get=='d'||get=='D')&&(choice-1)/3==choice/3){
Clear(choice);
if(y==Funcy(choice)+2||y==Funcy(choice)+19)
a[x]y]='|';
}
}
void Clear(int choice){//清空选择框
int x,y,i;
for(i=1;i<=9;i++){
if(a[Funcx(choice)+1][Funcy(choice)+2]=='+'&&i!=choice){
symbol='*';
}
if((c=Judge(choice,symbol))!=0){//判断输赢
if(c==2)
symbol='=';
c=0;
End(symbol);
break;
}
}
return 0;
}
void Init(){//初始化棋盘
int x=0,y=0;
int i='1';
for(x=0;x<31;x++)
int c=0;//Judge得出的结果
while(1){
system("cls");//每次循环清屏一次
Display();
if(player=='1'){//玩家下棋
while(1){//确定要下的位置
if((get=getch())!=' '){
if(get=='4'){
system("cls");
void Display();//打印棋盘
void Black(int choice);//黑棋
void White(int choice);//白棋
void Block(int choice);//添加选择框
void Clear(int choice);//清空之前的选择框
int Judge(int choice,char symbol);//判断输赢返回值0为无结果,1为获胜,2为平局
}
void End(char symbol){//结束
system("cls");
Display();
if(symbol=='*')
&&a[Funcx(5)+2][Funcy(5)+4]==a[Funcx(9)+2][Funcy(9)+4])
||
(a[Funcx(3)+2][Funcy(3)+4]==a[Funcx(5)+2][Funcy(5)+4]
&&a[Funcx(5)+2][Funcy(5)+4]==a[Funcx(7)+2][Funcy(7)+4])
if(player=='1'){
Init();
Block(5);
choice=5;
Play(player,choice);
}else if(player=='2'){
Init();
Play(player,choice);
}else if(player=='3'){
Help();
getch();
system("cls");
{
a[x][Funcy(choice)+d]='*';
a[x][Funcy(choice)+20-d]='*';
d=d+2;
}
}
int Judge(int choice,char symbol){//判断输赢返回值0为无结果,1为获胜,2为平局
if(
(
(a[Funcx(choice)+2][Funcy(choice)+4]==a[Funcx(choice+3)+2][Funcy(choice)+4]
printf("\n\n\n\n\n\n\t\t\t井\t字\t棋");
printf("\n\n\n\n\n\n\n\n\t\t\t1.玩家先手\n\n\t\t\t2.电脑先手\n\n\t\t\t3.帮助\n\n\t\t\t4.退出\n\n\n\n\n\t\t请输入: ");
player=getch();
printf("\t 'W'上移, 'S'下移, 'A'左移, 'D'右移\n\n");
printf("\t\t游戏中按'4'退出");
printf("\n\n\n\n\t\t按任意键退出");
}
int Play(char player,int choice){//对弈
char get;
char symbol;
}else{
Clear(choice);
break;
}
}
}//while结束
b++;
Black(choice);
player='0';
symbol='#';
}//if结束
else{//电脑下棋
choice=Found();
Block(choice);
b++;
White(choice);
player='1';
)
)
return 1;
if(b==9)
return 2;
return 0;
}
int Funcx(int choice){//将标号转换为行号
return (((choice-1)/3)%3)*10;
}
int Funcy(int choice){//将标号转换为列号