Two player guessing game in C programming - c

I have a two player guess random number game; player number (1 0r 2) with turn is randomly generated as the game begins and it's prompted to enter the player number. If the player number entered does not match the random player number then it should print "You have to wait your turn" and return to the enter Player number prompt. My program goes directly to the randome number that is to be guessed by the player instead of going back to asking for player number first and then moving onto asking for entering the random number that's to be guessed by player with his/her turn. How do I get it to go back to asking for the correct player number before moving forward.
Also one correct player number is entered; each player can choose to pass by entering "PASS" twice consecutively and thrice throughout the life of the game. How do I make these conditions work in the game. Thanks in advance to all.
Here is the code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <malloc.h>
int main(void) {
int player_num = 0; int number = 0; int player_input = 0;
int guess = 0; char input; char str[6] = {0}; int Player_1 = 1;
int Player_2 = 2; int Pass_1 = 3; int Pass_2 = 3; int i = 1;
int player_turn = 0; int turn = 0;
srand(time(NULL)); player_num = 1 + rand() % 2; /* Random number is generated */
srand(time(NULL)); number = 0 + rand() % 100; /* Random number is generated */
while(number != guess) {
printf("\nIt's player's %d turn\n", player_num);
printf("Player Number?\n");
scanf("%d", &player_input);
while (player_num != player_input) {
printf("You Have to wait your turn.\nPlayer number?\n");
}
if (Player_1 != player_num)
Player_2 = player_num;
if (i%2 == 1) {
player_num = Player_1;
} else {
player_num = Player_2;
}
i = i+1;
printf("Enter Your Guess, 0 - 100 or Pass: ");
scanf("%s", str);
if (strcmp(str, "pass") == 0){
if (player_num == Player_1){
Pass_2 = Pass_2 -1;
printf("Player 2 has %d more 'Pass' left!\n", Pass_2);
}
else {
Pass_1 = Pass_1 -1;
printf("Player 1 has %d more 'Pass' left!\n", Pass_1);
}
} else {
guess = atoi(str);
if(guess < number) /* if the guess is lower, output: the guess is too low */
printf("Your guess was to low.\n ");
else if(guess > number) /* if the guess is higher, output: the guess is too high */
printf("Your guess was to high.\n ");
else /* if the guess is equal to the random number: Success!! */
printf("Yes!! you got it!\n");
}
}
return 0;
}

