C language ยท Sanzi chess (specific steps and codes)

catalogue

preface

1, File allocation

2, Menu interface

3, Chessboard initialization

4, Print chessboard

a look

5, Player drop

play chess

6, Computer chess

7, Judge the outcome

8, Core code

Total code

game.h

game.c

text.c

The result of the game

Player wins

Computer win

Draw

preface

Using functions, branch and loop statements, arrays, multi file implementation.

Chessboard: a two-dimensional array of 3 rows and 3 columns is represented by ("");
Player: fill in the ranks to play chess with "*";
Computer: using time to generate random values to play chess ("#");

1, File allocation

       game.c) for the realization of the game

       text.c) for game detection

       game.h. function reference for game and library function reference (put the library function reference in the header file, so that the code will not be too bloated)

(between game.h is used as a reference, mainly to implement games and detect games. text.c is used for game detection; therefore, it is displayed when necessary)

2, Menu interface

Reasons for using the do while loop:

  • To improve simplicity, all we need is to quit when we don't play and re-enter when there is an input error, which can be realized concisely by using the nature that non-0 is right and 0 is wrong.
//Game directory
void text()
{
	int input = 0;
	do
	{
		mune();
		printf("Please select->");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("Exit the game\n");
			break;
		default:
			printf("Input error, please re-enter\n");
			break;
		}
	} while (input);
}

//Execution entry
int main()
{
	text();
	return 0;
}
//Game home page
void mune()
{
	printf("************************************\n");
	printf("**********     1 play     **********\n");
	printf("************************************\n");
	printf("**********     0 exit     **********\n");
	printf("************************************\n");
}

3, Chessboard initialization

//Initialize chessboard
char board[ROW][COL] = { 0 };
int i = 0, j = 0;
for (i = 0; i < ROW; i++)
{
	for (j = 0; j < COL; j++)
	{
		board[i][j] = ' ';
	}
}

4, Print chessboard

Reasons for using define to define constants:

  • Improve scalability. If you want to modify the size of the chessboard in the future, the code modification will be very convenient. You only need to modify this one.
#define ROW 3 / / line
#define COL 3 / / column

The most direct method can be realized, but it is not good enough.

//Print chessboard
void dis_printf(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		//print data
		printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
		//Print split lines
		if(i<row-1)
			printf("---|---|---\n");
	}
}

Reasons for using print by print:

  • Improve scalability. When the definition constant is changed, the chessboard will be printed correctly.
  • Improve the practicability, so that it is not only applicable to Sanzi chess, the more perfect the code is, the better. It can realize the execution under multiple conditions, not just meet the current requirements.
//Print chessboard
void dis_printf(char board[ROW][COL],int row,int col)
{
	int j = 0;
	int i = 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");
		}
	}
}

a look

5, Player drop

Note: players are not programmers. We can't ask others to know the array of C language. Players can only count from 1 instead of 0, so we need to subtract 1 from both rows and columns.

//Players play chess (*)
void play_game(char board[ROW][COL],int row,int col)
{
	int x = 0;
	int y = 0;
	while (1)
	{
		printf("Please play chess->");
		scanf("%d %d", &x, &y);
		x--;
		y--;
		if (x >= 0 && x <= row && y >= 0 && y <= col)
		{
			if (board[x][y] == ' ')
			{
				board[x][y] = '*';
				break;
			}
			else
			{
				printf("There are already chess, please download again\n");
			}
		}
		else
		{
			printf("Input error, please re-enter\n");
		}
	}
}

play chess

6, Computer chess

Note: our chessboard is limited by, so the randomly generated value needs to use "% row and column" to specify the range.  

//Computer chess (#)
void computer_printf(char board[ROW][COL],int row, int col)
{
	int x = 0;
	int y = 0;

	srand((unsigned int)time(NULL));
	while(1)
	{
		x = rand() % row;//0~2
		y = rand() % col;//0~2
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}
	}
}

7, Judge the outcome

The meaning of the returned result is agreed here:
(1) '*' means the player wins
(2) '#' means the computer wins
(3) 0 means no score
(4) 1 means draw

Smart here: we let players win and computers win by returning their pieces. This is easy to return, and the code will not be too cumbersome.

//Judgment result
//If you continue, you will return 0, the draw will return 1, if the computer wins, you will return #, if the player wins, you will return *
int end_game(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;

	//Judgment line
	for (i = 0; i < row; i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
		{
			return board[i][1];
		}
	}

	//Judgment column
	for (i = 0; i < col; i++)
	{
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
		{
			return board[1][i];
		}
	}

	//Judge diagonal
	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];
	}

	//Judge whether to continue or draw
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			if (board[i][j] == ' ')//Judge whether there are spaces in the chess game
				return 0;
		}
	}
	return 1;
}

8, Core code

We know that playing chess is a round system between players and computers. It will not stop until one wins or draws. Therefore, we need to create several correct loops, and then use the break statement to jump out of the loop!

So there is the following code combination.

