当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
Appium 滑动问题
uni.navigateBack 中的坑
HCIP第七天
典型的一次IO的两个阶段是什么?阻塞、非阻塞、同步、异步
暂未找到具体原因但解决了的bug
[ansible] playbook explains the execution steps in combination with the project
数据中心的网络安全操作规范
MySQL - Detailed Explanation of Database Transactions
WebForm DropDownList bind year and month respectively
MySQL压缩包方式安装,傻瓜式教学
MySQL事务(transaction) (有这篇就足够了..)
HCIP 第十天
离线部署通过tiup 配置好topology.yaml文件指定PD TV TIDBserver 是不是会自动在其他机器创建好对应得模块?
Debian 10 dhcp relay (dhcp 中继) dhcp 固定分配
Comprehensive experiment of MPLS and BGP
@RequestParam使用
BGP通过MPLS解决路由黑洞
redis-advanced
研发过程中的文档管理与工具
读入、输出优化

![[Unity3D] Beginner Encryption Skills (Anti-Cracking)](/img/07/4a0731dd66b058c07d6240ffd36eea.png)