First, srand needs to be invoked only once and at the start of the program.
Second, scanf was moved inside the second while loop to force user to enter the correct player number or keep asking until he/she gets it right.
The following code fixes all of the above. Please read the comments in the code to see the reasons for the changes:
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
int main(void) {
int player_num = 0; int number = 0; int player_input = 0;
int guess = 0; char input; char str[15] = {0}; // size was increased to compensate for pass pass input
int Player_1 = 1;
int Player_2 = 2; int Pass_1 = 3; int Pass_2 = 3; int i = 1;
int player_turn = 0; int turn = 0; int alternate=0;
int player1Flag=0,player2Flag=0;
int lastPlayer=0;
srand(time(NULL));
player_num = 1 + rand() % 2; /* Random number is generated */
lastPlayer = player_num;
number = 0 + rand() % 100; /* Random number is generated */
while(number != guess) {
while (player_num != player_input) {
printf("\nIt's player's %d turn\n", player_num);
printf("Player Number?\n");
scanf("%d", &player_input);
getchar();// to get rid of \n after the input
if(player_input!=player_num){
printf("You Have to wait your turn.\n");
}
}
if (Player_1 != player_num) Player_2 = player_num;
printf("Enter Your Guess, 0 - 100 or Pass: ");
scanf("%s",str);
if (strcmp(str, "pass") == 0){
if (player_num == Player_1){
player1Flag = player1Flag+1; // flag to detect if last input was a pass
if(player1Flag>1){
printf("Dude you passed in your last attempt .. dont be a pus*y\nEnter a guess : ");
scanf("%s",&str);
guess = atoi(str);
if(guess < number){ /* if the guess is lower, output: the guess is too low */
printf("Your guess was to low.\n ");
}else if(guess > number){ /* if the guess is higher, output: the guess is too high */
printf("Your guess was to high.\n ");
}else{ /* if the guess is equal to the random number: Success!! */
printf("Yes!! you got it!\n");
}
player1Flag = 0; // reset the pass flag = 1 as this pass isn't counted
}else{
Pass_2 = Pass_2 -1;
if(Pass_2<0){
printf("You have already passed Thrice\nEnter a guess: ");
scanf("%s",&str);
guess = atoi(str);
if(guess < number){ /* if the guess is lower, output: the guess is too low */
printf("Your guess was to low.\n ");
}else if(guess > number){ /* if the guess is higher, output: the guess is too high */
printf("Your guess was to high.\n ");
}else{ /* if the guess is equal to the random number: Success!! */
printf("Yes!! you got it!\n");
}
}else{
printf("Player 1 has %d more 'Pass' left!\n", Pass_2);
}
}
}
else{
player2Flag = player2Flag + 1;
if(player2Flag>1){
printf("Dude you passed in your last attempt .. dont be a pus*y\nEnter a guess : ");
scanf("%s",&str);
guess = atoi(str);
if(guess < number){ /* if the guess is lower, output: the guess is too low */
printf("Your guess was to low.\n ");
}else if(guess > number){ /* if the guess is higher, output: the guess is too high */
printf("Your guess was to high.\n ");
}else{ /* if the guess is equal to the random number: Success!! */
printf("Yes!! you got it!\n");
}
player2Flag=0;// reset the player2Flag = 1 as this pass isn't counted
}else{
Pass_1 = Pass_1 -1;
if(Pass_2<0){
printf("You have already passed Thrice\nEnter a guess: ");
scanf("%s",&str);
guess = atoi(str);
if(guess < number){ /* if the guess is lower, output: the guess is too low */
printf("Your guess was to low.\n ");
}else if(guess > number){ /* if the guess is higher, output: the guess is too high */
printf("Your guess was to high.\n ");
}else{ /* if the guess is equal to the random number: Success!! */
printf("Yes!! you got it!\n");
}
}else{
printf("Player 2 has %d more 'Pass' left!\n", Pass_2);
}
}
}
}else {
if (player_num == Player_1){
player1Flag = 0;//reset pass flag as this player enetered a guess
}else{
player2Flag = 0;
}
guess = atoi(str);
if(guess < number){ /* if the guess is lower, output: the guess is too low */
printf("Your guess was to low.\n ");
}else if(guess > number){ /* if the guess is higher, output: the guess is too high */
printf("Your guess was to high.\n ");
}else{ /* if the guess is equal to the random number: Success!! */
printf("Yes!! you got it!\n");
}
}
if(lastPlayer==1){
player_num = 2;
lastPlayer = 2;
}else if(lastPlayer==2){
player_num = 1;
lastPlayer = 1;
}
}
return 0;
}

You need to add an input within your while. In other words, instead of:
while (player_num != player_input) {
printf("You Have to wait your turn.\nPlayer number?\n");
}
try this:
while (player_num != player_input) {
printf("You Have to wait your turn.\nPlayer number?\n");
scanf("%d", &player_input);
}

Related

Find two largest numbers in input

I need to make a program that will perform the following task:
Enter N natural numbers. Complete the input with 0. Output the number
of the maximal number.
I have already done this, and you can see the code below:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void) {
int i = 0, num, max_place = -1;
int max = -2147483647;
printf("Start enter numbers, bruh (please end input with 0):\n");
scanf("%d", &num);
while (num != 0) {
if (num >= max) {
max = num;
max_place = i;
}
i++;
scanf("%d", &num);
}
if (max_place == -1) printf("Numbers were not entered");
else printf("\nMax number was on %d place, bruh", max_place + 1);
return 0;
}
The teacher then made the task more difficult – the program needs to print the maximum number and the next maximum after it of the entered numbers.
How can I do it?
If you can use arrays and sort use that way. if not, this is in your code
int main(void) {
int i = 0, num, max_place = -1, second_max_place = -1;
int max = -2147483647;
int second_max = -2147483647;
printf("Start enter numbers, bruh (please end input with 0):\n");
scanf("%d", &num);
while (num != 0) {
if (num == 0) break;
if (num >= max) {
second_max = max;
second_max_place = max_place;
max = num;
max_place = i;
}
if(num < max && num >= second_max){
second_max = num;
second_max_place = i;
}
i++;
scanf("%d", &num);
}
if (max_place == -1) printf("Numbers were not entered");
else{
printf("\nMax number was on %d place, bruh", max_place + 1);
printf("\nSecond Max number was on %d place, bruh", second_max_place + 1);
}
return 0;
}

User Input of Integers Infinite Loop Until User Inputs a Character (C)

