Hahaha ~ tic tac toe chess (unintentional version), come and have a preliminary taste of the fun of the code world

Keywords: C Algorithm data structure

preface

Hello everyone! I haven't seen you for a long time. The forever is here again. I've been very busy recently, so I haven't updated it for a long time. Last time I introduced the function related knowledge, and then this time we'll have a little episode to show you the tic tac toe game! However, due to the tight time recently, I'll show you the unintentional version of tic tac toe chess game, ha ha! The so-called unintentional version is that the computer does not carry advanced algorithms when playing chess, and will not have a real game with players! Of course, I will continue to follow up later and upgrade this unintentional version so that players and computers can tit for tat and experience the excitement ha!
So don't worry today. Let's take a look at the unintentional version with forever. In fact, after reading the simple unintentional version, you can also try to challenge, write, carry advanced algorithms and attach advanced wisdom to the computer. At this time, the high-level wisdom shown by the computer is given by you, which reflects your wisdom! After listening to the friends, do you really want to try ha!
Next, follow forever to feel the growth process of tic tac toe chess!

text

1, Basic process of game implementation

1. To create a project, first consider how many header files and source files your entire project needs to be composed of. The detailed project creation of forever is analyzed below;
2. What role is assigned to each file, that is, what content is put in it;
3. Next, put the main function, take the main function as the trunk, and then branch and open leaves;
4. Print the game menu;
5. Chessboard initialization;
6. Print chessboard;
7. Start playing chess: if the player is defined to play chess first;
8. Secondly, computer chess;
9. Judgment results;
10. The game is over.

2, Game implementation steps

1. Create project and assign functions

forever divides the whole program into two modules: header file module and source file module. The header file module contains: game.h
(including the header file, macro definition and function declaration that the whole program needs to call); The source files include: main.cpp (source file of main function), game.cpp (source file of game start), gamelogic.cpp (source file of game overall logic framework), test.cpp (source file of game specific operation content).

//Header file module
game.h   //Contains the header file, macro definition and function declaration that the whole program needs to call
//Source file module
main.cpp   //Main function (main function)
game.cpp  //Implementation of game start
gamelogic.cpp  //Implementation of the overall logical framework of the game
test.cpp  //Game specific operation content implementation

2. Add content to header file

Here I'll write in order. First write what you need at the beginning, and then what header files you need later. forever will add HA in each step in turn! Here's what you need from beginning to end!

//game.h contains the header file, macro definition and function declaration that the whole program needs to call

#include<stdio.h>

3. When, the main function comes out

//main.cpp main function (main function)
#include "game.h"

int main()
{
	game();//Call the game function to play!
	return 0;
}

4. Inside the game function: print the game menu

Friends! You must know the game menu when playing games! That's the interface! But this menu is super simple, only the beginning and end of the game.

//Implementation of game.cpp game start
#include "game.h"

void meau()
{
	printf("--- 0.exit ---\n");
	printf("--- 1.play ---\n");
}

In addition to the print menu, there is also a function to start the game, that is, the game() function mentioned above. At this time, there will be a declaration of the game() function in the header file:

#include<stdio.h>

void game();//Total game function

All the contents in game.cpp are:

#include "game.h"
//Print menu
void meau()
{
	printf("--- 0.exit ---\n");
	printf("--- 1.play ---\n");
}
//The game begins
void game()
{
	unsigned int number = 0;//Use the unsigned modifier to ensure that the input is a positive integer
	do
	{
		meau();//Print menu
		printf("Please select:>");
		scanf("%d", &number);
		switch (number)
		{
		case 0:printf("Pay attention to rest and protect your eyes. Ha, the game is over!\n");
			break;
		case 1:printf("Cheer up and think seriously. Ha, the game begins!\n");
			playgame();//Overall content framework of the game
			break;
		default:
			printf("This friend is a little skinny. Please choose the correct number!\n");
		}
	} while (number);
}

