Gobang Game

Catalog

  tic-tac-toe

thought

Code Run

tic-tac-toe

Ideas:

          Functions are defined in the header file and implemented internally within the function

            Are all spaces

1. Initialize the board   All are spaces (if not initialized, print out%c as a random value) as follows:  

  2. Print the board

3*3 Checkerboard prints as follows:

void displayboard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		printf(" %c | %c | %c \n", board[i][0], board[i][1],board[i][2]);
		if (i < row - 1)
			printf("---|---|---\n");
	}
}

  But if we change ROW,COL value to 5,5 board printing, there will be problems, how can we modify it?

The idea is as follows: Line one - > One     A | until the subscript is col-1

                  Line 2 --->one --- one | until it is subscribed to col-1 and only when it is less than row-1

The code was modified as follows:

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)
		{
			int j = 0;
			for (j = 0; j < col; j++)
			{
				printf("---");
				if (j < col - 1)
					printf("|");
			}
		}
		printf("\n");
	}
}

3. Start playing chess

Chess is for players, computer chess, player chess, computer chess...

Players play chess * and computers #

Players play chess to determine whether it fits the board and whether the position is occupied, and continue to cycle until the conditions are met.

Print as follows:

void playermove(char board[ROW][COL], int row, int col)
{
	int x;
	int y;
	printf("Player Walks\n");
	while (1)
	{
		printf("Please enter chess coordinates:");
		scanf_s("%d %d", &x,&y);
		//Is Chess Legal
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			//Is the location occupied
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = '*';
				break;
			}
			else
				printf("Coordinates occupied Please re-enter\n");
		}
		else
			printf("Wrong coordinates, please re-enter\n");
	}
}

  Computer chess is random, which uses the rand function we learned earlier. The rand function needs to call srand and it only needs to be called once (so it is placed in the main function)

srand((unsigned int)time(NULL)); --> Header files#include<stdlib.h>and#include<time.h>

Rows and columns in 3*3 can make x=rand()%3-->x 0-2, similarly in row*col, x=rand()%row

Print as follows:

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

  4. Judging Win or Lose

Players win, computers win, draws

Game over

Player wins--Return*--Game over

Computer won--Return#----Game over

Draw--Return to Q---------- End of Game

The game continues to return to C

The code is as follows:

void game()
{
	//2-D Array
	char board[ROW][COL];
	//Initialize the board
	initboard(board,ROW,COL);
	//Print board
	displayboard(board, ROW, COL);
	char x;
	while (1)
	{
		//Player Walks
		playermove(board, ROW, COL);
		displayboard(board, ROW, COL);
		x = iswin(board, ROW, COL);
		if (x != 'C')
			break;
		//Computer walk
		computermove(board, ROW, COL);
		displayboard(board, ROW, COL);
		x=iswin(board, ROW, COL);
		if (x != 'C')
			break;
	}
	if (x == '*')
		printf("Player won\n");
	else if (x == '#')
		printf("Computer won\n");
	else
		printf("It ends in a draw\n");
}

iswin function The game ends when one of the three lines is the same and not equal to null

                The game ends when one of the three columns is the same and not equal to empty

                The game ends when the diagonals are the same and not equal to space

Make the same judgement and return to one

                End a draw when the board is full

Note: board[i][0] == board[i][1] & & board[i][1] == board[i][2] &&board[i][0]!='' Cannot write as board[i][0] == board[i][1]   == Board[i][2]!=''

The code is as follows:

char iswin(char board[ROW][COL], int row, int col)
{
	int i = 0;
	//That's ok
	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];
	}
	//column
	for (i = 0; i < row; i++)
	{
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
			return board[0][i];
	}
	//Diagonal 1
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
		return board[0][0];
	//Diagonal 2
	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
		return board[0][2];
	//It ends in a draw
	int x = isfull(board, ROW, COL);
	if (x == 1)
	{
		return 'Q';
	}
	return 'C';
}

Judging whether the game is drawn--that is, whether the board is full

If there are spaces, they are not full, and if they are not, they are full

The code is as follows:

int isfull(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;//Not full
		}
	}
	return 1;//Full
}

The overall effect is as follows:

 

Okay, that's all for today's study. It's late at night at www~~

  Code Run:

test.c is as follows:

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdlib.h>
#include<time.h>
#include"game.h"
void menu()
{
	printf("*********************\n");
	printf("******  1.play  *****\n");
	printf("******  0.exit  *****\n");
	printf("*********************\n");
}
void game()
{
	//2-D Array
	char board[ROW][COL];
	//Initialize the board
	initboard(board,ROW,COL);
	//Print board
	displayboard(board, ROW, COL);
	char x;
	while (1)
	{
		//Player Walks
		playermove(board, ROW, COL);
		displayboard(board, ROW, COL);
		x = iswin(board, ROW, COL);
		if (x != 'C')
			break;
		//Computer walk
		computermove(board, ROW, COL);
		displayboard(board, ROW, COL);
		x=iswin(board, ROW, COL);
		if (x != 'C')
			break;
	}
	if (x == '*')
	{
		printf("Player won\n");
		displayboard(board, ROW, COL);
	}
	else if (x == '#')
	{
		printf("Computer won\n");
		displayboard(board, ROW, COL);
	}
	else
	{
		printf("It ends in a draw\n");
		displayboard(board, ROW, COL);
	}
}
int main()
{
	menu();
	srand((unsigned int)time(NULL));
	int input = 0;
	do
	{
		printf("Please select:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:game(); break;
		case 0:printf("Exit Game\n"); break;
		default:printf("Input error, please re-enter\n"); break;
		}
	} while (input);
	getchar();
	getchar();
	return 0;
}

game.c 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)
		{
			int j = 0;
			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;
	int y;
	printf("Player Walks\n");
	while (1)
	{
		printf("Please enter chess coordinates:");
		scanf_s("%d %d", &x,&y);
		//Is Chess Legal
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			//Is the location occupied
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = '*';
				break;
			}
			else
				printf("Coordinates occupied Please re-enter\n");
		}
		else
			printf("Wrong coordinates, please re-enter\n");
	}
}
void computermove(char board[ROW][COL], int row, int col)
{
	printf("Computer walk\n");
	while (1)
	{
		int x = rand() % row;
		int y = rand() % col;
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}
	}
}
int isfull(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;//Not full
		}
	}
	return 1;//Full
}
char iswin(char board[ROW][COL], int row, int col)
{
	int i = 0;
	//That's ok
	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];
	}
	//column
	for (i = 0; i < row; i++)
	{
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
			return board[0][i];
	}
	//Diagonal 1
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
		return board[0][0];
	//Diagonal 2
	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
		return board[0][2];
	//It ends in a draw
	int x = isfull(board, ROW, COL);
	if (x == 1)
	{
		return 'Q';
	}
	return 'C';
}

game.h is as follows:

//Symbol Definition
#define ROW 3
#define COL 3
//header file
#include<stdio.h>
//Function declaration
//Initialize the board
void initboard(char board[ROW][COL],int row,int col);
//Print board
void displayboard(char board[ROW][COL], int row, int col);
//Player Walks
void playermove(char board[ROW][COL], int row, int col);
//Computer walk
void computermove(char board[ROW][COL], int row, int col);
//Judging Win or Lose
char iswin(char board[ROW][COL], int row, int col);
//Judge a draw
int isfull(char board[ROW][COL], int row, int col);

Byebyrrow, Good night (o). z Z

Tags: WPF linq GNU

Posted on Mon, 06 Dec 2021 12:19:41 -0500 by kmarsh