当前位置:网站首页>Three chess (C language implementation)
Three chess (C language implementation)
2022-08-01 09:28:00 【xiaoHu_C】
今天小编给大家带来的是CA small game for beginners of language learning(三子棋),The whole process of the game is relatively easy to understand,But for contactCFor students who are new to language,Many details still need to be further grasped,This is a great help for our knowledge consolidation.

好了,闲话少说,Let's experience it together!
目录
2The block implementation of each module
1游戏的整体思路
I believe that you should have played a lot of small games,He is also very familiar with the flow of the game.when we start playing a game,The choice faced is nothing more than starting the game and exiting the game.Then according to this general direction,We can first write his main function process and menu.
在主函数中,Our task is to let the player choose,Therefore, it is necessary to provide selectable options in the menu.
有了菜单,Of course there is no shortage of options,We can pass it in the main functionswitch case statement to achieve this function,At the same time, we must also consider that players may play the game multiple times,因此我们选择do while循环来解决.实现过程,如下所示:

不可避免,Players may enter wrong numbers,So we also have to give the player a chance to re-enter,提醒玩家重新输入!
对于刚接触C语言的同学,We need to develop a good habit of taking one step at a time,每写一段代码,测试一下,This way we can correct mistakes in time,Avoid the wreckage of the final error check!

2The block implementation of each module
1)打印棋盘+初始化棋盘

刚开始,We can think of the chessboard as consisting of data and delimited rows.

如上图所示,We divided the chessboard into three parts: red, yellow, and blue,Each section consists of a row of data and a delimiter row,Note that the blue part lacks a separate line relative to the other two parts.

同理可得,We can also block each column like this.具体实现的代码如下:


But when we run it, the effect we see is different from what we expected,That's because we didn't initialize the board,We need to pass the data bits into a space as well.So we also need an initialization function.


After defining the initialization function,We can get the expected effect.
2)玩家下棋
What players need to pay attention to when playing chess is the rationality of the position of the move,The first point is that the coordinates of the pieces must be within the chessboard,If over the board,Then you can't fall;The second point is to determine whether the move position has been placed,If it was dropped,Then you have to change the position of the drop.

3)电脑下棋
在这里,In computer chess, we take the principle of randomness,We utilize a timestamp fitsrandTo randomly generate the coordinates of the computer to be placed.


4)判断输赢
The way of judging winners and losers in chess is simple,It is a line, a column or any one of the diagonal lines that satisfies the three sub-connections to form a line,Then one of them wins,游戏结束.Except for winning and losing,There are also draws,因此,In the program we set the player to win the return‘*’、电脑赢返回‘#’、平局返回‘Q’、继续游戏返回‘C’.

It contains a wrapper functionis_full,It is used to judge whether the chessboard is full.

3运行结果

The above are the running results of three different states,实现到这里,Our three-piece game is almost done.
Although our basic functions have been implemented,但是很容易发现,The computers in our backgammon don't seem to be that smart,It can only play chess at random,So interested students can try to optimize,Make your computer more capable!
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"game1.h"
//三子棋
void game()
{
//打印棋盘
// | |
//---|---|---
// | |
//---|---|---
// | |
char ret = 0;
char board[ROW][COL] = { 0 };
init_board(board, ROW, COL);
display_board(board, ROW, COL);
while (1)
{
player_move(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'C')
break;
display_board(board, ROW, COL);
computer_move(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'C')
break;
display_board(board, ROW, COL);
}
if (ret == '*')
printf("玩家赢\n");
else if (ret == '#')
printf("电脑赢\n");
else if (ret == 'Q')
printf("平局\n");
display_board(board, ROW, COL);
}
void menu()
{
printf("***************************\n");
printf("********1.Play Game********\n");
printf("********0.Exit Game********\n");
printf("***************************\n");
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do//Want to play the game multiple times,使用do whileNo matter what the situation is, you can play it once before making a judgment
{
menu();
printf("请选择>:");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,请重新输入!\n");
break;
}
} while (input);
return 0;
}#define _CRT_SECURE_NO_WARNINGS 1
#include"game1.h"
void init_board(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 display_board(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 player_move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家下棋:>\n");
while (1)
{
printf("Please enter the next sub-coordinates:>");
scanf("%d %d", &x, &y);
//Judge the legitimacy of the move
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
//Determine whether the location is already occupied
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("该位置已被占用,Please change location\n");
}
}
else
{
printf("非法输入,请重新输入\n");
}
}
}
void computer_move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑下棋:>\n");
while (1)
{
//Use timestamps to make the computer play chess randomly
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
//棋盘满了返回1没满返回0
int is_full(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++)
{
if (' ' == board[i][j])
{
return 0;
}
}
}
return 1;
}
char is_win(char board[ROW][COL], int row, int col)
{
//It is temporarily written to death here3×3的棋盘
int i = 0;
//Check if the rows are equal
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[0][i] != ' ')
{
return board[1][1];
}
//判断副对角线
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][i] != ' ')
{
return board[1][1];
}
//判断平局,Whether the board is full
if (is_full(board, row, col) == 1)
{
return 'Q';
}
return 'C';
}#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 3 //行
#define COL 3 //列
//初始化棋盘
void init_board(char board[ROW][COL], int row, int col);
//打印棋盘
void display_board(char board[ROW][COL], int row, int col);
//玩家下棋
void player_move(char board[ROW][COL], int row, int col);
//电脑下棋
void computer_move(char board[ROW][COL], int row, int col);
//判断输赢
char is_win(char board[ROW][COL], int row, int col);

边栏推荐
- 静态Pod、Pod创建流程、容器资源限制
- How does UXDB return the number of records for all tables in the current database?
- 《时代》杂志:元宇宙时代将改变世界
- STM32个人笔记-嵌入式C语言优化
- Introduction to ADAS
- Optimal dazzle Oracle database support what kinds of type of the time and date
- YOLOv7-Pose尝鲜,基于YOLOv7的关键点模型测评
- Redis middleware (from building to refuse pit)
- Manual upgrade and optimization tutorial of Lsky Pro Enterprise Edition
- 高级驾驶辅助系统ADAS简介
猜你喜欢

HoloView -- Tabular Datasets

HoloView--live data

HoloView——实时数据

Idea common plugins

rpm和yum

leetcode-6132: Make all elements in array equal to zero

The soul asks: How does MySQL solve phantom reads?

How to ensure the consistency of database and cache data?

various network protocols

Naive Bayes--Study Notes--Basic Principles and Code Implementation
随机推荐
Change Servlet project to SSM project
How to query database configuration parameters in GBase 8c, such as datestyle
用OpenCV的边缘检测
解析MySQL数据库:“SQL优化”与“索引优化”
JVM内存模型之深究模型特征
leetcode-6134:找到离给定两个节点最近的节点
基于tika实现对文件类型进行判断
实验。。。。
YOLOv7-Pose尝鲜,基于YOLOv7的关键点模型测评
notes....
How to implement deep copy in js?
获取页面数据的方法
USB Protocol (2) Terminology
Mysql数据库的部署以及初始化步骤
sqlserver怎么查询一张表中同人员的交叉日期
力扣周赛304 6135. 图中的最长环 内向基环树
网络个各种协议
Pod environment variables and initContainer
Ogg synchronizes oracle to mysql, there may be characters that need to be escaped in the field, how to configure escape?
优炫数据库支持Oracle哪几种时间及日期类型