Arguably, the greatest game ever invented is Rock-Paper-Scissors. If governments would only use this game to manage disputes, the world would be a better place. Thanks to the TV show, "Big Bang Theory", it may be that even the greatest game can be improved:
We are going to write Rock-Paper-Scissors-Lizard-Spock in this project.
To do this, we need to associate numbers with each object type. Note that the objects are ordered for a reason; we will use this ordering:
Object |
Number |
ROCK |
1 |
SPOCK |
2 |
PAPER |
3 |
LIZARD |
4 |
SCISSORS |
5 |
Here is a table that shows all of the possible RPSLS outcomes. The numbers in parentheses are (computer - human + 5) % 5, where % is the remainder function.
Cells are colored by who wins (ORANGE for human, BLUE for computer):
COMPUTER | ||||||
---|---|---|---|---|---|---|
HUMAN | ROCK=1 |
SPOCK=2 |
PAPER=3 |
LIZARD=4 |
SCISSORS=5 |
|
ROCK=1 |
0 |
1 |
2 |
3 |
4 |
|
SPOCK=2 |
4 |
0 |
1 |
2 |
3 |
|
PAPER=3 |
3 |
4 |
0 |
1 |
2 |
|
LIZARD=4 |
2 |
3 |
4 |
0 |
1 |
|
SCISSORS=5 |
1 |
2 |
3 |
4 |
0 |
Notice how we can see who wins by using these numbers. 1 or 2 means computer wins. 3 or 4 means human wins. 0 is a tie.
To write this program, we will need functions that:
Here is some sample output. The computer is generating choices randomly, so your output may be different, but it should tell you the following:
> (rpsls 'scissors) (Human went scissors! Computer went lizard! Human wins!) > (rpsls 'paper) (Human went paper! Computer went lizard! Computer wins!) > (rpsls 'lizard) (Human went lizard! Computer went scissors! Computer wins!) > (rpsls 'spock) (Human went spock! Computer went scissors! Human wins!) > (rpsls 'rock) (Both players went rock! Tie game!)
Below is code, with directions, that you can directly copy into the Scheme editor. Have fun!
(load "/Applications/PLT/scheme/simply.scm") ; Write name-to-number that takes an RPSLS object as its input and returns the ; number that corresponds to the object. Use the following correspondences: ; ROCK: 1, SPOCK: 2, PAPER: 3, LIZARD: 4, SCISSORS: 5 (define (name-to-number thing) (cond ... ; Write number-to-name that takes a number as an input and returns the object ; to which the number refers. Use the same correspondences as above (define (number-to-name num) (cond ... ; Write computer-choice that returns a number between 1 and 5. Use the ; procedure random, which takes one input, n, and returns a number between ; 0 and n-1. (define (computer-choice) ; No input! YOUR_CODE_HERE) ; Write rpsls that takes an object as its input. It then generates a random ; selection for the computer, determines who wins, and returns a sentence (define (rpsls thing) (let ((player (name-to-number thing)) (computer (computer-choice))) ; Clearly if the two inputs are the same it is a tie. ; What's interesting is that if the objects are ordered ; rock-spock-paper-lizard-scissors, then modular arithmetic ; can be used to determine the winner. The difference of ; (- computer player) modulo 5 tells us all we need to know. ; 5 is being added to (- computer player) because remainder ; has two positive numbers in its domain and (- computer player) ; would be negative 2/5 of the time. ; ; Here's the magical formula (the % sign means modulo) ; (computer - player + 5) % 5 ; If this is equal to zero, we have a tie ; If this is less than three, then the computer wins ; If this is greater than two, then the human wins ; YOUR_CODE_HERE. USE A cond, NOT A BUNCH OF if STATEMENTS. ))