Operation related results:

5. Chessboard initialization

At this time, a two-dimensional character array will be created and stored on the chessboard, because the latter two-dimensional character array will be used repeatedly, so the size of the character array forever will be presented to you in the way of macro definition! At this time, the macro definition will be added to the header file, and of course, the above playgame() function declaration:

#include<stdio.h>

#define LINE 3
#define LIST 3

void game();//Total game function
void playgame();//Overall logical framework of the game
void Firstboard(char board[LINE][LIST], int line, int list);//Initialize chessboard

Advantages of macro definition:
1. Facilitate the modification of procedures;
2. Improve the operation efficiency of the program;
The expansion of macro definition is completed in the preprocessing stage of the program. There is no need to allocate memory after running. It can partially realize the function of the function, but there are no problems such as pressing the stack when calling the function. It has high efficiency
3. Enhance code readability
4. Increase code flexibility

Here, all chessboards are initialized to spaces:

void Firstboard(char board[LINE][LIST], int line, int list)
{
	for (int i = 0; i < line; i++)
	{
		for (int j = 0; j < list; j++)
		{
			board[i][j] = ' ';
		}
	}
}

6. Print chessboard

Here, the header file will declare the print checkerboard function:

#include<stdio.h>

#define LINE 3
#define LIST 3

void game();//Total game function
void playgame();//Overall logical framework of the game
void Firstboard(char board[LINE][LIST], int line, int list);//Initialize chessboard
void printboard(char board[LINE][LIST], int line, int list);//Print chessboard

Here we will print the chessboard and show you a relatively rigid version:

void printboard(char board[line][list], int line, int list)
{
	for (int i = 0; i < line; i++)
	{
		printf(" %c | %c | %c\n", board[i][0], board[i][1], board[i][2]);
		if (i < line - 1)
		{
			printf("---|---|---\n");
		}
	}
}

Operation results:

It can be seen that the printing here is the simple and rough direct printing method. Of course, this method is the most direct, the simplest, but also the most rigid. What if we want to print more checkerboard grids at this time? Can't you do it? You have to modify a lot. If you print in this way, you have to print a lot. It really reduces the flexibility of the code. Therefore, modify the code as follows:

void printboard(char board[LINE][LIST], int line, int list)
{
	int i = 0, j = 0;
	for (i = 0; i < line; i++)
	{
		for (j = 0; j < list; j++)
		{
			printf(" %c ",board[i][j]);
			if (j < list - 1)
			{
				printf("|");
			}
		}
		printf("\n");
		if (i < line - 1)
		{
			for (j = 0; j < list; j++)
			{
				printf("---");
				if (j < list - 1)
				{
					printf("|");
				}
			}
			printf("\n");
		}
	}
}

Operation results:

Here, the forever uses the loop to write the whole code flexibly. If you want to print more grids, you can directly change the amount defined by the macro! So friends, when writing code in the future, try to pay attention to the readability, appreciation and flexibility of the code, so as to make your level of writing code higher and better!

7. Players play chess

Here, the header file will declare the player's chess function:

#include<stdio.h>

#define LINE 3
#define LIST 3

void game();//Total game function
void playgame();//Overall logical framework of the game
void Firstboard(char board[LINE][LIST], int line, int list);//Initialize chessboard
void printboard(char board[LINE][LIST], int line, int list);//Print chessboard
void playergame(char board[LINE][LIST], int line, int list);//Players play chess

If we require players to play chess first, we have to start playing chess here! Here we define that players use * as chess pieces!

void playergame(char board[LINE][LIST], int line, int list)
{
	printf("Player go\n Please enter coordinates:>");
	int x = 0, y = 0;
	while (1)
	{
		scanf("%d %d", &x, &y);
		if (x >0 && x <= line && y >0 && y <= list)//x. Y is 1-3, which is convenient for players to input
		{
			if (board[x - 1][y - 1] == ' ')//Here x-1 and y-1 correspond to array elements
			{
				board[x - 1][y - 1] = '*';
				break;
			}
			else
			{
				printf("PI Youyou, there are already chess pieces in this coordinate, please go down again!\n");
			}
		}
		else
		{
			printf("This friend is a little skinny. Please enter the correct range coordinates!\n");
		}
	}
}

