C language from entry to earth (special entry chapter) three piece chess games and how to make friends play games (programs) written by themselves (how to attach dynamic link library dll to exe)

catalogue

1: Three piece chess

1. Basic start process of the game ()

2. Implementation of Sanzi chess

2.1 the first step is to write out the basic process

2.2 step 2 print menu

2.3 realization of the game

2.3.1 initialize the chessboard

  2.3.2 printing chessboard

  2.3.3 players play chess

2.3.4 computer chess

  2.3.5 judgment

2.4 the whole code of the game

game.c:

2.5 expansion of Sanzi chess

2: vs how to publish exe files with dynamic link libraries

Step 1: vs - > tools - > command line - > developer PowerShell (P)

Step 2: check whether vs is Debug or release, x86 or x64

Step 3: find the location of exe

Step 4: query the dynamic link library (dll) required by your exe file

1: Three piece chess

1. Basic start process of the game ()

First, it must be the creation of the main function:

test.c:

int main()
{
	test();
	return 0;
}

Here, in order to make it look simple, we use the test function to receive the subsequent process

test.c:

//This is the test function we just used
void test()
{
	//Here we create an input to receive the value we just want to select
	int input = 0;
	//When we come in, we can print a menu to tell players what options are available
	menu();
	//For the start of the game, we can use the do while loop (use first and then judge), because as long as the player
	//Click in to make a selection.
	//This whole is the most initial template, we must remember ha!
	//In this way, if you write it yourself in the future, your thinking will be very clear. Only when your thinking is clear can you write good code. Of course, there are bugs
	//The probability will be much smaller! Don't a cup of tea, a cigarette, a BUG all day, ha ha! (experienced QAQ two days ago)
	do
	{
		printf("Please select:\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("Start the game\n");
			game();
			break;
			//This case 0 is for exit. You'd better specify it later. Why look at the following supplement
		case 0:
			printf("Exit the game\n");
			break;
		default:
			printf("Input error, please select again\n");
			break;
		}
		//Connect, because if input==0, when we want to exit, enter 0, and the above will print to exit the game,
		//But the real exit from the game depends on whether the value in while () is 0, but if we put input
		//It is passed into while (). When we enter 0, the while loop is judged to be false, and the real exit loop is closed
		//It's time to quit the game! So remember these two points. 1 is case: 0 to quit the game
		//2 is input in while().
	} while (input);
}

The above is the basic process of most games. Let's add it next.

2. Implementation of Sanzi chess

Before we start, let me explain to you that when we implement more complex code, we usually create three projects. Taking our game as an example, we create test.c game.c game.h. the front is where we test the code, and the latter two together are the implementation of the game.

OK, let's officially start:

With the above basic process of the game, we will realize the game. We still need to have an idea first. We should know what we want to do and how to do it. The key idea should be clear.

I'll show you the appearance of this three piece chess first:

We can see that the idea of our game implementation is roughly 5 points:

1. Initialize the chessboard
2. Print chessboard
3. Players play chess
4. Computer chess
5. Judgment

2.1 the first step is to write out the basic process

test.c:

void test()
{
	int input = 0;
	menu();
	do
	{
		printf("Please select:\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("Start the game\n");
			game();
			break;
	
		case 0:
			printf("Exit the game\n");
			break;
		default:
			printf("Input error, please select again\n");
			break;
		}
	} while (input);
}

2.2 step 2 print menu

You can design it yourself, but you'd better follow the two points I said above:

test.c:

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

2.3 realization of the game

Here's a point. We use #define to define ROW and COL, so that we can better maintain our code. If we want to change to a larger chessboard in the later stage, we can directly change these two values. Moreover, for the implementation of more code, we try to define them with meaningful values as much as possible, so as to improve the reading ability of the code.

test.c:

void game()
{
	char Board[ROW][COL] = { 0 };
	//Initialize chessboard
	InitBoard(Board, ROW, COL);
	//Print chessboard
	DisplayBoard(Board, ROW, COL);
	//play chess
	while (1)
	{
		
		//Players play chess*
		printf("Players play chess:\n");
		Player_move(Board, ROW, COL);
		DisplayBoard(Board, ROW, COL);
		//Computer chess#
		printf("Computer chess\n");
		Computer_move(Board, ROW, COL);
		DisplayBoard(Board, ROW, COL);

	}

Of course, the function declaration is indispensable when writing here. As we said earlier, we should put the function declaration in a header file called game.h. here I will directly send all the code to prevent too much confusion:

game.h:

#pragma once
//You don't have to worry about the above line. You'll talk about it in the file operation section, but you can mention that this means when there are two identical files
//When the header file is declared, only one will be retained. That's what it means!

#define ROW 3
#define COL 3
//This is the row and column just defined

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

//You can find that the name of the function has special meaning, which is very important! Otherwise, I don't know what to write after writing
//It's embarrassing if others don't know!
//Initialize checkerboard declaration
void InitBoard(char Board[ROW][COL],int row,int col);

//Print checkerboard statement
void DisplayBoard(char Board[ROW][COL],int row,int col);

//Player's chess statement
void Player_move(char Board[ROW][COL], int row, int col);

//Computer chess statement
void Computer_move(char Board[ROW][COL], int row,int col);

//Victory statement
char Is_win(char Board[ROW][COL], int row, int col);

//Draw statement
int Is_full(char Board[ROW][COL], int row, int col);

Then mention that the parameters we pass here are the array and its row and column numbers

In addition, the row and col in this way are lowercase, not uppercase, because the uppercase has been defined. We can't reuse them because they are not a thing at all. Another important reason is that the received row parameters must be variables! The uppercase here has been defined as a constant, so it must not be passed

Then, when receiving arrays or constants, you should add the type name in front and end with ";. There is no need to write the return type, because it is the declaration of the function (only telling the program that there is such a function). The return type is written in the definition of the function, and then the definition of the function is written in game.c.

2.3.1 initialize the chessboard

From the figure, we find that the format is space% c space|   First, we assign the elements of the chessboard to spaces:

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] = ' ';
		}
	}
}

  2.3.2 printing chessboard

