【牛客刷题】上手用C语言写一个三子棋小游戏超详解哦电脑优化

作者:[南航科院小张
南航科院小张的博客
专栏:从c语言的入门到进阶
学习知识不只是要懂,还要会用;想要找到好的工作,这里给大家介绍一件可以斩获诸多大厂offer的利器–牛客网
点击免费注册和我一起开始刷题之路吧!!!

在这里插入图片描述


前言

用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;
			}
			else
				printf("坐标被占用,请重新输入\n");
		}
		else
			printf("非法输入,请重新输入\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;
			}
			else
				printf("坐标被占用,请重新输入\n");
		}
		else
			printf("非法输入,请重新输入\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';
	}
	else
		return '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';
	}
	else
		return '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;
			}
			else
				printf("坐标被占用,请重新输入\n");
		}
		else
			printf("非法输入,请重新输入\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';
	}
	else
		return '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的可读性的!!!

总结

我后面还会再优化电脑的,让玩家永远不能赢,最多平局!!!
谢谢大家啦!!!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


学习编程是顺着互联网的发展潮流,是一件好事。新手如何学习编程?其实不难,不过在学习编程之前你得先了解你的目的是什么?这个很重要,因为目的决定你的发展方向、决定你的发展速度。
IT行业是什么工作做什么?IT行业的工作有:产品策划类、页面设计类、前端与移动、开发与测试、营销推广类、数据运营类、运营维护类、游戏相关类等,根据不同的分类下面有细分了不同的岗位。
女生学Java好就业吗?女生适合学Java编程吗?目前有不少女生学习Java开发,但要结合自身的情况,先了解自己适不适合去学习Java,不要盲目的选择不适合自己的Java培训班进行学习。只要肯下功夫钻研,多看、多想、多练
Can’t connect to local MySQL server through socket \'/var/lib/mysql/mysql.sock问题 1.进入mysql路径
oracle基本命令 一、登录操作 1.管理员登录 # 管理员登录 sqlplus / as sysdba 2.普通用户登录
一、背景 因为项目中需要通北京网络,所以需要连vpn,但是服务器有时候会断掉,所以写个shell脚本每五分钟去判断是否连接,于是就有下面的shell脚本。
BETWEEN 操作符选取介于两个值之间的数据范围内的值。这些值可以是数值、文本或者日期。
假如你已经使用过苹果开发者中心上架app,你肯定知道在苹果开发者中心的web界面,无法直接提交ipa文件,而是需要使用第三方工具,将ipa文件上传到构建版本,开...
下面的 SQL 语句指定了两个别名,一个是 name 列的别名,一个是 country 列的别名。**提示:**如果列名称包含空格,要求使用双引号或方括号:
在使用H5混合开发的app打包后,需要将ipa文件上传到appstore进行发布,就需要去苹果开发者中心进行发布。​
+----+--------------+---------------------------+-------+---------+
数组的声明并不是声明一个个单独的变量,比如 number0、number1、...、number99,而是声明一个数组变量,比如 numbers,然后使用 nu...
第一步:到appuploader官网下载辅助工具和iCloud驱动,使用前面创建的AppID登录。
如需删除表中的列,请使用下面的语法(请注意,某些数据库系统不允许这种在数据库表中删除列的方式):
前不久在制作win11pe,制作了一版,1.26GB,太大了,不满意,想再裁剪下,发现这次dism mount正常,commit或discard巨慢,以前都很快...
赛门铁克各个版本概览:https://knowledge.broadcom.com/external/article?legacyId=tech163829
实测Python 3.6.6用pip 21.3.1,再高就报错了,Python 3.10.7用pip 22.3.1是可以的
Broadcom Corporation (博通公司,股票代号AVGO)是全球领先的有线和无线通信半导体公司。其产品实现向家庭、 办公室和移动环境以及在这些环境...
发现个问题,server2016上安装了c4d这些版本,低版本的正常显示窗格,但红色圈出的高版本c4d打开后不显示窗格,
TAT:https://cloud.tencent.com/document/product/1340