当前位置:首页 > 通信资讯 > 正文

用c语言编写井字棋游戏(井字棋程序c语言)

本文实例为大家分享了C语言实现简单井字棋游戏的具体代码,供大家参考,具体内容如下

游戏截图

用c语言编写井字棋游戏(井字棋程序c语言)

用c语言编写井字棋游戏(井字棋程序c语言)

源代码

person.h

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 //玩家对战 void person() { int i,j; initMap(map); //打印棋局 displayMap(map); //未分出胜负且棋局未落满子前无限循环 while(1) { //获取玩家下子位置 getXY(&i,&j); //玩家落子 setPiece(map,i,j); //清屏 system("cls"); //打印棋局 displayMap(map); //玩家落子后判断是否已经分出胜负 if(isWin(map)==1) { //输出获胜方 displayWinner(); break; } //是否棋局已满 else if(count==row*col) { printf("平局!\n"); break; } //交替到对手回合 exchangeTurn(); } }

computer.h

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 //局面价值 int getSum(char map[][col]) { int sumO=0,sumX=0; int i,j; for(i=0;i<row;i++) { for(j=0;j<col;j++) { if(map[i][j]=='O') { sumO+=score[i][j]; } else if(map[i][j]=='X') { sumX+=score[i][j]; } } } return sumX-sumO; } //思考 void think(char map[][col]) { int canWin=0; int i,j; int x=-1,y=-1; int sum; int maxSum=-20; count++; for(i=0;i<row;i++) { for(j=0;j<col;j++) { if(map[i][j]==m) { map[i][j]='X'; //能获胜,直接落子此处 if(isWin(map)) { return; } //不能获胜,落子在分数最多的地方 else { map[i][j]='O'; if(isWin(map)) { map[i][j]='X'; return; } map[i][j]='X'; sum=getSum(map); if(sum>maxSum) { maxSum=sum; x=i; y=j; } } map[i][j]=m; } } } map[x][y]='X'; } void computer() { int i,j; char computerType='X'; initMap(map); //打印棋局 displayMap(map); //未分出胜负且棋局未落满子前无限循环 while(1) { //电脑落子 if(nowType==computerType) { printf("电脑落子:"); think(map); } //玩家落子 else { //获取玩家下子位置 getXY(&i,&j); //玩家落子 setPiece(map,i,j); } //清屏 system("cls"); //打印棋局 displayMap(map); //玩家落子后判断是否已经分出胜负 if(isWin(map)==1) { //输出获胜方 displayWinner(); break; } //是否棋局已满 else if(count==row*col) { printf("平局!\n"); break; } //交替到对手回合 exchangeTurn(); } }

main.c

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 #include<stdio.h> #include<stdlib.h> //棋局 char map[3][3]; //行列 int row=3,col=3; //棋子类型 char o='O',x='X',m=' '; //当前棋子类型 char nowType='O'; //已落子数量 int count=0; //初始化棋局 void initMap(char map[][col]) { int i,j; for(i=0;i<row;i++) { for(j=0;j<col;j++) { map[i][j]=m; } } } //打印当前棋局 void displayMap(char map[][col]) { int i,j; printf(" "); for(i=0;i<col;i++) { printf("%d ",i); } printf("\n "); for(i=0;i<2*col+1;i++) { printf("*"); } printf("\n"); for(i=0;i<row;i++) { printf("%d ",i); for(j=0;j<col;j++) { printf("%c ",map[i][j]); } printf("\n "); for(j=0;j<2*col+1;j++) { printf("*"); } printf("\n"); } } //获取用户输入 void getXY(int *i,int *j) { while(1) { printf("落子方:%c\n",nowType); printf("落子位置(x,y)="); scanf("%d %d",i,j); if(*i<0||*i>=row||*j<0||*j>=col||map[*i][*j]!=m) { printf("输入不合法!\n"); } else { return; } } } //交替下子 void exchangeTurn() { if(nowType==o) { nowType=x; } else { nowType=o; } } //下子 void setPiece(char map[][col],int i,int j) { map[i][j]=nowType; count++; } //判断是否分出胜负,分别从横竖斜三个方向数数 int isWin(char map[][col]) { int i,j; int flagR,flagC; for(i=0;i<row;i++) { flagR=0,flagC=0; for(j=0;j<col;j++) { if(map[i][j]==o) { flagR++; } else if(map[i][j]==x) { flagR--; } if(map[j][i]==o) { flagC++; } else if(map[j][i]==x) { flagC--; } } if(flagR==col||flagC==col||flagR==(-col)||flagC==(-col)) { return 1; } } flagR=0,flagC=0; for(i=0,j=0;i<row&&j<col;i++,j++) { if(map[i][j]==o) { flagR++; } else if(map[i][j]==x) { flagR--; } if(map[i][col-j-1]==o) { flagC++; } else if(map[i][col-j-1]==x) { flagC--; } } if(flagR==col||flagC==col||flagR==(-col)||flagC==(-col)) { return 1; } else { return 0; } } //输出胜方 void displayWinner() { printf("%c方获得胜利!\n",nowType); } //给局面打分的基础表(站位分) int score[3][3]={ {4,2,4}, {2,8,2}, {4,2,4} }; //引入自定义头文件 #include"person.h" #include"computer.h" int main() { int gameType; printf("1.人机对战\n其他.玩家对战\n"); scanf("%d",&gameType); if(gameType==1) { computer(); } else { person(); } return 0; }

代码解析

1、 其实棋类游戏设计最重要的就是模拟下棋的过程。
2、 我们知道井字棋是双方交替下子,一方执O,一方执X。
3、 比如O先下,玩家下完子后,我们判断一下此时他是否已经获胜(即是否出现横或竖或两斜线出现三子连珠的情况),没有获胜则判断是否已经将棋局下满子了,还是没有的话,轮到X的回合。再次执行此步骤。
4、 了解了过程就自然好设计了。因为棋局比较简单,我们用一个二维字符数组即可存储。落子位置用坐标(x,y),通过玩家输入即可进行模拟。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_36694133/article/details/104398793

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。