当前位置:网站首页>Write a small game in C (three chess)
Write a small game in C (three chess)
2022-08-02 08:24:00 【devour late at night】
三子棋
前言
Always remember grammar in our learning process and various concepts might as well make us unavoidably get bored,But let's change the direction to think about,We learn the contents of the can bring us some fun?唉,That's right is the game.From play games to make the game,Think of interest is elevated, the.Take a look at below in pureCKnowledge to implement a classic game三子棋,Knowledge is not much involved in the loop,数组,宏定义,And, of course, the random number generating method.Below and see how the specific implementation.
Implement the basic elements of
1. 菜单
When we enter the game will have menu display for players to choose,Otherwise the player into a black box tip not,Then why don't know.The menu as a lead,Lead players learn how to play.
int Meun()
{
printf("********************************\n");
printf("**** 1.play 0.exit ****\n");
printf("********************************\n");
}
Let's take a look at the menumain函数实现
int main()
{
int input = 0;
do
{
Meun();
printf("请选择:>");
scanf("%d",&input);
switch(input)
{
case 1:
break;
case 0:
printf("游戏已退出\n");
break;
default:
printf("请选择 1 或者 0.\n");
break;
}
}
}while(input);
return 0;
}
This design is for the sake of can play a disc after can choose whether to play.
2. 棋盘大小,And its initialization
2.1 棋盘大小
In order to prevent the main functionmain过于臃肿,We put the body of the game are ingame()这个函数中;
我们先创建一个二维数组,The two dimensional array we can this design
#define ROW 3
#define COL 3
void game()
{
char board[ROW][COL];
}
Macro definition in the future we can change the size of the board,Without the writing at the back of the fixed digital to change the board head.
2.2 初始化棋盘
Initialize all board said space is empty
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] = ' ';
}
}
}
3. 打印棋盘
There are two kinds of printing method:第一种 纯打印
void DisplayBoard(char board[ROW][COL], int row, int col)
{
int i = 0, j = 0;
for(i = 0; i < row; i++)
{
printf(" %c | %c | %c \n",board[i][0],board[i][1],board[i][2]);
if(i < row - 1)
printf("---|---|---|\n");
}
}
This printed board is also stiff,Is not the time for dynamic until the next time we need to modify will need to manually modify parameters,The second method shown below 动态打印
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 < col - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
}
printf("\n");
}
}
4. Players with a computer game
4.1 电脑下棋
The design of the computer to play chess here we simply some,Let it generate random pieces can be,Here we are going to introduce to generate a random number two header files
#include <stdlib.h>
#include <time.h>
randFunction of the generated random number is a pseudo-random number,Is each function call back the generated when the Numbers are the same.The random number, it is not what we want,But how to solve this problem?我们就要利用srandThis function to set a good random number seed.在mainFunction inside design can be:
srand((unsigned int)time(NULL));
Designed a random number we take a look at the computer to play chess subject
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
x = rand() % row; //模上row是为了生成0到row之间的数值
y = rand() % col; 模上col是为了生成0到col之间的数值
if (board[x][y] == ' ')
{
board[x][y] = 'X'; //The computer of chess asX
break;
}
}
}
4.2 玩家下棋
We give play chess player's scope is limited,Is the board size range,Outside the range board we give to re-enter,There is in the original pieces nor following,Also need to input.
Players don't like we know of the array subscript is from0开始,So we have players enter the coordinates of the less than1.具体看下面代码:
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("Players go please input coordinates:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = 'O';
}
else
{
printf("坐标被占用,请重新输入\n");
}
}
else
{
printf("坐标非法,超出范围");
}
}
4.3 判断输赢
When a player playing chess with computer has the following several ways:
- 玩家获胜.
- 电脑获胜.
- 平局.
- 输赢未定.
Might as well so that every time the next chess players and computer we will decide,Create a judgment of winning or losingIswinFunction of each next move would determine the,And return a character values.
The return value is different from person to person to design,Here I can so if the player win we return to the player pieces’O’,Computer win we return to pieces’X’,If we return to draw draw word first letter’D’,If the result return pending us’Q’.
char Iswin(char board[ROW][COL], int row, int col)
{
//水平3Is as
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];
}
}
//竖直3子一样
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[0][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 (Is_full(board, row, col) == 1)
{
return 'Q';
}
return 'D';
}
现在我们看看game函数的部分逻辑
void game()
{
//三子棋的过程
char board[ROW][COL]; //棋盘数组的创建
InitBoard(board, ROW, COL);
//打印棋盘
DisplayBoard(board, ROW, COL);
char ret = 0;
//下棋
while (1)
{
system("cls");
DisplayBoard(board, ROW, COL);
PlayerMove(board, ROW, COL);
ret = Iswin(board, ROW, COL);
if (ret != 'D')
break;
DisplayBoard(board, ROW, COL);
ComputerMove(board, ROW, COL);
ret = Iswin(board, ROW, COL);
if (ret != 'D')
break;
}
if (ret == 'X')
{
printf("玩家赢\n");
}
else if (ret == 'O')
{
printf("电脑赢\n");
}
else if(ret == 'Q')
{
printf("平局\n");
}
}
游戏整体代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 3
#define COL 3
void Meun()
{
printf("********************************\n");
printf("**** 1.play 2.exit ****\n");
printf("********************************\n");
}
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 < col - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
}
printf("\n");
}
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
x = rand() % row; //模上row是为了生成0到row之间的数值
y = rand() % col; 模上col是为了生成0到col之间的数值
if (board[x][y] == ' ')
{
board[x][y] = 'X'; //The computer of chess asX
break;
}
}
}
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("Players go please input coordinates:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = 'O';
}
else
{
printf("坐标被占用,请重新输入\n");
}
}
else
{
printf("坐标非法,超出范围");
}
}
char Iswin(char board[ROW][COL], int row, int col)
{
//水平3Is as
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];
}
}
//竖直3子一样
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[0][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 (Is_full(board, row, col) == 1)
{
return 'Q';
}
return 'D';
}
void game()
{
//三子棋的过程
char board[ROW][COL]; //棋盘数组的创建
InitBoard(board, ROW, COL);
//打印棋盘
DisplayBoard(board, ROW, COL);
char ret = 0;
//下棋
while (1)
{
system("cls");
DisplayBoard(board, ROW, COL);
PlayerMove(board, ROW, COL);
ret = Iswin(board, ROW, COL);
if (ret != 'D')
break;
DisplayBoard(board, ROW, COL);
ComputerMove(board, ROW, COL);
ret = Iswin(board, ROW, COL);
if (ret != 'D')
break;
}
if (ret == 'X')
{
printf("玩家赢\n");
}
else if (ret == 'O')
{
printf("电脑赢\n");
}
else if(ret == 'Q')
{
printf("平局\n");
}
}
int main()
{
int input = 0;
do
{
Meun();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("游戏已退出\n");
break;
default:
printf("请选择 1 或者 0.\n");
break;
}
}while (input);
return 0;
}
边栏推荐
猜你喜欢
MySQL - low level settings
MFC最详细入门教程[转载]
Shell变成规范与变量
商业智能平台BI 商业智能分析平台 如何选择合适的商业智能平台BI
2022年防止网络攻击的15个网络安全实践,你学会了吗?
cas:139504-50-0 美登素DM1|Mertansine|
Understand Chisel language. 31. Chisel advanced communication state machine (3) - Ready-Valid interface: definition, timing and implementation in Chisel
用C写小游戏(三子棋)
HCIP第三天
多版本node的安装与切换详细操作
随机推荐
HCIP 第十三天
王学岗-编译出运行的文件
数据中台:始于阿里,兴于DaaS
【Unity3D】初学加密技巧(反破解)
Application and case analysis of CASA model and CENTURY model
Biotin-EDA|CAS:111790-37-5| 乙二胺生物素
2022年防止网络攻击的15个网络安全实践,你学会了吗?
小说里的编程 【连载之二十四】元宇宙里月亮弯弯
HCIP 第十二天
Figure robot software digital twin station oil and gas pipelines, oil and gas transportation control platform
2022-08-01 第四小组 修身课 学习笔记(every day)
离线部署通过tiup 配置好topology.yaml文件指定PD TV TIDBserver 是不是会自动在其他机器创建好对应得模块?
uniapp 禁止默认返回事件
学习笔记(7)Funtion
理论问题与工程问题的差异在哪里?
mysql去除重复数据
数据表格化打印输出
用户身份标识与账号体系实践
Control 'ContentPlaceHolder1_ddlDepartment' of type 'DropDownList' must be placed inside a form tag with runat=server.
设置 height: auto 却无法触发 transition 动画的解决方案