Thursday, April 18, 2019

Rock, Paper, Scissors

     This week we will begin a very short series in programming and let you get started with your new Raspberry Pi, that is if you found last week’s article of any interest. First of all if you do not have a Raspberry Pi, don’t worry, you can skip the first steps and just use any computer you have available for the actual programming part.
     If you want a very detailed, step-by-step process for setting up your Raspberry Pi, head over to http://projects.raspberrypi.org  and look for setting up your Raspberry Pi. There are all kinds of projects on that site to keep you busy for a very long time, but I want to make sure you are able to get started without a computer. This will require a MicroSD Card of at least 6GB in size. A MicroSD card is the little card you can get just about anywhere that is usually used to store photos and video on your phone or camera. You will also need access to a computer to download an image to the SD card. If you don’t have a computer, or access to one, you can order an SD card with Raspbian pre-installed from Amazon. Hopefully you have a TV or computer monitor with an HDMI port, an HDMI cable, and a MicroUSB phone charger. Most TV’s with an HDMI port also have a USB port that actually supplies enough power to run the Raspberry Pi, so you should not need a power supply.
     You can get Raspbian, or any other of several different operating systems for the Pi by downloading a utility called NOOBS from http://www.raspberrypi.org/downloads you will then want to format your SD card using an SD card formatter that you download from https://www.sdcard.org/downloads/formatter_4/index.html. Then unzip the NOOBS archive to your SD card. Next, you will need to connect everything to your Raspberry Pi and turn it on. The photo shows where to connect everything. Connect the power cord last, and if things go well, you will see a red light in the top left corner of your Pi. As it starts up, also called booting, you will see raspberries appear across the top of the screen. The steps are fairly straightforward following prompts on the screen and after a few minutes of software downloads, the Raspbian desktop will appear. It looks very similar to a Windows computer desktop except that the menu is at the top of the screen.
If you made it this far, congratulations, you have a working computer. If you plan to use a different computer for the rest of the steps, you will need to install python 3 on your computer following the instructions at http://www.python.org. The Raspbian Operating System has python installed by default so you are ready to begin learning to program as soon as it successfully boots. You can also use an online version of python for learning at http://trinket.io, which allows you to write and test python code from the internet without installing anything.
     For this week’s project we are going to write a simple game of Rock, Paper Scissors and play against your computer. The rules are simple, just like playing the game against another person. Both you and the computer pick rock, paper, or scissors and the winner is decided by the following rules. Rock breaks the scissors, so rock wins. Paper covers the rock, so paper wins. Scissors cut the paper, so scissors wins.  It might sound hard to get a computer to play a simple game, but it isn’t that difficult, and I’m sure you can do it.
     First you will need to teach the computer to pick rock, paper, or scissors randomly. For this you will need to get a random value. Python has a library called random that does just what we need, but to use a library you have to import it. Only type the stuff between the quotes, not the quotes themselves. Using a single line of computer code: “from random import randint”.
We will let the player go first, which might not seem fair, but I promise the computer does not know what you pick when it picks, unless of course you allow it to cheat. Letting the computer always pick the winning move is a more advanced step you can add. To let the player pick we have to let the computer read what we type. This is done using the following line of code: “player=input(‘rock (r), paper (p), or scissors (s)?’)
     Next just to make sure the computer read it, we will print what the player types: “print(player, ‘ vs ’)”.
     Now for the computer’s turn, it only understands numbers for now so it will pick one, two or three: “chosen = randint(1,3)”. 
     We teach it what the numbers mean using a choice function called “if”; for if to work, python depends on spaces so this will be a sets of two lines, the second one starts with two spaces, “if chosen == 1:” and “  computer = ‘r’”. We then only want to check to see if the computer picked two if it did not pick one, so we use a command called “else if” which gets shortened to elif.  “elif chosen == 2:” and “  computer = ‘p’”. Finally we know that if it was not one or two it must be three so we just use “else” in the next two lines: “else:” and “  computer = ‘s’”.
     Now we can print what the computer chose: “print(computer)”. You can now run your code and decided for yourself who won. It takes 14 more lines of code, using nothing more than you already learned to let the computer tell you who won. To make it a little easier to follow I will show you the whole program.  You can download the sample code from our website at https://www.thelickingnews.com/p/code-samples.html.
from random import randint
player = input(‘rock (r), paper (p) or scissors (s)?’)
print (player, ‘vs’)
chosen = randint(1,3)
if chosen == 1:
  computer = ‘r’
elif chosen == 2:
  computer=’p’
else:
  computer=’s’
print (computer)
if player == computer:
  print(‘DRAW!’)
elif player == ‘r’ and computer == ‘s’:
  print(‘Player wins!’)
elif player == ‘r’ and computer == ‘p’:
  print(‘Computer wins!’)
elif player == ‘p’ and computer == ‘r’:
  print(‘Player wins!’)
elif player == ‘p’ and computer == ‘s’:
  print(‘Computer wins!’)
elif player == ‘s’ and computer == ‘r’:
  print(‘Computer wins!’)
elif player == ‘s’ and computer == ‘p’:
  print(‘Player wins!’)

No comments: