A distributed-memory Rock-Paper-Scissors game implemented in C using MPI (Message Passing Interface) for exactly 2 processes.
- About the Project
- Features
- Project Structure
- Getting Started
- Compilation & Execution
- Communication Modes
- Game Rules & Logic
- Testing
- Troubleshooting
- Submission Notes
- Academic Integrity
This program simulates a game of Rock-Paper-Scissors between two parallel processes. Process 0 acts as the referee: it prints all outputs, determines the winner of each round, keeps the score, and ends the game when one process reaches 5 points. If both processes choose the same move, the round is a draw and is replayed.
- Language: Written entirely in C.
- Framework: Utilizes MPI for distributed process communication.
- Constraints: Runs with exactly 2 processes.
- Modes: -
0→ Point-to-Point Communication1→ Collective Communication
- Mechanics: Random move generation on both processes seeded uniquely to avoid mirrored plays.
- Environment: Compatible with Linux environments using
mpiccandmpirun. - Clean: No file reading or writing. Includes a
Makefilefor easy compilation.
.
├── game.c # Main source code
├── Makefile # Build instructions
└── README.md # Project documentation
To compile and run this project, you need:
- A Linux environment (intended target).
- A C compiler that supports MPI (typically
mpicc). - An MPI runtime launcher (e.g.,
mpirunormpiexec).
Check if MPI is installed:
mpicc --version
mpirun --versionIf both commands work, your environment is ready.
If MPI is not installed, use the appropriate command for your OS:
Ubuntu / Debian
sudo apt update
sudo apt install openmpi-bin libopenmpi-devFedora
sudo dnf install openmpi openmpi-develArch Linux
sudo pacman -S openmpimacOS (Homebrew)
brew install open-mpiNote: On macOS, behavior may vary slightly depending on your shell and MPI configuration. Linux is the intended target environment.
Compile the project using the included Makefile:
makeIf successful, this generates the game executable using a command similar to mpicc -Wall -O2 -o game game.c.
Run the game with exactly 2 MPI processes:
mpirun -n 2 ./gameThe program will prompt you to select a mode:
Select mode: 0 (P2P) or 1 (Collective)
Enter 0 or 1.
The game starts
Select mode: 0 (P2P) or 1 (Collective)
0
Mode: 0
Turn 1, Process-0: ROCK, Process-1: PAPER
Process-1 wins, Score: 0 - 1
Turn 2, Process-0: SCISSORS, Process-1: ROCK
Process-1 wins, Score: 0 - 2
Turn 3, Process-0: PAPER, Process-1: PAPER
Draw, replay the round.
...
Process-0 has won the game with score: 5 - 4 in 11 turns.
The game ends
This mode uses direct communication between processes.
- Process 1 generates its move and sends it to Process 0.
- Process 0 generates its own move, receives Process 1’s move, determines the winner, and updates the score.
- Key MPI Functions:
MPI_Send,MPI_Recv
This mode uses collective MPI communication.
- Each process generates its move.
- Moves are collected at Process 0.
- Process 0 determines the round result and broadcasts the updated scores back to all processes.
- Key MPI Functions:
MPI_Gather,MPI_Bcast
Basic Rules:
- Rock 🪨 beats Scissors ✂️
- Scissors ✂️ beats Paper 📄
- Paper 📄 beats Rock 🪨
Scoring & Game Flow:
- The round winner gets 1 point.
- A tie results in a draw (no points, round is replayed).
- The game immediately ends when a process hits 5 points.
Randomness: Each MPI process seeds its random number generator separately using time and rank information. This ensures runs aren't identical and processes don't blindly mimic each other.
A complete test sequence to verify your build:
make clean
make
mpirun -n 2 ./gameRecommended Test Cases:
- P2P Mode: Run
mpirun -n 2 ./gameand enter0. Verify Process 0 prints output and the game ends at 5 points. - Collective Mode: Run
mpirun -n 2 ./gameand enter1. Verify collective logic functions and ends at 5 points. - Invalid Process Count: Run
mpirun -n 3 ./game. The program should print an error message and exit safely. - Invalid Mode: Run
mpirun -n 2 ./gameand enter7. The program should report an invalid mode and exit cleanly without crashing.
| Issue | Solution |
|---|---|
make: mpicc: No such file or directory |
MPI compiler is not installed or not in your PATH. Install MPI first. |
mpirun: command not found |
MPI runtime is missing. Install OpenMPI or MPICH. |
| Program does not compile | Ensure the file is exactly game.c, Makefile is Makefile, and Makefile commands use tabs, not spaces. |
| Error about number of processes | You must run with exactly 2 processes: mpirun -n 2 ./game. |
| Output is different every run | This is expected! Moves are randomly generated. |
| Both processes print messages | Only Process 0 should print. Ensure your printf statements are guarded by if (rank == 0). |
This project was prepared for an academic homework assignment. Demonstrating MPI process management, point-to-point/collective communication, and distributed state synchronization.
Notice: If you are a student taking the same course, do not copy or submit this work as your own. Use it only within your course rules and academic integrity guidelines.