i'm new to C - I have intended to make a program that shows whether the user input int is odd or even, until the user decides to quit by inputing a char 'x'. The loop kind of works by detecting odd numbers and terminating the program with 'x', however glitches with even numbers - why is that so? Would appreciate it if you could point out the flaws in the code. Thank you
#include <stdio.h>
int main(void)
{
int i=0;
char x = "x";
printf("Enter an integer to check whether your number is odd or even\n");
printf("Enter an ´x´ at any time to quit the program\n");
do
{
scanf("%d", &i);
if (i % 2 == 0)
{
printf("The number is even\n");
}
else if (i % 2 != 0)
{
printf("The number is odd\n");
}
else("%c ", &x);
{
scanf(" %c", &x);
getchar();
printf("The program will now terminate\n");
return 0;
}
}
while (i > 0);
i++;
return 0;
}
Very close but I've marked a couple of changes:
#include <stdio.h>
int main(void)
{
int i=0;
char x = 'x'; // Note: single quotes for char, double for string
printf("Enter an integer to check whether your number is odd or even\n");
printf("Enter an ´x´ at any time to quit the program\n");
do
{
int n = scanf("%d", &i); // Check if number was read
if (n == 1) {
if (i % 2 == 0)
{
printf("The number is even\n");
}
else // Only other possibility
{
printf("The number is odd\n");
}
} else // No number, see if there's an 'x'
{
scanf(" %c", &x);
if (x == 'x')
{
printf("The program will now terminate\n");
return 0;
} else
{
printf("Unknown input %c\n", x);
}
}
}
while (i > 0); // Will also end if user enters <= 0
return 0;
}

How to end a "do...while" loop after 5 attempts? In C

I got my program to fully work, all except I need the program to stop asking after 5 failed attempts. I have absolutely no idea how to go about that. What it is suppose to do, if the number entered is incorrect 5 times in a row, there would then be a sorry message at the end. I can't figure out how to insert the counter into the code, I know what I put in is probably nowhere near correct.
P.S. The code has to have multiple functions. Some I'd rather put into one myself but instruction say multiple.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int number;
void welcomeMessage(){
printf("Welcome to my new guessing game!\n");
printf("Let's get started!\n");
}
int randomNumber(int number){
int range;
srand(time(NULL));
range = (20 - 1) + 1;
number = rand() % range + 1;
return number;
}
int guessInput(){
int guess;
printf("I'm thinking of a number between 1 and 20.\n");
printf("Care to give it a guess? Be careful! You only get 5 tries!: ");
scanf("%d", &guess);
return guess;
}
int guess_input(){
int guess;
scanf("%d", &guess);
return guess;
}
int wrongAnswer(int guess){
if(guess < number)
{
printf("Try again, your guess is too low: ");
return 1;
} else if(guess > number) {
printf("Give it another try, your guess was a bit too high: ");
return 1;
}
return 0;
}
int correctAnswer(int guess){
if(guess == number){
printf("Great job! That time you got it right!\n");
return 0;
} else{
return 1;
}
}
void sorryMessage(int number){
printf("Sorry, the correct number was %number.\n");
printf("Better luck next time!");
}
int main(){
int number;
int loopCount = 5
int guess;
int correct_answer = 1;
int wrong_answer = 1;
welcomeMessage();
number = randomNumber();
guessInput();
do {
correct_answer = correctAnswer(guess);
wrong_answer = wrongAnswer(guess);
guess = guess_input();
}
while(correct_answer == 1);
if(loopcount = 5){
sorryMessage
}
return 0;
}
I have made minimal changes to your program to get it to compile and run as expected.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int number;
void welcomeMessage(){
printf("Welcome to my new guessing game!\n");
printf("Let's get started!\n");
}
int randomNumber(){
int range;
srand((int)time(NULL));
range = (20 - 1) + 1;
number = rand() % range + 1;
return number;
}
int guessInput(){
int guess;
printf("I'm thinking of a number between 1 and 20.\n");
printf("Care to give it a guess? Be careful! You only get 5 tries!: ");
scanf("%d", &guess);
return guess;
}
int guess_input(){
int guess;
scanf("%d", &guess);
return guess;
}
int wrongAnswer(int guess){
if(guess < number)
{
printf("Try again, your guess is too low: ");
return 1;
}
else if(guess > number)
{
printf("Give it another try, your guess was a bit too high: ");
return 1;
}
return 0;
}
int correctAnswer(int guess){
if(guess == number){
printf("Great job! That time you got it right!\n");
return 1;
} else{
return 0;
}
}
void sorryMessage(int number){
printf("Sorry, the correct number was %d\n",number);
printf("Better luck next time!");
}
int main(){
int number;
int loopCount = 0;
int guess;
int correct_answer;
int wrong_answer;
welcomeMessage();
number = randomNumber();
do
{
loopCount += 1;
if( loopCount > 5)
{
sorryMessage(number);
break;
}
guess = guessInput();
correct_answer = correctAnswer(guess);
wrong_answer = wrongAnswer(guess);
} while( !correct_answer );
return 0;
}