Let's analyze it first

However, we found that if the three parts are printed directly, the adaptability to subsequent code changes is very poor

 

These two red lines should be connected from the top to the bottom, but they draw the second line for fear that everyone can't see clearly

But these are also the three parts and the two, just saying that there is no "|" on the far right, so we use if ha

We can find that the above code has strong adaptability. We can use row and col to control the number of prints,

As follows:

game.c:

void DisplayBoard(char Board[ROW][COL], int row, int col)
{
	
	int i = 0;
	int j = 0;
	for (i = 0; i < ROW; i++)
	{
		//Print line
		for (j = 0; j < COL; j++)
		{
			printf(" %c ", Board[i][j]);
			if(j<COL-1)
			printf("|");
		}
		printf("\n");
		//Print column
		for (j = 0; j < COL; j++)
		{
			printf("---");
			if (j < COL-1)
				printf("|");
		}
		printf("\n");
	}
	
}

  2.3.3 players play chess

game.c:

void Player_move(char Board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	while (1)
	{
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (Board[x - 1][y - 1] == ' ')
			{
				Board[x - 1][y - 1] = '*';
				break;
			}
			else
			{
				printf("I lost!!!\n");
			}
		}
		else
		{
			printf("Output goes!!!!\n Please re-enter:\n");
		}

	}
}

Here we use a while loop. We can jump out of the loop only when the player really enters the number, so we have to judge before that, and the input must be a valid value.

2.3.4 computer chess

game.c:

void Computer_move(char Board[ROW][COL], int row, int col)
{

	while (1)
	{
		int x = rand() % ROW;
		int y = rand() % COL;
		if (Board[x][y] == ' ')
		{
			Board[x][y] = '#';
			break;
		}
	}
}

Here, our computer chess uses the random number generation method, which is the simplest implementation method. It is worth noting that

Rand()% ROW generates numbers from 0 to 2. Then, as above, find a space and enter it. (because the numbers generated here are all 0 to 2, it is impossible to cross the chessboard, so there is no need to input judgment). Of course, I use the rand() function to generate random number seeds with srand, and then use the timestamp to generate seeds, that is: (unsigned int) srand(time(NULL)), and then put this formula in:

test.c:

You can generate pseudo-random numbers without repetition. As for the principle, it's OK to understand it at present. At present, you don't need to understand it.

  2.3.5 judgment

After players play chess with the computer, we need to judge the chessboard, lose (#) win (*) or draw (Q) or continue (C)

game.c:

char Is_win(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][1] != ' ')
		{
			return Board[i][1];
		}
	}
	//Three columns
	for (i = 0; i < col; i++)
	{
		if (Board[0][i] == Board[1][i] && Board[1][i] == Board[2][i] && Board[1][i] != ' ')
		{
			return Board[1][i];
		}
	}
	//Diagonal judgment
	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];
	}

	//Judge the draw
	if (1 == Is_full(Board, row, col))
	{
		return 'Q';
	}
	//continue
	return 'C';
}

Here we refer to a function Is_full() to judge the draw, because if the chessboard is full, if you haven't won, it's a draw. Of course, if not, then continue the game.

game.c:

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;
}

Here we find that this function has a return value (int). Because we need to judge the reception of its return value, we also need to define a variable to receive (ret).

In this way, all the ideas of the game are completed!!!

You can recall it!!!  

2.4 the whole code of the game

test.c:

#define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"

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

//1. Initialize the chessboard
//2. Print chessboard
//3. Players play chess
//4. Computer chess
//5. Judgment

