【牛客刷题】上手用C语言写一个三子棋小游戏超详解哦(电脑优化)
作者:[南航科院小张
南航科院小张的博客
专栏:从c语言的入门到进阶
学习知识不只是要懂,还要会用;想要找到好的工作,这里给大家介绍一件可以斩获诸多大厂offer的利器–牛客网
点击免费注册和我一起开始刷题之路吧!!!
文章目录
- 前言
- 这个代码也可以运用于五子棋!!!
- 一、游戏来历及规则介绍
- 二、代码详解及步骤
- 1.构建框架
- 2.游戏菜单
- 2.1.main函数的内容
- 3.(大头)game函数的实现!!!
- 3.1棋子的实现
- 3.2 棋盘的打印
- 3.3玩家下棋
- 3.4电脑下棋(优化电脑下棋)
- 3.5判断输赢
- 代码总解析(全部代码)
- text.c代码
- game.c代码
- game.h头文件代码
- 总结
前言
用C语言写一个三子棋小游戏
这个代码也可以运用于五子棋!!!
一、游戏来历及规则介绍
是黑白棋的一种。三子棋是一种民间传统游戏,又叫九宫棋、圈圈叉叉、一条龙、井字棋等。将正方形对角线连起来,相对两边依次摆上三个双方棋子,只要将自己的三个棋子走成一条线,对方就算输了。但是,有很多时候会出现和棋的情况。
游戏规则
如果两个人都掌握了技巧,那么一般来说就是平棋。一般来说,第二步下在中间最有利(因为第一步不能够下在中间),下在角上次之,下在边上再次之。最大的好处就是随便找个地方就可以玩这个简单而有趣的游戏了。
二、代码详解及步骤
1.构建框架
text.c源文件是主代码,框架都在该源文件中;
game.c源文件是游戏函数代码的实现的地方,就是实现函数的地方;
game.h是引用头文件和函数声明的地方;
分三个地方写代码看似麻烦,其实让代码更好地实现功能和分装等等功能。(函数的声明和实现一般都是如此,分开来写的,要学会!!!)
2.游戏菜单
void menu() {printf("****************************\n");printf("*** 1.Play 0.Exit ***\n");printf("****************************\n");
}
简单的菜单,也可以写的漂亮一点,也可以写中文都可以的,自己想搞咋样的菜单就搞上去,花里胡哨的都可以的;
2.1.main函数的内容
int main()
{int input = 0;srand((unsigned int)time(NULL));do {menu();printf("请输入>\n");scanf("%d", &input);switch (input) {case Play: game();break;case Exit:printf("退出游戏\n"); break;default:printf("输入错误,请重新输入:\n"); break;}} while (input);return 0;
}
主要就是输入1或者0或者其他东西嘛,1就调用game函数开始玩游戏咯,0就是退出游戏,跳出循环;要是输入其他的就提醒一下输入错误,重新输入;大头是game函数的实现!!!
3.(大头)game函数的实现!!!
3.1棋子的实现
下棋我们就输入一下坐标来下棋落子,这样就要我们用一下二维数组了,实际上也是贯穿整个游戏代码的,这个非常重要啊各位,就先写个二维数组表示棋子,但是大家要知道一开始棋盘上是没有棋子的,所以我们先放空格到数组里面;
这个是在text.c上面的二维数组函数代码;
上面那个明显的就是在game.h头文件是函数的声明啦
void Chess(char chess[Row][Col], int row, int col) {int i = 0, j = 0;for (i = 0; i < row; i++) {for (j = 0; j < col; j++) {chess[i][j] =' ';}printf("\n");}
}
上面代码是二维数组的初始化,全部都是空格,便于下棋子;
代码下面图片非常重要,解释一下,在game.h头文件里定义的行和列,就是棋盘的行列,打印棋盘时和棋子坐标要用的,这样定义更好该,换成五子棋的时候3该5就行,再优化一下代码就是五子棋了!!!
后面下棋的时候直接将数组赋值的,我们玩家下的棋我就赋’X’,电脑是’O’;
3.2 棋盘的打印
头文件中的打印棋盘的函数定义;
text.c中的打印棋盘函数;
void Chessboard(char chess[Row][Col], int row, int col) {int i = 0, j = 0;for (i = 0; i < row; i++) {for (j = 0; j < col; j++) {printf(" %c ",chess[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");}}
}
上面代码是打印棋盘的;
棋盘有点简陋哈,不过还是可以玩的哈,其中’|‘和’—‘,两个符号构成了棋盘,右边是少打印一次’|‘的,最下面是少打印’—'一行的,所以有了上面代码的i<row-1和j<col-1;
3.3玩家下棋
玩家下棋函数(text.c)
void playermove(char chess[Row][Col], int row, int col) {int x = 0, y = 0;printf("玩家走\n");while (1) {printf("请输入要下的棋子坐标\n");scanf("%d %d", &x, &y);if (x <= row && x >= 1 && y >= 1 && y <= col) {if (chess[x - 1][y - 1] == ' ') {chess[x - 1][y - 1] = 'X';break;}elseprintf("坐标被占用,请重新输入\n");}elseprintf("非法输入,请重新输入\n");}
}
玩家输入坐标下棋,实现的话是前提是空格才能落子!!!(注意代码实现)
3.4电脑下棋(优化电脑下棋)
主函数中电脑下棋函数调用(text.c)
#include "game.h"
void Chess(char chess[Row][Col], int row, int col) {int i = 0, j = 0;for (i = 0; i < row; i++) {for (j = 0; j < col; j++) {chess[i][j] =' ';}printf("\n");}
}
void Chessboard(char chess[Row][Col], int row, int col) {int i = 0, j = 0;for (i = 0; i < row; i++) {for (j = 0; j < col; j++) {printf(" %c ",chess[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 playermove(char chess[Row][Col], int row, int col) {int x = 0, y = 0;printf("玩家走\n");while (1) {printf("请输入要下的棋子坐标\n");scanf("%d %d", &x, &y);if (x <= row && x >= 1 && y >= 1 && y <= col) {if (chess[x - 1][y - 1] == ' ') {chess[x - 1][y - 1] = 'X';break;}elseprintf("坐标被占用,请重新输入\n");}elseprintf("非法输入,请重新输入\n");}
}
int AImove(char chess[Row][Col], int row, int col) {int i = 0, j = 0;if (chess[0][0] == chess[1][1] && chess[1][1] == chess[0][2] && chess[1][1] != ' ' && chess[2][2] == ' '&&chess[2][0]==' '){chess[2][0] = 'O';return 1;}for (i = 0; i < row; i++) {if (chess[i][0] == chess[i][1]&&chess[i][1]!=' '&&chess[i][2]==' ') {chess[i][2] = 'O';return 1;}if (chess[i][1] == chess[i][2] && chess[i][1] != ' '&&chess[i][0]==' ') {chess[i][0] = 'O';return 1;}if (chess[i][0] == chess[i][2] && chess[i][0] != ' '&&chess[i][1]==' ') {chess[i][1] = 'O';return 1;} }for (j = 0; j < col; j++) {if (chess[0][j] == chess[1][j] && chess[1][j] != ' '&&chess[2][j]==' ') {chess[2][j] = 'O';return 1;}if (chess[1][j] == chess[2][j] && chess[1][j] != ' '&&chess[0][j]==' ') {chess[0][j] = 'O';return 1;}if (chess[0][j] == chess[2][j] && chess[2][j] != ' '&&chess[1][j]==' ') {chess[1][j] = 'O';return 1;}}if (chess[1][1] == chess[0][0] && chess[1][1] != ' '&&chess[2][2]==' '){chess[2][2] = 'O';return 1;}if (chess[1][1] == chess[2][2] && chess[1][1] != ' '&&chess[0][0]==' '){chess[0][0] = 'O';return 1;}if (chess[2][2] == chess[0][0] && chess[2][2] != ' '&&chess[1][1]==' '){chess[1][1] = 'O';return 1;}if (chess[1][1] == chess[0][2] && chess[1][1] != ' '&&chess[2][0]==' '){chess[2][0] = 'O';return 1;}if (chess[1][1] == chess[2][0] && chess[1][1] != ' '&&chess[0][2]==' '){chess[0][2] = 'O';return 1;}if (chess[0][2] == chess[2][0] && chess[2][0] != ' '&&chess[1][1]==' '){chess[1][1] = 'O';return 1;}
}
void Computermove(char chess[Row][Col], int row, int col) {printf("电脑走\n");while (1) {int ret = AImove(chess, Row, Col);if (ret == 1)break;else {int x = rand() % row;int y = rand() % col;if (chess[x][y] == ' ') {chess[x][y] = 'O';break;}}}
}
int Isfull(char chess[Row][Col], int row, int col) {int i = 0, j = 0;for (i = 0; i < row; i++) {for (j = 0; j < col; j++) {if (chess[i][j] == ' ')return 1;}}return 0;
}
char Result(char chess[Row][Col], int row, int col) {int i = 0, j = 0;for (i = 0; i < row; i++) {if (chess[i][0] == chess[i][1]&&chess[i][1] == chess[i][2] && chess[i][1] != ' ') {return chess[i][0];}}for (j = 0; j < col; j++) {if (chess[0][j] == chess[1][j]&&chess[1][j] == chess[2][j] && chess[1][j] != ' ') {return chess[1][j];}}if (chess[0][0] == chess[1][1]&&chess[1][1] == chess[2][2] && chess[1][1] != ' ') {return chess[1][1];}if (chess[0][2] == chess[1][1]&&chess[1][1] == chess[2][0] && chess[1][1] != ' ') {return chess[1][1];}int ret=Isfull(chess, row, col);if (ret == 1) {return 'C';}elsereturn 'P';
}
电脑是随机落子的,但是要是遇到了两个棋子是一样的,只要是一条线上的三个点中两个点有棋子,横竖斜着的都是如此,说明不是玩家快赢了就是电脑快赢了,电脑都要堵住那个口,防止或者构成一条线!!!
3.5判断输赢
这个是在主函数上的判断函数,用返回值来判断结果
int Isfull(char chess[Row][Col], int row, int col) {int i = 0, j = 0;for (i = 0; i < row; i++) {for (j = 0; j < col; j++) {if (chess[i][j] == ' ')return 1;}}return 0;
}
char Result(char chess[Row][Col], int row, int col) {int i = 0, j = 0;for (i = 0; i < row; i++) {if (chess[i][0] == chess[i][1]&&chess[i][1] == chess[i][2] && chess[i][1] != ' ') {return chess[i][0];}}for (j = 0; j < col; j++) {if (chess[0][j] == chess[1][j]&&chess[1][j] == chess[2][j] && chess[1][j] != ' ') {return chess[1][j];}}if (chess[0][0] == chess[1][1]&&chess[1][1] == chess[2][2] && chess[1][1] != ' ') {return chess[1][1];}if (chess[0][2] == chess[1][1]&&chess[1][1] == chess[2][0] && chess[1][1] != ' ') {return chess[1][1];}int ret=Isfull(chess, row, col);if (ret == 1) {return 'C';}elsereturn 'P';
}
上面代码判断了几种情况,玩家赢了,电脑赢了,平局,继续下棋四种情况,就是看棋盘是有没有一条线嘛判断电脑或者玩家输赢,或者是不是棋盘满了但是没人赢了这种情况就是平局嘛,然后的情况就是继续下棋咯;
代码总解析(全部代码)
text.c代码
#include "game.h"
void menu() {printf("****************************\n");printf("*** 1.Play 0.Exit ***\n");printf("****************************\n");
}
void game() {char chess[Row][Col] = { 0 };Chess(chess, Row, Col);Chessboard(chess, Row, Col);char ret = 0;while (1) {playermove(chess, Row, Col);Chessboard(chess, Row, Col);ret = Result(chess, Row, Col);if (ret != 'C')break;Computermove(chess, Row, Col);Chessboard(chess, Row, Col);ret = Result(chess, Row, Col);if (ret != 'C')break;}if (ret == 'X') {printf("恭喜你赢了,小小的AI怎么可能是你的对手!!!\n");Chessboard(chess, Row, Col);}else if (ret == 'O') {printf("手抖了一下失误了,让电脑赢了\n");Chessboard(chess, Row, Col);}else {printf("实力相当,平局,不服的再来一局!!!\n");Chessboard(chess, Row, Col);}}
int main()
{int input = 0;srand((unsigned int)time(NULL));do {menu();printf("请输入>\n");scanf("%d", &input);switch (input) {case Play: game();break;case Exit:printf("退出游戏\n"); break;default:printf("输入错误,请重新输入:\n"); break;}} while (input);return 0;
}
game.c代码
#include "game.h"
void Chess(char chess[Row][Col], int row, int col) {int i = 0, j = 0;for (i = 0; i < row; i++) {for (j = 0; j < col; j++) {chess[i][j] =' ';}printf("\n");}
}
void Chessboard(char chess[Row][Col], int row, int col) {int i = 0, j = 0;for (i = 0; i < row; i++) {for (j = 0; j < col; j++) {printf(" %c ",chess[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 playermove(char chess[Row][Col], int row, int col) {int x = 0, y = 0;printf("玩家走\n");while (1) {printf("请输入要下的棋子坐标\n");scanf("%d %d", &x, &y);if (x <= row && x >= 1 && y >= 1 && y <= col) {if (chess[x - 1][y - 1] == ' ') {chess[x - 1][y - 1] = 'X';break;}elseprintf("坐标被占用,请重新输入\n");}elseprintf("非法输入,请重新输入\n");}
}
int AImove(char chess[Row][Col], int row, int col) {int i = 0, j = 0;if (chess[0][0] == chess[1][1] && chess[1][1] == chess[0][2] && chess[1][1] != ' ' && chess[2][2] == ' '&&chess[2][0]==' '){chess[2][0] = 'O';return 1;}for (i = 0; i < row; i++) {if (chess[i][0] == chess[i][1]&&chess[i][1]!=' '&&chess[i][2]==' ') {chess[i][2] = 'O';return 1;}if (chess[i][1] == chess[i][2] && chess[i][1] != ' '&&chess[i][0]==' ') {chess[i][0] = 'O';return 1;}if (chess[i][0] == chess[i][2] && chess[i][0] != ' '&&chess[i][1]==' ') {chess[i][1] = 'O';return 1;} }for (j = 0; j < col; j++) {if (chess[0][j] == chess[1][j] && chess[1][j] != ' '&&chess[2][j]==' ') {chess[2][j] = 'O';return 1;}if (chess[1][j] == chess[2][j] && chess[1][j] != ' '&&chess[0][j]==' ') {chess[0][j] = 'O';return 1;}if (chess[0][j] == chess[2][j] && chess[2][j] != ' '&&chess[1][j]==' ') {chess[1][j] = 'O';return 1;}}if (chess[1][1] == chess[0][0] && chess[1][1] != ' '&&chess[2][2]==' '){chess[2][2] = 'O';return 1;}if (chess[1][1] == chess[2][2] && chess[1][1] != ' '&&chess[0][0]==' '){chess[0][0] = 'O';return 1;}if (chess[2][2] == chess[0][0] && chess[2][2] != ' '&&chess[1][1]==' '){chess[1][1] = 'O';return 1;}if (chess[1][1] == chess[0][2] && chess[1][1] != ' '&&chess[2][0]==' '){chess[2][0] = 'O';return 1;}if (chess[1][1] == chess[2][0] && chess[1][1] != ' '&&chess[0][2]==' '){chess[0][2] = 'O';return 1;}if (chess[0][2] == chess[2][0] && chess[2][0] != ' '&&chess[1][1]==' '){chess[1][1] = 'O';return 1;}
}
void Computermove(char chess[Row][Col], int row, int col) {printf("电脑走\n");while (1) {int ret = AImove(chess, Row, Col);if (ret == 1)break;else {int x = rand() % row;int y = rand() % col;if (chess[x][y] == ' ') {chess[x][y] = 'O';break;}}}
}
int Isfull(char chess[Row][Col], int row, int col) {int i = 0, j = 0;for (i = 0; i < row; i++) {for (j = 0; j < col; j++) {if (chess[i][j] == ' ')return 1;}}return 0;
}
char Result(char chess[Row][Col], int row, int col) {int i = 0, j = 0;for (i = 0; i < row; i++) {if (chess[i][0] == chess[i][1]&&chess[i][1] == chess[i][2] && chess[i][1] != ' ') {return chess[i][0];}}for (j = 0; j < col; j++) {if (chess[0][j] == chess[1][j]&&chess[1][j] == chess[2][j] && chess[1][j] != ' ') {return chess[1][j];}}if (chess[0][0] == chess[1][1]&&chess[1][1] == chess[2][2] && chess[1][1] != ' ') {return chess[1][1];}if (chess[0][2] == chess[1][1]&&chess[1][1] == chess[2][0] && chess[1][1] != ' ') {return chess[1][1];}int ret=Isfull(chess, row, col);if (ret == 1) {return 'C';}elsereturn 'P';
}
game.h头文件代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define Row 3
#define Col 3
enum games {Exit,Play
};
void Chess(char chess[Row][Col], int row, int col);
void Chessboard(char chess[Row][Col], int row,int col);
void playermove(char chess[Row][Col],int row,int col);
void Computermove(char chess[Row][Col], int row, int col);
char Result(char chess[Row][Col], int row, int col);
头文件上的枚举是为了增加主函数中switch中case的可读性的!!!
总结
我后面还会再优化电脑的,让玩家永远不能赢,最多平局!!!
谢谢大家啦!!!
【牛客刷题】上手用C语言写一个三子棋小游戏超详解哦(电脑优化)相关推荐
- vs2010c语言小游戏,用C语言写一个三子棋小游戏(用VS2010实现,加强版)
前几天写了一个三子棋小游戏,但是比较简单,因为以前写的游戏棋盘只能是3x3那么大.今天对那个程序做了两点改进: 1.现在可以实现在任意大小的棋盘上下棋了. 2.因为棋盘可能变大,为了玩家方便确定坐标, ...
- c语言写一个简单的小游戏-推箱子
在学习C语言之后,写了一个简单的小游戏来锻炼自己的代码以及C语言知识的掌握能力. 推箱子作为手机上最常见的简单游戏,其代码也相对简单,想法也比较简单,下面为其代码和运行图. /************ ...
- 一个简单的c 游戏编程语言,编程达人 c语言写一个简单的小游戏-推箱子
在学习C语言之后,写了一个简单的小游戏来锻炼自己的代码以及C语言知识的掌握能力. 推箱子作为手机上最常见的简单游戏,其代码也相对简单,想法也比较简单,下面为其代码和运行图. /************ ...
- C语言 进阶版三子棋小游戏
目录 前言 游戏运行效果: 游戏代码: 1.test.c文件 2. game.h头文件 3. game.c 一.框架部分 二.游戏函数实现 1.创建数组并初始化 2.打印数组 3.玩家下棋 4.电 ...
- C语言实现三子棋小游戏---超详细讲解
实现步骤 第1步---打印一个菜单提示选择是否进入游戏 第2步---创建棋盘 第3步---初始化棋盘 第4步---打印棋盘 第5步---下棋,并判断输赢 第6步---将上述函数做成项目 完整代码 第1 ...
- 用C语言写一个简单的小游戏——猜数字
我们该如何设计这个程序? 1.首先应该打印一个菜单,让玩家选择玩游戏或者退出游戏 2.当玩家选择玩游戏,我们让电脑生成一个1~100的随机数,让玩家去猜 3.如果玩家猜的数比电脑生成的数大,我们提示猜 ...
- 使用C语言写一个三子棋
1.游戏开始界面 void menu() {printf("**********************\n");printf("** 1.play 0.exit **\ ...
- C语言简单实现三子棋小游戏
游戏规则: 三子棋是一种民间传统游戏,又叫九宫棋.圈圈叉叉.一条龙等.游戏是在一个3*3的棋盘里面下棋的.只要有三个一样的连成一条线,就表示有一方胜出:如果下完了9个格子还没有玩家胜出,游戏为平局. ...
- 三子棋小游戏带你走进编程世界(c语言版)
目录 一.游戏实现的整体思路 二.实现游戏大体框架 二.游戏函数的实现 1.初始化键盘 2.打印棋盘 3.下棋及胜负判断 (1)玩家下棋 (2)电脑下棋 (3)判断输赢 三.结语及源码 三子棋想必大家 ...
最新文章
- Echarts柱状图的点击事件
- android虚拟键盘挡住布局,Android全屏时软键盘遮住输入框修改布局解决方案
- Symbian签名和Uid相关内容的整理(一)
- csp-s模拟测试41「夜莺与玫瑰·玫瑰花精·影子」
- 美图赏析:拆解USB无线网卡,电路方案非常经典
- php凑整10算法,凑整法练习题.doc
- JMeter源码集成到Eclipse
- 从自卑的阴影中走出来
- Spring 创建代理类流程跟踪
- jquery easyui 表单结合对话框
- Python自动化之Django中间件
- IBM TSM官方最全资料
- 雷达人体静止感应技术,云望爱希ISee雷达感应成品,高精度探测应用
- 【谷粒商城】全网最全笔记(1/4)
- 遥感原理与应用——遥感影像及其特征、遥感图像处理
- xlwings 安装及排错: DLL load failed while importing win32api
- 如何在中国大陆三大运营商申请公网IPV4地址
- linux-- input子系统分析
- 极客日报:iPhone13系列9月15日正式推出;微信视频号支持发布1小时视频;Firefox 92正式发布
- 《Electron 开发》 环境配置和Helloworld
热门文章
- 产品:“写个banner这么费劲?”
- ibm r50隐藏分区_网友经历:IBM R50本本内存升级手记
- 【摘】UI设计中对比色颜色的选取
- 基于51单片机的数字气压计
- php excel 右对齐,excel中单元格对齐方式在哪里设置?
- Chapter 2 认识游戏
- 程序员把开发搬到云服务器,如何将IDEA开发的java web项目移植到腾讯云服务器
- 偏门赚钱项目:公众号打赏引流日赚500元
- 知乎高赞:让自己更优秀的 16 条法则
- Android篇 --Notification(消息通知)