Looping with a while statement inside an if statement

So in my code you have to guess a number if you under or overestimate. How do I get the while loop to loop back to the input so you can input another number?
#include <stdio.h>
int main(int argc, const char * argv[])
{
int number = 45;
printf("Please insert a number");
scanf("%d", &number);
if (number == 45){
printf("good job! You got the number right.");
return 0;
}
else if (number < 45){
while (number < 45 || number > 45){
printf("Your number is too low");
return 1;
}
}
else if (number > 45){
printf("Your number is too high!");
return 1;
}
}
Just put a while (1) around your code and remove the one wrongly placed :
#include <stdio.h>
int main(int argc, const char * argv[])
{
while (1) {
int number = 45;
printf("Please insert a number");
scanf("%d", &number);
if (number == 45){
printf("good job! You got the number right.");
return 0;
}
else if (number < 45){
printf("Your number is too low");
}
else{
printf("Your number is too high!");
}
}
}
return is closing the program. So you don't want to return when the user hasn't guessed your number.
while (1) is the loop which will only exit when return 0 is launched (when the player has won).
Also, your last else if is not needed. Replace it by a else, because it is the only remaining case.
Wrap your code with a loop:
int number = 45;
while (1) {
printf("Please insert a number");
if (scanf("%d", &number) != 1) {
printf("not valid number, try again");
continue;
}
if (number == 45){
printf("good job! You got the number right.");
return 0;
}
if (number < 45){
printf("Your number is too low");
continue;
}
if (number > 45){
printf("Your number is too high!");
continue;
}
}
Note that you should also check the return value of scanf to find out whether the number has been successfully read.

Debugging my c program, number guessing game

I need help debugging this program and I don't know what is wrong. I am using putty and the Vi editor to run my program. This is my code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <malloc.h>
int main(void) {
int playerNumber = 0;
int number = 0;
int playerInput = 0;
int guess = 0;
char input;
char str[6] = {0};
int playerA = 0;
int playerB = 0;
int passA = 3;
int passB = 3;
int i = 0;
int playerTurn = 0;
srand(time(NULL));
playerNumber = 1 + rand() % 2; /* Random number is generated */
printf("\nPlayer %d goes fist\n", playerNumber);
printf("Player Number?\n");
while (playerNumber != playerInput) {
scanf("%d", &playerInput);
if (playerNumber != playerInput) printf("You Have to wait your turn.\nPlayer number?\n");
playerNumber = playerA;
if (playerA = 1) playerB = 2;
else playerB = 1;
srand(time(NULL));
number = 0 + rand() % 100; /* Random number is generated */
printf("Enter Your Guess, 0 - 100 or Pass: "); /* Input your guess */
while(number != guess) {
for(i = 1; i < 1000; i++) {
if (i%2 == 1) playerTurn = playerA;
else PlayerTurn = playerB;
scanf("%s", str);
if (strcmp(str, "pass") == 0) printf("Player Number?\n");
else {
guess = atoi(str);
if(guess < number) /* if the guess is lower, output: the guess is to low */
printf("Your guess was to low.\n Player Number:\n ");
else if(guess > number) /* if the guess is higher, output: the guess is to high */
printf("Your guess was to high.\n Player Number:\n ");
else /* is the guess is equial to the random number: Success!! */
printf("Yes!! you got it!\n");
return 0;
}
}
}
}
}
This is what I get as an error:
project2total.c: In function main':
project2total.c:49: error:PlayerTurn' undeclared (first use in this function)
project2total.c:49: error: (Each undeclared identifier is reported only once
project2total.c:49: error: for each function it appears in.)
C is case-sensitive. In your function, there is no PlayerTurn declared, but you seem to have declared playerTurn. Simply correcting the upper-case P to lower-case will work, assuming that this is actually the variable that you want to refer to. :)

Resources