16
16
* Represents a scene where the user is prompted to enter the correct code.
17
17
*/
18
18
public class SecretCodePrompt extends Scene {
19
+ // Variable to store the secret code entered by the user
19
20
private Code secretCode = null ;
21
+
22
+ // The current game status
20
23
private final Status status ;
24
+
25
+ // List of guesses made by the code breaker
21
26
private final List <Code > guesses ;
27
+
28
+ // List of responses corresponding to each guess
22
29
private final List <Response > responses ;
23
30
24
31
/**
@@ -34,33 +41,43 @@ public SecretCodePrompt(final JFrame frame,
34
41
final Status status ,
35
42
final List <Code > guesses ,
36
43
final List <Response > responses ) {
44
+ // Call the superclass constructor to initialize the frame
37
45
super (frame );
38
46
47
+ // Log the creation of the SecretCodePrompt scene
39
48
Log .info ("Creating CorrectCodePrompt scene" );
40
49
50
+ // Initialize the status, guesses, and responses fields
41
51
this .status = status ;
42
52
this .guesses = guesses ;
43
53
this .responses = responses ;
44
54
55
+ // Create and configure a label to prompt the user to enter the correct code
45
56
final JLabel promptLabel = new JLabel ("Please enter the correct code:" );
46
57
promptLabel .setAlignmentX (Component .CENTER_ALIGNMENT );
47
58
frame .add (promptLabel );
48
59
60
+ // Draw the code panel and add it to the frame
49
61
final JPanel codePanel = drawCodePanel ();
50
62
frame .add (codePanel );
51
63
64
+ // Draw the prompt panel and add it to the frame
52
65
final JPanel promptPanel = drawPromptPanel (codePanel );
53
66
frame .add (promptPanel );
54
67
68
+ // Draw the proceed button and add it to the frame
55
69
final JButton proceedButton = drawProceedButton ();
56
70
frame .add (proceedButton );
57
71
72
+ // Draw and register the home button
58
73
HomeButton .drawHomeButton (frame );
59
74
HomeButton .registerHomeHandlers (frame );
60
75
76
+ // Draw and register the help button
61
77
Help .drawHelpButton (frame );
62
78
Help .registerHelpHandlers ();
63
79
80
+ // Refresh the frame to display the updated components
64
81
refreshFrame ();
65
82
}
66
83
@@ -70,13 +87,16 @@ public SecretCodePrompt(final JFrame frame,
70
87
* @return The code panel.
71
88
*/
72
89
private static JPanel drawCodePanel () {
90
+ // Create a new JPanel with a FlowLayout
73
91
final JPanel codePanel = new JPanel (new FlowLayout ());
74
92
93
+ // Add CodeCircle components to the panel for each position in the code
75
94
for (int i = 0 ; i < Mastermind .CODE_LENGTH ; ++i ) {
76
95
final CodeCircle codeCircle = new CodeCircle (Color .lightGray );
77
96
codePanel .add (codeCircle );
78
97
}
79
98
99
+ // Return the configured code panel
80
100
return codePanel ;
81
101
}
82
102
@@ -87,34 +107,43 @@ private static JPanel drawCodePanel() {
87
107
* @return The prompt panel.
88
108
*/
89
109
private JPanel drawPromptPanel (final JPanel codePanel ) {
110
+ // Create a new CodeInput instance
90
111
final CodeInput codeInput = new CodeInput ();
91
- final JPanel buttonsPanel = codeInput .drawButtons ();
92
112
113
+ // Draw the buttons for the code input and wrap them in a panel
114
+ final JPanel buttonsPanel = codeInput .drawButtons ();
93
115
final JPanel buttonsPanelWrapper = new JPanel (new BorderLayout ());
94
116
buttonsPanelWrapper .add (buttonsPanel , BorderLayout .PAGE_START );
95
117
118
+ // Add an action listener to handle code input events
96
119
codeInput .addActionListener (codeColorIndices -> {
120
+ // Map the indices to Code.Color values
97
121
final List <Code .Color > codeColors = codeColorIndices
98
122
.stream ()
99
123
.map (Code .Color ::fromIndex )
100
124
.toList ();
101
125
126
+ // If the code has the correct length, create a new Code instance
102
127
if (codeColors .size () == Mastermind .CODE_LENGTH ) {
103
128
this .secretCode = new Code (codeColors );
104
129
}
105
130
131
+ // Map the Code.Color values to AWT Color values
106
132
final List <Color > codeAWTColors = codeColors
107
133
.stream ()
108
134
.map (GameBoard .CODE_COLOR_TO_AWT_COLOR ::get )
109
135
.collect (Collectors .toList ());
110
136
137
+ // Fill the remaining positions with light gray color if needed
111
138
while (codeAWTColors .size () < Mastermind .CODE_LENGTH ) {
112
139
codeAWTColors .add (Color .lightGray );
113
140
}
114
141
142
+ // Draw the guess on the code panel
115
143
GameBoard .drawGuess (codePanel , codeAWTColors );
116
144
});
117
145
146
+ // Return the configured prompt panel
118
147
return buttonsPanelWrapper ;
119
148
}
120
149
@@ -125,10 +154,13 @@ private JPanel drawPromptPanel(final JPanel codePanel) {
125
154
* @return The proceed button.
126
155
*/
127
156
private JButton drawProceedButton () {
157
+ // Create a new JButton with the label "Proceed"
128
158
final JButton proceedButton = new JButton ("Proceed" );
129
159
proceedButton .setAlignmentX (Component .CENTER_ALIGNMENT );
130
160
161
+ // Add an action listener to handle button click events
131
162
proceedButton .addActionListener (e -> {
163
+ // If the secret code is not set, show a warning message
132
164
if (secretCode == null ) {
133
165
Log .warning ("Trying to proceed with invalid secret code" );
134
166
@@ -140,10 +172,12 @@ private JButton drawProceedButton() {
140
172
return ;
141
173
}
142
174
175
+ // Log the entered secret code and proceed to the next scene
143
176
Log .info ("Secret code entered: " + secretCode );
144
177
new CodeBreakerResult (frame , status , secretCode , guesses , responses );
145
178
});
146
179
180
+ // Return the configured proceed button
147
181
return proceedButton ;
148
182
}
149
183
}
0 commit comments