In this homework assignment you will simulate a game of Rock, Paper, Scissors. One player will be the computer, choosing a play at random, and the other player will be a human user of the program. Your program will allow for multiple rounds of the game, keep track of how many rounds each of the players has won, and determine the overall winner by who won the most rounds.
In case you are not familiar with the game of Rock, Paper, Scissors, you can read more about it on Wikipedia.
In this assignment we will use a random number generator to simulate the computer choosing which option to play. (Note: In computer science, the generation of truly random numbers is a significant challenge, the exploration of which is beyond the scope of this class. Most of the time, we generate what are actually pseudo-random numbers, which are predictable sequences created by a mathematical formula. For a more detailed discussion, head over to random.org. )
First, we will need to include additional libraries, <time.h> and <stdlib.h>. Do this at the top of your program in the same location you include <stdio.h>.
Next, we wil do something called “seeding” the randomizer. This is a process that helps the numbers seem more random by starting the mathematical sequence at different points. The “seed” is the starting point. To seed the randomizer, include the following code once at the beginning of the main program (make sure it’s not in any loop), before generating any random numbers.
srand((unsigned) time(NULL));
Finally, we are able to generate pseudo-random numbers using the function rand(). This function by itself produces a number between 0 and a maximum value specified by the system (called RAND_MAX). You will need to determine how to use this function to generate a random number between 1 and 3 (for help, see your textbook pages 172-174). Call this function each time you want a new number.
Note that it’s important to save your randomly generated number to a variable if you would like to use it again later on. To consider why, compare these two code snippets and determine the problem.
//version 1
rand();
if(rand() == 1)
// do something with the random number
else if (rand() ==2)
// do something else
//version 2
int num = rand();
if(num ==1)
// do something with the random number
else if(num ==2)
// do something else
Your program should implement the following behaviors:
When you present users with a choice of several options, print them with numbers and ask them to type the number corresponding to the option they want. We will explore alternative ways to collect input from users later in the semester.
Your program must check the validity of all inputs; a number of rounds that is 0 or negative should be rejected, as should any attempt to select an option that does not exist. When a user provides an invalid input, print a message to report that the input was invalid (include an explanation of why) and ask them to try again. We haven’t yet looked at how we can read non-numeric data, so you may assume users will always type a number when you ask them to. Just don’t assume the number will be acceptable without checking it first.
You will submit your work for this homework assignment on gradescope.
Please upload your implementation in a file named rockpaperscissors.c, along with a testing transcript.
You can create a testing transcript by running:
$ script tests.txt
The command above will record the commands you run and any outputs to a file named tests.txt.
Run the program as many times as you need to thoroughly test your implementation, then run exit to end the transcript.
Include tests.txt with your implementation when you submit your work to gradescope.
You should make sure your testing covers at least the following cases:
It is in your interest to be thorough with your testing. Your tests must cover these cases to receive credit for the testing portion of your grade, but testing your code will help you find bugs that might otherwise have hurt your score in other parts of the assignment.
Your assignment will be graded on a scale of 0–100 points based on the following criteria:
.c file which includes the author (which should just be you), and any resources you used or help you received including tutors, mentors, online and other resources (you do not need to include the instructor, the course website, links posted on this page, or your textbook). This serves as an academic honesty statement. An absence of such a statement signals that you did not receive any help or use any resources beyond our class materials. (0 points)There will be potential deductions for any code quality violations. For now, there are six requirements for quality code:
if–else block that explains what the purpose of that code is. The comment should not simply restate the code; it should add information for a human who reads the code and is confused.clang -o rockpaperscissors.o rockpaperscissors.c