Browse Source

Thing runs but is wrong

master
school 7 years ago
parent
commit
190abfcba2
  1. 122
      Hangman.java

122
Hangman.java

@ -8,8 +8,7 @@ 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,8 +19,7 @@ public class Hangman extends Application
private int currentHangmanPart;
private boolean activeGame;
public void start(Stage primaryStage)
{
public void start(Stage primaryStage) {
primaryStage.setTitle("Hangman");
Font gameFont = Font.font("Consolas", 22.0);
@ -35,18 +33,19 @@ public class Hangman extends Application
resetButton.relocate(25,350);
main.getChildren().add(resetButton);
resetButton.setOnAction( e -> {
resetButton.setOnAction(
e -> {
Pane newPane = setUpLayout(statusBox);
newPane.getChildren().add(resetButton);
scene.setRoot(newPane);
});
scene.setOnKeyPressed( e -> {
if(activeGame)
{
scene.setOnKeyPressed(
e -> {
if (activeGame) {
String guess = e.getText();
guess = //use a string method to convert guess to lowercase
checkGuess(/*what object should be passed here?*/, statusBox);
guess = guess.toLowerCase();//use a string method to convert guess to lowercase
checkGuess(guess, statusBox);
}
});
@ -54,8 +53,7 @@ public class Hangman extends Application
primaryStage.show();
}
private Pane setUpLayout(Text statusBox)
{
private Pane setUpLayout(Text statusBox) {
Pane main = new Pane();
Font gameFont = Font.font("Consolas", 22.0);
@ -73,10 +71,9 @@ public class Hangman extends Application
lettersPane.setVgap(5);
//create letterGuesses
this.letterGuesses = // finish this line
for(int i = 0; i < 26; i++)
{
String temp = //finish this line
this.letterGuesses = new Text[26];// finish this line
for (int i = 0; i < 26; i++) {
String temp = "l";//finish this line
this.letterGuesses[i] = new Text(temp);
letterGuesses[i].setFont(gameFont);
lettersPane.add(letterGuesses[i], i % 13, i / 13);
@ -84,8 +81,7 @@ 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);
@ -98,8 +94,7 @@ public class Hangman extends Application
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,8 +109,7 @@ 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]);
@ -140,90 +134,95 @@ 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) /* correct guess */ {
this.letterGuesses[index].setFill(Color.GREEN);
checkIfWon(s, statusBox);
}
else // incorrect guess
{
else /* incorrect guess */ {
this.letterGuesses[index].setFill(Color.RED);
checkIfLost(s, statusBox);
}
}
}
private int findLetter(String s)
{
private int findLetter(String s) {
int index = Dictionary.ALPHABET.indexOf(s);
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) {
//write your code here
if(won)
{
boolean won = true;
int i = 0;
while (won && i < this.lettersRevealedSoFar.length) {
won = lettersRevealedSoFar[i].getText() != "_";
i += 1;
}
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);
}
@ -233,8 +232,7 @@ public class Hangman extends Application
}
}
public static void main(String[] args)
{
public static void main(String[] args) {
launch(args);
}
}
Loading…
Cancel
Save