Class GameBoard
A gameboard consists of rows of panels, each containing two sub-panels:
- A panel for guesses, where the player inputs their code guesses.
- A panel for hints, which provides feedback on the guesses.
The gameboard is designed to look like a physical mastermind board.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final HashMap
<Code.Color, Color> A mapping ofCode.Color
values to their correspondingColor
instances. -
Constructor Summary
ConstructorDescriptionConstructs a new GameBoard instance and initializes the game board layout. -
Method Summary
Modifier and TypeMethodDescriptionstatic void
Renders a visual representation of a guess on the given panel.static void
Updates the specified parent JPanel to display feedback hints in a grid format, based on the given response.Retrieves the main game board panel.void
updateGuess
(int rowNumber, List<Code.Color> colors) Updates the guessed colors for a specified row on the game board.void
updateGuessFromColorIndices
(int rowNumber, List<Integer> colorIndices) Updates the guessed colors for a specific row on the game board based on the provided color indices.void
updateHints
(int rowNumber, Response hints) Updates the hint panel for a specified row on the game board based on the provided feedback.
-
Field Details
-
CODE_COLOR_TO_AWT_COLOR
A mapping ofCode.Color
values to their correspondingColor
instances.
-
-
Constructor Details
-
GameBoard
public GameBoard()Constructs a new GameBoard instance and initializes the game board layout.The board consists of rows representing guesses and corresponding hint panels.
Each row is divided into two panels:
- A panel for guesses, where the player inputs their code guesses.
- A panel for hints, which provides feedback on the guesses.The GameBoard layout is created using the GridBagLayout manager, and each row is appended to the board with the necessary constraints.
Initializes four placeholders (light gray color) for guesses and four placeholders (black and white feedback pegs) for hints in each row.
Additionally, individual rows of the board are stored in an internal list for ease of future updates to guess and hint panels.
-
-
Method Details
-
drawGuess
Renders a visual representation of a guess on the given panel. Updates the specified parent JPanel to display a row of color-coded squares, where each square corresponds to a color in the provided list.If the list of colors contains fewer elements than the required code length, an IllegalArgumentException is thrown.
Each square is rendered as a JLabel with a border colored according to the corresponding color in the list. The size of each square is standardized. The updated panel layout is refreshed after rendering.
- Parameters:
parent
- The JPanel where the guess will be rendered. This panel's content will be cleared before rendering.colors
- A list of Color objects representing the player's guess. Must contain at least the number of colors defined byMastermind.CODE_LENGTH
.- Throws:
IllegalArgumentException
- if the size of the colors list is less thanMastermind.CODE_LENGTH
.
-
drawHints
Updates the specified parent JPanel to display feedback hints in a grid format, based on the given response. The hints include black, white, and light gray markers, corresponding to exact matches, partial matches, and empty spaces respectively.The method determines the number of black and white pegs needed from the response and fills the remaining spaces with light gray. It then organizes these hints into a 2x2 grid visual representation within the panel.
Each hint is represented as a small JLabel with a border colored according to the response values. Black borders represent correct colors in the correct position, white borders represent correct colors in the wrong position, and light gray borders represent no matches.
The panel is refreshed to reflect the updated content after rendering the hints.
- Parameters:
parent
- The JPanel to which the hints will be added. This panel is cleared before rendering the new hints.response
- The Response object containing the number of exact matches (black pegs) and partial matches (white pegs) to be represented.
-
updateGuessFromColorIndices
Updates the guessed colors for a specific row on the game board based on the provided color indices.This method converts the given list of color indices into a corresponding list of
Code.Color
objects, which represent the color codes used in the game. It then delegates the process of updating the guess visually on the game board toupdateGuess(int, List)
.The indices in the list must correspond to valid colors defined in the
Code.Color
enumeration.- Parameters:
rowNumber
- The row number on the game board where the guess should be updated. Must be within the bounds of the game board's rows.colorIndices
- A list of integers representing the indices of the colors for the guess. Each index corresponds to a color code as defined inCode.Color
.
-
updateGuess
Updates the guessed colors for a specified row on the game board.This method takes a list of
Code.Color
values representing the guess. It converts these game-specific color representations into correspondingColor
instances to render on the visual game board. If the given list of colors contains fewer entries than the code length required by the game, the remaining entries are filled with a placeholder color (light gray).The method retrieves the associated guess panel for the specified row and updates it by calling the
drawGuess(JPanel, List)
method with the converted colors.- Parameters:
rowNumber
- The row number on the game board to update. This specifies which row will display the given guess. Must be within the bounds of the game board's rows.colors
- A list ofCode.Color
objects representing the guessed colors for the row. The list can contain fewer entries than the required code length, in which case placeholder colors are used for the remaining positions.
-
updateHints
Updates the hint panel for a specified row on the game board based on the provided feedback.This method retrieves the hint panel corresponding to the specified row number and updates it with visual feedback based on the given
Response
. The feedback typically consists of black, white, and light gray markers to represent exact matches, partial matches, and no matches, respectively.The visual update is performed by delegating the rendering of hints to the
drawHints(JPanel, Response)
method.- Parameters:
rowNumber
- The row number on the game board whose hint panel should be updated. Must be within the bounds of the game board's rows.hints
- TheResponse
object containing feedback information to be displayed as hints for the specified row. It specifies the number of exact matches and partial matches.
-
getBoardPanel
Retrieves the main game board panel.The returned panel, structured using a
GridBagLayout
, serves as the primary component that visually represents the game board. It contains rows of panels, each consisting of a guess panel and a hint panel representing player guesses and feedback respectively.This panel is initialized and updated internally within the
GameBoard
class, reflecting the current state of the game.- Returns:
- The
JPanel
representing the main game board.
-