Operation results:

8. Computer chess

As always, the header file will declare the computer chess function:

#include<stdio.h>
#Include < stdlib. H > / / rand() header file call
#Include < time. H > / / time header file call

#define LINE 3
#define LIST 3

void game();//Total game function
void playgame();//Overall logical framework of the game
void Firstboard(char board[LINE][LIST], int line, int list);//Initialize chessboard
void printboard(char board[LINE][LIST], int line, int list);//Print chessboard
void playergame(char board[LINE][LIST], int line, int list);//Players play chess
void computergame(char board[LINE][LIST], int line, int list);//Computer chess

Here the computer starts playing chess! Let's stipulate that the pieces in computer chess are + symbols!

void computergame(char board[LINE][LIST], int line, int list)
{
	printf("Computer walk\n");
	int x = 0, y = 0;
	while (1)
	{
		x = rand() % line;//Generate random number
		y = rand() % list;
		if (board[x][y] == ' ')
		{
			board[x][y] = '+';
			break;
		}
	}
}

Operation results:

x = rand() % line; // Generate random number: here is to let the computer generate random coordinates when playing chess, that is, playing chess randomly, which also corresponds to the unintentional version in our title! Ha ha ha ha! There is no thinking. What is randomly generated is what ha! Of course, when rand () is used here, an srand((unsigned int) time (NULL)) must be used in the main function, as shown below:

int main()
{
	srand((unsigned int)time(NULL));
	game();
	return 0;
}

9. Judgment result

Here, a function for judging the outcome should be declared in the header file:

#include<stdio.h>
#Include < stdlib. H > / / rand() header file call
#Include < time. H > / / time header file call

#define LINE 3
#define LIST 3

void game();//Total game function
void playgame();//Overall logical framework of the game
void Firstboard(char board[LINE][LIST], int line, int list);//Initialize chessboard
void printboard(char board[LINE][LIST], int line, int list);//Print chessboard
char is_win(char board[LINE][LIST], int line, int list);//Judge whether to win or lose
void playergame(char board[LINE][LIST], int line, int list);//Players play chess
void computergame(char board[LINE][LIST], int line, int list);//Computer chess
void is_guo(char ret);//Judge the final result

That's all the content in the header file!

Each move of chess needs to judge whether the game is over. If it is not over, continue to play with each other. If it is over, judge the victory or draw. Therefore, there are four situations:
1. Not finished continue (return 'w')
2. At the end, the player wins (return to '*')
3. At the end, the computer wins (return to '+')
4. Draw at the end (return to 'p')

Here is the function to judge the win or lose and the final result:

//Judge whether to win or lose
char is_win(char board[LINE][LIST], int line, int list)
{
	//Judgment line
	for (int i = 0; i < line; i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
		{
			return board[i][1];
		}
	}
	//Judgment column
	for (int j = 0; j < list; j++)
	{
		if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ')
		{
			return board[1][j];
		}
	}
	//Judge 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];
	}

	
	if (is_full(board, line, list) == 1)
	{
		return 'p';
	}
	return 'w';
}

//Judge the final result
void is_guo(char ret)
{
	if (ret == '*')
	{
		printf("Congratulations, you have won!\n");
	}
	else if (ret == '+')
	{
		printf("I'm sorry, you lost!, Keep up your efforts!\n");
	}
	else
	{
		printf("That's good. It's a perfect match!\n");
	}
}

Then in is_ Put an is in the win function_ The full function determines whether the chessboard is full, which is used to determine whether there is a draw:

