I want to write a simple rock-paper-scissors game as a shell script for my Linux system. Can you step me through the process?
Well, you picked an extraordinarily complicated game with many variations and the programming task is going to be quite tricky. Just kidding! Rock Paper Scissors, or “RPS” as it’s known, is pretty darn easy to simulate because there aren’t really many variants or possible outcomes.
If you’ve never played it before, it’s a one-v-one game where each person secretly chooses one of three possible options (rock, paper or, you guessed it, scissors). They reveal their choices simultaneously and then there are rules about what beats what. For example, scissors beat paper because “scissors cut paper”, and rock beats scissors because “rock beats scissors”. If both players pick the same option it’s a tie and the game proceeds.
While you can play it as a one-off, it’s also generally played as a best of three to even things out slightly, though if everything’s completely random then you’ll win 33.33% of the time: for any given choice, there’s a 1/3 chance that you’ll have a tie, where both pick the same thing, a 1/3 chance that you’ll win, and a 1/3 chance that you’ll lose.
Except in the real game there’s psychology involved. In fact, according to the World Rock, Paper, Scissors Society (yes, it’s a real thing), overall, rock is chosen 35.4%, paper 35% of the time and scissors only 29.6% of the time. Got it? Which isn’t to say that the three of ’em can’t be best of friends, as shown in the endearing Google Android TV spot:
But let’s stick with the completely random choice for a software emulator. The easy way to choose a random number between 1-3 in a Linux or even Mac shell script is to use the variable $RANDOM thusly:
compchoice=$(( ($RANDOM % 3) + 1 ))
The “%” is a modulus function and causes the random integer to be divided by 3, resulting in a 0..2 value. Add one and you’ve got the 1…3 value. Easy enough.
With a simple shell array, you can add the name of the choice (and remember, arrays start at index 0):
declare -a RPS; RPS=(nothing rock paper scissors)
Then the choice itself is specified simply as:
choicename=${RPS[$compchoice]}
Those three lines are good enough for a tiny script where the computer can randomly choose between rock, paper and scissors:
declare -a RPS; RPS=(nothing rock paper scissors) compchoice=$(( ($RANDOM % 3) + 1 )) echo "The computer chose ${RPS[$compchoice]}" exit 0
But of course it’s considerably more fun to have the computer decide who won, and that’s done by prompting the user for their choice before the computer decides. Easily done with numeric values, and that then also matches the computer’s numeric choice methodology too:
echo -n "Please choose (1 = rock / 2 = paper / 3 = scissors): "
read choice
That’s the vast majority of the work. Now it’s just time to compare, hopefully with some informative output messages. This is best done in a function, so that’s what I’ll suggest. Here’s one way to do it that’s a chain of if-then-elif tests:
results() { # output results of the game, increment wins if appropriate if [ $choice = $compchoice ] ; then echo "You both chose $choicename. TIED!" # rock beats scissors. paper beats rock. scissors beat paper. # OR: 1 beats 3, 2 beats 1, and 3 beats 2. elif [ $choice -eq 1 -a $compchoice -eq 3 ] ; then echo "Your rock beats the computer's scissors! Huzzah!!" elif [ $choice -eq 2 -a $compchoice -eq 1 ] ; then echo "Your paper beats the computer's rock! Hurray!" elif [ $choice -eq 3 -a $compchoice -eq 2 ] ; then echo "Your scissors cut - and beat - the computer's paper! YAY!" elif [ $choice -eq 3 -a $compchoice -eq 1 ] ; then echo "The computer's rock beats your scissors! Boo." elif [ $choice -eq 1 -a $compchoice -eq 2 ] ; then echo "The computer's paper beats your rock! Ptoi!" elif [ $choice -eq 2 -a $compchoice -eq 3 ] ; then echo "The computer's scissors cut - and beat - your paper! Bummer." fi }
Straightforward, just a lot of typing. But really, that’s 95% of the program. All you need is a looping mechanism so that you’re “stuck” in the program until you want to finish, and a way to keep track of games played and games won. I’ll let you add those yourself. For my version of the script, a quick run reveals that scissors isn’t a bad strategy:
Please choose (1 = rock / 2 = paper / 3 = scissors): 3 Your scissors cut - and beat - the computer's paper! YAY!
Playing an all-scissors strategy produced a 50% win rate, interestingly (6 games out of 12). What about paper?
Please choose (1 = rock / 2 = paper / 3 = scissors): 2 The computer's scissors cut - and beat - your paper! Bummer.
In fact, playing all paper only won 4 of 14 games on a trial, and rock, the most popular choice? That produces a win rate of 3 out of 14, worse than paper!
So there you have it. Scientific? Not really. But, uh, rock, paper, scissors. Come on. 🙂
Now, for extra credit you can add “lizard” and “Spock” to your game. Google it!
Oh Dave – you fell for doing someone’s homework assignment. Couldn’t’t resist it eh? 😉
Hey, as long as it’s an *interesting* homework assignment, I’m willing to consider it! 🙂