CSCI 131 - Techniques in Programming, Spring 2014

    Home | | Schedule | | Assignments | | Lectures | | Resources

    Project Two Specifications
    Rock, Scissors, Paper -- A Simulation

    Introduction. This project focuses on conditional statements (if...else; switch) in the context of simulating a single play of the children's game, Rock, Scissors, Paper.

    Background. In the wild, this is a two-person game played as follows. The two adversaries stand facing each other. At an agreed-upon moment each throws forward a single hand/fist signaling one of the three options: Rock (a clenched fist), Scissors (index and middle finger extended, the others folded under the palm), Paper (all fingers extended straight out). If the two options signaled are the same, a tie is declared. If Rock is shown by one adversary and Scissors by the other, Rock wins (Rock smashes Scissors). If Scissors and Paper are shown, Scissors wins (Scissors cut Paper). If Paper and Rock are shown, Paper wins (Paper covers Rock).

    Both players throw same option. It's a tie.
    Player 1 throws Paper.
    Player 2 throws a Rock. Player 1 wins.
    Player 2 throws Scissors. Player 2 wins.
    Player 1 throws a Rock.
    Player 2 throws Paper. Player 2 wins.
    Player 2 throws Scissors. Player 1 wins.
    Player 1 throws Scissors.
    Player 2 throws Paper. Player 1 wins.
    Player 2 throws a Rock. Player 2 wins.

    In our simulation, we will input the player's throws from a file, and we announce who wins. The player's options will be read as 'R' or 'r' (for Rock), 'S' or 's' (for Scissors), or 'P' or 'p' (for Paper).

    The specifications:

    • Provide appropriate #include directives
    • Declare the necessary variables. You'll need character variables into which to read 'R' (or 'r'), 'S' (or 's'), or 'P' (or 'p') for each of Player 1 and Player 2. In this project you will read all input from an external file, and so you will need a name for the input stream.
    • Your program should open proj2.dat for input. Use an if statement to be sure that the file opens correctly. In the event that it fails to open properly, write an appropriate message to the monitor and exit (return control to the operating system).
    • Read the input, one character for each player. There will be exactly 2 characters of input in proj2.dat. They will be the first two characters in the first line of the file. That is, the two letters of input will not be preceded by blank characters.
    • Convert any lowercase ('p', 'r' or 's') to the corresponding uppercase character. (You might use an if statement).
    • Use an if statement to check to make sure the user entered an appropriate character. If they did not, write an appropriate message to the monitor and exit.
    • Write a nested if statement to process the input. The "outer" if statement should test for each possible input from the first player. The code for each clause must contain statement(s) to provide output to the output file and an "inner" if statement to process each possible input from the second player. The "inner" if statement must test for each possible input from the second player. The code for each clause must contain statement(s) to provide output to the output file.

      If you prefer, you may write the conditional as an if...else, if...else if...else statement, switch statements, or a combination using a combination of conditions and logical operators (&&, || or !). Your logic must be correct (i.e. the conditions must be correct) or you will lose points.

    The output: The output file (which must be named proj2.out) must contain two non-blank lines of output for a single play of the game. There must be a row of dashes and a blank line at the beginning and end of the file.

    If the input file reads "RS", the output file must appear as follows:
    
    --------------------------------------------------------------
    Player 1 plays Rock and Player 2 plays Scissors.
    Player 1 wins! Rock smashes Scissors.
    --------------------------------------------------------------
    
    If the input file reads "RP", the output must appear as follows:
    
    --------------------------------------------------------------
    Player 1 plays Rock and Player 2 plays Paper.
    Player 2 wins! Paper covers Rock.
    --------------------------------------------------------------
    
    If the input file reads "sS", the output must appear as follows:
    
    --------------------------------------------------------------
    Player 1 plays Scissors and Player 2 plays Scissors.
    It's a tie!  Neither wins; neither loses.
    --------------------------------------------------------------
    
    If the input file reads "sp", the output file must read as follows:
    
    --------------------------------------------------------------
    Player 1 plays Scissors and Player 2 plays Paper.
    Player 1 wins! Scissors cuts Paper.
    --------------------------------------------------------------
    

    Program testing: The directory ~csci131/PROJECTS/PROJ2 contains, among other things, files named test2, proj2a.dat, proj2b.dat, proj2c.dat, proj2d.dat, model2.out and model_monitor2.out. Copy these files into a directory which also contains your proj2.cc.

    First test your code by making your own data file, named proj2.dat. You can make the file yourself by opening a file in emacs and typing two characters (e.g. rs) at the very beginning of the file. Make sure you don't put any spaces or blank lines before the characters! You can look at proj2a.dat for an example. If you prefer, you can copy one of the data files copied from the csci131 directory:

      cp proj2a.dat proj2.dat

    You can then run your program by typing:

      ./proj2

    You can view the output of the program by typing:

      more proj2.out

    or by opening proj2.out in an emacs window.

    Change the characters in proj2.dat to test a variety of possible inputs.

    Once you are convinced your program works as it should, you can test it by typing ./test2. The test2 script will compile your program and run it five times, once using proj2a.dat once using proj2b.dat, once using proj2c.dat, once using proj2d.dat and finally after deleting the proj2.dat file and running it with no input file available. When you run test2, there will be some text printed to your terminal window. This should look like the text given in the file named model_monitor2.out that you copied from the csci131 directory. The file output from the runs will be written into a file named output2. This file will appear in the same directory into which you copied test2. The perfect output, well, the output produced by my own proj2.cc can be seen in the file, model2.out that you copied from csci131.

    Please view the grading header for this project to see how points will be allocated.

    To submit your finished project:
    1. Submit your program file electronically in the directory which contains your (presumably thoroughly tested) proj2.cc. (The submit program will not accept a program that does not compile).

    2. Print out the grading header, put your name on it and staple it to the front of the hard copy of the file you submitted electronically.

    3. Hand in a hard copy of the file you submitted electronically (along with the grading header). Hand this to your instructor in class on the project's due date.

    Note: The submit program compiles your program before accepting it. If your program does not compile successfully or if something else is wrong with your submission, the submit program will report to you that the submission failed and it will give you a (sometimes cryptic) reason for the failure.

    You may submit version after version of proj2.cc. Each successful submission will replace any previously submitted version. That is, only your last successfully submitted version will be visible to and will be graded by your instructor or her/his grader.

    Get started early and have fun!

    Honor code: Please review the collaboration policy on the main course webpage. Also refer to the math and CS department honor code policy.


    Home | | Schedule | | Assignments | | Lectures | | Resources


    Constance Royden--croyden@cs.holycross.edu
    Computer Science 131
    Last Modified: February 13, 2014
    Page Expires: September 2, 2014