当前位置:网站首页>C语言实现【三子棋游戏】(步骤分析和实现源码)
C语言实现【三子棋游戏】(步骤分析和实现源码)
2022-07-01 06:52:00 【周周汪】
游戏规则
相信大家都玩过五子棋,那么三子棋其实也一样,就是在一个3x3(也可以更大)的棋盘上,当有一方形成3连棋(横竖斜)的时候则获胜。
如图所示:
实现代码
首先我们需要一个game.h的头文件,这里面是我们各个接口函数的声明。
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 3
#define COL 3
//初始化棋盘
void InitBoard(char board[ROW][COL], int row, int col);
//打印棋盘
void DisplayBoard(char board[ROW][COL], int row, int col);
//玩家下棋
void PlayerMove(char board[ROW][COL], int row, int col);
//电脑下棋
void ComputerMove(char board[ROW][COL], int row, int col);
//判断游戏输赢
//要返回4种不同的状态
//玩家赢 - '*'
//电脑赢 - '#'
//平局 - ‘Q’
//继续 - 'C'
char IsWin(char board[ROW][COL], int row, int col);
其次我们需要在game.c中完成各个函数的实现
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void InitBoard(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
void DisplayBoard(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
//打印数据
int j = 0;
for (j = 0; j < col; j++)
{
printf(" %c ", board[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 board[ROW][COL], int row, int col)
{
printf("玩家走:>\n");
int x = 0;
int y = 0;
while (1)
{
printf("请输入坐标(第几行 第几列 ):>");
scanf("%d%d", &x, &y);//2 1 -- > 1 0
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("坐标被占用,请重新输入\n");
}
}
else
{
printf("坐标非法,超出范围\n");
}
}
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑走:>\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int IsFull(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
{
return 0;//不满
}
}
}
return 1;//棋盘满
}
char IsWin(char board[ROW][COL], int row, int col)
{
//行
int i = 0;
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
{
return board[i][0];
}
}
//列
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
{
return board[0][i];
}
}
//对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
return board[1][1];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
return board[1][1];
//判断平局
if (IsFull(board, row, col))
{
return 'Q';
}
//游戏继续
return 'C';
}
最后在test.c中进行测试
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf("***********************************\n");
printf("*********** 1. play *********\n");
printf("*********** 0. exit *********\n");
printf("***********************************\n");
}
void game()
{
//三子棋的过程
char board[ROW][COL];//棋盘数组
//初始化棋盘 - board的元素都给成空格
InitBoard(board, ROW, COL);
//打印棋盘
DisplayBoard(board, ROW, COL);
//下棋
char ret = 0;
while (1)
{
PlayerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = IsWin(board, ROW, COL);
if (ret != 'C')
{
break;
}
ComputerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = IsWin(board, ROW, COL);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf("玩家赢\n");
}
else if (ret == '#')
{
printf("电脑赢\n");
}
else
{
printf("平局\n");
}
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}
这样我们的三子棋的实现就完成了。
效果展示

总结
总的来说三子棋的实现还是比较简单易懂的。
当然我们实现的版本是最垃圾的版本,电脑的智商为0,因为电脑下的位置只是随机下在空位置上,我们并没有为电脑设计算法。
比如:
我们可以给电脑设计,当玩家马上要3连棋了,电脑要去堵截,当电脑可以3连棋了,电脑要知道,还有往哪里走赢得概率更大等等。
感谢阅读,我们下期再见
如有错 欢迎提出一起交流
边栏推荐
- MySQL table partition creation method
- Postgraduate entrance examination directory link
- 女生适合学产品经理吗?有什么优势?
- 软件工程领域的名词描述
- How the esp32 deep sleep current is lower than 10uA
- [lingo] find the shortest path problem of undirected graph
- Database notes
- Insufficient free space after clearing expired cache entries - consider increasing the maximum cache space
- 在支付宝上买基金安全吗?哪里可以买基金
- Notes on probability theory
猜你喜欢

问题:OfficeException: failed to start and connect(三)

EasyNVS云管理平台功能重构:支持新增用户、修改信息等

灰度何以跌下神坛?

2022 Jiangsu Vocational College skills competition (secondary vocational school) network construction and application open competition volume

【电气介数】电气介数及考虑HVDC和FACTS元件的电气介数计算

K8s set up redis cluster

产品学习(二)——竞品分析

【FPGA帧差】基于VmodCAM摄像头的帧差法目标跟踪FPGA实现

【微信小程序】如何搭积木式开发?

Why are so many people turning to product managers? What is the development prospect of product manager?
随机推荐
【Tikhonov】基于Tikhonov正则化的图像超分辨率重建
[matlab] solve nonlinear programming
8 figures | analyze Eureka's first synchronization registry
Database notes
Database objects: view learning records
3. Disabling copy construction
PAT (Advanced Level) Practice 1057 Stack
ESP32 ESP-IDF GPIO按键中断响应
8 张图 | 剖析 Eureka 的首次同步注册表
Understand esp32 sleep mode and its power consumption
代码实战——从零开始搭建自己的Diffusion models/Score-based generative models
2022 Jiangsu Vocational College skills competition (secondary vocational school) network construction and application open competition volume
Idea easy to use plug-in summary!!!
DC-4靶机
问题解决:OfficeException: failed to start and connect(一)
ESP32在电池供电时用ULP监测电池电压
Problem: officeexception: failed to start and connect (II)
Several ways of gson's @jsonadapter annotation
发现了一个 MySQL 的巨坑:update 更新别再用影响行数做判断了!!!
Code practice - build your own diffusion models / score based generic models from scratch