作者:[南航科院小张
南航科院小张的博客
专栏:从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 举报,一经查实,本站将立刻删除。