7/08/2012

Python on the Raspberry Pi: Roshambo

Python on the Raspberry Pi: Roshambo


While I was doing Python on the Rasberry Pi: GPIO, I came up with an idea. I wanted to use six GPIO pins for output, and three GPIO pins for input from a button. I have not yet wired this up, but I spent part of a day writing some Python code that would be the game to play with these three buttons, Roshambo.

Git the Code

In order to have access to all of the Python examples in this post, and more, you will need to have git installed, and clone my PythonOnTheRaspberryPi repository.

  • If you don't have the program git installed, then install it with:
    • sudo apt-get install git
  • Clone my PythonOnTheRaspberryPi repository:
    • git clone https://github.com/wrightrocket/PythonOnTheRaspberryPi.git
    • cd PythonOnTheRaspberryPi

Roshambo Interactive

Here's a short interactive demonstrating a dictionary named gameD storing and retrieving key and value pairs. The random module's choice function is demonstrated to show how a random selection of a certain key could be done, and to use it's seed function for better randomization. The function didPlayerWin(player, program) is pasted into the session, and it is imported from the rps.py module with the statement: from rps import didPlayerWin. This function shows how the logic works to determine which weapon won in a meeting between the player and the program.

pi@raspberrypi:~/PythonOnTheRaspberryPi$ python
Python 2.6.6 (r266:84292, Dec 27 2010, 21:57:32) 
[GCC 4.4.5 20100902 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> gameD = {}
>>> gameD['r']='Rock'
>>> gameD['p']='Paper'
>>> gameD['s']='Scissors'
>>> print gameD
{'p': 'Paper', 's': 'Scissors', 'r': 'Rock'}
>>> gameD['s']
'Scissors'
>>> gameD.keys()
['p', 's', 'r']
>>> gameD.values()
['Paper', 'Scissors', 'Rock']
>>> gameD.items()
[('p', 'Paper'), ('s', 'Scissors'), ('r', 'Rock')]
>>> import random
>>> random.seed()
>>> # seed() is used for better randomization
... 
>>> random.choice(gameD.keys())
'p'
>>> random.choice(gameD.keys())
'r'
>>> random.choice(gameD.keys())
's'
>>> random.choice(gameD.keys())
'p'
>>> random.choice(gameD.keys())
'p'
>>> def didPlayerWin(player, program):
...     ''' determine if player beats program '''
...     if player == 'r':
...         if program == 'r':
...             return 'tie: rock meets rock'
...         elif program == 's':
...             return 'win: rock crushes scissors'
...         else:
...             return 'lose: paper covers rock'
...     elif player == 's':
...         if program == 'r':
...             return 'lose: rock crushes scissors'
...         elif program == 's':
...             return 'tie: scissors meets scissors'
...         else:
...             return 'win: scissors cut paper'
...     elif player == 'p':
...         if program == 'r':
...             return 'win: paper covers rock'
...         elif program == 's':
...             return 'lose: scissors cut paper'
...         else:
...             return 'tie: paper meets paper'
...     else:
...         ''' catch all for testing '''
...         return 'error: for unknown reason'
... 
>>> mypick = 'r'
>>> programChoice = random.choice(gameD.keys())
>>> didPlayerWin(mypick, programChoice)
'lose: paper covers rock'
>>> programChoice
'p'
>>> gameD[programChoice]
'Paper'
>>> del didPlayerWin # deletes the function or name from namespace
>>> from rps import didPlayerWin # you must have downloaded or created rps.py 
>>> mypick = 's'
>>> programChoice = random.choice(gameD.keys())
>>> programChoice
's'
>>> didPlayerWin(mypick, programChoice)
'tie: scissors meets scissors'

Rock, Paper, Scissors Game

As the game might be better known as Rock, Paper, Scissors, there are two modules available at my github repository that implement this game: rps.py and roshambo.py

rps.py

Very straightforward example of implementing the Rock, Paper, Scissors game using functions.
Uses a class to create object instances to represent the player's and the program's random choice. The rps class objects have a magical __cmp__ method that is automatically called when comparisons are made between two object instances like userObject > programObject.

Dictionaries are used to store key and value pairs for tracking the name or actions that a particular instance of an object like userObject has assigned to it. If the userChoice is 'r', then  userObject = rps(userChoice) creates a rps object which represents a rock.  For example, the key 'r' is used in the rpsTypeDict to be able to retrieve the name 'Rock' by using self.rpsTypeDict[self.rpsType] in the magical __str__ method that is used whenever the object is referenced as a string or printed.

Rock, Paper, Scissors, Lizard, Spock Game

There are two other modules which implement the more advanced game of Rock, Paper, Scissors, Lizard, Spock: roshambobigbang.py and roshambobigbang_solution.py.
Invented by Sam Kass, All hail Sam Kass!, the game of ROCK PAPER SCISSORS SPOCK LIZARD, was referred to on the Big Bang Theory several times, so I decided to name the file after the original game with the name of the television series added to it. I had initially noticed that the name of the game in Wikipedia is referred to as ROCK PAPER SCISSORS LIZARD SPOCK, which is why my code refers to the weapons in that order.

This example builds upon the previous roshambo, but extends the game to Rock, Paper, Scissors, Lizard, Spock. The game can be automated where the player's choice of weapon is also randomly selected. In order to keep track of the different actions of the weapons, as they meet other weapons, a dictionary within a dictionary is used. Statistics are also reported for the choice of weapon by the player and the program. A matrix of how many times each weapon met another weapon is also reported. This matrix is also represented by dictionaries within a dictionary. There are two challenges to the viewer of the code which would help to make the code more dynamic and flexible.

This solution to the two challenges presented in roshambobigbang.py is nearly identical to roshambobigbang.py except that the way the statistics and distribution matrix are printed.

Thanks!

Thanks to Sam Kass and the Wikipedia for having the information and images about these games available:
http://www.samkass.com/theories/RPSSL.html
http://en.wikipedia.org/wiki/Rock-paper-scissors
http://en.wikipedia.org/wiki/Rock-paper-scissors-lizard-Spock
http://en.wikipedia.org/wiki/File:Rock_paper_scissors.jpg
http://en.wikipedia.org/wiki/File:Rock_Paper_Scissors_Lizard_Spock_en.svg
http://en.wikipedia.org/wiki/File:Pierre_ciseaux_feuille_l%C3%A9zard_spock_aligned.svg

No comments:

About Me - WrightRocket

My photo

I've worked with computers for over 30 years, programming, administering, using and building them from scratch.

I'm an instructor for technical computer courses, an editor and developer of training manuals, and an Android developer.