The most complete and strongest number guessing game in history

Posted by scrappy1855 on Fri, 31 Dec 2021 17:23:10 +0100

This article is completely original!!!!!

This article is completely original!!!!!

This article is completely original!!!!!

If similar, please contact the author

catalogue

preface

Code list

Code analysis

summary

preface

I haven't updated my blog because of the end of the school term. It has been more than a month since the last blog. On the last day of 2021, I learned from the pain, reflected on myself, and became angry. (not for school homework, of course.It mainly uses the contents of functions and pointers. It is relatively simple. The analysis process will take the easy part.
The code realizes almost all functions of the number guessing game, including the following:

 

 

Code list

Like Sanzi chess and minesweeping, this project is realized in the form of multiple files, which is more intuitive and readable. Get ready, get ready

Header file head h:

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <math.h>
#include <malloc.h>
#include <time.h>
#include <windows.h>
#include <stdlib.h>
void Num(int* p, int n, int N);
int guess(int* p2, int* p1,int n);
int guess2(int* p2, int* p1, int n,int N);

Source file test C (master file):

#include "head.h"
void game2()//Breakthrough mode
{
	int i = 0;
	int ch = 0;
	for (i = 1; i <= 5; i++)
	{
		system("cls");
		int chance = 0;//Record the number of guesses
		int number = rand() % (int)pow(10, i);
		int* p1 = (int*)malloc(sizeof(int) * i);
		int* p2 = (int*)malloc(sizeof(int) * i);
		printf("Welcome to No%d Off, the limited number of times of this off is%d Times, the score required to pass is%d Points, dry Dad!\n",i,5*i,4*i);
		Num(p1, number, i);
		do
		{
			chance++;
			ch = guess2(p2, p1, i,chance);
		} while (ch&&chance<5*i);
		free(p1);
		free(p2);
		p1 = p2 = NULL;
		if (ch == 2)
			break;
	}
}
void game()//Normal mode
{
	system("cls");
	int level = 0;
	int ch=0;
	printf("Please select game difficulty\n");
	scanf("%d", &level);
	int number = rand() % (int)pow(10, level);//Set the number of digits to guess according to the difficulty
	int* p1 = (int*)malloc(sizeof(int) * level);//A dynamic array holds each bit of a number
	Num(p1, number, level);
	int* p2 = (int*)malloc(sizeof(int) * level);
	do
	{
		ch=guess(p2, p1, level);
	} while (ch);
	free(p1);
	free(p2);
	p1 = p2 = NULL;
}
void menu()
{
	printf("***********************\n");
	printf("*******Please select***********\n");
	printf("*******0.exit**********\n");
	printf("*******1.Normal mode*******\n");
	printf("*******2.Breakthrough mode*******\n");
	printf("*******3.Game Description*******\n");
	printf("*******4.Developer*******\n");
	printf("***********************\n");
}
void test_game()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("Quit the game, 886 pretty boy\n");
			break;
		case 2:
			game2();
			break;
		case 3:
			system("cls");
			printf("Common mode rules:\n After the user enters the difficulty, enter the corresponding number.\n If the guess is wrong, the corresponding prompt will be given (how many digits are correct and how many digits are correct but the position is incorrect),There is no limit to the number of guesses\n Customs clearance mode rules:\n There is a limit on the number of guesses. There are two pass conditions. If the user guesses the number correctly within the specified number of times or the score exceeds the specified value, he will pass\n Scoring requirements:\n(a) A number guessed by the user is different from H If the corresponding numbers in are the same, the user will score 2 bulls (points)). \n(b)A number guessed by the user is different from H The corresponding number in is different, but it is different from H If the other middle digits are the same, the user scores 1 cow (score).\n");
			break;
		case 4:
			system("cls");
			printf("Invincible handsome Wu Yingjie (copyright warning)\n");
			break;
		default:
			system("cls");
			printf("Li is a demon in Gan? Can you choose the wrong one? Reselect.\n");
			break;
		}
	} while (input);
}
int main()
{
	test_game();
	return 0;
}

Source file game c:

