Question: wRITE A PSEUDOCODE FOR THIS PROGRAM #include #include #include #include #define MAX_PASSES 3 /*constant for maximum number of passes*/ int turn=0; /*variable to keep the
wRITE A PSEUDOCODE FOR THIS PROGRAM
#include
#include
#include
#include
#define MAX_PASSES 3 /*constant for maximum number of passes*/
int turn=0; /*variable to keep the track of turns*/
int passesLeft[]={MAX_PASSES,MAX_PASSES}; /*array to hold the passes left for two players*/
int previouslyPassed[]={0,0}; /*another array, to denote if the player has previously passed a turn (continuously)*/
/*method to prompt the user and receive the player number (either 1 or 2)*/
int getPlayerNumber(){
int num=0;
printf("Player Number: ");
scanf("%d",&num);
fflush(stdin); /*flushing the buffer*/
if(num==turn){
/*valid player number*/
return num;
}else{
if(num==1 || num==2){
/*not the right turn*/
printf("You have to wait for your turn ");
}else{
/*entered something other than 1 and 2*/
printf("Invalid number, try again! ");
}
/*prompt again and return*/
return getPlayerNumber();
}
}
/*method to prompt the user to enter a guess and return it*/
int getGuess(){
printf("Enter your guess: ");
char text[10];
/*getting guess as text*/
scanf("%s",&text);
if(strcmp(text,"PASS")==0){
/*player entered PASS, returning -1 to denote PASS*/
return -1;
}else{
/*converting text to integer and returning it*/
int num=atoi(text);
return num;
}
}
int main(){
/*seeding random number generator*/
srand(time(NULL));
/*generating a number between 1 and 100*/
int number=rand()%100 +1;
/*deciding which player should play first, will generate eithe 1 or 2*/
turn=rand()%2 +1;
printf("Player %d will play first ",turn);
/*variable to keep the track of user guess*/
int guess=0;
/*loops until the right guess is entered*/
while(guess!=number){
/*getting valid player number*/
int p=getPlayerNumber();
/*getting guess*/
guess=getGuess();
if(guess==-1){
/*player choose to PASS*/
if(passesLeft[p-1]==0){
/*no passes left*/
printf("You dont have any passes left! ");
}else{
/*checking if the player passed a turn consecutively*/
if(previouslyPassed[p-1]==1){
printf("You cant pass for two times in a row! ");
}else{
/*passing the turn, decrementing the passes left*/
passesLeft[p-1]--;
/*displaying the remaining pass details*/
printf("You have passed %d number of times, you have %d more times left ",MAX_PASSES-passesLeft[p-1],passesLeft[p-1]);
/*setting a flag in previouslyPassed array to show that the player has passes a turn */
previouslyPassed[p-1]=1;
/*swapping the turns*/
if(turn==1){
turn=2;
}else{
turn=1;
}
}
}
}else{
/*checking if the guess is higher than the number*/
if(guess>number){
printf("Too high! ");
}else if(guess /*the guess is lower than the number*/ printf("Too low! "); }else{ /*right guess*/ printf("Right guess! Player %d wins ",p); } /*setting the previouslyPassed flag to 0, to denote that the player has not passed the current turn*/ previouslyPassed[p-1]=0; /*swapping the turns*/ if(turn==1){ turn=2; }else{ turn=1; } } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
