This is the 19th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021 @TOC

The third is

Three son chess game code details


@TOC


preface

We must have played backgammon, today to explain the three pieces of backgammon is also a truth, whether it is row or column, or diagonal, as long as the line into a line can win the game. Unlike normal games, this one was completely random, so it took skill for the computer to win. The game code will be called CHESS for short.


First, code framework

CHESS has three main files, which are test.c, game.c, and game.h.

Game. c: Responsible for writing specific game functions, such as Checkwin, Initboard, PlayerMove, ComputerMove, DisplayBoard.

Test.c: Is responsible for the theme part of the code, including main functions, menu interface, etc.

Game. h: Is mainly responsible for the generation dock file, which contains the various functions needed for Game. c, for example

Checkwin, InitBoard, etc.

Second, the test. C

Game function

#include"game.h"Game.h contains the header file required by the game code, so this can be done for good.
void game(a)
{
	char ret = 0;
	char board[ROW][COL];// Initialize to a space
	InitBoard(board,ROW,COL);// Initialize the checkerboard
	DisplayBoard(board, ROW, COL);// Print the checkerboard
	while (1)// the loop condition is always satisfied
	{

		/ / the player go
		PlayerMove(board,ROW,COL);
		ret = CheckWin(board,ROW,COL);
		if(ret ! ='c')
		{
			break;
		}
		DisplayBoard(board,ROW,COL);
		/ / computer
		ComputerMove(board,ROW,COL);
		ret = CheckWin(board,ROW,COL);
		if(ret ! ='c')
		{
			break;
		}
		DisplayBoard(board, ROW, COL);
		// Decide whether to win or lose 1: computer wins -'#' 2: player wins -'*' 3: tie -'q' 4: Continue -'c'
		
	}
	if (ret == The '*')
	{
		printf("Player wins \n");
	}
	else if (ret == The '#')
	{
		printf("Computer wins \n");
	}
	else if (ret == 'q')
		printf("The draw \ n");
	DisplayBoard(board, ROW, COL);
	
}
Copy the code

Third, the game. C

The code is as follows:

#include"game.h"
void InitBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		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++)
		{
			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"); }}}void PlayerMove(char board[ROW][COL],int row,int col)
{
	int x = 0;
	int y = 0;
	while (1)
	{
		printf("Player go :");
		scanf_s("%d %d", &x, &y);
		if (x >= 1 && x <= 3 && y >= 1 && y <= 3)
		{

			if (board[x- 1][y- 1] != ' ')
			{
				printf("Coordinates occupied, re-enter \n");
        	}
			else
			{
				board[x- 1][y- 1] = The '*';
				break; }}else
		{
			printf("Coordinates illegal, re-enter \n"); }}}void ComputerMove(char board[ROW][COL], int row, int col)
{
	printf("Computer go \n");

	while (1)// loop indefinitely until the coordinates are valid
	{
		int x = rand() % row;
		int y = rand() % col;
		if (board[x][y] == ' ')
		{
			board[x][y] = The '#';
			break; }}}int is_full(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i< row; i++)
	{
		for (j = 0; j < col; j++)
		{
			if (board[i][j] == ' ')
				return 0; }}return 1;
}
char CheckWin(char board[ROW][COL], int row,int col)
{
	int i = 0;
	/ / three lines
	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++)/ / three columns
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] ! =' ')
		{
			return board[0][i];
		}
	/ / 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];
	}
	/ / draw
	if (is_full(board,row,col)==1)
	{
		return 'q';
	}
	// The game continues
	If there is no win, there is no draw
	return 'c';
}

Copy the code

#define _CRT_SECURE_NO_WARNNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>// header file, is the computer to generate random numbers, random chess
#define ROW 3  // Define rows and columns here, as long as you include game.h. You can use it directly, you don't have to change it in the function, it's very convenient.
#define COL 3
void InitBoard(char board[ROW][COL], int row, int col);
// Players play chess
void PlayerMove(char board[ROW][COL], int row, int col);
void DisplayBoard(char board[ROW][COL], int row, int col);
// Computer plays chess
void ComputerMove(char board[ROW][COL], int row, int col);
char  CheckWin(char board[ROW][COL], int row, int col);
int is_full(char board[ROW][COL], int row, int col);
Copy the code

The game demo

conclusion

Pandas is used to manipulate data in pandas. Pandas provides a large number of functions and methods that allow you to manipulate data quickly and easily.