[C Language] Three-piece chess game implementation (details and process)

Table of Contents

(0) Game ideas

(1) Print menu

(2) Initial array

(3) Print the chessboard (data + dividing line)

(4) Players play chess

(5) Computer chess

(6) Determine winning or losing*

(7)Total code

(8) Running instance


(0) Game Ideas

The implementation of three-piece chess;

1. Continue as soon as the game is over without exiting

2. Three file processing: game.h, game.c, text.c

3. Steps:

(1) Print menu

(2) Initial array

(3) Print the chessboard (data + dividing line)

(4) Players play chess

(5) Computer chess

(6) Determine winning or losing

#include <stdio.h>

void menu()
{
printf("******************************\
");
printf("************1:Start game************\
");
printf("*********0:Exit game************\
");
printf("******************************\
");
printf("******Please enter the corresponding number************\
");
}

int main()
{
int input = 0;
do
{
menu();
scanf("%d", & amp;input);
switch(input)
{
case 1:
game();
case 0:
break;
default:
printf("Input error, please re-enter\
");
break;
}
} while (input);

return 0;
}

【illustrate】

1. Use input to enter, 1 to enter the game, 0 to exit the game, and other values to re-enter, use the switch statement

2. To play the game repeatedly, use the do while statement to loop

3. Use the input value to determine whether it is true or false, which facilitates looping and termination.

(2) Initial array

Main function:

#include "game.h"

void game()
{
//Create array
char board[ROW][COL] = {0};
//Initialize array
Initboard(board, ROW, COL);
\t
}

game.h file:

#pragma once
#include <stdio.h>
#define ROW 3
#define COL 3

Initboard(char board[ROW][COL], int row, int col);

game.c file:

#include "game.h"

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] = ' ';
}
}
}

【illustrate】

1. Create a two-dimensional array to implement three-piece chess, that is, 3×3

2. Use macros to define ROW and COL as 3, so that they can be reused without causing many 3s to appear in the code, which is more beautiful and convenient.

3. The two-dimensional array function accepts pointer types that do not use char* board, otherwise it cannot be referenced later.

4.Double loop initializes the array to spaces

(3) Print chessboard (data + dividing line)

game.c file:

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("\
");
for ( j = 0; j < col; j + + )
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
printf("\
");
}
}

【illustrate】

1. Implement this chessboard

2. In order to print any size chessboard, just change the values of ROW and COL, so use loop printing

3.That is, this is a unit, use a loop Print the chessboard

4. Pay attention to printing \
to achieve line breaks

(4) Players play chess

//Players play chess
Playerboard(char board[ROW][COL], int row, int col)
{
int x, y = 0;
while (1)
{
printf("Please enter the coordinates, two numbers, separated by spaces: >");
scanf("%d %d", & amp;x, & amp;y);
if (x > 0 & amp; & amp; x < row + 1 & amp; & amp; y > 0 & amp; & amp; y < col + 1)
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("Input error, please re-enter\
");
}
}
}

【illustrate】

1. In order to achieve player input here, that is, change the value of the coordinates

2. In order to prevent players from inputting randomly, it is necessary to set a limit on x and y.

3. The player is not a programmer, so starting from 1, then the coordinates need to be -1

(5)Computer playing chess

//Computer playing chess
Computerboard(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("Computer playing chess\
");
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
            break;
}
}
}

【illustrate】

1. The computer generates random values when playing chess, and you can only play chess when there are no stones in the target grid.

2. The random values of x and y should be within the loop to ensure that the random values are re-randomized when errors occur.