#include "head.h"
void Num(int* p, int n, int N)
{
	int i = 0;
	for (i = N; i > 0; i--)
	{
		*(p + i-1) = n % 10;
		n /= 10;
	}
}
int guess(int* p2, int* p1,int n)
{
	int i = 0;
	int j = 0;
	int count1 = 0;
	int count2 = 0;
	int guess = 0;
	int tmp = 0;
	printf("Please enter a%d digit\n", n);
	scanf("%d", &guess);
	tmp = guess;
	for (i = n; i > 0; i--)//Put every bit in the array p2
	{
		*(p2 + i - 1) = tmp % 10;
		tmp /= 10;
	}
	//Start comparing guesses
	for (i = 0; i < n; i++)
	{
		if (*(p2 + i) == *(p1 + i))
		{
			count1++;//Position and numbers are the same
		}
		for (j = 0; j < n; j++)
		{
			if (*(p2 + i) ==*(p1 + j))
			{
				count2++;//Number of identical numbers
				break;
			}
		}
	}
	if (count1 == n)
	{
		printf("Congratulations, you guessed right\n");
		return 0;
	}
	else
	{
		printf("The number of correct positions of the entered values is%d\n The number of correct values but incorrect positions is%d\n", count1, count2 - count1);
		return 1;
	}
}
int guess2(int* p2, int* p1, int n,int N)
{
	int score = 0;//Record score
	int i = 0;
	int j = 0;
	int count1 = 0;
	int count2 = 0;
	int guess = 0;
	int tmp = 0;
	printf("Please proceed to step%d Second guess\n", N);
	printf("Please enter a%d digit\n", n);
	scanf("%d", &guess);
	tmp = guess;
	for (i = n; i > 0; i--)//Put every bit in the array p2
	{
		*(p2 + i - 1) = tmp % 10;
		tmp /= 10;
	}
	//Start comparing guesses
	for (i = 0; i < n; i++)
	{
		if (*(p2 + i) == *(p1 + i))
		{
			count1++;//Position and numbers are the same
		}
		for (j = 0; j < n; j++)
		{
			if (*(p2 + i) == *(p1 + j))
			{
				count2++;//Number of identical numbers
				score++;
				break;
			}
		}
	}
	score += count1;
	if (count1 == n)
	{
		printf("Congratulations on your visit%d You guessed right in the second time. You've passed the test, cow\n", 5 * n);
		return 0;
	}
	else if (score>=4*n)
	{
		printf("Congratulations on your score%d,Passed, cow\n",score);
		return 0;
	}
	else
	{
		printf("The number of correct positions of the entered values is%d\n The number of correct values but incorrect positions is%d\n", count1, count2 - count1);
		if (N == 5 * n)//The last guess was wrong again
		{
			printf("Congratulations on your failure, G la\n");
			return 2;
		}
		return 1;
	}

}

I believe that many big guys here are confident. Lying in bed, closing their eyes and outputting with their feet can write better code than me.

 

Code analysis

Let's start with the idea. The first thing we need to do is to think about what they will see at first sight from the perspective of players.

Panel implementation:

After printing the menu, you must select it. Start with the switch statement directly

void test_game()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("Quit the game, 886 pretty boy\n");
			break;
		case 2:
			game2();
			break;
		case 3:
			system("cls");
			printf("Common mode rules:\n After the user enters the difficulty, enter the corresponding number.\n If the guess is wrong, the corresponding prompt will be given (how many digits are correct and how many digits are correct but the position is incorrect),There is no limit to the number of guesses\n Customs clearance mode rules:\n There is a limit on the number of guesses. There are two pass conditions. If the user guesses the number correctly within the specified number of times or the score exceeds the specified value, he will pass\n Scoring requirements:\n(a) A number guessed by the user is different from H If the corresponding numbers in are the same, the user will score 2 bulls (points)). \n(b)A number guessed by the user is different from H The corresponding number in is different, but it is different from H If the other middle digits are the same, the user scores 1 cow (score).\n");
			break;
		case 4:
			system("cls");
			printf("Invincible handsome Wu Yingjie (copyright warning)\n");
			break;
		default:
			system("cls");
			printf("Li is a demon in Gan? Can you choose the wrong one? Reselect.\n");
			break;
		}
	} while (input);
}

This part of the code is relatively simple and will not be explained.

The following is the code of normal mode and breakthrough mode

void game()//Normal mode
{
	system("cls");
	int level = 0;
	int ch=0;
	printf("Please select game difficulty\n");
	scanf("%d", &level);
	int number = rand() % (int)pow(10, level);//Set the number of digits to guess according to the difficulty
	int* p1 = (int*)malloc(sizeof(int) * level);//A dynamic array holds each bit of a number
	Num(p1, number, level);//Put each digit of the number to be guessed into the array
	int* p2 = (int*)malloc(sizeof(int) * level);
	do
	{
		ch=guess(p2, p1, level);
	} while (ch);
	free(p1);
	free(p2);
	p1 = p2 = NULL;
}

The common mode code first allows the user to enter the difficulty, which corresponds to the number of digits of the number. Here, two dynamic arrays are used to store each digit of the random number automatically generated by the computer and the number guessed by the player. Remember that it is each digit. Remember that running out of dynamic arrays here is to free memory, and assign the used pointers to null pointers (to avoid wild pointers)

