|
|
|
@ -8,7 +8,8 @@ import javafx.stage.*; |
|
|
|
import javafx.scene.control.*; |
|
|
|
import javafx.scene.input.*; |
|
|
|
|
|
|
|
public class Hangman extends Application { |
|
|
|
public class Hangman extends Application |
|
|
|
{ |
|
|
|
private Dictionary dict = new Dictionary(); |
|
|
|
private String secretWord; |
|
|
|
|
|
|
|
@ -20,7 +21,8 @@ public class Hangman extends Application { |
|
|
|
private boolean activeGame; |
|
|
|
|
|
|
|
@Override |
|
|
|
public void start(Stage primaryStage) { |
|
|
|
public void start(Stage primaryStage) |
|
|
|
{ |
|
|
|
primaryStage.setTitle("Hangman"); |
|
|
|
Font gameFont = Font.font("Consolas", 22.0); |
|
|
|
|
|
|
|
@ -35,30 +37,32 @@ public class Hangman extends Application { |
|
|
|
main.getChildren().add(resetButton); |
|
|
|
|
|
|
|
resetButton.setOnAction( |
|
|
|
e -> { |
|
|
|
Pane newPane = setUpLayout(statusBox); |
|
|
|
newPane.getChildren().add(resetButton); |
|
|
|
scene.setRoot(newPane); |
|
|
|
}); |
|
|
|
e -> { |
|
|
|
Pane newPane = setUpLayout(statusBox); |
|
|
|
newPane.getChildren().add(resetButton); |
|
|
|
scene.setRoot(newPane); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
scene.setOnKeyPressed( |
|
|
|
e -> { |
|
|
|
//System.out.println("Key pressed!!"); |
|
|
|
if (activeGame) { |
|
|
|
String guess = e.getText(); |
|
|
|
//System.out.println(guess); |
|
|
|
guess = guess.toLowerCase();//use a string method to convert guess to lowercase |
|
|
|
checkGuess(guess, statusBox); |
|
|
|
e -> { |
|
|
|
//System.out.println("Key pressed!!"); |
|
|
|
if (activeGame) |
|
|
|
{ |
|
|
|
String guess = e.getText(); |
|
|
|
//System.out.println(guess); |
|
|
|
guess = guess.toLowerCase();//use a string method to convert guess to lowercase |
|
|
|
checkGuess(guess, statusBox); |
|
|
|
|
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
primaryStage.setScene(scene); |
|
|
|
primaryStage.show(); |
|
|
|
} |
|
|
|
|
|
|
|
private Pane setUpLayout(Text statusBox) { |
|
|
|
private Pane setUpLayout(Text statusBox) |
|
|
|
{ |
|
|
|
Pane main = new Pane(); |
|
|
|
Font gameFont = Font.font("Consolas", 22.0); |
|
|
|
|
|
|
|
@ -77,7 +81,8 @@ public class Hangman extends Application { |
|
|
|
|
|
|
|
//create letterGuesses |
|
|
|
this.letterGuesses = new Text[26];// finish this line |
|
|
|
for (int i = 0; i < 26; i++) { |
|
|
|
for (int i = 0; i < 26; i++) |
|
|
|
{ |
|
|
|
String temp = String.valueOf(Dictionary.ALPHABET.charAt(i));//finish this line |
|
|
|
this.letterGuesses[i] = new Text(temp); |
|
|
|
letterGuesses[i].setFont(gameFont); |
|
|
|
@ -86,7 +91,8 @@ public class Hangman extends Application { |
|
|
|
|
|
|
|
//set up lettersRevealedSoFar |
|
|
|
this.lettersRevealedSoFar = new Text[this.secretWord.length()]; |
|
|
|
for (int i = 0; i < lettersRevealedSoFar.length; i++) { |
|
|
|
for (int i = 0; i < lettersRevealedSoFar.length; i++) |
|
|
|
{ |
|
|
|
this.lettersRevealedSoFar[i] = new Text("_"); |
|
|
|
this.lettersRevealedSoFar[i].setFont(gameFont); |
|
|
|
word.add(this.lettersRevealedSoFar[i], i, 0); |
|
|
|
@ -95,11 +101,12 @@ public class Hangman extends Application { |
|
|
|
//set up the gallows |
|
|
|
Pane figure = new Pane(); |
|
|
|
Shape[] gallows = { new Line(120, 40, 120, 10), |
|
|
|
new Line(120, 10, 40, 10), |
|
|
|
new Line(40, 10, 40, 250), |
|
|
|
new Rectangle(150, 7) }; |
|
|
|
new Line(120, 10, 40, 10), |
|
|
|
new Line(40, 10, 40, 250), |
|
|
|
new Rectangle(150, 7) }; |
|
|
|
gallows[3].relocate(20, 250); |
|
|
|
for (int i = 0; i < gallows.length; i++) { |
|
|
|
for (int i = 0; i < gallows.length; i++) |
|
|
|
{ |
|
|
|
gallows[i].setStroke(Color.BLACK); |
|
|
|
gallows[i].setStrokeWidth(2.0); |
|
|
|
figure.getChildren().add(gallows[i]); |
|
|
|
@ -114,7 +121,8 @@ public class Hangman extends Application { |
|
|
|
this.hangman[4] = new Line(120, 150, 100, 200); // left leg |
|
|
|
this.hangman[5] = new Line(120, 150, 140, 200); // right leg |
|
|
|
|
|
|
|
for (int i = 0; i < this.hangman.length; i++) { |
|
|
|
for (int i = 0; i < this.hangman.length; i++) |
|
|
|
{ |
|
|
|
this.hangman[i].setStroke(Color.WHITE); |
|
|
|
this.hangman[i].setStrokeWidth(3.0); |
|
|
|
figure.getChildren().add(this.hangman[i]); |
|
|
|
@ -139,77 +147,71 @@ public class Hangman extends Application { |
|
|
|
return main; |
|
|
|
} |
|
|
|
|
|
|
|
private void checkGuess(String s, Text statusBox) { |
|
|
|
private void checkGuess(String s, Text statusBox) |
|
|
|
{ |
|
|
|
int index = this.findLetter(s); |
|
|
|
|
|
|
|
if (!alreadyGuessed(index)) { |
|
|
|
if (!alreadyGuessed(index)) |
|
|
|
{ |
|
|
|
boolean found = foundInSecretWord(s); |
|
|
|
|
|
|
|
if (found) /* correct guess */ { |
|
|
|
if (found) { |
|
|
|
this.letterGuesses[index].setFill(Color.GREEN); |
|
|
|
checkIfWon(s, statusBox); |
|
|
|
} |
|
|
|
else /* incorrect guess */ { |
|
|
|
else { |
|
|
|
this.letterGuesses[index].setFill(Color.RED); |
|
|
|
checkIfLost(s, statusBox); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
private int findLetter(String s) { |
|
|
|
if (s.length() == 1) { |
|
|
|
private int findLetter(String s) |
|
|
|
{ |
|
|
|
if (s.length() == 1) |
|
|
|
{ |
|
|
|
return Dictionary.ALPHABET.indexOf(s); |
|
|
|
} |
|
|
|
else { |
|
|
|
else |
|
|
|
{ |
|
|
|
return -1; |
|
|
|
} |
|
|
|
/*boolean isLetter = (index != -1) && (s.length() == 1); |
|
|
|
//Determine if s contains a single letter of the alphabet, using String methods |
|
|
|
//Return the index of this letter in the array letterGuesses |
|
|
|
//e.g. "a" is stored in index 0, "b" in index 1, etc. |
|
|
|
//If the string is not a letter, return -1 |
|
|
|
int ret = -1; |
|
|
|
if (isLetter) { |
|
|
|
for (int i = 0; i < this.letterGuesses.length; i ++) { |
|
|
|
if (this.letterGuesses[i].getText() == s) { |
|
|
|
ret = i; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return ret;*/ |
|
|
|
|
|
|
|
//write your code for this method |
|
|
|
} |
|
|
|
private boolean alreadyGuessed(int index) { |
|
|
|
if (index >= 0) { |
|
|
|
private boolean alreadyGuessed(int index) |
|
|
|
{ |
|
|
|
if (index >= 0) |
|
|
|
{ |
|
|
|
Color currColor = (Color)this.letterGuesses[index].getFill(); |
|
|
|
if (currColor.equals(Color.BLACK)) { |
|
|
|
if (currColor.equals(Color.BLACK)) |
|
|
|
{ |
|
|
|
return false; |
|
|
|
} |
|
|
|
else { |
|
|
|
else |
|
|
|
{ |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
private boolean foundInSecretWord(String s) { |
|
|
|
private boolean foundInSecretWord(String s) |
|
|
|
{ |
|
|
|
//Determine if s is a correct or incorrect guess |
|
|
|
//Return true if s corresponds to a letter in the secret word, and return false otherwise |
|
|
|
|
|
|
|
|
|
|
|
//write your code for this method |
|
|
|
boolean inSecretWord = this.secretWord.indexOf(s) != -1; |
|
|
|
/* boolean guessed = false; |
|
|
|
int i = 0; |
|
|
|
while (!guessed && i < this.lettersRevealedSoFar.length) { |
|
|
|
guessed = this.lettersRevealedSoFar[i].getText() == s; |
|
|
|
i += 1; |
|
|
|
} */ |
|
|
|
|
|
|
|
return inSecretWord;// && !guessed; |
|
|
|
} |
|
|
|
private void checkIfWon(String s, Text statusBox) { |
|
|
|
private void checkIfWon(String s, Text statusBox) |
|
|
|
{ |
|
|
|
int i = 0; |
|
|
|
char let = s.charAt(0); |
|
|
|
for (i = 0; i < this.lettersRevealedSoFar.length; i ++) { |
|
|
|
if (this.secretWord.charAt(i) == let) { |
|
|
|
for (i = 0; i < this.lettersRevealedSoFar.length; i ++) |
|
|
|
{ |
|
|
|
if (this.secretWord.charAt(i) == let) |
|
|
|
{ |
|
|
|
this.lettersRevealedSoFar[i].setText(s); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -217,32 +219,38 @@ public class Hangman extends Application { |
|
|
|
boolean won = true; |
|
|
|
i = 0; |
|
|
|
String letter; |
|
|
|
while (won && i < this.lettersRevealedSoFar.length) { |
|
|
|
while (won && i < this.lettersRevealedSoFar.length) |
|
|
|
{ |
|
|
|
letter = lettersRevealedSoFar[i].getText(); |
|
|
|
won = letter != "_"; |
|
|
|
//System.out.println("\"" + letter + "\"" + "," + won); |
|
|
|
i += 1; |
|
|
|
} |
|
|
|
if (won) { |
|
|
|
if (won) |
|
|
|
{ |
|
|
|
statusBox.setText("YOU WIN!!!"); |
|
|
|
statusBox.setFill(Color.GREEN); |
|
|
|
this.activeGame = false; |
|
|
|
} |
|
|
|
else { |
|
|
|
else |
|
|
|
{ |
|
|
|
statusBox.setText("Letter " + s + " is \ncorrect!"); |
|
|
|
statusBox.setFill(Color.FORESTGREEN); |
|
|
|
} |
|
|
|
} |
|
|
|
private void checkIfLost(String s, Text statusBox) { |
|
|
|
if (this.currentHangmanPart < 6) { |
|
|
|
private void checkIfLost(String s, Text statusBox) |
|
|
|
{ |
|
|
|
if (this.currentHangmanPart < 6) |
|
|
|
{ |
|
|
|
this.hangman[this.currentHangmanPart].setStroke(Color.BLACK); |
|
|
|
this.currentHangmanPart++; |
|
|
|
|
|
|
|
statusBox.setText("Letter " + s + " is \nnot correct."); |
|
|
|
statusBox.setFill(Color.FIREBRICK); |
|
|
|
} |
|
|
|
else { |
|
|
|
for (int i = 0; i < this.lettersRevealedSoFar.length; i++) { |
|
|
|
else |
|
|
|
{ |
|
|
|
for (int i = 0; i < this.lettersRevealedSoFar.length; i++) |
|
|
|
{ |
|
|
|
this.lettersRevealedSoFar[i].setText(this.secretWord.charAt(i)+""); |
|
|
|
this.lettersRevealedSoFar[i].setFill(Color.FIREBRICK); |
|
|
|
} |
|
|
|
@ -252,7 +260,8 @@ public class Hangman extends Application { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
public static void main(String[] args) |
|
|
|
{ |
|
|
|
launch(args); |
|
|
|
} |
|
|
|
} |