Starting from scratch

Posted by sebastiaandraaisma on Wed, 29 Dec 2021 07:07:12 +0100

catalogue

1. Menu interface

2.game() function

2.1 defining arrays

2.2 initialize the chessboard

2.3 print the initialization chessboard and construct the chessboard pattern

2.4 players play chess and print the board PlayerMove(board,ROW,COL)

2.5 computer playing chess and printing ComputerMove(board,ROW,COL)

2.6 judgment of winning and losing

3. Complete code

 3.1 test.c

3.2game.c

1. Menu interface

We're going to build test C file, game C file, game H file. Test file detection program game C is used to encapsulate the functions of the game, and the header file is used to declare.

The logic of designing the game interface must be 1 The selection interface has the option to play the game and the option to exit the game. 2 We can play this game in cycles.

With the above ideas, we need a menu to select and cycle. The effect of using do while cycle here will be better, because compared with other cycles, do while can talk about the menu first and promise to judge the cycle in the next step. Here I give the code of the game menu

void meun()
{
	printf("******************************************\n");
	printf("************      1.play      ************\n");
	printf("************      0.exit      ************\n");
	printf("******************************************\n");
}
int main()
{
	int input = 0;
	do
	{
		meun();
		printf("Please select:>\n");
		scanf("%d",&input);
		switch (input)
		{
		case 1:
			printf("Three Gobang begins\n");
			game();
			break;
		case 0:
			printf("Exit the game\n");
			break;
		default:
			printf("Error in input, please re-enter\n");
			break;
		}
	} while (input);
	return 0;
}

This string of code will exit the game when we enter 1 into 1 of the switch statement and start our game() function. When we select 0, we will exit the game. Then the construction of the game function is very important

2.game() function

    //1. Define array
    //2. Print initialization checkerboard
    //3. Players play chess
    //4. Print the player's chessboard after playing chess
    //5. The computer plays chess and agrees to the chessboard
    //6. Win or lose judgment

The whole game consists of these six modules

2.1 defining arrays

First, define the array, in which the player's chess pieces are represented by "*" and the computer's chess pieces are represented by "#". This is a character type, so the type is char.

Therefore, any 3x3 array can be used. But we found that both players and computers play chess in this 3x3 array, that is to say, there are a large number of 3 in the code behind us. If we want to turn it into a 5x5 chessboard one day, we need to change all 3 to 5, which is really bad. I use ROW to represent the ROW and COL to represent the column

void game()
{
	//1. Define array
	char board[ROW][COL];
	//2. Print initialization chessboard
	//3. Players play chess
	//4. Print the player's chessboard after playing chess
	//5. The computer plays chess and agrees to the chessboard
	//6. Judgment of winning or losing
}

Then we enter the game The H file uses the character constant defined by define to give three values to COL and ROW. as follows

We define it in the header file. If we want to use it in the test file, we have to think about game H file applies for all in test C file to apply for header file #include"game.h"

2.2 initialize the chessboard

Initializing the chessboard is to make everything in the array blank before playing chess. All elements in the array should be blank. Then, we have to print the initialized chessboard. Printing this chessboard must be constructed by us

 

The chessboard we constructed here looks like this We create the DisplayBoard() function to print the chessboard array, but at this time, the arrays in the chessboard are blank. That is, spaces

2.3 print the initialization chessboard and construct the chessboard pattern

oid 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++)
		{
			if (j < col - 1)
				printf(" %c |", board[i][j]);
			else
				printf(" %c ",board[i][j]);
		}
		printf("\n");
		if (i < row - 1)
		{
			for (j = 0; j < row; j++)
			{
				if (j < col - 1)
					printf("---|");
				else
					printf("---");
			}
		}
		printf("\n");	
	}
}

Here we print out the chessboard array.

So when playing chess, players must play chess, and then computers play chess. Obviously, this is a cycle

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"game.h"
void game()
{
	//1. Define array
	char board[ROW][COL];
	//2. Print initialization chessboard
	    //2-1 initialize chessboard
	InitBoard(board,ROW,COL);
	    //2-2 print initial checkerboard
	DisplayBoard(board,ROW,COL);
	while (1)
	{   //3. When the player plays chess, print the chessboard after the player plays chess
		PlayerMove(board, ROW, COL);
		//4. The computer plays chess and agrees to the chessboard
		
	    //5. Judgment of winning or losing
	}
	
}
void meun()
{
	printf("******************************************\n");
	printf("************      1.play      ************\n");
	printf("************      0.exit      ************\n");
	printf("******************************************\n");
}
int main()
{
	int input = 0;
	do
	{
		meun();
		printf("Please select:>\n");
		scanf("%d",&input);
		switch (input)
		{
		case 1:
			printf("Three Gobang begins\n");
			game();
			break;
		case 0:
			printf("Exit the game\n");
			break;
		default:
			printf("Error in input, please re-enter\n");
			break;
		}
	} while (input);
	return 0;
}

It's easy to think of using loops

while(1)
{
Players play chess and agree to the board.
Judge whether you win or lose.
The computer plays chess and prints the chessboard
 Judge whether to win or lose
}

2.4 players play chess and print the board PlayerMove(board,ROW,COL)

void PlayerMove(char board[ROW][COL],int row,int col)
{
     int x=0;
     int y=0;
     printf("Players play chess, please enter coordinates\n");
     while(1)
     {
        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]='*';
                 break;
           else
                 printf("Coordinates occupied, please re-enter");   
     
         }  
         else
          printf("The coordinates are incorrect. Please re-enter\n");
      }
      DispiayBoard(board,ROW,COL);
}

