
Mastermind
code-breaking game · Java + Javalib
A complete, playable Mastermind: a randomly generated secret, classic exact and inexact peg feedback that scores duplicate colours correctly, and a win or reveal-on-loss end state.
Break the hidden 4-peg code. Click a colour to place it (or focus the board and press 1 to 6), Backspace to undo, then Submit. Each row scores exact pegs (right colour, right slot) and inexact pegs (right colour, wrong slot).
Guess 1 of 10. 0 of 4 pegs placed.
Break a fresh 4-peg secret in your browser: exact and inexact feedback, the same scoring rule as the original.
The source for this course project is kept private. Request access through the contact form and I will gladly share the code and walk through it.
Request accessMastermind, a CS2510 (Fundamentals of Computer Science 2) project co-built with a partner, models the code-breaking game with an interface-and-class data design: colour sequences are represented as recursive lists (an ILoColor interface with empty and cons variants), and the stored guesses as a list of those lists. The game is parameterised by code length, colour count, allowed-guess budget and whether duplicates are permitted, then rendered as an interactive graphical world via the Javalib worldimages and funworld libraries, with number keys to enter a colour, enter to submit and backspace to undo. The feedback engine is the heart of it: exact matches count pegs that are the right colour in the right position, while inexact matches use the standard overlap rule, summing the minimum of each colour's count across the answer and the guess and subtracting the exacts, so duplicate colours are scored correctly. The browser demo here ports that exact scoring rule to a 4-peg, 6-colour board, generating a fresh secret on the client for each game.
- Java
- Javalib
- Recursion
- Interfaces
- OOP
- Secret length
- 4 pegs (demo config)
- Colour set
- 6 colours (demo config)
- Duplicates
- allowed
- Feedback
- exact + inexact pegs
What I'd improve
The data design leans on hand-rolled recursive lists, which was the point of the course but means the match-counting walks the lists repeatedly. With more time I would tighten the feedback engine into a single pass with colour-count maps, factor the rendering out of the game-state class, and add property-based tests that check the feedback against known answer-and-guess pairs.