int is_full(char board[LINE][LIST], int line, int list)
{
	for (int i = 0; i < line; i++)
	{
		for (int j = 0; j < list; j++)
		{
			if (board[i][j] == ' ')
			{
				return 0;
			}
		}
		return 1;
	}
}

3, Game result demonstration

1. Congratulations, youyou won!

2. The computer won, I'm sorry, keep up!

3. What a match!

4, Game module

1. Header file module

#include<stdio.h>
#Include < stdlib. H > / / rand() header file call
#Include < time. H > / / time header file call

#define LINE 3
#define LIST 3

void game();//Total game function
void playgame();//Overall logical framework of the game
void Firstboard(char board[LINE][LIST], int line, int list);//Initialize chessboard
void printboard(char board[LINE][LIST], int line, int list);//Print chessboard
char is_win(char board[LINE][LIST], int line, int list);//Judge whether to win or lose
void playergame(char board[LINE][LIST], int line, int list);//Players play chess
void computergame(char board[LINE][LIST], int line, int list);//Computer chess
void is_guo(char ret);//Judge the final result

2. Source file module

1. main.cpp (source file of main function) module

#include "game.h"
//Main function (main function)
int main()
{
	srand((unsigned int)time(NULL));
	game();
	return 0;
}

2. game.cpp (source file for game start) module

#include "game.h"
//The game begins to realize
void meau()
{
	printf("--- 0.exit ---\n");
	printf("--- 1.play ---\n");
}
void game()
{
	unsigned int number = 0;
	do
	{
		meau();
		printf("Please select:>");
		scanf("%d", &number);
		switch (number)
		{
		case 0:printf("Pay attention to rest and protect your eyes. Ha, the game is over!\n");
			break;
		case 1:printf("Cheer up and think seriously. Ha, the game begins!\n");
			playgame();
			break;
		default:
			printf("This friend is a little skinny. Please choose the correct number!\n");
		}
	} while (number);
}

3. gamelogic.cpp (game overall logic framework source file) module

#include "game.h"
//Overall logical framework of the game

void playgame()
{
	//Create a two-dimensional character array
	char board[LINE][LIST] = { 0 };//LINE is the number of rows after macro definition, and LIST is the number of columns after macro definition
	//Chessboard initialization, initialize the chessboard as a space
	Firstboard(board, LINE, LIST);//Call this function
	//Print chessboard
	printboard(board, LINE, LIST);
	//Here we define that players start playing chess first
	char ret = 0;
	while (1)
	{
		playergame(board, LINE, LIST);
		printboard(board, LINE, LIST);
		ret = is_win(board, LINE, LIST);
		if (ret != 'w')
		{
			break;
		}
		Computer chess 
		computergame(board, LINE, LIST);
		printboard(board, LINE, LIST);
		ret = is_win(board, LINE, LIST);
		if (ret != 'w')
		{
			break;
		}
	}
	//Player victory*
	//Computer victory+
	//Draw p
	//Not ended w
	is_guo(ret);
}

4. test.cpp (game specific operation content source file) module

#include "game.h"
//Specific operation content of the game
//Chessboard initialization
void Firstboard(char board[LINE][LIST], int line, int list)
{
	for (int i = 0; i < line; i++)
	{
		for (int j = 0; j < list; j++)
		{
			board[i][j] = ' ';
		}
	}
}
//Print chessboard
void printboard(char board[LINE][LIST], int line, int list)
{
	int i = 0, j = 0;
	for (i = 0; i < line; i++)
	{
		for (j = 0; j < list; j++)
		{
			printf(" %c ",board[i][j]);
			if (j < list - 1)
			{
				printf("|");
			}
		}
		printf("\n");
		if (i < line - 1)
		{
			for (j = 0; j < list; j++)
			{
				printf("---");
				if (j < list - 1)
				{
					printf("|");
				}
			}
			printf("\n");
		}
	}
}

//Start playing chess

