C语言实现三子棋(通过数组)
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <time.h> |
- 创建一个全局数组
因为如果数组大小变化,游戏规则和实现思路都会有很大的变化,所以不进行宏定义常量来定义数组
| int main() |
| { |
| |
| do |
| { |
| int i = 0; |
| printf("1.Start the game\n"); |
| printf("2.Quit the game\n"); |
| printf("Please enter your options->"); |
| scanf("%d", &i); |
| switch (i) |
| { |
| case 1: |
| game(); |
| break; |
| case 2: |
| return 0; |
| default: |
| printf("Input error,please enter again!\n"); |
| break; |
| } |
| } while(1); |
| } |
| void game() |
| { |
| initBoard(); |
| srand((unsigned)time(NULL)); |
| char temp = ' '; |
| do |
| { |
| printBoard(); |
| userPlay(); |
| temp = judge(); |
| if (temp != ' ') |
| break; |
| robotPlay(); |
| temp = judge(); |
| } while (temp == ' '); |
| printBoard(); |
| switch (temp) |
| { |
| case '@': |
| printf("User WIN!\n"); |
| break; |
| case '$': |
| printf("Bobot WIN!\n"); |
| break; |
| case '*': |
| printf("Dogfall !\n"); |
| break; |
| dafault: |
| break; |
| } |
| } |
- 设计棋盘样式
有兴趣的可以搞的更加花里胡哨,这里草草了事,呸,简单设计一下 哈
- 打印棋盘
重点就在这了,棋盘设计的再牛逼,也得能打印出来才行
这里棋盘设计的比较简易,打印起来也比较简单,关键是思路要清晰
| void printBoard() |
| { |
| printf("##########\n"); |
| int i = 0,j = 0; |
| for (i = 0; i < 3; i++) |
| { |
| for (j = 0; j < 3; j++) |
| { |
| if (j != 2) |
| { |
| printf("%c|",board[i][j]); |
| } |
| else |
| printf("%c\n",board[i][j]); |
| |
| } |
| if (i != 2) |
| printf("_|_|_\n"); |
| } |
| printf("##########\n"); |
| } |
- 玩家下棋
本人习惯用界面编程的坐标系表示坐标,因此下面赋值时横坐标和纵坐标倒过来了
| void userPlay() |
| { |
| printf("Please enter coordinates->"); |
| int x = 0,y = 0; |
| while(1) |
| { |
| scanf("%d %d", &x, &y); |
| if (x > 0 && x < 4 && y > 0 && y < 4) |
| { |
| |
| if (board[y - 1][x - 1] == ' ') |
| { |
| board[y - 1][x - 1] = '@'; |
| break; |
| } |
| else |
| printf("There are chess pieces here,please enter again!->"); |
| } |
| else |
| printf("Input error,please enter again!->"); |
| } |
| } |
| |
- 电脑下棋
这里关键是 要生成可行的坐标,以及随机数的生成
| void robotPlay() |
| { |
| int i = 0, j = 0; |
| while (1) |
| { |
| i = rand() % 3; |
| j = rand() % 3; |
| if (board[i][j] == ' ') |
| { |
| board[i][j] = '$'; |
| break; |
| } |
| } |
| |
| } |
- 判断游戏胜负
这里就到了最难分析的一部分了
废话不多说,直接看代码
| char judge() |
| { |
| int i = 0, j = 0; |
| |
| if (board[i][j] == board[i + 1][j + 1] && board[i][j] == board[i + 2][j + 2] && board[i][j] != ' ') |
| return board[i][j]; |
| if (board[i][j + 2] == board[i + 1][j + 1] && board[i][j + 2] == board[i + 2][j] && board[i][j] != ' ') |
| return board[i][j + 2]; |
| |
| for (i = 0,j = 0; j < 3; j++) |
| { |
| if (board[i][j] == board[i + 1][j] && board[i][j] == board[i + 2][j] && board[i][j] != ' ') |
| return board[i][j]; |
| } |
| for (i = 0,j = 0; i < 3; i++) |
| { |
| if (board[i][j] == board[i][j + 1] && board[i][j] == board[i][j + 2] && board[i][j] != ' ') |
| return board[i][j]; |
| } |
| |
| for (i = 0; i < 3; i++) |
| { |
| for (j = 0;j < 3; j++) |
| { |
| if (board[i][j] == ' ') |
| return board[i][j]; |
| } |
| } |
| |
| return '*'; |
| |
| } |
源代码链接(Github)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】