2.5 computer playing chess and printing ComputerMove(board,ROW,COL)

void ComputerMove(char board[ROW][COL], int row, int col)
{
	while (1)
	{
		printf("Computer start\n");
		int x = rand() % row;
		int y = rand()% col ;
		if (board[x ][y] ==' ')
		{
			board[x][y] = '#';
			break;
		}
	}
	DisplayBoard(board,ROW,COL);
}

When we use the rand() function, we must use srand. The randomly generated value here should be written srand((unsigned int)time(UNLL)) in the main function; In order to ensure that the random value changes continuously.

2.6 judgment of winning and losing

The judgment of winning or losing should be to judge the winning or losing once after each step. If there is no winning or losing, continue the next step. This should be a cycle

char IsWin(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][0];
	}
	for (i = 0; i < col; i++)
	{
		if ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) && (board[0][i] != ' '))
			return board[0][i];
	}
	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];
	if (IsFull(board, row, col))
		return 'Q';
	return 'C';
}
int IsFull(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		for (j = 0; j < col; j++)
		{
			if (board[i][j] == ' ')
				return 0;
		}
	}
	return 1;


}
void game()
{
	char ret = 0;
	srand((unsigned int)time(NULL));
	//1. Define array
	char board[ROW][COL];
	//2. Print initialization chessboard
	    //2-1 initialize chessboard
	InitBoard(board,ROW,COL);
	    //2-2 print initial checkerboard
	DisplayBoard(board,ROW,COL);
	while (1)
	{   //3. When the player plays chess, print the chessboard after the player plays chess
		PlayerMove(board, ROW, COL);
		ret = IsWin(board, ROW, COL);
		if (ret != 'C')
			break;
		//4. The computer plays chess and agrees to the chessboard
		ComputerMove(board, ROW, COL);
		ret = IsWin(board, ROW, COL);
		if (ret != 'C')
			break;
	}
	if (ret == '#')
		printf("Computer win\n");

	if (ret == '*')
		printf("Player wins\n");
	else
		printf("it ends in a draw\n");
	
}

3. Complete code

 3.1 test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"game.h"
void game()
{
	char ret = 0;
	srand((unsigned int)time(NULL));
	//1. Define array
	char board[ROW][COL];
	//2. Print initialization chessboard
	    //2-1 initialize chessboard
	InitBoard(board,ROW,COL);
	    //2-2 print initial checkerboard
	DisplayBoard(board,ROW,COL);
	while (1)
	{   //3. When the player plays chess, print the chessboard after the player plays chess
		PlayerMove(board, ROW, COL);
		ret = IsWin(board, ROW, COL);
		if (ret != 'C')
			break;
		//4. The computer plays chess and agrees to the chessboard
		ComputerMove(board, ROW, COL);
		ret = IsWin(board, ROW, COL);
		if (ret != 'C')
			break;
	}
	if (ret == '#')
		printf("Computer win\n");

	if (ret == '*')
		printf("Player wins\n");
	else
		printf("it ends in a draw\n");
	
}
void meun()
{
	printf("******************************************\n");
	printf("************      1.play      ************\n");
	printf("************      0.exit      ************\n");
	printf("******************************************\n");
}
int main()
{
	int input = 0;
	do
	{
		meun();
		printf("Please select:>\n");
		scanf("%d",&input);
		switch (input)
		{
		case 1:
			printf("Three Gobang begins\n");
			game();
			break;
		case 0:
			printf("Exit the game\n");
			break;
		default:
			printf("Error in input, please re-enter\n");
			break;
		}
	} while (input);
	return 0;
}

3.2game.c

#include"game.h"
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
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] = ' ';
		}
	}
}
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++)
		{
			if (j < col - 1)
				printf(" %c |", board[i][j]);
			else
				printf(" %c ",board[i][j]);
		}
		printf("\n");
		if (i < row - 1)
		{
			for (j = 0; j < row; j++)
			{
				if (j < col - 1)
					printf("---|");
				else
					printf("---");
			}
		}
		printf("\n");	
	}
}
void PlayerMove(char board[ROW][COL], int row, int col)
{
	printf("Player start\n");
	int x = 0;
	int y = 0;
	while (1)
	{

		printf("Please enter coordinates");
		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] = '*';
				break;
			}
			else
			{
				printf("The coordinates are occupied, please re-enter\n");
			} 
		}
		else
			printf("Coordinate input error, please re-enter\n");
	}
	DisplayBoard(board, ROW, COL);
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
	while (1)
	{
		printf("Computer start\n");
		int x = rand() % row;
		int y = rand()% col ;
		if (board[x ][y] ==' ')
		{
			board[x][y] = '#';
			break;
		}
	}
	DisplayBoard(board,ROW,COL);
}
char IsWin(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][0];
	}
	for (i = 0; i < col; i++)
	{
		if ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) && (board[0][i] != ' '))
			return board[0][i];
	}
	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];
	if (IsFull(board, row, col))
		return 'Q';
	return 'C';
}
int IsFull(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		for (j = 0; j < col; j++)
		{
			if (board[i][j] == ' ')
				return 0;
		}
	}
	return 1;


}

3.3game.h

#pragma once
#define ROW 3
#define COL 3

void InitBoard(char board[ROW][COL], int row,int col);
void DisplayBoard( char board[ROW][COL],int row,int col);
void PlayerMove(char board[ROW][COL],int row,int col);
void ComputerMove(char board[ROW][COL],int row,int col);
char IsWin(char board[ROW][COL],int row, int col);

 

Topics: C C++ Game Development