3. * rand needs srand((unsignded int)time(NULL) to support

At the same time, the header file #include #include is required

(6)Judge winning or losing*

game.c file:

//Judge winning or losing
int Isfull(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 Iswin(char board[ROW][COL], int row, int col)
{
//One line wins
int i = 0;
for (i = 0; i < row; i + + )
{
int j = 0;
if (board[i][j] == board[i][j + 1] & amp; & amp; board[i][j] == board[i][j + 2] & amp; & amp; board[i][j] != ' ')
{
return board[i][j];
}
}
//One column wins
for (i = 0; i < col; i + + )
{
int j = 0;
if (board[j][i] == board[j][i + 1] & amp; & amp; board[j][i] == board[j][i + 2] & amp; & amp; board[j][i] != ' ')
{
return board[j][i];
}
}
//slash wins
if (board[1][1] == board[2][2] & amp; & amp; board[2][2] == board[0][0] & amp; & amp; board[1] [1] != ' ')
{
return board[1][1];
}
if (board[1][1] == board[0][2] & amp; & amp; board[0][2] == board[2][0] & amp; & amp; board[1] [1] != ' ')
{
return board[1][1];
}
//Tie
if (Isfull(board, ROW, COL) == 1)
{
return 'Q';
}
\t//Continue the game
return 'C';
}

【illustrate】

1. Determine who wins through the return value of the Iswin function of game.c, and make the judgment in the main function, as shown in the figure

while (1)
{
//Players play chess
Playerboard(board, ROW, COL);
Displayboard(board, ROW, COL);
//Judge winning or losing
ret = Iswin(board, ROW, COL);
if (ret != 'C')
{
break;
}
//Computer plays chess
Computerboard(board, ROW, COL);
Displayboard(board, ROW, COL);
//Judge winning or losing
ret = Iswin(board, ROW, COL);
if (ret != 'C')
{
break;
}
}

if (ret == '#')
{
printf("Computer wins\
");
}
if (ret == '*')
{
printf("Player wins\
");
}
if (ret == 'Q')
{
printf("tie\
");
}

2. Continue until the result is obtained, so the loop statement

3. For convenience, first determine whether to continue the game. If there is already a result, jump out of the loop and determine the result.

(7) Total Code

//game.h header file

#pragma once
#include <stdio.h>
#define ROW 3
#define COL 3
#include <stdlib.h>
#include <time.h>

//Initialize the chessboard
 void Initboard(char board[ROW][COL], int row, int col);
//Print the chessboard
void Displayboard(char board[ROW][COL], int row, int col);
//Players play chess
void Playerboard(char board[ROW][COL], int row, int col);
//Computer plays chess
void Computerboard(char board[ROW][COL], int row, int col);
//Judge winning or losing
char Iswin(char board[ROW][COL], int row, int col);
//Main function

#define _CRT_SECURE_NO_WARNINGS 1

void menu()
{
printf("******************************\
");
printf("************1:Start game************\
");
printf("*********0:Exit game************\
");
printf("******************************\
");
}

#include "game.h"

void game()
{
srand((unsigned int)time(NULL));
int ret = 0;
//Create array
char board[ROW][COL] = {0};
//Initialize array
Initboard(board, ROW, COL);
//Print the chessboard
Displayboard(board, ROW, COL);

while (1)
{
//Players play chess
Playerboard(board, ROW, COL);
Displayboard(board, ROW, COL);
//Judge winning or losing
ret = Iswin(board, ROW, COL);
if (ret != 'C')
{
break;
}
//Computer plays chess
Computerboard(board, ROW, COL);
Displayboard(board, ROW, COL);
//Judge winning or losing
ret = Iswin(board, ROW, COL);
if (ret != 'C')
{
break;
}
}

if (ret == '#')
{
printf("Computer wins\
");
}
if (ret == '*')
{
printf("Player wins\
");
}
if (ret == 'Q')
{
printf("tie\
");
}
}


int main()
{
int input = 0;
do
{
menu();
printf("Please enter:>");
scanf("%d", & amp;input);
switch(input)
{
case 1:
game();
case 0:
break;
default:
printf("Input error, please re-enter\
");
break;
}
} while (input);

return 0;
}
//game.c file

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

//Initialize array
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] = ' ';
}
}
}

//Print the chessboard
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("\
");
for ( j = 0; j < col; j + + )
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
printf("\
");
}
}

//Players play chess
void Playerboard(char board[ROW][COL], int row, int col)
{
int x, y = 0;
while (1)
{
printf("Please enter the coordinates, two numbers, separated by spaces: >");
scanf("%d %d", & amp;x, & amp;y);
if (x > 0 & amp; & amp; x < row + 1 & amp; & amp; y > 0 & amp; & amp; y < col + 1)
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("Input error, please re-enter\
");
}
}
}

//Computer plays chess
void Computerboard(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("Computer playing chess\
");
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}

//Judge winning or losing
int Isfull(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 Iswin(char board[ROW][COL], int row, int col)
{
//One line wins
int i = 0;
for (i = 0; i < row; i + + )
{
int j = 0;
if (board[i][j] == board[i][j + 1] & amp; & amp; board[i][j] == board[i][j + 2] & amp; & amp; board[i][j] != ' ')
{
return board[i][j];
}
}
//One column wins
for (i = 0; i < col; i + + )
{
int j = 0;
if (board[j][i] == board[j][i + 1] & amp; & amp; board[j][i] == board[j][i + 2] & amp; & amp; board[j][i] != ' ')
{
return board[j][i];
}
}
//slash wins
if (board[1][1] == board[2][2] & amp; & amp; board[2][2] == board[0][0] & amp; & amp; board[1] [1] != ' ')
{
return board[1][1];
}
if (board[1][1] == board[0][2] & amp; & amp; board[0][2] == board[2][0] & amp; & amp; board[1] [1] != ' ')
{
return board[1][1];
}
//Tie
if (Isfull(board, ROW, COL) == 1)
{
return 'Q';
}
\t//Continue the game
return 'C';
}

(8) Running instance

This concludes today. Thank you for reading. Please give me your advice. Thank you.

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Algorithm skill tree Home page Overview 57531 people are learning the system