-
Notifications
You must be signed in to change notification settings - Fork 0
/
HangmanFrameParent.java
232 lines (196 loc) · 7.35 KB
/
HangmanFrameParent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
/**********************************************************************************************
* Program: Parent
*
**********************************************************************************************/
@SuppressWarnings("serial")
public class HangmanFrameParent extends JFrame {
protected static final int FRAME_WIDTH = 1000;
protected static final int FRAME_HEIGHT = 400;
protected static String input;
protected static String AIinput; //[Specific to AI]
protected static char guess;
protected HangmanLogic gameVariables;
protected ArrayList<String> listOfLetters;
protected JTextField letterField;
protected JButton submitButton;
protected JLabel wordLabel;
protected JLabel guessesRemainingLabel;
protected JLabel feedbackLabel;
protected HangmanComponent component;
protected JPanel gamePanel;
protected JPanel guessPanel; //field for entering a letter and submitting this as a guess; bottom
protected JPanel feedbackPanel; //special feedback messages
// file names of sounds
String clickSound = "click.wav"; // mouse click sound
String wrongSound = "wrong.wav"; // wrong letter sound
String rightSound = "right.wav"; // right letter sound
String winSound = "win.wav"; // game over winner sound
//constructor
public HangmanFrameParent(HangmanLogic gameVariables, ArrayList<String> listOfLetters) {
//set variables
this.gameVariables = gameVariables; //object of type HangmanLogic that was created by the driver gets passed here.
Collections.shuffle(listOfLetters);
this.listOfLetters = listOfLetters;
//use helper methods to create GUI components & attach them to frame
createLabelField();
createTextField();
createButton(" "); //[Specific to AI]
createHangingMan();
createPanel();
//set frame size & title
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setTitle("Hangman Game");
}
/* Method playGame()
* Tests whether game is over.
* Calls createButton which does all the work.
*/
public void playGame()
{
while(!gameVariables.isGameOver())
{
for (int i = 0; i < listOfLetters.size(); i++)
{
AIinput = listOfLetters.get(i); //[Specific to AI]
if (!gameVariables.isGameOver())
{
input = null; //resets local var input so that no infinite loop
while(input == null)
{
createButton(AIinput); // this method does all of the work
}
try { //wait between turns if it's an AI, rather than wait for button click //[Specific to AI]
Thread.sleep(2500);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
//Prepare to exit
letterField.setEditable(false);
wordLabel.setForeground (Color.blue);
wordLabel.setText("The correct word is : " + gameVariables.getKeyPhrase());
if(gameVariables.getNumGuessesLeft() <= 0)
{
feedbackLabel.setForeground (Color.red);
feedbackLabel.setText("I am sorry, that was your last guess, and you have lost the game.");
}
else
{
feedbackLabel.setForeground (Color.green);
feedbackLabel.setText("Congratulations! You have won the game!");
// opens an input stream to sound files
try {
InputStream in = new FileInputStream(winSound);
AudioStream as = new AudioStream(in);
AudioPlayer.player.start(as);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.exit(0); //TK THIS WASN"T IN ADAM" CODE
}
/* Method createLabelField()
* Generates JLabels for all the text printed to the GUI
* Calls createButton which does all the work.
*/
public void createLabelField() {
feedbackLabel = new JLabel("Welcome to hangman! The word you are trying to guess is shown below, with dashes " +
"representing unknown charaters. The word is " + gameVariables.getKeyPhrase().length() + " characters long");
wordLabel = new JLabel("The word so far: " + gameVariables.getKnownKeyPhrase() + " ");
wordLabel.setForeground(Color.blue);
guessesRemainingLabel = new JLabel("Guesses remaining: " + gameVariables.getNumGuessesLeft() + " ");
}
/* Method createTextField()
* Generates field where a letter will be entered.
*/
public void createTextField() {
final int FIELD_WIDTH = 1;
letterField = new JTextField(FIELD_WIDTH);
letterField.setText("");
}
/* Method createButton()
*/
@SuppressWarnings("deprecation")
protected void createButton(String AIinputvar) { //[Specific to AI]
submitButton = new JButton("Submit Letter"); //The "submit" button is not used in the AI version. It's there for show.
feedbackLabel.setText("Keep going!"); //reset feedback in case it displayed an error on last turn
submitButton.setEnabled(false);
letterField.setText(AIinputvar); //AI puts a guess into the letter field //[Specific to AI]
input = letterField.getText(); //get input
guess = input.charAt(0); //guess has now been confirmed at only one character
try //now for the meaningful part of checking the input character
{
boolean goodGuess = gameVariables.guessCharacter(guess);
if (goodGuess == false)
{
component.badGuessManager(); //updates the component every time there is a bad guess
component.repaint();//repaints the component part of the frame
}
}
catch (InvalidInputException e)
{
feedbackLabel.setText("The guess " + e.getMessage() + " is invalid, you are only allowed to guess letters in my version of this game, try again");
}
catch (AlreadyGuessedException e)
{
feedbackLabel.setText("You have already guessed " + e.getMessage() + ", try again");
}
guessesRemainingLabel.setText("Guesses remaining: " + gameVariables.getNumGuessesLeft() + " "); //updates the remaining guesses on the GUI
wordLabel.setForeground (Color.blue);
wordLabel.setText("The word so far: " + gameVariables.getKnownKeyPhrase() + " ");
}
/* Method createHangingMan()
* Creates object of type HangmanComponent
*/
public void createHangingMan() {
component = new HangmanComponent();
}
/* Method createPanel()
* Creates The panel that holds the user-interface components
*/
public void createPanel() {
gamePanel = new JPanel();
guessPanel = new JPanel();
feedbackPanel = new JPanel();
gamePanel.setLayout(new BorderLayout());
guessPanel.add(wordLabel);
guessPanel.add(guessesRemainingLabel);
guessPanel.add(letterField);
guessPanel.add(submitButton);
feedbackPanel.add(feedbackLabel);
gamePanel.add(feedbackPanel, BorderLayout.NORTH);
gamePanel.add(component, BorderLayout.CENTER);
gamePanel.add(guessPanel, BorderLayout.SOUTH);
add(gamePanel);
}
}