Let's start guessing numbers

int guess(int* p2, int* p1,int n)
{
	int i = 0;
	int j = 0;
	int count1 = 0;
	int count2 = 0;
	int guess = 0;
	int tmp = 0;
	printf("Please enter a%d digit\n", n);
	scanf("%d", &guess);
	tmp = guess;
	for (i = n; i > 0; i--)//Put every bit in the array p2
	{
		*(p2 + i - 1) = tmp % 10;
		tmp /= 10;
	}
	//Start comparing guesses
	for (i = 0; i < n; i++)
	{
		if (*(p2 + i) == *(p1 + i))
		{
			count1++;//Position and numbers are the same
		}
		for (j = 0; j < n; j++)
		{
			if (*(p2 + i) ==*(p1 + j))
			{
				count2++;//Number of identical numbers
				break;
			}
		}
	}
	if (count1 == n)
	{
		printf("Congratulations, you guessed right\n");
		return 0;
	}
	else
	{
		printf("The number of correct positions of the entered values is%d\n The number of correct values but incorrect positions is%d\n", count1, count2 - count1);
		return 1;
	}
}

Here, compare each digit of the two numbers respectively, and get the number with the same number and position and the same number with only numbers. Then print it as a hint (Europeans don't need to guess right once). If you guess right, return 0 to exit the loop, and if you guess wrong, return 1 to continue guessing. Very simple logic

 

Let's look at the breakthrough version, which is similar to the above

void game2()//Breakthrough mode
{
	int i = 0;
	int ch = 0;
	for (i = 1; i <= 5; i++)
	{
		system("cls");
		int chance = 0;//Record the number of guesses
		int number = rand() % (int)pow(10, i);
		int* p1 = (int*)malloc(sizeof(int) * i);
		int* p2 = (int*)malloc(sizeof(int) * i);
		printf("Welcome to No%d Off, the limited number of times of this off is%d Times, the score required to pass is%d Points, dry Dad!\n",i,5*i,4*i);
		Num(p1, number, i);
		do
		{
			chance++;
			ch = guess2(p2, p1, i,chance);
		} while (ch&&chance<5*i);
		free(p1);
		free(p2);
		p1 = p2 = NULL;
		if (ch == 2)
			break;
	}
}

There are five levels here (random numbers can generate five digits at most), so carry out five cycles, and each cycle will generate different random numbers instead of the same. Here you can test it. I have tested it.

The guessing function here determines the winning or losing conditions, which are somewhat different from those above

int guess2(int* p2, int* p1, int n,int N)
{
	int score = 0;//Record score
	int i = 0;
	int j = 0;
	int count1 = 0;
	int count2 = 0;
	int guess = 0;
	int tmp = 0;
	printf("Please proceed to step%d Second guess\n", N);
	printf("Please enter a%d digit\n", n);
	scanf("%d", &guess);
	tmp = guess;
	for (i = n; i > 0; i--)//Put every bit in the array p2
	{
		*(p2 + i - 1) = tmp % 10;
		tmp /= 10;
	}
	//Start comparing guesses
	for (i = 0; i < n; i++)
	{
		if (*(p2 + i) == *(p1 + i))
		{
			count1++;//Position and numbers are the same
		}
		for (j = 0; j < n; j++)
		{
			if (*(p2 + i) == *(p1 + j))
			{
				count2++;//Number of identical numbers
				score++;
				break;
			}
		}
	}
	score += count1;
	if (count1 == n)
	{
		printf("Congratulations on your visit%d You guessed right in the second time. You've passed the test, cow\n", 5 * n);
		return 0;
	}
	else if (score>=4*n)
	{
		printf("Congratulations on your score%d,Passed, cow\n",score);
		return 0;
	}
	else
	{
		printf("The number of correct positions of the entered values is%d\n The number of correct values but incorrect positions is%d\n", count1, count2 - count1);
		if (N == 5 * n)//The last guess was wrong again
		{
			printf("Congratulations on your failure, G la\n");
			return 2;
		}
		return 1;
	}

}


Pay attention to the rules. If the position and number are the same, 2 points will be added, so after the for traversal, the score will be added with count1, that is, the number of positions and numbers are the same. If you guessed wrong the last time, you will fail to pass, return 2 and exit this pass.

summary

The code still has many shortcomings, such as the implementation of screen clearing function and the beautification of panel (it's really ugly, but I'm too lazy to do it). If you have any questions, you can actively leave a message for discussion. If you have any questions and better suggestions, you are also welcome to point out, arigado. The school can't go home until 1.20. It's estimated that it will stop for a long time. Win at the end of the term and have a high grade point.

Slip away!

Topics: C Algorithm