Project 26: Rock Paper Spock Arena 🖖¶

Difficulty: 🟢 Beginner

Run cells top to bottom. Each step builds on the previous one and saves real files under outputs/rock_paper_spock_arena/.

← Back to index

Description¶

Play dozens of Rock-Paper-Scissors-Lizard-Spock rounds against the computer and chart who wins.

Libraries & Modules¶

  • random
  • matplotlib.pyplot

Python Concepts You'll Practice¶

Enums-via-dicts, branching logic, scorekeeping

🎛️ Parameter Variations¶

  • ROUNDS — match length
  • PLAYER_BIAS — force a favorite move ratio
In [1]:
# Install dependencies for this project (safe to re-run)
import sys
!{sys.executable} -m pip install pillow numpy matplotlib wordcloud qrcode[pil] python-barcode fpdf2 jinja2 folium plotly pandas scipy ipywidgets -q
Skipping pip install (already installed)
In [2]:
# Interactive Jupyter setup — run this cell first
%matplotlib inline

from pathlib import Path
from IPython.display import display, Image as IPImage, HTML, Markdown, Audio, IFrame

OUTPUT_DIR = Path("outputs") / "rock_paper_spock_arena"
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
print(f"✅ Outputs folder: {OUTPUT_DIR.resolve()}")
✅ Outputs folder: C:\Users\Hansel Yan\Projects\CodeItAll\outputs\rock_paper_spock_arena

Step 1 — Battle simulator¶

In [3]:
import random

# 🎛️ TWEAK THESE
ROUNDS = 60
SEED = 3
MOVES = ["rock", "paper", "scissors", "lizard", "spock"]
WINS = {
    "rock": {"scissors", "lizard"},
    "paper": {"rock", "spock"},
    "scissors": {"paper", "lizard"},
    "lizard": {"spock", "paper"},
    "spock": {"scissors", "rock"},
}

random.seed(SEED)
score = {"you": 0, "cpu": 0, "draw": 0}
history = []
for _ in range(ROUNDS):
    you = random.choice(MOVES)
    cpu = random.choice(MOVES)
    if you == cpu:
        result = "draw"
    elif cpu in WINS[you]:
        result = "you"
    else:
        result = "cpu"
    score[result] += 1
    history.append((you, cpu, result))

print(score)
print("Sample rounds:")
for row in history[:5]:
    print(" ", row)
{'you': 28, 'cpu': 21, 'draw': 11}
Sample rounds:
  ('paper', 'spock', 'you')
  ('spock', 'paper', 'cpu')
  ('scissors', 'spock', 'cpu')
  ('lizard', 'spock', 'you')
  ('rock', 'spock', 'cpu')

Step 2 — Scoreboard chart¶

In [4]:
import matplotlib.pyplot as plt

labels = list(score.keys())
vals = [score[k] for k in labels]
fig, ax = plt.subplots(figsize=(7, 4))
ax.bar(labels, vals, color=["#111111", "#8b1e1e", "#888888"])
ax.set_title("Rock Paper Spock Arena — results")
ax.set_ylabel("Rounds")
fig.tight_layout()
out = OUTPUT_DIR / "spock_scoreboard.png"
fig.savefig(out, dpi=140)
plt.show()
(OUTPUT_DIR / "spock_log.txt").write_text(
    "\n".join(f"{a}|{b}|{r}" for a, b, r in history),
    encoding="utf-8",
)
print(f"Saved {out}")
No description has been provided for this image
Saved outputs\rock_paper_spock_arena\spock_scoreboard.png

✅ Project complete¶

Check your output folder: outputs/rock_paper_spock_arena/

Next: Project 27: Starfield Postcard · Index