c language foundation Gobang, very easy to understand, detailed explanation, easy to use

preface

Tip: programming after station b and video learning

Tip: the following is the main content of this article. The following cases can be used for reference

Gobang

This blog mainly wrote Gobang code about c language
Idea:
Gobang we can divide the code into two parts: the chessboard and the chessboard. Of course, we need to know the position of the chessboard and the size of the chessboard. Then, in Gobang, we need to judge the outcome every time we play Gobang. Although in life, at the beginning of Gobang, we don't think about the outcome, but later, we think carefully about whether we will think about the outcome every time we play chess. Of course, in retrospect, for programming, the position of playing chess also needs to be considered

Header file

Because it is a small program, I did not write it in the header file, but put it together with the source file for easy modification

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#define Player1 1 / / here are three situations of the game: Player 1 wins, player 2 wins, and the game continues
#define Player2 2
#define next 3
#define ROW 11 / / chessboard size
#define COL 11

Since I have already annotated it, I will not repeat it

display chessboard

I chose to show the chessboard first, so that it would be convenient for me to write code for verification later
Because I like to count from 1, I let x and y count from 1
⭐ Note: x and Y here are not x and Y in the global variable. For a function, when the name is repeated, the variables in the function will be used first rather than the x and y of the global variables
Then, player 1 is represented by &, and player 2 is represented by *

void display(int board[][COL],int row,int col){//This function is used to display the chessboard
	int x;
	printf("\t");
	for (x = 1; x < row; x++) {
		printf("%d\t", x);
	}
	printf("\n");
	for (x = 1; x < row; x++) {
		int y = 1;
		printf("%d\t", x);
		for (; y < row; y++) {
			if (board[x][y] == Player1)
				printf("&\t");
			else if (board[x][y] == Player2)
				printf("*\t");
			else
				printf("#\t");
		}
		printf("\n");
	}
}

Play chess (PlayMove)

Before playing chess code, I first show whether the chessboard can succeed, and then play chess function
We need to pass in the two-dimensional array of chessboard, and then who represents the player currently playing chess, and who 2 is for me to avoid playing chess in this position if there are pieces on the chessboard
Here, you need to cycle while until you play chess successfully (to avoid cross-border or foul)

void PlayMove(int board[][COL],int row,int col,int who,int who2) {
	//The first who is to represent the current chess player, and the second who is to avoid playing chess in the position of existing pieces
	while (1) {
		printf("game player%d go:", who);
		scanf("%d%d", &x, &y);//Here x and y are global variables used to get the position of playing chess
		if (x >= 1 && x < row && y >= 1 && y < col) {//You can't cross the border
			if (board[x][y] != who && board[x][y] != who2) {
				board[x][y] = who;
				break;
			}
			else
				printf("Position occupied!\n");
			
		}
		else
			printf("Out of range!\n");
	}

}

Body function (game)

First, open up the memory of the chessboard and display the chessboard, and then play chess in the while loop to display the chessboard
Since it is necessary to judge the outcome after each chess game, the Isover function can exit the loop and display the results only when Isover returns Player1 or Player2

void game() {//The game function is the body function
	int board[ROW][COL];//Define chessboard size
	memset(board, ROW, COL);//Create memory for the chessboard
	int result = next;
	display(board, ROW, COL);
	while (1) {
		PlayMove(board,ROW,COL,Player1,Player2);//Player 1 goes first, then displays the chessboard, determines the outcome, and finally returns the result
		display(board, ROW, COL);
		result = Isover(board, ROW, COL, Player1);
		if (result != next)//If the result is next, the game will continue. If it is not next, exit the loop and return the result
			break;
		PlayMove(board, ROW, COL, Player2,Player1);
		display(board, ROW, COL);
		result = Isover(board, ROW, COL, Player2);
		if (result != next)
			break;
	}
	printf("The winner is Player%d",result);
}

