Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

3.1.2 Rochambeau The famous game of Rock-Paper-Scissors, invented in China a long long time ago is sometimes known in the US as Roshambo or Rochambeau.

3.1.2 Rochambeau

The famous game of Rock-Paper-Scissors, invented in China a long long time ago is sometimes known in the US as Roshambo or Rochambeau. It apparently refers to the Comte de Rochambeau, a French nobleman who fought against the British during the Revolutionary War.

As you surely know, the game of Rochambeau involves two players, who simultaneously play one of the three possible shapes. The winner is the player whose shape beats the other one: Scissors beats Paper, Paper beats Rock, Rock beats Scissors. If both players play the same shape, it is a tie!

Write program rochambeau.c that allows a human player to play Rochambeau against the computer, as illustrated in the following example.

$ ./rochambeau [Rr]ock, [Pp]aper or [Ss]cissors? (or [Qq]uit) r Rock versus Scissors... You win! [Rr]ock, [Pp]aper or [Ss]cissors? (or [Qq]uit) P Paper versus Paper... Tie! [Rr]ock, [Pp]aper or [Ss]cissors? (or [Qq]uit) s Scissors versus Rock... You lose! [Rr]ock, [Pp]aper or [Ss]cissors? (or [Qq]uit) a Invalid move, try again! [Rr]ock, [Pp]aper or [Ss]cissors? (or [Qq]uit) q $ 

Instead of being represented by characters, the possible moves have to be coded by integers (Rock is 0, Paper is 1, and Scissors is 2). It will make the logic to determine the winner of a game much easier to program (especially if you notice the numbering order). Note that you should use macro constants to represent the moves.

Your program should be composed of four functions:

A function to get the move from the human player after displaying the prompt. This function should also echo what the player played. It should not return until a valid moved has been entered. The prototype for this function should be int get_player_move(void). The returned integer value should represent which move was played.

Note that if the player wanted to quit, this function could return a special value to the caller.

A function to get the move from the computer and echo it before return it. In order to get a random value for the computers move, youll have to include the headers stdlib.h and time.h, and adapt the following code:

/* Get a random number between 0 and 9 into variable "r" */ r = rand() % 10; 

Note that it is not the best way of generating a random number (the distribution you get from using the modulo is not accurate), but it should be perfectly enough for this assignment.

The prototype for this function should be int get_cpu_move(void).

A function to print the winner, according to the players move and the computers move. The prototype for this function should be void print_winner(int player_move, int cpu_move).

The main function is the brain of the program; it repetitively calls the three functions introduced above until the player decides to quit; it starts by getting the move from the human player, then gets the move from the computer, print the winner and starts again.

In the main function, youll also have to initialize the random number generator (otherwise it would always generate the same sequence of random numbers, which is not very fun for a game). Put the following two lines at the beginning of the function, before the main game loop.

/* Initialize the random number generator */ srand(time(0)); 

Include comments showing how it works if possible

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions