Skip to content
C++ project #06Beginner

Rock Paper Scissors with an Adaptive Opponent

A best-of-N match against a computer that is either random or learns your favourite move — with a seedable random generator for repeatable games.

Estimated time
~2 hours to build
Files
5 files · 292 lines
Technologies
C++17, <array>, <chrono>, <cstdint>

Introduction

A simple game that hides two interesting ideas. First, enum class makes moves and outcomes type-safe. Second, the opponent can adapt: it counts your moves and plays whatever beats your most common one. A seeded linear congruential generator makes every game reproducible, which is what lets the test compare exact output.

Problem statement

Ask for a seed (0 means use the clock), an opponent type and an odd number of rounds. Play rounds until someone reaches a majority of wins; draws don't count toward the total. Accept r/p/s or full words, reject anything else without using up a round, show each round's result and score, and announce the match winner.

Objectives

  • Model fixed choices with enum class and switch
  • Write a tiny pseudo-random number generator
  • Keep opponent state in a class
  • Implement a simple frequency-based strategy

Features

  • Random or adaptive opponent
  • Best-of-N matches (even numbers are rounded up to odd)
  • Invalid moves are rejected without consuming a round
  • Reproducible games from a seed

Concepts demonstrated

  • enum class
  • switch
  • Pseudo-random numbers
  • Object state
  • Game loop

System architecture

Move and Outcome are enum classes; beats(m) returns the move that beats m and judge compares two moves using it. Random is a 32-bit LCG. Opponent holds a Random, a mode flag and counts of the player's moves; in adaptive mode, after three observations, it plays beats(most_frequent) unless there is a tie for most frequent, in which case it falls back to random. The game loop in main reads moves until either side reaches best_of / 2 + 1 wins.

Folder structure

TextProject tree
rock-paper-scissors/
├── README.md
├── Makefile
├── src/main.cpp
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -O2
SRC := $(wildcard src/*.cpp)
HDR := $(wildcard src/*.h src/*.hpp)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CXX) $(CXXFLAGS) -Isrc $(SRC) -o app
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/main.cpp
Enums and helpers (name, parse_move, beats, judge), the Random and Opponent classes, and the match loop.
Makefile
Build automation: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
g++ -std=c++17 -Wall -Wextra -Isrc src/*.cpp -o app
# or simply: make

Run

Terminal
./app # Linux / macOS
.\app.exe # Windows

Test against the expected output

Terminal
./app < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# Get-Content tests\sample-input.txt | .\app.exe > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
Seed (0 = random): Opponent (random/adaptive): Best of how many rounds (odd number)?
Playing best of 7 against the adaptive opponent. First to 4 wins.
Round 1 - your move (r/p/s): You: rock CPU: rock -> draw [0-0]
Round 2 - your move (r/p/s): You: rock CPU: paper -> CPU wins the round [0-1]
Round 3 - your move (r/p/s): 'lizard' is not a move.
Round 3 - your move (r/p/s): You: rock CPU: rock -> draw [0-1]
Round 4 - your move (r/p/s): You: paper CPU: paper -> draw [0-1]
Round 5 - your move (r/p/s): You: rock CPU: paper -> CPU wins the round [0-2]
Round 6 - your move (r/p/s): You: scissors CPU: paper -> you win the round [1-2]
Round 7 - your move (r/p/s): You: rock CPU: paper -> CPU wins the round [1-3]
Round 8 - your move (r/p/s): You: paper CPU: paper -> draw [1-3]
Round 9 - your move (r/p/s): You: scissors CPU: paper -> you win the round [2-3]
Round 10 - your move (r/p/s): You: rock CPU: paper -> CPU wins the round [2-4]
Rounds played: 10 (4 draws)
The CPU won the match 4-2.
Input
7
adaptive
7
rock
r
lizard
r
p
r
s
r
p
s
r
p

Testing

  • The scripted game uses seed 7 and the adaptive opponent; compare with tests/expected-output.txt.
  • Because the player keeps choosing rock, the opponent starts playing paper from round 4.
  • Use seed 0 to get a different game every run.

Common errors

warning: control reaches end of non-void function

Some compilers can't tell a switch over an enum class is exhaustive; add a return after the switch, as name does.

The opponent is predictable

That is the point of adaptive mode — vary your moves, or try random mode.

Possible improvements

  • Add Rock-Paper-Scissors-Lizard-Spock
  • Let the adaptive opponent look at your last two moves (pattern matching)
  • Keep statistics across matches in a file

Additional challenges

  1. 1Write a strategy that beats the adaptive opponent most of the time.
  2. 2Replace the LCG with std::mt19937 and compare.
  3. 3Add a two-player hot-seat mode.

Built and tested it yourself? Mark it to update your dashboard.