Gobang game (implemented in C language)

Posted by yanisdon on Tue, 25 Jan 2022 00:33:24 +0100

To realize the game, we must first think about the principle and layout of the game.

First, let's take a look at the board and rules of the game.

The computer (randomly) and the player play the next game at a time, and the two sides take turns until either of the computer and the player wins, or the chessboard is full, and the game ends. Winning judgment: as long as the computer or player has three pieces connected together, which can be horizontal, vertical and oblique, the party wins and the game ends. If the chessboard is full, it will be judged as a draw.

Next, let's start the code implementation:

First, we print out a game menu and make it possible to repeat the game or exit the game.

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

Create a variable in the main function to accept the input number, and use the switch function to choose to play the game or quit.

    int in = 0;
	srand((unsigned int)time(NULL));//This is the use of time stamps to achieve the generation of random numbers
    //Put the selection of the beginning of the game into the loop
	do 
	{
		menu();//Print selection menu
		printf("Please select\n");
		scanf_s("%d", &in);
		switch (in)
		{
		case 1:
			printf("Start the game\n");//Enter 1 to start the game
			game();//The game is implemented above
			break;
		case 0:
			printf("Exit the game\n");//When 0, print exits the game and ends the loop
			break;
		default:
			printf("Input error");//Enter other re-enter
			break;
		}
	} while (in);//If you enter 0, you exit the game

To implement the code of game, we put the implementation of game into a new source file, and create a new header file to store the created function and the referenced library function header file.

void InitBoard(char board[ROW][COL], int r, int c)//Define the chessboard and initialize it as a space
{
	int i, j;
	for (i = 0; i < r; i++)
	{
		for (j = 0; j < c; j++)
		{
			board[i][j] = ' ';
		}
	}
}

Next, print out the chessboard

void Displayboard(char board[ROW][COL], int r, int c)//Print out a checkerboard in the shape of a well
{
	int i = 0;
	for (i = 0; i < r; i++)
	{
		int j = 0;
		for (j = 0; j < c; j++)
		{
			printf(" %c ", board[i][j]);
			if (j < c - 1)
				printf("|");
		}
		printf("\n");
		if (i < r - 1)
		{
			for (j = 0; j < r; j++)
			{
				printf("---");
				if (j < r - 1)
					printf("|");
			}
			printf("\n");
		}
	}
}

The printing effect is as follows:

Then we'll start playing chess! And we should put chess in the while loop. When the game is divided or the chessboard is full, we can jump out of the loop. We use * for players' pieces and # for computers

void player_move(char board[ROW][COL], int r, int c)//Functions of players playing chess
{
	int x = 0, y = 0;//Define two variables to store the coordinates entered by the player
	printf("Players play chess");
	while (1)
	{
		printf("Enter coordinates:");
		scanf_s("%d%d", &x, &y);
		if (x >= 1 && x <= r && y >= 1 && y <= c)//Judge whether it is a valid input
		{
			if (board[x - 1][y - 1] == ' ')//If the entered coordinates are not empty, you can click next
			{
				board[x - 1][y - 1] = '*';//Assign this coordinate to the player's chess pieces
				break;
			}
			else
			{
				printf("Coordinates occupied, re-enter");
			}
		}
		else
		{
			printf("Illegal coordinates, re-enter");
		}
	}
}

Next is computer random chess

void computer_move(char board[ROW][COL], int r, int c)
{
	int x = 0, y = 0;
	while (1)
	{
		x = rand() % r;//Random values within the coordinate range are assigned to the coordinates by rand function
		y = rand() % c;
		if (board[x][y] == ' ')//Determine whether it is a space
		{
			board[x][y] = '#';
			break;
		}
	}
}

Then judge whether to win or lose after each chess game to see whether the cycle is over.

static int if_full(char board[ROW][COL], int r, int c)//This code is used to determine whether the chessboard is full
{
	int i = 0;
	for (i = 0; i < r; i++)
	{
		int j = 0;
		for (j = 0; j < c; j++)
		{
			if (board[i][j] == ' ')
				return 0;
		}
	}
	return 1;
}
char is_win(char board[ROW][COL], int r, int c)
{
	int i = 0;
	for (i = 0; i < r; i++)
	{
        //If the elements of the three lattices are equal and not blank, it means there is a win or lose
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
		{
			return board[i][1];
		}
	}//Judge whether the column meets the winning or losing conditions
	for (i = 0; i < c; i++)
	{
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
		{
			return board[1][i];
		}
	}//Judge whether the line meets the conditions of winning or losing
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
	{
		return board[1][1];
	}//Judge whether the diagonal meets the conditions of winning or losing
	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
	{
		return board[1][1];
	}//Judge whether the diagonal meets the conditions of winning or losing
	if (if_full(board, r, c) == 1)//Judge whether the chessboard is full
	{
		return 'Q';//Q is returned when the chessboard is full
	}
	return 'C';//Otherwise, it returns C. when the return value is not C, the current round of the game is over
}

Then let's look inside the game function.

void game()
{
	char ret = 0;//Define a variable to judge whether to win or lose
	char board[ROW][COL] = { 0 };//This is the array defined to print the chessboard
	InitBoard(board, ROW, COL);//Initialize chessboard
	Displayboard(board, ROW, COL);//Print chessboard
	while (1)
	{
		player_move(board, ROW, COL);//Players play chess
		Displayboard(board, ROW, COL);//Print chessboard
		ret = is_win(board, ROW, COL);//Judge whether to win or lose
		if (ret != 'C')//If the return value is not C, the game ends
		{
			break;
		}
		computer_move(board,ROW,COL);//Computer chess
		printf("Computer chess\n");
		Displayboard(board, ROW, COL);//Print chessboard
		ret = is_win(board, ROW, COL);//Judge whether to win or lose
		if (ret != 'C')
		{
			break;
		}
	}
	if (ret == '*')//If the return value is *, the player wins
		printf("The player won\n");
	else if (ret == '#')//If the return value is #, the player wins
		printf("The computer won\n");
	else//Otherwise, there will be a draw
		printf("it ends in a draw\n");
}

Let's take a look at the code in the header file we created

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define COL 3
#define ROW 3
void InitBoard(char board[ROW][COL], int r, int c);//Initialize chessboard
void Displayboard(char board[ROW][COL], int r, int c);//Print chessboard
void player_move(char board[ROW][COL], int r, int c);//Players play chess
void computer_move(char board[ROW][COL], int r, int c);//Computer chess
static int if_full(char board[ROW][COL], int r, int c);//Judge whether the chessboard is full
char is_win(char board[ROW][COL], int r, int c);//Judge whether to win or lose

Finally, let's have a good time playing Sanzi.

I won't release the whole code. Basically, that's all. Just integrate it.

(for one key and three connections))

Topics: C Game Development