Rpssl

Hi,

Introduction: “Rock, paper, scissors, Spock, lizard” is probably one of the most commons beginner’s Python program after “Hello world!”. I just took the one I had previously created and enabled the use of the Explorer HAT as a kind of game controller.

Background: I have always different projects in mind, I will use the Explorer HAT for creating a rover in the near future, I am also using my SenseHAT to log the environment, I am involved in the design of a gas sensing HAT and so on. I think the Explorer HAT has a lot of options so I started thinking about creating something with it and the game I heard about in Big Bang Theory came to my mind. Credits go to Sam Kass and Karen Bryla for creating the original game. The code is as follows:

#!/usr/bin/env python
#-*-coding:utf-8-*-
print("""
This program turns your Explorer HAT into a rock, paper, scissors, Spock, lizard playing machine!

Created by Iker García.

Hit touch pad 1 to select rock.
Hit touch pad 2 to select Spock.
Hit touch pad 3 to select paper.
Hit touch pad 4 to select lizard.
Hit touch pad 5 to select scissors.

Press CTRL+C to exit.
""")

    import explorerhat
    import random
    import time

    choices = ["rock", "Spock", "paper", "lizard", "scissors"]
    player = 0
    RaspberryPi = 0
    result = 0
    score = 0

    def choice(ch, evt):
      
      global choices #Define variables as global. 
      global player
      global RaspberryPi
      global result
      global score
     
      if ch < 6: #We are only going to use the first five touch pads.
        if evt == 'press':
          player = ch-1 #Saves player choice as a number, to calculate the result.
          print("Player chooses: %s." % choices[ch-1]) #Prints our choice.
      else: 
        print("Incorrect choice\n")
        return
      RaspberryPi = random.randrange(0,5)
      print("Raspberry Pi is thinking...")
      time.sleep(2) #Added to pretend that the Raspberry Pi is taking this game seriously.
      print("Raspberry Pi chooses: %s." % choices[RaspberryPi]) #Prints Raspberry Pi's choice.
      result = player - RaspberryPi #Calculates the winner.
      if result == 0:
        score += 0 #Updates the score
        print("Player and Raspberry Pie tie!")
        print("Score: %d\n" % score)
      elif result % 5 < 3:
        score += 1
        print("Player wins!")
        print("Score: %d\n" % score)  
      else:
        score -= 1
        print("Raspberry Pi wins")
        print("Score: %d\n" % score)
        
    while True:
     try:  
       explorerhat.touch.pressed(choice) #Calls the function when a touch pad is presed.
     except KeyboardInterrupt:
       break

I think everything is explained in the same code, so you can see how it works. Anyway, you can ask whateveryou want. The name this time is RPSSL.

Here is the repo: https://github.com/IkerGarcia/RPSSL. Contributions are welcome!

Actually I am thinking that it would be a good idea to add a 16x2 LCD and a RGB LED matrix, to shine depending on the result.

Regards.

1 Like

Nice work! My brain is too mushed to understand how/why the scoring works, but I’ll take your word for it :D

You can rewrite:

while True:
     try:  
       explorerhat.touch.pressed(choice) #Calls the function when a touch pad is presed.
     except KeyboardInterrupt:
       break

to:

explorerhat.touch.pressed(choice) #Calls the function when a touch pad is presed.

while True:
    try:  
        pass
    except KeyboardInterrupt:
        break

This is because explorerhat.touch.pressed passes the function you specify into the magical inner guts of the Explorer HAT library where it loops for you, checks if a button has been pressed, and calls that function.

Hi,

Thank you for your reply, it means a lot to me.

I want to make a little clarification about my initial message, when I said that I am involved in a lot of projects I wanted to say “start thinking in different things instead of finishing at least one” hahaha I can be still considered a newbie.

I updated the repo with your code fix. Also amended the commit, cause I realized that I put such unclear commit messages. I won’t amend the older ones, but as this one was created a few days ago I think it’s better this way. I am also so new to GitHub, excuse me for this.

Hope not to sound pretentious, but if you like the game you can add to the examples without any problem!

I sent this message without the explanation of the scoring, time to edit:

Tie: it’s clear. If both numbers are the same, we get that.

Player wins: we need to get either 2 or 1 as the result of (player-RaspberryPi) % 5. In order to achieve this, player-RasperryPi can be: 2,1, -3, -4.

_Computer wins: remaining options. If player-RaspberryPi is: 3,4, -1, -2, Raspberry Pi wins. _

Now just put all the options in a circle like this: rock-Spock-paper-lizard-scissors-(close the circle with the rock). Each options wins against the choices that are placed in the two positions before itself in the circle, and loses against the other two. Example: Spocks wins both against rock or scissors.

Now put a number to each of the choices, as in the code: 0-1-2-3-4 (close circle with a 0). Using Spock’s example, we get 1 o -3 as the result of player-RaspberryPi, calculating the module makes us win! However, we get -1 or -2 against the other two choices, being defeated.

Hope the explanation is clear.

Regards.