Complete code

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#define Player1 1 / / here are three situations of the game: Player 1 wins, player 2 wins, and the game continues
#define Player2 2
#define next 3
#define ROW 11 / / chessboard size
#define COL 11

int x = 0;//The global variables x and y are used to determine the current position of playing chess
int y = 0;

void display(int board[][COL],int row,int col){//This function is used to display the chessboard
	int x;
	printf("\t");
	for (x = 1; x < row; x++) {
		printf("%d\t", x);
	}
	printf("\n");
	for (x = 1; x < row; x++) {
		int y = 1;
		printf("%d\t", x);
		for (; y < row; y++) {
			if (board[x][y] == Player1)
				printf("&\t");
			else if (board[x][y] == Player2)
				printf("*\t");
			else
				printf("#\t");
		}
		printf("\n");
	}
}

int Isover(int board[][COL],int row,int col,int who) {//This function is used to determine the outcome
	int Place_x[8] = { 0,0,1,-1,-1,1,-1,1 };//Place_x and Place_y represents the eight directions of the current chess piece
	int Place_y[8] = { -1,1,0,0,-1,1,1,-1 };
	int num[4] = { 1,1,1,1 };//num array is used to count the pieces on four lines
	for (int i = 0; i < 8; i++) {
		if (x + Place_x[i] >= 1 && y + Place_y[i] >= 1 && x + Place_x[i] <= ROW && y + Place_y[i] <= COL) {
			int _x = x;//Each time the for loop starts, you need to return to the current chess piece position
			int _y = y;
			for (int j = 0; j <= 4; j++) {
				_x += Place_x[i];
				_y += Place_y[i];
				if (board[x][y] == board[_x][_y]) {//If the same player plays chess, the num array count is + 1
					num[i / 2] += 1;
					
				}
				else break;
				
			}
		}
	}
	for (int i = 0; i < 4; i++) {//When the total number of num arrays, that is, the number of pieces on a line, has 5 or more, the winner is returned
		if (num[i] >= 5) {
			return who;
		}
	}
	return next;//If the above for function does not meet the conditions, the game continues
}

void PlayMove(int board[][COL],int row,int col,int who,int who2) {
	//The first who is to represent the current chess player, and the second who is to avoid playing chess in the position of existing pieces
	while (1) {
		printf("game player%d go:", who);
		scanf("%d%d", &x, &y);//Here x and y are global variables used to get the position of playing chess
		if (x >= 1 && x < row && y >= 1 && y < col) {//You can't cross the border
			if (board[x][y] != who && board[x][y] != who2) {
				board[x][y] = who;
				break;
			}
			else
				printf("Position occupied!\n");
			
		}
		else
			printf("Out of range!\n");
	}

}

void game() {//The game function is the body function
	int board[ROW][COL];//Define chessboard size
	memset(board, ROW, COL);//Create memory for the chessboard
	int result = next;
	display(board, ROW, COL);
	while (1) {
		PlayMove(board,ROW,COL,Player1,Player2);//Player 1 goes first, then displays the chessboard, determines the outcome, and finally returns the result
		display(board, ROW, COL);
		result = Isover(board, ROW, COL, Player1);
		if (result != next)//If the result is next, the game will continue. If it is not next, exit the loop and return the result
			break;
		PlayMove(board, ROW, COL, Player2,Player1);
		display(board, ROW, COL);
		result = Isover(board, ROW, COL, Player2);
		if (result != next)
			break;
	}
	printf("The winner is Player%d",result);
}

int main() {
	int a = 1;
	while(a){
		game();
		printf("Please enter 1 to continue and 0 to exit\n");
		scanf("%d", &a);
	}
	return 0;
}

This code is written with reference to the video of up master SunistC of station b and the Gobang code of bitegg. Thank you for watching. I hope you can praise it. Thank you

Keywords: C

Added by magic123 on Tue, 21 Sep 2021 01:24:36 +0300