In C language Guess My Number (High-Low) Two Players Take Their Turn Create a pr
ID: 3599680 • Letter: I
Question
In C language Guess
My Number (High-Low) Two Players Take Their Turn Create a program that will simulate the child's number guessing game with two players who will play against the computer. The program should have the following requirements: 1. When the program is invoked the computer will pseudorandomly choose which player will go first. 2. Play begins when the computer interface asks "Player Number". A player number (either 1 or 2) is entered from the keyboard. If a player enters their number at the keyboard and it is not their turn the computer will report back "You have to wait your turn" and return to the query "Player Number". If it is a player's turn the computer will query "Enter Your Guess". The player may either enter a guess or indicate a desire to pass the play to the other player by entering the word PASS at the prompt. If the player enters a guess the computer will report back that the guess is too high, too low or correct and return to the query "Player Number" and pass play to the other player. 3. No player may pass two turns in a row, and cannot exceed more than three passes for the entire game. If the player chooses to pass the computer should report "You have passed X number of times, you have Y more times left" before returning the query "Player Number". 4. The play progresses until one user provides a correct guess.
Explanation / Answer
/* Compiled on the GNU C compiler */
/* The brief structure of the program is as follows: initially, the srand() function is used to initialize the
seed. rand() is called to get the number to be guessed and the first player. We then enter an infinite
loop where the input prompt is given and the input is read and processed. We break out of the loop
when the number has been guessed correctly */
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define PASS -1
#define MAX_PASS 3
int read_player_number();
long read_guess();
int main()
{
srand(time(NULL));
int my_number = rand(); /* This is the number to be guessed */
/* printf("Guessed number = %d ", my_number); */
int num_pass[2] = {0, 0}; /*Number of passes by each player, initialised to zero */
int prev_pass[2] = {0, 0}; /*Whether previous choice was PASS */
int current_player = (rand() % 2) + 1; /* Pseudo number with range [1,2] */
/* printf("Starting player: %d ", current_player); */
while(1)
{
int player_entered;
long guess;
player_entered = read_player_number();
if(player_entered != current_player)
{
printf("You have to wait your turn ");
continue; /* Go back to beginning of loop */
}
guess = read_guess();
if(guess == PASS)
{
if(prev_pass[player_entered - 1] == 1)
{
printf("No player may pass two turns in a row ");
continue;
}
if(num_pass[player_entered - 1] == MAX_PASS)
{
printf("You have already passed %d times ", MAX_PASS);
continue;
}
prev_pass[player_entered - 1] = 1;
num_pass[player_entered - 1] ++;
printf ("You have passed %d number of times, you have %d more times left ",
num_pass[player_entered - 1], MAX_PASS - num_pass[player_entered - 1]);
}
else /* A guess is entered */
{
prev_pass[player_entered - 1] = 0; /* Reset previous entry for PASS */
if(guess > my_number)
{
printf("Guess is too high ");
}
else if(guess < my_number)
{
printf("Guess is too low ");
}
else /* correct */
{
printf("Guess is correct ");
break; /* Exit the infinite loop and end the program */
}
}
current_player = 3 - player_entered; /* Change the player turn */
}
return 0;
}
int read_player_number()
{
char input[256];
printf("Player Number ");
scanf("%s", input);
return atoi(input);
}
long read_guess()
{
char input[256];
printf("Enter Your Guess ");
scanf("%s", input);
if(strcasecmp(input, "PASS") == 0)
return PASS;
return atoi(input);
}