Question

C only (not C++)
Making Decision & Looping (15 program. Display the winning message for the winner. program that simulates a game. There are two players (called Playerl and Player2). At the start of player chooses a number from 1 to 10 (cannot be the same ®) Then, the program will generate a number from 1 to 10. The game ends when either Playerl or player2 hits the number generated by the Example out Player 1 6 Point 2: 5 Player 2: 3 Point 3: 3 The game starts... The winner is player 2 Point 1: 4

0 0
Add a comment Improve this question Transcribed image text
Answer #1

the code contains an infinite while loop that contains the statements to generate a random number in each iteration and this number is compared with the numbers of player 1 and 2 . And if any of those match then the respective player wins and the loop terminates.

//code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
   int p1,p2;
   int randnum, count=0;
   time_t t;
     /* Intializes random number generator */
   srand((unsigned) time(&t));
printf("Player 1: ");
scanf("%d",&p1);
printf("Player 2: ");
scanf("%d", &p2);

   while(1)
   {
        count++;
      randnum = (rand() % 10)+1;   //random number from 1-10
      printf("\nPoint %d: %d", count, randnum);
      if(randnum == p1)
      {
         printf("\nThe winner is Player 1");
         break;
      }
      else if(randnum == p2)
      {
         printf("\nThe winner is Player 2");
         break;
      }
    
   }

   return(0);
}

//output:

Add a comment
Know the answer?
Add Answer to:
C only (not C++) Write a C program that simulates a game. There are two players...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ program This program involves writing a VERY simplified version of the card game War. You...

    C++ program This program involves writing a VERY simplified version of the card game War. You may know this game or not but my rules are these 1. Split the deck between player1 and player2. Only the face values matter (2-14) and not the suits 2. Each player puts a card down on the table. The higher face value wins that hand. If the card values match, you will simply indicate tie and neither player wins.The original rules would require...

  • hi can you please solve this python problem for me?? Three pets live in a happy...

    hi can you please solve this python problem for me?? Three pets live in a happy house: dog, cat and a mouse. The dog often chases the cat, the cat likes to chase the mouse, however the mighty dog runs away when it sees the mouse. Develop a game which can be played by two players. The program asks the two players (player1 and player2) to choose either 1 for dog, 2 for cat, or 3 for mouse. The rules...

  • Write a C++ program that simulates a lottery game. Your program should use functions and arrays....

    Write a C++ program that simulates a lottery game. Your program should use functions and arrays. Define two global constants: - ARRAY_SIZE that stores the number of drawn numbers (for example 5) -MAX_RANGE that stores the highest value of the numbers ( for example 9 ) The program will use an array of five integers named lottery, and should generate a random number in the range of 0 through 9 for each element of the array. The user should enter...

  • Introduction Write a MIPS program to allow a user to play a simple variation of the...

    Introduction Write a MIPS program to allow a user to play a simple variation of the game BINGO. Your program should display the following game board and the player wants to choose a location to mark BINGO A position can be marked by entering its column letter (eit B', 'I', 'N' 'G', or CO and row number (either 1, 2, 3, 4, or 5). An 'X' should mark the positions already marked. An underscore, e C 1 should represent unmarked...

  • C# Code: Write the code that simulates the gambling game of craps. To play the game,...

    C# Code: Write the code that simulates the gambling game of craps. To play the game, a player rolls a pair of dice (2 die). After the dice come to rest, the sum of the faces of the 2 die is calculated. If the sum is 7 or 11 on the first throw, the player wins and the game is over. If the sum is 2, 3, or 12 on the first throw, the player loses and the game is...

  • I want to have a C++ program that simulates Black jack game with 2 players using vectors along wi...

    I want to have a C++ program that simulates Black jack game with 2 players using vectors along with the betting amount. The out put must clearly show the suit and value of cards on the console which was dealt to each player and dealer. and then calculate to check who is the winner. repeat the dealing 5 times. Code should be clearly sperated into header files, .cpp files and the main.cpp that calls the functions of the respective class....

  • Java Listed below is code to play a guessing game. In the game, two players attempt...

    Java Listed below is code to play a guessing game. In the game, two players attempt to guess a number. Your task is to extend the program with objects that represent either a human player or a computer player. boolean checkForWin (int guess, int answer) { System.out.println("You guessed" + guess +"."); if (answer == guess) { System.out.println( "You're right! You win!") ; return true; } else if (answer < guess) System.out.println ("Your guess is too high.") ; else System.out.println ("Your...

  • IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors...

    IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet....

  • (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet. The...

  • . (15 pts) Consider milti- r a multi-player zame over the Internet where N players attempt to shoot a monster rrain) in a random fashion as controlled by a game server. The agent program at each...

    . (15 pts) Consider milti- r a multi-player zame over the Internet where N players attempt to shoot a monster rrain) in a random fashion as controlled by a game server. The agent program at each player site maintains a variable that indlicates who shot the monster. The variable at ith player site, denoted as tes," is set to j if lhe İth player believes that jth player shot the monster. Here, the display at player i shows a scoreboard...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT