C on the tip of the tongue -- slow cooking of Sanzi chess

Joe's Dragon maid 2021-11-25 15:14:02

Traditional art can

Xiaobian is a freshman. I won't repeat it , Welcome the boss to point out (QQ:1319365055)
Previous blogs Am I ! Am I ! Please search for bloggers 【 Know the blue of the sky 】 Am I ! Am I ! Please search for bloggers 【 Know the blue of the sky 】 Or scan code to enter !
Joe's gitee The code base ( The grey man Welcome to visit , Am I !
 Insert picture description here

(https://blog.51cto.com) Thank you for your support !

Transition zone

It's Beijing time 3:12,1:12 Start writing , I was going to be here 12 Catch up with a blog before , Think about it. , There are too many things , Today I will stay up late and write my blog , It was itchy enough not to write yesterday , Anyway, I'll think about extending my life without class in the first or second class tomorrow morning .

 Insert picture description here

The film begins

Get ready

Tonight is a big project , But we're not in a hurry , A good workman does his work well , You must sharpen your tools first , Let's see what we need “ device ”:
1. A big pie function ( Random value rand function ,srand function , Self defined function ……);
2. A handful of header files :<stdlib.h>,<time.h>,<stdio.h>;
3. The pattern opens : Packaging function , Improve readability and sense of logic ;test.c Deposit main function , Used as an entry to a program , quote game.h The header file ( Refer to the header file written by yourself. Remember that it is in double quotation marks “game.h”),game.c The main function of the project is stored in ,game.h In, the declarations of various functions and VS The provided header file contains ;
4. The use of a lot of two-dimensional arrays ;
5. Intricate branches and nested use of loop statements ;
6. Analytical brain seeds and industrious hands ;
If you have no problem with any of the above , Then we don't have to talk nonsense , Go straight to the point .

Onion strategy

Treat the problem like an onion, peel it layer by layer , Next, I'll take you to peel the onions
**

1. The golden oil starts with

int main()
{

test();
return 0;
}

2. You have to serve the menu first

With a first test The function calls out the menu , The reason why it is not written in main Function inside is convenient to eat .

void test()
{

int input = 0;
srand((unsigned int)time(NULL));// Initialization function of random number generator , Using time stamp to generate pseudo-random number 
do// Perform the process at least once 
{

menu();
printf(" Please select the menu :>");
scanf("%d", &input);// Store values in input To realize the menu selection function 
switch (input)
{

case 1:
{

printf(" Sanzi \n");
game();// Enter the main body of the game 
break;
}
case 0:
{

printf(" About to quit ……");
break;
}
default:
{

printf(" Play ?\n");// Prevent special circumstances, prevent skin 
break;
}
}
} while (input);// Realize the repeated play of the game 
}

3. The entrance to the game —— menu

Simply made a menu , You can play freely , My lazy nature creates its mediocre appearance .

void menu()
{

printf("------------------------------------\n");
printf("------------------------------------\n");
printf("-------1.P L A Y-----------\n");
printf("-------0.E X I T-----------\n");
printf("------------------------------------\n");
printf("------------------------------------\n");
}

When you type “ 1 ” It officially began when .

4. The subject of the game

Here we need to use our brains , Because things that need to be arranged suddenly soar , Making rules focuses on efficiency and purpose .
First, under the rules , What needs to be realized is the material expression of Sanzi chess , That is, the design of the chessboard , The big point is to build a game environment . The chessboard is square , Then we use the similarity of determinants of two-dimensional arrays to simulate the chessboard , Here I set a name board Array of .
**

1. Board initialization

**
In order to make a chessboard , I have to tell the compiler the outline of a chessboard with data : Since it's a three piece chess , Let's start with the simplest 3*3 Start , Use them separately ROW( That's ok ),COL( Column ) To specify array elements .
For the convenience of writing and reading , We will ROW and COL stay game.h The macro definition in the header file uses :

# define ROW 3
# define COL 3

There is also an advantage in writing this way , It's easy to change the size of the chessboard , Just change the size of the number , Don't bother to change this and that everywhere .

void Printboard(char board[ROW][COL], int row, int col)
{

int i = 0;// Array index from 0 Start 
int j = 0;
for (i = 0; i < row; i++)
{

for (j = 0; j < col; j++)// Nested loops to complete the entire array 
{

board[i][j] = ' ';// take i That's ok j The array of columns is initialized to null , Used to lay out and put pieces 
}
}
}

**

2. Print chessboard

**
Everything is ready but the east wind , Code directly :

void Display(char board[ROW][COL], int row, int col)
{

int i, j = 0;
for (i = 0; i < row; i++)
{

for (j = 0; j < col; j++)
{

printf(" %c ", board[i][j]);// Put in %c Store the pieces as a piece bit 
if (j < col - 1);// Beautiful chessboard, remove the outermost separation line 
printf("|");
}
printf("\n");// Line feed print line 
if(i<row-1)
{

for (j = 0; j < col; j++)
{

printf("---");
if (j < col - 1);
printf("|");// Similarly, print separator lines 
}
}
printf("\n");
}
}

Customized here Display function , The effect is as follows :
 Insert picture description here
**

Chess player operation

**
Once the chessboard is done well, you can play it , If you want to realize it here , We need the player's chess function and a 24 Hours of online play with the Han man-machine , We will use player Functions and X_MAN Function implementation ( Naming is an art ):

void player(char board[ROW][COL], int row, int col)// The player 
{

int x = 0;// Number of analog lines 
int y = 0;// Number of analog Columns 
printf(" Player, please enter the chess piece coordinates :>"); Coordinate simulation chess grid
scanf("%d%d", &x, &y);
while (1)
{

if (row >= x && x >= 1 && y >= 1 && y <= col)// Define the effective scope 
{

if (board[x - 1][y - 1] == ' ')// Hommization , Design according to the chessboard in the player's eyes 
{

board[x - 1][y - 1] = '*';// If this case is empty , Then put the pieces * Put in 
Display(board, row, col);// Displays the current layout 
break;
}
else
{

printf(" Invalid chess space , Please re-enter \n");// Prevent occupation 
break;
}
}
else
{

printf(" You're out of the chessboard !\n");// Cross border warning 
}
}
}

by comparison , It's easier to write about man-machine ;
Here, pay attention to two key points :1. Remember to put the pseudo-random number initialization function in the upper function or main function ;
2. Computers don't need to be like players “ -1” operation , The computer itself can recognize ;

void x_man(char board[ROW][COL], int row, int col)// Man-machine 
{

int x = 0;
int y = 0;
printf(" Under man-machine :\n");
while(1)
{

x = rand() % ROW;//ROW,COL Range 0~2, obtain 0~2 Random value 
y = rand() % COL;// Empathy 
if (board[x][y] == ' ')
{

board[x][y] = '#';
Display(board, row, col);
break;
}
}
}

**

Judge

**
You have to judge to win or lose in chess, right , Or we're just having a typing party with the computer .
Let's just make one winor Function to implement , The basic idea is to realize three situations :
1. Game player wins :
Divided into three horizontal columns to win , Three vertical columns win , Diagonal win . My idea of realizing conditional judgment is simple and rough , Transitivity is connected to three elements, two of which are equal , Finally, ensure that there are no spaces ( If one of them is not empty );
2. Five five open ; We need another Full Function to satisfy the judgment of draw , That is, when the chessboard is full and does not meet the winning or losing conditions, we give the result of a draw
3. Computers win ;
As follows :

char winor(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] != ' ')// Win 
{

return board[i][1];
}
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')// Win 
{

return board[1][i];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')// Diagonal win 
{

return board[0][0];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')// Diagonal win 
{

return board[0][2];
}
if (1==Full(board, row, col))//Full Function to determine the draw 
{

return 'p';
}
return 'c';
}

Full The function is defined as follows :

int Full(char board[ROW][COL], int row, int col)
{

int i,j = 0;
for (i = 0; i < ROW; i++)
{

for (j = 0; j < COL; j++)
{

if (board[i][j] == ' ')// If there is a space, it does not meet the draw and continue the cycle 
{

return 0;
}
}
}
return 1; If no space is found after the loop, it returns 1 It's a draw
}

When the above steps are completed , We substitute into the subject game Logical assembly in function , as follows :

void game()
{

char board[ROW][COL] = {
 0 };
Printboard(board, ROW, COL); // Initialize chessboard 
Display(board, ROW, COL);// Print chessboard 
char ret = 0;
while (1)// Loop to continue the game 
{

player(board, ROW, COL);// Players go first 
ret = winor(board, ROW, COL);// Enter result judgment 
if (ret != 'c')// Not “ continue ” The result jumps out 
{

break;
}
x_man(board, ROW, COL);// The man-machine walks behind 
ret = winor(board, ROW, COL);// Judge again 
if (ret != 'c')
{

break;
}
}
if (ret == 'p')
printf(" It ends in a draw !\n");
else if (ret == '*')
printf(" You win !\n");
else
printf(" Did you lose \n");// The return value corresponds to three game results 
}

Strategic overview

The whole game is finished here , To sort out the complete project is as follows :

test.c

# include"game.h"
void menu()
{

printf("------------------------------------\n");
printf("------------------------------------\n");
printf("-------1.P L A Y-----------\n");
printf("-------0.E X I T-----------\n");
printf("------------------------------------\n");
printf("------------------------------------\n");
}
void game()
{

char board[ROW][COL] = {
 0 };
Printboard(board, ROW, COL);
Display(board, ROW, COL);
char ret = 0;
while (1)
{

player(board, ROW, COL);
ret = winor(board, ROW, COL);
if (ret != 'c')
{

break;
}
x_man(board, ROW, COL);
ret = winor(board, ROW, COL);
if (ret != 'c')
{

break;
}
}
if (ret == 'p')
printf(" It ends in a draw !\n");
else if (ret == '*')
printf(" You win !\n");
else
printf(" Did you lose \n");
}
void test()
{

int input = 0;
srand((unsigned int)time(NULL));
do
{

menu();
printf(" Please select the menu :>");
scanf("%d", &input);
switch (input)
{

case 1:
{

printf(" Sanzi \n");
game();
break;
}
case 0:
{

printf(" About to quit ……");
break;
}
default:
{

printf(" Play ?\n");
break;
}
}
} while (input);
}
int main()
{

test();
return 0;
}

game.c

# define _CRT_SECURE_NO_WARNINGS 
# include"game.h"
void Printboard(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++)
{

board[i][j] = ' ';
}
}
}
void Display(char board[ROW][COL], int row, int col)
{

int i, j = 0;
for (i = 0; i < row; i++)
{

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(char board[ROW][COL], int row, int col)// The player 
{

int x = 0;
int y = 0;
printf(" Player, please enter the chess piece coordinates :>");
scanf("%d%d", &x, &y);
while (1)
{

if (row >= x && x >= 1 && y >= 1 && y <= col)
{

if (board[x - 1][y - 1] == ' ')
{

board[x - 1][y - 1] = '*';
Display(board, row, col);
break;
}
else
{

printf(" Invalid chess space , Please re-enter \n");
break;
}
}
else
{

printf(" You're out of the chessboard !\n");
}
}
}
void x_man(char board[ROW][COL], int row, int col)// Man-machine 
{

int x = 0;
int y = 0;
printf(" Computer go :\n");
while(1)
{

x = rand() % ROW;
y = rand() % COL; //0~2
if (board[x][y] == ' ')
{

board[x][y] = '#';
Display(board, row, col);
break;
}
}
}
int Full(char board[ROW][COL], int row, int col)
{

int i,j = 0;
for (i = 0; i < ROW; i++)
{

for (j = 0; j < COL; j++)
{

if (board[i][j] == ' ')
{

return 0;
}
}
}
return 1;
}
char winor(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][1];
}
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
{

return board[1][i];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
{

return board[0][0];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
{

return board[0][2];
}
if (1==Full(board, row, col))
{

return 'p';
}
return 'c';
}

game.h

#pragma once
# define ROW 3
# define COL 3
# include<stdio.h>
# include<stdlib.h>
# include<time.h>
void game();
void Printboard(char board[ROW][COL], int row, int col);
void menu();
void Display(char board[ROW][COL], int row, int col);
void player(char board[ROW][COL], int row, int col);
void x_man(char board[ROW][COL], int row, int col);
char winor(char board[ROW][COL], int row, int col);

The operation results are as follows :

 Insert picture description here
Come here today , Lie flat, family , Or you'll have to lie flat in class tomorrow

Please bring the original link to reprint ,thank
Similar articles

2021-11-25

2021-11-25

2021-11-25

2021-11-25

2021-11-25

2021-11-25

2021-11-25