Homework 2: Rock, Paper, Scissors

Assigned
  • September 11, 2024
Due
  • September 18, 2024 10:30pm
Collaboration
    Regular policies for collaboration apply to this homework assignment. Make sure you review and understand the policies on the syllabus before you discuss your work on this assignment with anyone else.

Overview

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.

Random Numbers

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

Requirements

Your program should implement the following behaviors:

  1. Ask the user how many rounds of Rock, Paper, Scissors they’d like to play
  2. At the beginning of each round, print the current number of wins for each player, and prompt the user for Rock, Paper, or Scissors for the current round. Use integers to stand in for each of the options, and be sure to communicate to the user what options they have for input.
  3. If an invalid option is selected by the user, do not count this as one of the rounds, allow the user to choose again. Conversely, if the result of the round is a tie (both players choose the same option), this is a valid round.
  4. After each round, print a summary of what was chosen by each player and who won that round. Please print “Rock” rather than the number 1, for example.
  5. After all rounds are completed, print who is the overall winner.
  6. Throughout, make use of spacing and new line characters to make the terminal output easy to follow.

Handling Inputs

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.

What to Submit

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:

  • Valid inputs for number of rounds and user selections (there are quite a few combinations of valid inputs, therefore there should be several tests within this category)
  • Invalid inputs for number of rounds (negative numbers, zero)
  • Invalid inputs for user selections (you determine these based on your program design)

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.

Grading

Your assignment will be graded on a scale of 0–100 points based on the following criteria:

  • A statement at the top of your .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)
  • Implementation of a game of Rock, Paper, Scissors with the number of rounds specified by the user. (10 points)
  • Correct generation and use of a “random” play by the computer (15 points)
  • Correct determination of the winner of each game, along with record-keeping of how many games won total (25 points)
  • Testing transcript that covers at least the cases listed above (25 points)
  • Helpful prompts and feedback for users, especially when they provide invalid inputs (25 points)

There will be potential deductions for any code quality violations. For now, there are six requirements for quality code:

  1. Indent the body of all loops and conditionals one level deeper than the code just outside the loop. Be consistent with your indentation style.
  2. Use curly braces for every loop or conditional body, regardless of the number of lines.
  3. Include comments that explain what your code does. There should be at least one comment by the start of each loop and ifelse 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.
  4. Give variables descriptive names that help a reader understand what they will be used for.
  5. Do not read from or display the value of any variable without first initializing it.
  6. Your code must compile with no errors and no warnings with the following command
    clang -o rockpaperscissors.o rockpaperscissors.c