A C++ coursework simulation of two players over 20 rounds. Choose a strategy for each player and inspect the round-by-round moves and final scores.
Requires a C++17 compiler and Make; Python 3 is needed for the CLI checks. No external libraries are required.
make
./ipd_sim
./ipd_sim --seed 42Alternatively: c++ -std=c++17 main.cpp -o ipd_sim.
The optional seed makes random choices repeatable with the same executable and C library. The chosen seed is printed. Random sequences are not promised to match across different C library implementations. Without a seed, the program uses the current time.
Example input:
t
e
c
n
This selects tit-for-tat for Player 1, always-defect for Player 2, an initial cooperation by Player 1, then exits after the game. Expected scores: 19 and 24. Answer y at the replay prompt to start a fresh two-player game. EOF exits, including partway through configuration.
| Code | Strategy |
|---|---|
e |
Always defect |
g |
Always cooperate |
t |
Copy the opponent's previous move; choose the initial move interactively |
r |
Choose cooperation or defection with rand() % 2 |
| Moves | Player 1 payoff | Player 2 payoff |
|---|---|---|
| Cooperate / Cooperate | 3 | 3 |
| Defect / Defect | 1 | 1 |
| Defect / Cooperate | 5 | 0 |
| Cooperate / Defect | 0 | 5 |
Both decisions in a round use the opponent's previous round, so one player's update cannot reveal a current move to the other. The source retains the original Strategy, Player, and Game classes. The CLI starts exactly two players directly; its internal player-removal helper checks that an ID exists before resizing.
This is a teaching implementation, not a tournament framework or an empirical comparison of strategy performance. Round count and payoffs are fixed in source; rand() is a simple classroom random source.
make test
make clean
make CXXFLAGS="-std=c++17 -O1 -g -Wall -Wextra -Wpedantic -fsanitize=address,undefined -fno-omit-frame-pointer" testChecks cover five independently known payoff cases, replay, repeatable explicit seeds, invalid seed arguments, input exhaustion, missing player IDs, and player-index bounds. GitHub Actions runs the suite with sanitizers on Ubuntu.
Original coursework by Ashmit Shankar Mukherjee, dated December 2, 2023: Assignment 3, Computer Programming for Engineers, NYU Abu Dhabi. Later maintenance corrects replay, lifecycle, input handling, and repeatability.
Licensed under the MIT License.