Mastermind¶
Goal: Students design a text-based Mastermind game that gives structured feedback about guesses and hidden code.
Duration: 1-3 class hours.
Prerequisites: Lists, loops, conditionals, functions, and the earlier game modules.
Why It Belongs Here¶
Mastermind is named in the source curriculum as an additional text-based game. It is a useful bridge between the rule-based games and the later vision and robotics work because the player receives partial information, updates a belief about hidden state, and chooses the next action.
Game Shape¶
The computer chooses a secret sequence of colors or digits. The player makes a guess and receives feedback such as:
- Exact matches: the value and position are correct.
- Partial matches: the value is present but in another position.
- Misses: the value does not occur in the secret code.
Students should decide whether repeated values are allowed, how many guesses are available, and how the game reports feedback. Those choices are part of the design exercise.
Suggested Build¶
- Play a paper version and write down the state, actions, feedback, and end conditions.
- Generate a hidden code and validate the player's input.
- Write a function that compares a guess with the code.
- Add a loop, guess limit, win message, and loss message.
- Extend the game with a computer guesser or a history of previous guesses.
def score_guess(secret, guess):
exact = sum(a == b for a, b in zip(secret, guess))
remaining_secret = [value for a, value in zip(secret, guess) if a != value]
remaining_guess = [value for a, value in zip(secret, guess) if a != value]
partial = sum(remaining_guess.count(value) for value in set(remaining_guess))
return exact, partial
The starter comparison above is intentionally a discussion prompt: students should test repeated values and improve the scoring logic if their rule set allows duplicates.
Continuity¶
Mastermind makes the course's recurring pattern explicit: observe feedback, update an internal state, and choose another action. The same loop appears in Hangman, Tic-Tac-Toe strategy, camera board recognition, and robot control.