void game()
{
	//Initialize chessboard
	char board[ROW][COL] = { 0 };
	int i = 0, j = 0;
	for (i = 0; i < ROW; i++)
	{
		for (j = 0; j < COL; j++)
		{
			board[i][j] = ' ';
		}
	}

	//Print chessboard	
	dis_printf(board, ROW, COL);
	int ret = 0;
	while (1)
	{
		//Players play chess
		printf("Players play chess\n");
		play_game(board, ROW, COL);

		//Judgment result
		ret = end_game(board, ROW, COL);
		if (ret != 0)
		{
			break;
		}

		//Print chessboard
		dis_printf(board, ROW, COL);

		//Computer chess
		printf("Computer chess\n");
		computer_printf(board, ROW, COL);

		//Judgment result
		ret = end_game(board, ROW, COL);
		if (ret != 0)
		{
			break;
		}

		//Print chessboard
		dis_printf(board, ROW, COL);
	}
	
	ret = end_game(board, ROW, COL);
	if (ret == '*')
	{
		printf("Player wins\n");
	}		
	if (ret == '#')
	{
		printf("Computer win\n");
	}
	if (ret == 1)
	{
		printf("it ends in a draw\n");
	}
}

Just put an if statement behind the player and the computer to judge that as long as you don't continue the game, you will jump out of the loop, and then judge who wins and draws outside the loop. In this way, you only need to write the code of this judgment, which can avoid the bloated code.

Total code

game.h

#pragma once
//Function reference for game, description of header file

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROW 3 / / line
#define COL 3 / / column

//home page
void mune();

//game
void game();

//Print chessboard
void dis_printf(char board[ROW][COL],int row,int col);

//Players play chess
void play_game(char board[ROW][COL], int row, int col);

//Computer chess
void computer_printf(char board[ROW][COL], int row, int col);

//Judgment result
int end_game(char board[ROW][COL], int row, int col);

game.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

//For game implementation

//Game home page
void mune()
{
	printf("************************************\n");
	printf("**********     1 play     **********\n");
	printf("************************************\n");
	printf("**********     0 exit     **********\n");
	printf("************************************\n");
}

//Print chessboard
void dis_printf(char board[ROW][COL],int row,int col)
{
	int j = 0;
	int i = 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");
		}
	}
}


//Players play chess (*)
void play_game(char board[ROW][COL],int row,int col)
{
	int x = 0;
	int y = 0;
	while (1)
	{
		printf("Please play chess->");
		scanf("%d %d", &x, &y);
		x--;
		y--;
		if (x >= 0 && x <= row && y >= 0 && y <= col)
		{
			if (board[x][y] == ' ')
			{
				board[x][y] = '*';
				break;
			}
			else
			{
				printf("There are already chess, please download again\n");
			}
		}
		else
		{
			printf("Input error, please re-enter\n");
		}
	}
}

//Computer chess (#)
void computer_printf(char board[ROW][COL],int row, int col)
{
	int x = 0;
	int y = 0;

	srand((unsigned int)time(NULL));
	while(1)
	{
		x = rand() % row;//0~2
		y = rand() % col;//0~2
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}
	}
}


//Judgment result
//If you continue, you will return 0, the draw will return 1, if the computer wins, you will return #, if the player wins, you will return *
int end_game(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;

	//Judgment line
	for (i = 0; i < row; i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
		{
			return board[i][1];
		}
	}

	//Judgment column
	for (i = 0; i < col; i++)
	{
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
		{
			return board[1][i];
		}
	}

	//Judge diagonal
	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];
	}

	//Judge whether to continue or draw
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			if (board[i][j] == ' ')//Judge whether there are spaces in the chess game
				return 0;
		}
	}
	return 1;
}

text.c

#define _CRT_SECURE_NO_WARNINGS 1
//For game detection
#include "game.h"

void game()
{
	//Initialize chessboard
	char board[ROW][COL] = { 0 };
	int i = 0, j = 0;
	for (i = 0; i < ROW; i++)
	{
		for (j = 0; j < COL; j++)
		{
			board[i][j] = ' ';
		}
	}

	//Print chessboard	
	dis_printf(board, ROW, COL);
	int ret = 0;
	while (1)
	{
		//Players play chess
		printf("Players play chess\n");
		play_game(board, ROW, COL);

		//Judgment result
		ret = end_game(board, ROW, COL);
		if (ret != 0)
		{
			break;
		}

		//Print chessboard
		dis_printf(board, ROW, COL);

		//Computer chess
		printf("Computer chess\n");
		computer_printf(board, ROW, COL);

		//Judgment result
		ret = end_game(board, ROW, COL);
		if (ret != 0)
		{
			break;
		}

		//Print chessboard
		dis_printf(board, ROW, COL);
	}
	
	ret = end_game(board, ROW, COL);
	if (ret == '*')
	{
		printf("Player wins\n");
	}		
	if (ret == '#')
	{
		printf("Computer win\n");
	}
	if (ret == 1)
	{
		printf("it ends in a draw\n");
	}
}

void text()
{
	int input = 0;
	do
	{
		mune();
		printf("Please select->");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("Exit the game\n");
			break;
		default:
			printf("Input error, please re-enter\n");
			break;
		}
	} while (input);
}

int main()
{
	text();
	return 0;
}

The result of the game

(it's a pity that the computer is very stupid because it's random..., so it's difficult for the computer to win, and it's even harder to draw...)

Player wins

Computer wins

Draw

Keywords: C

Added by Wade on Mon, 31 Jan 2022 15:56:33 +0200