Skip to content
This repository was archived by the owner on Jan 18, 2025. It is now read-only.

Commit 5f13b13

Browse files
comment secret code prompt.java
1 parent e65a156 commit 5f13b13

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

culminating-mastermind/app/src/main/java/mastermind/gui/scenes/SecretCodePrompt.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,16 @@
1616
* Represents a scene where the user is prompted to enter the correct code.
1717
*/
1818
public class SecretCodePrompt extends Scene {
19+
// Variable to store the secret code entered by the user
1920
private Code secretCode = null;
21+
22+
// The current game status
2023
private final Status status;
24+
25+
// List of guesses made by the code breaker
2126
private final List<Code> guesses;
27+
28+
// List of responses corresponding to each guess
2229
private final List<Response> responses;
2330

2431
/**
@@ -34,33 +41,43 @@ public SecretCodePrompt(final JFrame frame,
3441
final Status status,
3542
final List<Code> guesses,
3643
final List<Response> responses) {
44+
// Call the superclass constructor to initialize the frame
3745
super(frame);
3846

47+
// Log the creation of the SecretCodePrompt scene
3948
Log.info("Creating CorrectCodePrompt scene");
4049

50+
// Initialize the status, guesses, and responses fields
4151
this.status = status;
4252
this.guesses = guesses;
4353
this.responses = responses;
4454

55+
// Create and configure a label to prompt the user to enter the correct code
4556
final JLabel promptLabel = new JLabel("Please enter the correct code:");
4657
promptLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
4758
frame.add(promptLabel);
4859

60+
// Draw the code panel and add it to the frame
4961
final JPanel codePanel = drawCodePanel();
5062
frame.add(codePanel);
5163

64+
// Draw the prompt panel and add it to the frame
5265
final JPanel promptPanel = drawPromptPanel(codePanel);
5366
frame.add(promptPanel);
5467

68+
// Draw the proceed button and add it to the frame
5569
final JButton proceedButton = drawProceedButton();
5670
frame.add(proceedButton);
5771

72+
// Draw and register the home button
5873
HomeButton.drawHomeButton(frame);
5974
HomeButton.registerHomeHandlers(frame);
6075

76+
// Draw and register the help button
6177
Help.drawHelpButton(frame);
6278
Help.registerHelpHandlers();
6379

80+
// Refresh the frame to display the updated components
6481
refreshFrame();
6582
}
6683

@@ -70,13 +87,16 @@ public SecretCodePrompt(final JFrame frame,
7087
* @return The code panel.
7188
*/
7289
private static JPanel drawCodePanel() {
90+
// Create a new JPanel with a FlowLayout
7391
final JPanel codePanel = new JPanel(new FlowLayout());
7492

93+
// Add CodeCircle components to the panel for each position in the code
7594
for (int i = 0; i < Mastermind.CODE_LENGTH; ++i) {
7695
final CodeCircle codeCircle = new CodeCircle(Color.lightGray);
7796
codePanel.add(codeCircle);
7897
}
7998

99+
// Return the configured code panel
80100
return codePanel;
81101
}
82102

@@ -87,34 +107,43 @@ private static JPanel drawCodePanel() {
87107
* @return The prompt panel.
88108
*/
89109
private JPanel drawPromptPanel(final JPanel codePanel) {
110+
// Create a new CodeInput instance
90111
final CodeInput codeInput = new CodeInput();
91-
final JPanel buttonsPanel = codeInput.drawButtons();
92112

113+
// Draw the buttons for the code input and wrap them in a panel
114+
final JPanel buttonsPanel = codeInput.drawButtons();
93115
final JPanel buttonsPanelWrapper = new JPanel(new BorderLayout());
94116
buttonsPanelWrapper.add(buttonsPanel, BorderLayout.PAGE_START);
95117

118+
// Add an action listener to handle code input events
96119
codeInput.addActionListener(codeColorIndices -> {
120+
// Map the indices to Code.Color values
97121
final List<Code.Color> codeColors = codeColorIndices
98122
.stream()
99123
.map(Code.Color::fromIndex)
100124
.toList();
101125

126+
// If the code has the correct length, create a new Code instance
102127
if (codeColors.size() == Mastermind.CODE_LENGTH) {
103128
this.secretCode = new Code(codeColors);
104129
}
105130

131+
// Map the Code.Color values to AWT Color values
106132
final List<Color> codeAWTColors = codeColors
107133
.stream()
108134
.map(GameBoard.CODE_COLOR_TO_AWT_COLOR::get)
109135
.collect(Collectors.toList());
110136

137+
// Fill the remaining positions with light gray color if needed
111138
while (codeAWTColors.size() < Mastermind.CODE_LENGTH) {
112139
codeAWTColors.add(Color.lightGray);
113140
}
114141

142+
// Draw the guess on the code panel
115143
GameBoard.drawGuess(codePanel, codeAWTColors);
116144
});
117145

146+
// Return the configured prompt panel
118147
return buttonsPanelWrapper;
119148
}
120149

@@ -125,10 +154,13 @@ private JPanel drawPromptPanel(final JPanel codePanel) {
125154
* @return The proceed button.
126155
*/
127156
private JButton drawProceedButton() {
157+
// Create a new JButton with the label "Proceed"
128158
final JButton proceedButton = new JButton("Proceed");
129159
proceedButton.setAlignmentX(Component.CENTER_ALIGNMENT);
130160

161+
// Add an action listener to handle button click events
131162
proceedButton.addActionListener(e -> {
163+
// If the secret code is not set, show a warning message
132164
if (secretCode == null) {
133165
Log.warning("Trying to proceed with invalid secret code");
134166

@@ -140,10 +172,12 @@ private JButton drawProceedButton() {
140172
return;
141173
}
142174

175+
// Log the entered secret code and proceed to the next scene
143176
Log.info("Secret code entered: " + secretCode);
144177
new CodeBreakerResult(frame, status, secretCode, guesses, responses);
145178
});
146179

180+
// Return the configured proceed button
147181
return proceedButton;
148182
}
149183
}

0 commit comments

Comments
 (0)