void game()
{
	char Board[ROW][COL] = { 0 };
	//Initialize chessboard
	InitBoard(Board, ROW, COL);
	//Print chessboard
	DisplayBoard(Board, ROW, COL);
	//play chess
	int ret = 0;
	while (1)
	{

		//Players play chess*
		printf("Players play chess:\n");
		Player_move(Board, ROW, COL);
		DisplayBoard(Board, ROW, COL);
		ret = Is_win(Board, ROW, COL);
		if (ret != 'C')
		{
			break;
		}
		//Computer chess#
		printf("Computer chess\n");
		Computer_move(Board, ROW, COL);
		DisplayBoard(Board, ROW, COL);
		ret = Is_win(Board, ROW, COL);
		if (ret != 'C')
		{
			break;
		}
	}
	if (ret == '*')
	{
		printf("Player wins!!!\n");
	}
	else if (ret == '#')
	{
		printf("Computer victory!!!\n");
	}

	else
	{
		printf("it ends in a draw\n");
	}
}

void test()
{
	int input = 0;

	srand((unsigned)time(NULL));
	menu();
	do
	{
		printf("Please select:\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("Start the game\n");
			game();
			break;
		case 0:
			printf("Exit the game\n");
			break;
		default:
			printf("Input error, please select again\n");
			break;
		}
	} while (input);
}


int main()
{
	test();
	return 0;
}

game.h:

#pragma once


#define ROW 3
#define COL 3

#include<stdio.h>
#include<stdlib.h>
#include<time.h>


//Initialize checkerboard declaration
void InitBoard(char Board[ROW][COL], int row, int col);

//Print checkerboard statement
void DisplayBoard(char Board[ROW][COL], int row, int col);

//Player's chess statement
void Player_move(char Board[ROW][COL], int row, int col);

//Computer chess statement
void Computer_move(char Board[ROW][COL], int row, int col);

//Victory statement
char Is_win(char Board[ROW][COL], int row, int col);

//Draw statement
int Is_full(char Board[ROW][COL], int row, int col);

game.c:

#define _CRT_SECURE_NO_WARNINGS 1

#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;
	int j = 0;
	for (i = 0; i < ROW; i++)
	{
		//Print line
		for (j = 0; j < COL; j++)
		{
			printf(" %c ", Board[i][j]);
			if (j < COL - 1)
				printf("|");
		}
		printf("\n");
		//Print column
		for (j = 0; j < COL; j++)
		{
			printf("---");
			if (j < COL - 1)
				printf("|");
		}
		printf("\n");
	}

}

void Player_move(char Board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	while (1)
	{
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (Board[x - 1][y - 1] == ' ')
			{
				Board[x - 1][y - 1] = '*';
				break;
			}
			else
			{
				printf("I lost!!!\n");
			}
		}
		else
		{
			printf("Output goes!!!!\n Please re-enter:\n");
		}

	}
}

void Computer_move(char Board[ROW][COL], int row, int col)
{

	while (1)
	{
		int x = rand() % ROW;
		int y = rand() % COL;
		if (Board[x][y] == ' ')
		{
			Board[x][y] = '#';
			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;
}



//Player wins and returns*
//Computer win return#
//Draw return Q
//Continue to return to C
char Is_win(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][1] != ' ')
		{
			return Board[i][1];
		}
	}
	//Three columns
	for (i = 0; i < col; i++)
	{
		if (Board[0][i] == Board[1][i] && Board[1][i] == Board[2][i] && Board[1][i] != ' ')
		{
			return Board[1][i];
		}
	}
	//Diagonal judgment
	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];
	}

	//Judge the draw
	if (1 == Is_full(Board, row, col))
	{
		return 'Q';
	}
	//continue
	return 'C';
}



2.5 expansion of Sanzi chess

As you can see, at present, our computer depends on the generation of random numbers, so the computer is very clumsy, so we can think about it ourselves. For example, if the computer finds that two of them are connected, it will directly put them in the third to win, and if it finds that players are connected, it will block players, etc. This is a thinking question!

2: vs how to publish exe files with dynamic link libraries

When we study, we often compile some programs written by ourselves. We can certainly run them on our own computer. But sometimes on other people's computers, we will find that some programs can't run.

This is because your exe is transplanted to another computer and does not contain the dynamic link library (dll) required by the program, so the program cannot run.

Next, I'll teach you how to publish exe files with dynamic link library with vs:

Of course, before we start, we check the file extension so that we can find it better  

Step 1: vs - > tools - > command line - > developer PowerShell (P)

Step 2: check whether vs is Debug or release, x86 or x64

 

 

Step 3: find the location of exe

 

Step 4: query the dynamic link library (dll) required by your exe file

Dumpbin / dependencies your program name. exe    (this is an instruction) (there is a space after dumpbin, please note)


 

 

That's all for today!!!

After a few days of liver, it's not easy to QAQ (word count 1w +)! If you think the author is a little helpful to you!

Just a little praise and attention!!! Of course, subscription is even more desirable!

Finally, thank you for watching!!!

Your support is the greatest driving force for the author to write!!!

See you next time!!!

Tags: C Back-end Visual Studio

Posted on Wed, 10 Nov 2021 03:47:31 -0500 by EternalSorrow