Ask Dave Taylor
  • Facebook
  • Instagram
  • Linkedin
  • Pinterest
  • Twitter
  • YouTube
  • Home
  • YouTube Videos
  • Top Categories
  • Subscribe via Email
  • Ask A Question
  • Meet Dave
  • Home
  • Linux Shell Script Programming
  • How do I program Rock, Paper, Scissors?

How do I program Rock, Paper, Scissors?

October 10, 2016 / Dave Taylor / Linux Shell Script Programming / 2 Comments

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:

rock, paper and scissors from google android tv advert

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!

Let’s Stay In Touch!

Never miss a single article, review or tutorial here on AskDaveTaylor, sign up for my fun weekly newsletter!
Name: 
Your email address:*
Please enter all required fields
Correct invalid entries
No spam, ever. Promise. Powered by FeedBlitz
Please choose a color:
Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!
bash shell, game programming, games, rock paper scissors, rock paper scissors game, shell games, shell programming, shell script programming

2 comments on “How do I program Rock, Paper, Scissors?”

  1. Jon says:
    October 13, 2016 at 11:05 am

    Oh Dave – you fell for doing someone’s homework assignment. Couldn’t’t resist it eh? 😉

    Reply
    • Dave Taylor says:
      October 13, 2016 at 11:32 am

      Hey, as long as it’s an *interesting* homework assignment, I’m willing to consider it! 🙂

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search

Recent Posts

  • How Do I Find Available Disk Space on my Mac System?
  • How to Identify Plants and Flowers with Google Lens on Android?
  • Windows Security Wants Me to Enable “App & Browser Control”?
  • Can I Change the Theme & Layout on Comcast Web Mail?
  • Easy Way to Install Ubuntu Linux on an M1 Mac System?

On Our YouTube Channel

1More ComfoBuds Mini ANC Earbuds -- REVIEW

Andobil Windshield Sun Shade Umbrella -- DEMO & REVIEW

Categories

  • AdSense, AdWords, and PPC Help (106)
  • Amazon, eBay, and Online Shopping Help, (161)
  • Android Help (197)
  • Apple iPad Help (144)
  • Apple Watch Help (52)
  • Articles, Tutorials, and Reviews (344)
  • Auto Tech Help (11)
  • Business Advice (199)
  • Chrome OS Help (23)
  • Computer & Internet Basics (761)
  • d) None of the Above (165)
  • Facebook Help (383)
  • Google, Chrome & Gmail Help (177)
  • HTML & Web Page Design (245)
  • Instagram Help (47)
  • iPhone & iOS Help (604)
  • iPod & MP3 Player Help (173)
  • Kindle & Nook Help (92)
  • LinkedIn Help (85)
  • Linux Help (164)
  • Linux Shell Script Programming (86)
  • Mac & MacOS Help (891)
  • Most Popular (16)
  • Outlook & Office 365 Help (24)
  • PayPal Help (69)
  • Pinterest Help (53)
  • Reddit Help (17)
  • SEO & Marketing (81)
  • Spam, Scams & Security (92)
  • Trade Show News & Updates (23)
  • Twitter Help (217)
  • Video Game Tips (66)
  • Web Site Traffic Tips (62)
  • Windows PC Help (915)
  • Wordpress Help (204)
  • Writing and Publishing (72)
  • YouTube Help (46)
  • YouTube Video Reviews (159)
  • Zoom, Skype & Video Chat Help (56)

Archives

Social Connections:

Ask Dave Taylor


Follow Me on Pinterest
Follow me on Twitter
Follow me on LinkedIn
Follow me on Instagram


AskDaveTaylor on Facebook



microsoft insider mvp


This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to our terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. Our lawyer says "Thanks for your cooperation."
© 2022 by Dave Taylor. "Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.
Privacy Policy - Terms and Conditions - Accessibility Policy