Recently I have caught a cold ("> 2)", can we slow down the update to 3 days and 2 changes
_| ̄|● ===>▇█▇▇▇█▇
>For attention, for approval, for comment<
Thanks♪(・ω・)ノ
1A2B rules of the game introduction: (don't look, don't know, a look startled)
You and your opponent should choose a four digit number respectively. Please do not repeat the numbers.
After the start of the game, the two sides guess the four digits selected by each other. The guess results will be listed in their own guess history list, and the results will be represented by A and B.
A represents the number of guessed numbers that are the same and in the correct position.
B stands for the number of guessed figures with the same number but different positions.
For example, if the other party's number is 1234, and you guessed the number is 5283, where 2 was guessed and the position is correct, and 3 was guessed but the position is not correct, the result would be 1A1B.
The game is won by the person who guesses the number of the opponent completely first (that is, the player who gets 4A first).
#include <iostream> #include <stdlib.h> #include <time.h> #include <string.h> #include <stdbool.h> void InitializeGame(void); void GetInput(char * input); bool CheckAnswer(char * input); bool GiveTips(char * input); void GetRandom(char * random); using namespace std; char answer[5] = ""; char input[10] = ""; int times = 0; int main(int argc, char** argv) { char c; while (true){ cout << "Input " S"Start the game“ Q"Quit game." << endl; c = toupper(getchar()); while(getchar() != '\n'); if (c == 'Q') break; else if(c == 'S'){ cout << "The game begins! Enter your answer:" << endl; times = 0; InitializeGame();//Initialize game // cout << "The answer is: " << answer << endl; GetInput(input); //Enter guess //Check if the guess is correct and give a prompt while(GiveTips(input) == false){ times++; GetInput(input); } times++; cout << "Congratulations! You've got it " << times << " times." << endl; }else cout << "Receive only " S"And " Q". " << endl; } return 0; } /****************************************************************************** *Function name: void InitializeGame(void) *Function function: initialize the game and generate random numbers *Entry parameter: None *Return value: None *******************************************************************************/ void InitializeGame(void){ static bool init_rand = false; if (init_rand == false){ srand ((unsigned) time(NULL)); //Initialize random number seed if not initialized init_rand = true; } GetRandom(answer);//Generate random number // cout << answer << endl; } /****************************************************************************** *Function name: void GetInput(char * input) *Function function: read a string *Entry parameter: returns the read string *Return value: None *******************************************************************************/ void GetInput(char * input){ gets(input); while(true){ if(strlen(input) != 4){ cout << "Please enter a 4-digit number!" << endl; gets(input); continue; } if(CheckAnswer(input) == false){ cout << "You can't have two identical characters in your answer!" << endl; gets(input);//Illegal re input continue; } break; } } /****************************************************************************** *Function name: bool checkanswer(char * input) *Function function: judge whether the answer is legal, that is, whether there are duplicate numbers *Entry parameter: input is the answer to be judged *Return value: true if correct, false otherwise *******************************************************************************/ bool CheckAnswer(char * input){ char temp[5]; strcpy (temp, input); for(int i = 0; i < 4; i++){ for(int j = i + 1; j < 4; j++) if(temp[i] == input[j]) return false; } return true; } /****************************************************************************** *Function name: void GiveTips(char * input) *Function function: give prompt according to the entered answer *Entry parameter: answer to be judged *Return value: None *******************************************************************************/ bool GiveTips(char * input){ // cout << "I'm checking." << endl; int a = 0, b = 0; for(int i = 0; i < 4; i++){ for(int j = 0; j < 4; j++){ // cout << "i:" << i << "j:" << j << endl; if (input[i] == answer[j]){ if(i == j) a++; else b++; continue; } } } cout << "Tips:" << a << "A" << b << "B\n" << endl; if (a == 4) return true; cout << "Enter another answer:"; return false; } /****************************************************************************** *Function name: void GetRandom(char * random) *Function function: generate a four digit random number with unequal digits *Entry parameter: random is the random number returned *Return value: None *Note: Sir, make an integer array of 0-9, and then randomly take four numbers from it. For each one, the position will be - 1 *******************************************************************************/ void GetRandom(char * random){ int i, j[10], k; for (i = 0; i < 10; i++){ j[i] = i; } for(i = 0; i < 4; i++){ //Generate the i th random number k = (int)rand() % 10;//k is subscript. while (j[k] == -1){ k = (k + 1) % 10; } random[i] = '0' + j[k]; j[k] = -1; } }
This time is relatively short
This is actually my seventh post yo!
One of your likes, one of your concerns and one of your comments is the biggest encouragement to me, a middle school student
>For attention, for approval, for comment<
Thanks♪(・ω・)ノ