Application of two-dimensional array: Sanzi chess

Multi file programming: in the process of writing code, it is impossible to put all the code into one file. Here, the code executed by the game, the declared function and the implementation of the function are written separately, which can improve the readability of the code, facilitate the modification of the code, reduce coupling, improve the reusability of the function, and the interface is simple and clean.

PS: put the function declaration of the game in game H, write the implementation of function functions to game In the app, although the file names here can be different, they should be the same, so that when people read the code, they will see that it is an entire module.

game.h

#pragma once
#define  _CRT_SECURE_NO_WARNINGS 1
#include<iostream>

//It's convenient to change the chessboard later
#define ROW 3
#define COL 3

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

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

//Players go
void play_move(char board[ROW][COL], int row, int col, char n);

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

//Judge whether to win or lose
char IsWin(char board[ROW][COL], int row, int col);

 game.cpp

#include"game.h"

void init_board(char board[ROW][COL], int row, int col)
{
	for (int i = 0; i < ROW;i++) {
		for (int j = 0; j < COL;j++) {
			board[i][j] = ' ';
		}
	}
}

//This writing method is controlled by ROW and COL. if you want to change the size of the chessboard in the future, you don't have to change the function.
void dispaly_board(char board[ROW][COL], int row, int col)
{
	for (int i = 0; i < ROW; i++) {

		for (int j = 0; j < COL;j++) {
			printf(" %c ",board[i][j]);
			if (j < COL - 1) {
				printf("|");
			}
		}
		printf("\n");

		if (i < ROW - 1) { //Control printf("---"); The number of lines to print. The last line will not be printed
			for (int k = 0; k < COL; k++) {
				if (k < COL - 1) { //Controls the printing of the last column
					printf("---|");
				}
				else {
					printf("---");
				}
			}
		}
		printf("\n");
	}
}

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

void computer_move(char board[ROW][COL],int row,int col,char n)
{
	int x = 0, y = 0;
	printf("Players play chess:>\n");
	while (1) {
		x = rand() % row;
		y = rand() % col;
		if (board[x][y] == ' ') {
			board[x][y] = n;
			break;
		}
	}
}


int is_full(char board[ROW][COL], int row, int col)
{
	for (int i = 0; i < row;i++) {
		for (int j = 0; j < col; j++) {
			if (board[i][j]==' ') {
				return 0;
			}
		}
	}
	return 1;
}

char IsWin(char board[ROW][COL], int row, int col)
{
    //Judgment line
	for (int i = 0; i < row;i++) {
		if (board[i][0]== board[i][1]&& board[i][1]== board[i][2]&& board[i][2]!=' ') {
			return board[i][2];
		}
	}

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

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

	if (is_full(board, row, col)==1) {
		return 'Q';
	}
	return 'C';
}


Three piece chess cpp

#define  _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include"game.h"

//Interface
void menu()
{
	printf("***************\n");
	printf("**** 1.play ***\n");
	printf("**** 2.exit ***\n");
	printf("***************\n");
}

void play()
{
	char ret = 0;
	char checkerboard[ROW][COL]={0};
	init_board(checkerboard, ROW, COL); //Initialize chessboard
	dispaly_board(checkerboard, ROW, COL); //Draw a chessboard

	while (1) {
		play_move(checkerboard, ROW, COL, '*'); //Players play chess
		dispaly_board(checkerboard, ROW, COL);
		ret = IsWin(checkerboard, ROW, COL);
		if (ret!='C') {
			break;
		}
		computer_move(checkerboard, ROW, COL, '#');// Computer chess
		dispaly_board(checkerboard, ROW, COL);
		ret = IsWin(checkerboard, ROW, COL);
		if (ret != 'C') {
			break;
		}
	}

	if (ret=='*') {
		printf("The player won.\n");
	}
	else if (ret=='#'){
		printf("The computer won.\n");
	} 
	else{
		printf("it ends in a draw.\n");
	}

}

void game()
{
	srand((unsigned int)time(NULL));
	int select = 0;
	do
	{
		menu();
		printf("Please select:>\n");
		scanf("%d",&select);
		switch (select) {
		case 1:
			play();
			system("pause");
			system("cls");
			break;
		case 2:
			exit(0);
			break;
		default:
			printf("Input error\n");
		}
	} while (1);
}


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

Here, we use a two-dimensional array to store the pieces of three-dimensional chess, that is to say, it is equivalent to using a two-dimensional array as the chessboard of three-dimensional chess, and the characters stored in it are pieces. Then every time you play, it is equivalent to putting characters in the array. When the pieces in several directions are the same, the strings are equal, and then judge whether you win or lose.

Here is the random value taken by the computer.
It should be noted here that the case of the chessboard should not be dead when writing the function function. In this way, when you want to change the chessboard in the future, you should modify the whole function and increase the workload. Use #define ROW 3. If you want the size of the chessboard, you can change the size of these two.

Why is it that the judgment in the function function here does not directly use the ROW # and # COL defined by the macro to judge the conditions of the loop, but is passed in as a parameter. Isn't this unnecessary. This is because if the macro definition disappears after this function is used in other projects, it is still necessary to modify the function. Using this writing method, the macro is directly regarded as a parameter without touching the internal structure of the function.

Keywords: C C++

Added by MLJJ on Sun, 06 Mar 2022 16:09:03 +0200