当前位置:网站首页>C language implementation of three chess
C language implementation of three chess
2022-07-23 05:22:00 【My Borges】
Catalog
I believe all of you have played Sanzi and minesweeping , But who has ever thought that he can realize three piece chess on the computer ? Let's realize it together today .
Sanzi
First of all, let's watch Sanzi , The realization of Sanzi chess is divided into two steps , First, test logic , Game logic .
Test logic
#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 arr[ROW][COL] = { 0 };
init_arr(arr, ROW, COL);
show_arr(arr, ROW, COL);
}
int main()
{
int input = 0;
do
{
menu();
printf(" Please select ->: ");
scanf("%d", &input);
switch (input)
{
case 1:
printf(" Start the game \n");
game();
break;
case 0:
printf(" Quit the game ");
break;
default:
printf(" Input error, please input again \n");
break;
}
} while (input);
return 0;
}Test logic refers to test.c Basic logic in Like here , If the player chooses 1 Then play the game , choice 0 Then quit the game , Select other to remind you of input errors , Request to re-enter .
Then let's look at the logic of the game .
The game logic
The logic of the game is divided into three steps , You must first have a plate for you to play chess , Then you have to play chess on the chessboard , Finally, you have to know whether you win or lose . So our game logic is divided into these three steps : Initialize chessboard , Players and computers play chess , Judgement of winning or losing .
Initialize chessboard
First, give a two-dimensional array with three rows and three columns , Initialize each of its elements as a space

We hope to be able to print the contents shown on the screen , Then we should write two functions , An initialization array , Print the array in the format shown in
This is the header code
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#define ROW 3
#define COL 3 // The settings here are convenient for future changes
void init_arr(char arr[ROW][COL], int row, int col);// Lowercase is to avoid conflicts with constant names
void show_arr(char arr[ROW][COL], int row, int col);// Print array void init_arr(char arr[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
arr[i][j] = ' ';
}
}
}This is the initialization function code
void show_arr(char arr[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf(" %c ", arr[i][j]);
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
}
}This is the print code
In this case, our chessboard will be printed , Now let's play chess .
Playing chess
Chess is divided into computer chess and player chess .
Players need to manually input coordinates when playing chess , And you need to judge whether the input coordinates exceed the range of the array , Or whether it is occupied . Computer chess can be played by setting random numbers , And can limit its coordinate range , So the computer only needs to judge whether it is occupied . Set the player to play chess *, Computer chess is set to #. First, let's watch players play chess , Players need to manually input coordinates when playing chess , So first create two variables x and y, Then judge whether it crosses the boundary or uses . Code up !
void player(char arr[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf(" Please enter the coordinates ->: ");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (' ' == arr[x - 1][y - 1])
{
arr[x - 1][y - 1] = '*';
break;
}
else
{
printf(" This coordinate is occupied , Please re-enter ");
}
}
else
{
printf(" Illegal coordinates , Please re-enter ");
}
}
}Then the computer plays chess , I need to use rand Function to set a random number , And there is no need to judge whether it crosses the boundary , Other parts are the same as players playing chess .
void computer(char arr[ROW][COL], int row, int col)
{
while (1)
{
int x = rand() % row;
int y = rand() % col;
if (' ' == arr[x][y])
{
arr[x][y] = '#';
break;
}
}
}Judge victory
There are four situations , The winning players are , Computers win , Draw or not over continue .
So let's write a function , Make players win and return *, The computer won back #, Draw back Q, Keep going back c
There are three ways to win , List three , There are three diagonals . Judge separately .
To judge a draw, you need to know whether the chessboard is full , So we can write another function to judge whether the chessboard is full , Return when full 1,
Otherwise return to zero . Code up !
Judge the victory
int i = 0;
int j = 0;
// Line victory
for (i = 0; i < row; i++)
{
int count = 0;
for (j = 0; j < col-1; j++)
{
if (arr[i][j] == arr[i][j + 1] && arr[i][j] != ' ')
{
count++;
if (count == 2)
{
return arr[i][j];
}
}
else
{
break;
}
}
}for (j = 0; j < col; j++)
{
int count = 0;
for (i = 0; i < row - 1; i++)
{
if (arr[i][j] == arr[i+1][j] && arr[i][j] != ' ')
{
count++;
if (count == row - 1)
{
return arr[i][j];
}
}
else
{
break;
}
}
}
// Diagonally
// Principal diagonal
int count = 0;
for (i = 0; i < row-1; i++)
{
if (arr[i][i] == arr[i + 1][i + 1] && arr[i][i] != ' ')
{
count++;
if (count == row - 1)
{
return arr[i][i];
}
}
else
break;
}// Anti diagonal
for (i = 0; i < row; i++)
{
for (j = 1; j < col; j++)
{
int count = 0;
if (arr[i][col - j] == arr[i + 1][col - j - 1]&& arr[i][col-j] != ' ')
{
count++;
if (count == row - 1)
{
return arr[i][i];
}
}
else
break;
}
}int is_full(char arr[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
int count = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (' ' == arr[i][j])
return 0;
else
count++;
if (count == row * col)
return 1;
}
}
}
if (1 == is_full(arr, row, col))
{
return 'Q';
}
When we change the defined constant into 5 when , We can also play Gobang .


The computer is a little stupid , But after that, I'll come back and write an awesome Gobang !
边栏推荐
- PR初始入门
- Low in SoC_ Power simple control
- Leetcode - 494. Objectifs et
- Apple 注销账户 Revoke Token
- 我的第一篇博客 | 记录编程之路的初心与目标
- Ping的原理和实现
- Hefei University of technology information theory and coding course design, including code, visual interface, course design report
- Druid source code read 6-preparedstatementpool source code and usage scenario analysis
- 常用工具和资源
- Leetcode-583. Deleting two strings
猜你喜欢
随机推荐
如何用C语言实现无头单向非循环链表Single List ?
交换二进制位中的奇偶位
Leetcode-213. house raiding II
Nodejs implements scheduled tasks
Druid source code read the status in 9-druiddatasource and druidconnection
Cookie
Introduction to makefile
Construction training camp module I operation
My redis collation
链表的基本操作
C语言实现扫雷
获取电脑硬件信息
GIC Introduction (III) -- gic400 register
Leetcode-172. zero after factorial
表单验证和正则表达式(一)
leetcode-172.阶乘后的零
Apple 注销账户 Revoke Token
leetcode-504.七进制数
Detailed explanation of Axi agreement
leetcode-买卖股票的最佳时机含手续费









