A robust, idiomatic Rust implementation of the classic code-breaking game Master Mind (also known as Bulls and Cows), developed for the CS2102 Programming Lab course at Ashoka University.
The objective of this project is to implement an interactive, terminal-based code-breaking game using the Rust programming language. The game models the classic logic puzzle Bulls and Cows:
- The computer randomly selects a secret sequence of
$M$ integers, each chosen from the inclusive range$[1, N]$ . - Duplicate numbers are permitted in the secret code.
- The player repeatedly guesses the sequence.
- For each guess, the game returns structured feedback in terms of Bulls and Cows:
- Bull: A number that is correct in both value and exact position.
- Cow: A number that is present in the secret code, but placed in a different position.
- The game concludes when the player successfully identifies all
$M$ numbers in their exact positions ($M$ Bulls) or enters the exit command (quit). - Upon winning, the player is prompted whether they wish to play another round with a newly generated secret code.
The project demonstrates core Rust idioms, including memory safety, ownership, borrowing, pattern matching, error propagation with Result, deterministic and randomized testing, and standard Cargo workflow management.
-
Pseudo-Random Code Generation: Generates a secret sequence of length
$M$ from range$1..=N$ with uniform range selection using therandcrate. -
Two-Pass Scoring Algorithm (
score_guess): Accurately computes Bulls and Cows while strictly preventing duplicate numbers from being counted more than once. -
Robust Input Parser (
parse_guess):- Accepts tokens delimited by arbitrary whitespace (single spaces, multiple consecutive spaces, tabs, and mixed whitespace).
- Enforces exact token count (
$M$ tokens). - Validates integer syntax (rejecting floating-point numbers, negative values, and non-numeric characters).
- Enforces boundary conditions (
$1 \le \text{value} \le N$ ).
- Non-Punitive Input Validation: Invalid guesses display descriptive error messages and prompt the user again without consuming an attempt counter.
-
Graceful Session Handling:
- Case-insensitive
quitcommand allows the player to exit cleanly during gameplay. - End-Of-File (
EOF) / standard input closure is detected and handled without panics.
- Case-insensitive
-
Replay Loop (
ask_play_again): Interactively queries the player upon winning to start a new game with a fresh target or exit gracefully. - Attempt Tracking: Only valid guesses that are evaluated against the secret code increment the attempt counter.
- Automated Test Suite: 6 unit and property tests covering deterministic scoring, repeated digit tracking, randomized equivalence testing (10,000 iterations), parser tolerance, parser rejection, and target bounds.
- Graphical User Interface (GUI) or Web Interface (the application is strictly terminal-based).
- Configurable
$M$ and$N$ via command-line flags (parameters are declared as compile-time constantsMandN). - Persistent score storage, high score leaderboards, or file I/O.
- Turn limits or timer-based gameplay modes.
The game parameters are defined in src/main.rs as compile-time constants:
const M: usize = 4; // Secret code length (number of positions)
const N: u32 = 6; // Maximum allowed value (digits are chosen from 1..=N)-
Allowed values: Any whole number
$x \in {1, 2, 3, 4, 5, 6}$ . -
Repetitions: Numbers may repeat in the secret code (e.g.,
1 1 2 2or5 5 5 5).
- Bull: A digit in the player's guess that matches both the value and the exact index of a digit in the secret code.
- Cow: A digit in the player's guess that matches a digit in the secret code, but is situated at a different index.
| Secret Code (Target) | Player Guess | Bulls | Cows | Explanation |
|---|---|---|---|---|
1 2 3 4 |
1 2 3 4 |
4 | 0 | All digits match their exact positions. |
1 2 3 4 |
4 3 2 1 |
0 | 4 | All 4 digits are present, but every digit is misplaced. |
1 2 3 4 |
1 2 4 3 |
2 | 2 | 1 and 2 are Bulls; 4 and 3 are Cows. |
1 2 3 4 |
5 5 6 6 |
0 | 0 | None of the guessed numbers appear in the target. |
1 1 2 2 |
1 2 1 1 |
1 | 2 | Position 0 has Bull (1). Position 1 (2) matches target position 2 or 3 (Cow). Position 2 (1) matches target position 1 (Cow). Position 3 (1) cannot be matched because all 1s in target have been accounted for. |
1 1 1 1 |
1 1 2 2 |
2 | 0 | Two 1s match exactly (2 Bulls). The guessed 2s are not in the secret code. |
1 1 2 2 |
2 2 1 1 |
0 | 4 | All digits match in reverse pairs; 0 Bulls, 4 Cows. |
- Rust Compiler:
rustcversion 1.99.0 or higher. - Cargo:
cargoversion 1.99.0 or higher. - Rust Edition: 2024 edition (specified in
Cargo.toml).
The project configuration in Cargo.toml declares:
[package]
name = "mastermind"
version = "0.1.0"
edition = "2024"
[dependencies]
rand = "0.10"As resolved in Cargo.lock:
rand v0.10.3: Used for random sequence generation (rand::rng()andrng.random_range(1..=N)).- Sub-dependencies managed by Cargo include
chacha20 v0.10.2,getrandom v0.4.3, andrand_core v0.10.1.
mastermind/
├── Cargo.lock # Concrete dependency tree with locked crate versions
├── Cargo.toml # Cargo manifest defining metadata, edition, and dependencies
├── src/
│ └── main.rs # Complete application source code and unit tests
├── README.md # Primary user and developer documentation
├── TESTING.md # Comprehensive testing, verification, and bug report
├── DESIGN.md # Architecture, algorithm analysis, and state transitions
└── DEVELOPMENT_LOG.md # Reproducible build log and terminal command audit
- src/main.rs: Houses constants (
M,N), scoring logic (score_guess), input parsing (parse_guess), target generation (generate_target), game loop logic (play_game), replay prompt (ask_play_again), entry point (main), and test module (mod tests). - Cargo.toml: Declares package name, version, edition, and dependencies.
- Cargo.lock: Ensures reproducible builds by recording exact package hashes.
Navigate to the directory containing the project:
cd "c:\Users\User\Downloads\Programming Lab\Midterm_Project\mastermind"Check that the compiler and build system are present:
rustc --version
cargo --versionEnsure the codebase adheres to standard Rust formatting:
cargo fmt --checkValidate compilation without producing binary artifacts:
cargo checkRun the entire automated test suite:
cargo testLaunch the debug binary for gameplay:
cargo runTo compile an optimized production binary:
cargo build --releaseThe compiled executable will be located at:
- Windows:
target/release/mastermind.exe - Linux/macOS:
target/release/mastermind
- Upon launch, a welcome banner and instructions are displayed.
- The computer selects a hidden sequence of 4 numbers between 1 and 6. The secret code is never displayed during normal gameplay.
- At the prompt, enter 4 numbers separated by spaces or tabs:
Enter 4 numbers from 1 to 6, separated by spaces or tabs (or type 'quit'): 1 2 3 4 - The system evaluates the guess and prints the result:
Result: 1 Bull(s), 2 Cow(s). - If the guess is malformed (e.g. fewer than 4 numbers, numbers outside
$[1, 6]$ , or non-numeric tokens), an error message is printed and the attempt is not counted. - To exit the game at any prompt, type
quit(case-insensitive). - When all 4 digits match their positions (4 Bulls), the game announces:
Correct! You solved the code in X valid guess(es). - The game asks whether you wish to play again:
Would you like to play again? (y/n):- Enter
yoryesto generate a new secret code and start a fresh round. - Enter
n,no,quit, orexitto terminate the program.
- Enter
====================================
MASTER MIND: BULLS & COWS
====================================
I'm thinking of 4 numbers, each between 1 and 6.
Numbers may repeat.
A Bull means the number and position are correct.
A Cow means the number is correct but the position is not.
Type 'quit' during a guess to exit.
A new secret code has been selected.
Enter 4 numbers from 1 to 6, separated by spaces or tabs (or type 'quit'): Invalid guess: Please enter exactly 4 numbers, separated by spaces or tabs.
Enter 4 numbers from 1 to 6, separated by spaces or tabs (or type 'quit'): You chose to quit the current game.
Thanks for playing!
Control flow proceeds sequentially through modular, single-responsibility functions:
flowchart TD
Main[fn main] --> Banner[Print Banner & Instructions]
Banner --> GameLoop{Replay Loop}
GameLoop --> GenTarget[fn generate_target]
GenTarget --> PlayGame[fn play_game]
PlayGame --> ReadInput[Read Line from stdin]
ReadInput --> CheckQuit{Check 'quit' / EOF?}
CheckQuit -- Yes --> EndGame[Return false to main]
CheckQuit -- No --> ParseGuess[fn parse_guess]
ParseGuess -- Err --> Warn[Print Warning & Retry]
Warn --> ReadInput
ParseGuess -- Ok --> ScoreGuess[fn score_guess]
ScoreGuess --> DisplayResult[Print Bulls & Cows]
DisplayResult --> WinCheck{Bulls == M?}
WinCheck -- No --> ReadInput
WinCheck -- Yes --> Congratulate[Congratulate & Return true]
Congratulate --> AskReplay[fn ask_play_again]
AskReplay -- Yes --> GameLoop
AskReplay -- No --> ExitApp[Print Exit Message]
EndGame --> ExitApp
-
fn generate_target() -> Vec<u32>- Uses
rand::rng()to draw$M$ integers in$1..=N$ . - Returns an owned
Vec<u32>.
- Uses
-
fn parse_guess(line: &str) -> Result<Vec<u32>, String>- Splits input on arbitrary whitespace (
split_whitespace()). - Verifies exact length (
$M$ ). - Parses each token as
u32withmap_err. - Validates that each number lies within
$1..=N$ . - Returns
Ok(Vec<u32>)or a user-friendlyErr(String).
- Splits input on arbitrary whitespace (
-
fn score_guess(target: &[u32], guess: &[u32]) -> (usize, usize)- Accepts borrowed slices
&[u32]. - Runs two independent passes with boolean flags to avoid double-counting.
- Returns a
(bulls, cows)tuple.
- Accepts borrowed slices
-
fn play_game(target: &[u32]) -> bool- Manages the interactive prompt for a single secret code.
- Tracks valid attempt count.
- Returns
trueif the player won, orfalseif they quit or closed stdin.
-
fn ask_play_again() -> bool- Prompts the user with
(y/n)upon a win. - Returns
trueto restart orfalseto exit.
- Prompts the user with
-
fn main()- Top-level orchestration loop.
When secret codes and guesses contain repeated digits (e.g., Target: [1, 1, 2, 2], Guess: [1, 2, 1, 1]), naive frequency counting or single-pass checking easily leads to counting an exact match as both a Bull and a Cow, or counting a single target digit against multiple guess digits.
fn score_guess(target: &[u32], guess: &[u32]) -> (usize, usize) {
let mut target_used = vec![false; target.len()];
let mut guess_used = vec![false; guess.len()];
let mut bulls = 0;
let mut cows = 0;
// Pass 1: Count exact matches (Bulls)
for i in 0..target.len() {
if target[i] == guess[i] {
bulls += 1;
target_used[i] = true;
guess_used[i] = true;
}
}
// Pass 2: Match remaining digits in different positions (Cows)
for i in 0..guess.len() {
if guess_used[i] {
continue;
}
for j in 0..target.len() {
if !target_used[j] && guess[i] == target[j] {
cows += 1;
guess_used[i] = true;
target_used[j] = true;
break;
}
}
}
(bulls, cows)
}Suppose Target = [1, 1, 2, 2] and Guess = [1, 2, 1, 1]:
-
Pass 1 (Bulls):
- Index 0:
target[0] == 1andguess[0] == 1$\rightarrow$ Match!bulls = 1,target_used[0] = true,guess_used[0] = true. - Index 1:
1 != 2$\rightarrow$ No match. - Index 2:
2 != 1$\rightarrow$ No match. - Index 3:
2 != 1$\rightarrow$ No match. - Status after Pass 1:
bulls = 1.target_used = [T, F, F, F],guess_used = [T, F, F, F].
- Index 0:
-
Pass 2 (Cows):
- Guess Index 0: Skipped (
guess_used[0]is true). - Guess Index 1 (
guess[1] == 2):- Scan target indices
$0..4$ . Target index 2 hastarget[2] == 2and!target_used[2]. - Match!
cows = 1,guess_used[1] = true,target_used[2] = true. Inner loop terminates (break).
- Scan target indices
- Guess Index 2 (
guess[2] == 1):- Target index 1 has
target[1] == 1and!target_used[1]. - Match!
cows = 2,guess_used[2] = true,target_used[1] = true. Inner loop terminates (break).
- Target index 1 has
- Guess Index 3 (
guess[3] == 1):- Scan target indices: index 0 is used; index 1 is used; index 2 is used; index 3 has value
2(does not match1). - No match found.
- Scan target indices: index 0 is used; index 1 is used; index 2 is used; index 3 has value
- Guess Index 0: Skipped (
-
Result:
(1 Bull, 2 Cows). Exactly correct.
-
Time Complexity:
- Pass 1 runs in
$O(M)$ time where$M$ is sequence length. - Pass 2 runs nested loops with worst-case
$(M - \text{bulls})^2$ comparisons, which is bounded by$O(M^2)$ . - For
$M = 4$ , the maximum number of comparisons is$4 + 16 = 20$ operations, which is$O(1)$ constant time.
- Pass 1 runs in
-
Auxiliary Space Complexity:
- Two boolean tracking vectors of length
$M$ :$O(M)$ auxiliary memory. - For
$M = 4$ , this consumes only 8 bytes of heap memory, which is negligible.
- Two boolean tracking vectors of length
letandlet mut: Variables are immutable by default (let target = generate_target()). Mutability is explicitly scoped where state must evolve (let mut attempts = 0; let mut line = String::new();).- Constants and Primitive Types:
const M: usize = 4;andconst N: u32 = 6;demonstrate compile-time constants. Types likeusize,u32, andboolensure type safety across index operations. - Ownership and Borrowing: Slices
&[u32]are borrowed byscore_guessandplay_gamewithout copying or consuming the underlying vector. Functions return owned collections (Vec<u32>) where newly created data must transfer ownership. - Immutable vs. Mutable References: Immutable borrows (
target: &[u32]) enable concurrent read-only inspection. Mutable borrows (&mut answer,&mut line) grant in-place modification permissions to standard I/O functions. - Vectors and Indexing: Fixed-length vectors initialized using
vec![false; target.len()]and dynamically populated withguess.push(value)show safe collection management. - Tuples: The compound return type
(usize, usize)cleanly groups related return values without requiring a single-use custom struct. Result,Ok,Err, and Error Mapping:parse_guessreturnsResult<Vec<u32>, String>, transforming standard parse errors into informative strings using.map_err(|_| format!("..."))?.- Pattern Matching: Exhaustive
matchexpressions handle standard I/O outcomes (Ok(0)for EOF,Ok(_),Err(e)) and user choices ("y" | "yes" => true,_ => ...). - Loops and Control Flow:
loop,for i in 0..target.len(),continue, andbreakillustrate clean, idiomatic control flow without recursion. - Standard I/O and Flushing:
print!outputs prompts without trailing newlines, andio::stdout().flush()ensures text appears immediately before blocking for input. - Unit Testing: Tests placed in
mod testswith#[cfg(test)]compile only during testing, executing assertions (assert_eq!,assert!) safely.
Input validation in parse_guess follows a multi-stage defense-in-depth pipeline:
// 1. Whitespace normalization and token counting
let tokens: Vec<&str> = line.split_whitespace().collect();
if tokens.len() != M {
return Err(format!("Please enter exactly {M} numbers, separated by spaces or tabs."));
}
// 2. Numeric parsing per token
let value = token
.parse::<u32>()
.map_err(|_| format!("'{token}' is not a valid whole number."))?;
// 3. Range checking per value
if !(1..=N).contains(&value) {
return Err(format!("Every number must be between 1 and {N}."));
}- Whitespace variation: Spaces, multiple spaces, tabs (
\t), and leading/trailing whitespace are cleanly handled bysplit_whitespace(). - Wrong token count: Inputs like
1 2 3(too few) or1 2 3 4 5(too many) are rejected. - Malformed characters: Inputs like
1 two 3 4or1 2.5 3 4return parsing error messages. - Out-of-range digits:
0or7are rejected with boundary error messages. - Graceful EOF: If standard input reaches EOF (e.g. piped input terminates or
Ctrl+D/Ctrl+Z),read_lineyieldsOk(0), which triggers a clean termination message rather than an infinite loop.
-
Hardcoded Game Configuration:
$M$ and$N$ are hardcoded constants. Changing sequence length or digit range requires modifying source code and recompiling. - Terminal Exclusivity: The user interface is strictly plain text in the terminal.
-
No Turn Limit: The game allows unlimited attempts until the player either wins or enters
quit.
-
Command-Line Arguments: Use
clapto support flags such ascargo run -- --length 5 --range 8. -
Difficulty Presets: Offer Easy (
$M=3, N=5$ ), Medium ($M=4, N=6$ ), and Hard ($M=5, N=8$ ) modes. - Turn Limit & Scoring: Add a maximum allowed attempt limit (e.g. 10 guesses) and an attempt-based score multiplier.
- ANSI Colored Feedback: Use terminal colors (e.g., green for Bulls, yellow for Cows) for enhanced visual clarity.