//Players play chess
void playergame(char board[LINE][LIST], int line, int list)
{
	printf("Player go\n Please enter coordinates:>");
	int x = 0, y = 0;
	while (1)
	{
		scanf("%d %d", &x, &y);
		if (x >0 && x <= line && y >0 && y <= list)
		{
			if (board[x - 1][y - 1] = ' ')
			{
				board[x - 1][y - 1] = '*';
				break;
			}
			else
			{
				printf("PI Youyou, there are already chess pieces in this coordinate, please go down again!\n");
			}
		}
		else
		{
			printf("This friend is a little skinny. Please enter the correct range coordinates!\n");
		}
	}
}

//Computer chess
void computergame(char board[LINE][LIST], int line, int list)
{
	printf("Computer walk\n");
	int x = 0, y = 0;
	while (1)
	{
		x = rand() % line;
		y = rand() % list;
		if (board[x][y] == ' ')
		{
			board[x][y] = '+';
			break;
		}
	}
}

//Judge whether the chessboard is full, so as to judge whether it is a draw
int is_full(char board[LINE][LIST], int line, int list)
{
	for (int i = 0; i < line; i++)
	{
		for (int j = 0; j < list; j++)
		{
			if (board[i][j] == ' ')
			{
				return 0;
			}
		}
		return 1;
	}
}

//Judge whether to win or lose
char is_win(char board[LINE][LIST], int line, int list)
{
	//Same judgment line
	for (int i = 0; i < line; i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
		{
			return board[i][1];
		}
	}
	//The judgment columns are the same
	for (int j = 0; j < list; j++)
	{
		if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ')
		{
			return board[1][j];
		}
	}
	//Judge that the diagonal is the same
	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 (is_full(board, line, list) == 1)
	{
		return 'p';
	}
	return 'w';
}

//Judge the final result
void is_guo(char ret)
{
	if (ret == '*')
	{
		printf("Congratulations, you have won!\n");
	}
	else if (ret == '+')
	{
		printf("I'm sorry, you lost!, Keep up your efforts!\n");
	}
	else
	{
		printf("That's good. It's a perfect match!\n");
	}
}

4, Overall code implementation

#include<stdio.h>
#Include < stdlib. H > / / rand() header file call
#Include < time. H > / / time header file call

#define LINE 3
#define LIST 3

void game();//Total game function
void playgame();//Overall logical framework of the game
void Firstboard(char board[LINE][LIST], int line, int list);//Initialize chessboard
void printboard(char board[LINE][LIST], int line, int list);//Print chessboard
char is_win(char board[LINE][LIST], int line, int list);//Judge whether to win or lose
void playergame(char board[LINE][LIST], int line, int list);//Players play chess
void computergame(char board[LINE][LIST], int line, int list);//Computer chess
void is_guo(char ret);//Judge the final result

void meau()
{
	printf("--- 0.exit ---\n");
	printf("--- 1.play ---\n");
}

void game()
{
	unsigned int number = 0;
	do
	{
		meau();
		printf("Please select:>");
		scanf("%d", &number);
		switch (number)
		{
		case 0:printf("Pay attention to rest and protect your eyes. Ha, the game is over!\n");
			break;
		case 1:printf("Cheer up and think seriously. Ha, the game begins!\n");
			playgame();
			break;
		default:
			printf("This friend is a little skinny. Please choose the correct number!\n");
		}
	} while (number);
}

void playgame()
{
	//Create a two-dimensional character array
	char board[LINE][LIST] = { 0 };//LINE is the number of rows after macro definition, and LIST is the number of columns after macro definition
	//Chessboard initialization, initialize the chessboard as a space
	Firstboard(board, LINE, LIST);//Call this function
	//Print chessboard
	printboard(board, LINE, LIST);
	//Here we define that players start playing chess first
	char ret = 0;
	while (1)
	{
		playergame(board, LINE, LIST);
		printboard(board, LINE, LIST);
		ret = is_win(board, LINE, LIST);
		if (ret != 'w')
		{
			break;
		}
		Computer chess 
		computergame(board, LINE, LIST);
		printboard(board, LINE, LIST);
		ret = is_win(board, LINE, LIST);
		if (ret != 'w')
		{
			break;
		}
	}
	is_guo(ret);
}

