Video of terminal playing Very Simple version of rock, paper, scissors.

This is a very simple, single-player version of the rock, paper, scissor game written by Chris Bradfield at KidsCanCode which demonstrates the essence of the game in a remarkably short program.

It is a text game which plays over the USB serial console, harking back to the teleprinters and terminals from the early days of computing. The video (animated gif) above shows the game being played.

This runs on any board that supports CircuitPython and can also run on CPython.

Installation

Download the CircuitPython clue-verysimple-rpsgame.py below by clicking on the links and then using Save as.... The files are hosted on Adafruit's GitHub repo for this project. You can also use the Download: Project Zip link at the top of the code below.

Plug your board into your computer via a known-good USB data cable. A flash drive named CIRCUITPY should appear in your file explorer/finder program. Copy the clue-verysimple-rpsgame.py to the CIRCUITPY drive and rename it to code.py.

Code

# clue-verysimple-rpsgame v1.0
# CircuitPython rock paper scissors game simple text game
# based on https://www.youtube.com/watch?v=dhaaZQyBP2g

# Tested with CLUE and Circuit Playground Bluefruit (Alpha)
# and CircuitPython and 5.3.0

# copy this file to CLUE/CPB board as code.py

# MIT License

# Copyright (c) 2015 Chris Bradfield, KidsCanCode LLC

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import random

moves = ["r", "p", "s"]
player_wins = ["pr", "sp", "rs"]

print("Rock, paper scissors game: enter first letter for move or q for quit")
while True:
    player_move = input("Your move: ")
    if player_move == "q":
        break

    computer_move = random.choice(moves)
    print("You:", player_move)
    print("Me:", computer_move)
    if player_move == computer_move:
        print("Tie")
    elif player_move + computer_move in player_wins:
        print("You win!")
    else:
        print("You lose!")

Code Discussion

The code uses a while loop which runs forever repeating game after game. The player's text input is compared against the computer's random choice with the following code.

# Logic to determine winner
    if player_move == computer_move:
        print("Tie")
    elif player_move + computer_move in player_wins:
        print("You win!")
    else:
        print("You lose!")

The if is checking for a draw (tie) with a straightforward comparison. The elif is using a different technique, it concatenates the two players' values with + and uses in to check if they are present in the player_wins list. Note: this is different to the in used with for statements.

player_wins = ["pr", "sp", "rs"]

This is a form of lookup table where the data is stored in a Python list. The winning combinations are stored without any accompanying additional data - their presence indicates a win. If draws have already been eliminated then the only remaining result if it is not a win is a loss. The else statement efficiently implements this.

In this case, the hybrid use of the equality test plus a lookup table keeps the code very compact. Lookup tables are often used to increase performance with the trade-off of some additional memory/storage use. They can be used in hardware too and are sometimes known by the acronym LUT.

There are a few features missing from this game. One notable absence is the lack of data validation on the player's typed input. Invalid input is not reported and causes the player to lose.

Evolving the Game and some History

While very old multi-user computers are often associated with green screen terminals, many of the first electrical computers in the 1950s did have graphical screens and inevitably some games were written for these.

The console screens were small cathode-ray tubes. These were related to a type of dynamic memory technology from that era, the Williams-Kilburn tube.

Christopher Strachey's photographs of his noughts and crosses (tic-tac-toe) game on the EDSAC and draughts (checkers) on the Ferranti Mark I from 1952 are shown here.

Alvy Ray Smith's photograph is from a replica of the Manchester Baby (SSEM) proving that simple animation of images was possible in 1948.

The images here are from The Dawn of Digital Light.

The next version of the game uses the TFT LCD screen on the CLUE or TFT Gizmo attached to a Circuit Playground Bluefruit for a display. It is a two-player game using Bluetooth Low Energy for communication.

This guide was first published on Sep 02, 2020. It was last updated on Sep 02, 2020.

This page (Very Simple Game) was last updated on Mar 26, 2021.

Text editor powered by tinymce.