void Firstboard(char board[LINE][LIST], int line, int list)
{
	for (int i = 0; i < line; i++)
	{
		for (int j = 0; j < list; j++)
		{
			board[i][j] = ' ';
		}
	}
}
//Print chessboard
void printboard(char board[LINE][LIST], int line, int list)
{
	int i = 0, j = 0;
	for (i = 0; i < line; i++)
	{
		for (j = 0; j < list; j++)
		{
			printf(" %c ",board[i][j]);
			if (j < list - 1)
			{
				printf("|");
			}
		}
		printf("\n");
		if (i < line - 1)
		{
			for (j = 0; j < list; j++)
			{
				printf("---");
				if (j < list - 1)
				{
					printf("|");
				}
			}
			printf("\n");
		}
	}
}

void playergame(char board[LINE][LIST], int line, int list)
{
	printf("Player go\n Please enter coordinates:>");
	int x = 0, y = 0;
	while (1)
	{
		scanf("%d %d", &x, &y);
		if (x >0 && x <= line && y >0 && y <= list)
		{
			if (board[x - 1][y - 1] = ' ')
			{
				board[x - 1][y - 1] = '*';
				break;
			}
			else
			{
				printf("PI Youyou, there are already chess pieces in this coordinate, please go down again!\n");
			}
		}
		else
		{
			printf("This friend is a little skinny. Please enter the correct range coordinates!\n");
		}
	}
}
//Computer chess
void computergame(char board[LINE][LIST], int line, int list)
{
	printf("Computer walk\n");
	int x = 0, y = 0;
	while (1)
	{
		x = rand() % line;
		y = rand() % list;
		if (board[x][y] == ' ')
		{
			board[x][y] = '+';
			break;
		}
	}
}
int is_full(char board[LINE][LIST], int line, int list)
{
	for (int i = 0; i < line; i++)
	{
		for (int j = 0; j < list; j++)
		{
			if (board[i][j] == ' ')
			{
				return 0;
			}
		}
		return 1;
	}
}

char is_win(char board[LINE][LIST], int line, int list)
{
	//Judgment line
	for (int i = 0; i < line; i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
		{
			return board[i][1];
		}
	}
	//Judgment column
	for (int j = 0; j < list; j++)
	{
		if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ')
		{
			return board[1][j];
		}
	}
	//Judge 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];
	}

	
	if (is_full(board, line, list) == 1)
	{
		return 'p';
	}
	return 'w';
}

void is_guo(char ret)
{
	if (ret == '*')
	{
		printf("Congratulations, you have won!\n");
	}
	else if (ret == '+')
	{
		printf("I'm sorry, you lost!, Keep up your efforts!\n");
	}
	else
	{
		printf("That's good. It's a perfect match!\n");
	}
}

The above is the unintentional version of tic tac toe chess! Friends can try to play and even try to improve it!

epilogue

This issue of forever brings you an unintentional version of tic tac toe chess, so that you can experience the fun in the code, get unlimited fun from the boring code, and experience the happiness in the code. Of course, in the later stage, I will continue to improve this unintentional version of tic tac toe chess, and then I will bring you a intentional version of ha!
I hope this work can bring you happiness and let you learn some knowledge from it. If there are deficiencies or mistakes, I hope the leaders can criticize and correct. We welcome the leaders to comment and correct!
Thank you for watching. Bye!
All the above codes can be run. The compilation environment used is vs2019. Pay attention to adding the compilation header file #define when running_ CRT_ SECURE_ NO_ WARNINGS 1

Posted by agisthos on Wed, 24 Nov 2021 15:37:13 -0800