Introduction
Hangman is a paper and pencil guessing game for two or more players. One player thinks of a word and the other tries to guess it by suggesting letters or numbers within a certain number of guesses. The word to guess is represented by a row of dashes, representing each letter of the word. If the guessing player suggests a letter which occurs in the word, the other player writes it in all its correct positions. If the suggested letter or number does not occur in the word, then the player loses the chance.
If the player makes enough incorrect guesses such that he/she loses all his/her chances, then the game is over. I have developed a hangman in JavaFX. You can see the source code below.
-
-
-
-
-
- package hangman;
- import java.util.Arrays;
- import java.util.Stack;
- import javafx.application.Application;
- import javafx.event.ActionEvent;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.scene.control.Alert;
- import javafx.scene.control.Alert.AlertType;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.TextField;
- import javafx.scene.image.Image;
- import javafx.scene.image.ImageView;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Line;
- import javafx.scene.text.Font;
- import javafx.stage.Stage;
-
-
-
-
- public class Hangman extends Application {
-
- private Image gameLogo, gameImage, playBtnImg, exitBtnImg;
- private ImageView iv, iv2;
- private Button playBtn, exitBtn, startGameBtn, restartGameBtn;
- private Label label1, winLabel, looseLabel, chances, label2;
- private String word = "";
- private TextField secretWord;
- private Button[] alphabet;
- private Font font, font2;
- private final char alph = 'A';
- private Line[] dashes;
- private Group root;
- private int total_no_of_chances = 8;
- private final Stack lineStack = new Stack();
- private boolean gameOver = false, gameStart = false;
- int count = 0;
- @Override
- public void start(Stage primaryStage) {
-
- font = new Font("Poor Richard", 22);
- font2 = new Font("Poor Richard", 40);
-
- gameLogo = new Image(Hangman.class.getClassLoader().getResource("Images/logo.png").toString());
-
- iv = new ImageView();
- iv.setImage(gameLogo);
- iv.setLayoutX(15);
- iv.setLayoutY(15);
- gameImage = new Image(Hangman.class.getClassLoader().getResource("Images/hanging.gif-c200").toString());
- iv2 = new ImageView(gameImage);
- iv2.setLayoutX(270);
- iv2.setLayoutY(290);
- iv2.setFitHeight(200);
- iv2.setFitWidth(200);
- playBtnImg = new Image(Hangman.class.getClassLoader().getResource("Images/playbutton.png").toString());
- playBtn = new Button("", new ImageView(playBtnImg));
- playBtn.setLayoutX(50);
- playBtn.setLayoutY(270);
- restartGameBtn = new Button("Restart Game");
- restartGameBtn.setFont(font);
- restartGameBtn.setLayoutX(220);
- restartGameBtn.setLayoutY(270);
- restartGameBtn.setVisible(false);
- exitBtnImg = new Image(Hangman.class.getClassLoader().getResource("Images/exitbutton.png").toString());
- exitBtn = new Button("", new ImageView(exitBtnImg));
- exitBtn.setLayoutX(50);
- exitBtn.setLayoutY(360);
- label1 = new Label("Enter the word while the other player turn around");
- label1.setFont(font);
- label1.setLayoutX(50);
- label1.setLayoutY(100);
- label1.setVisible(false);
- winLabel = new Label("You Win");
- winLabel.setFont(font2);
- winLabel.setLayoutX(230);
- winLabel.setLayoutY(100);
- winLabel.setVisible(false);
- looseLabel = new Label("You Loose");
- looseLabel.setFont(font2);
- looseLabel.setLayoutX(220);
- looseLabel.setLayoutY(100);
- looseLabel.setVisible(false);
- chances = new Label("Chances Left::" + total_no_of_chances);
- chances.setFont(font2);
- chances.setLayoutX(350);
- chances.setLayoutY(400);
- chances.setVisible(false);
- label2 = new Label("Guess The Word");
- label2.setFont(font2);
- label2.setLayoutX(220);
- label2.setLayoutY(150);
- label2.setVisible(false);
- secretWord = new TextField();
- secretWord.setFont(font);
- secretWord.setPrefWidth(350);
- secretWord.setLayoutX(80);
- secretWord.setLayoutY(150);
- secretWord.setVisible(false);
- startGameBtn = new Button("", new ImageView(new Image(Hangman.class.getClassLoader().getResource("Images/startbutton.png").toString())));
- startGameBtn.setLayoutX(120);
- startGameBtn.setLayoutY(200);
- startGameBtn.setVisible(false);
- alphabet = new Button[26];
- for (int i = 0, j = 0; i < alphabet.length; i++) {
- alphabet[i] = new Button("" + (char)(alph + i));
- alphabet[i].setFont(font);
- alphabet[i].setVisible(false);
- if (i <= 13) {
- alphabet[i].setLayoutX(20 + i * 40);
- alphabet[i].setLayoutY(10);
- } else {
- alphabet[i].setLayoutX(20 + j * 40);
- alphabet[i].setLayoutY(50);
- j++;
- }
- }
- dashes = new Line[100];
- playBtn.setOnAction((ActionEvent ae) -> {
- ScreenReset();
- Scene2();
- });
- exitBtn.setOnAction((ActionEvent ae) -> {
- System.exit(0);
- });
- startGameBtn.setOnAction((e) -> HandleButton(e));
- restartGameBtn.setOnAction((e) -> HandleButton(e));
- for (Button alphabet1: alphabet) {
- alphabet1.setOnAction((e) -> HandleButton(e));
- }
-
- root = new Group();
- root.getChildren().add(iv);
- root.getChildren().add(iv2);
- root.getChildren().add(playBtn);
- root.getChildren().add(exitBtn);
- root.getChildren().add(label1);
- root.getChildren().add(secretWord);
- root.getChildren().add(startGameBtn);
- root.getChildren().addAll(Arrays.asList(alphabet));
- root.getChildren().add(winLabel);
- root.getChildren().add(restartGameBtn);
- root.getChildren().add(looseLabel);
- root.getChildren().add(chances);
- root.getChildren().add(label2);
- Scene scene = new Scene(root, 620, 500);
- scene.setFill(Color.LIGHTGREEN);
- primaryStage.setTitle("Hangman");
- primaryStage.setScene(scene);
- primaryStage.setResizable(false);
- primaryStage.show();
- }
-
- private void ResetGame() {
- secretWord.clear();
- word = "";
- for (Button b: alphabet) b.setDisable(false);
- total_no_of_chances = 8;
- gameOver = false;
- count = 0;
- }
-
- private void ScreenReset() {
- iv.setVisible(false);
- iv2.setVisible(false);
- playBtn.setVisible(false);
- exitBtn.setVisible(false);
- label1.setVisible(false);
- secretWord.setVisible(false);
- startGameBtn.setVisible(false);
- for (Button alphabet1: alphabet) {
- alphabet1.setVisible(false);
- }
- restartGameBtn.setVisible(false);
- winLabel.setVisible(false);
- looseLabel.setVisible(false);
- chances.setVisible(false);
- label2.setVisible(false);
- }
-
- private void Scene2() {
- label1.setVisible(true);
- secretWord.setVisible(true);
- startGameBtn.setVisible(true);
- }
-
- private void Scene3() {
- for (Button alphabet1: alphabet) {
- alphabet1.setVisible(true);
- }
- for (int i = 0; i < word.length(); i++) {
- dashes[i] = new Line(10 + (i * 30 + 10), 250, (10 + (i + 1) * 30), 250);
- }
- for (int i = 0; i < word.length(); i++) {
- root.getChildren().add(dashes[i]);
- }
- chances.setVisible(true);
- label2.setVisible(true);
- }
-
- private void Scene4() {
- winLabel.setVisible(true);
- restartGameBtn.setVisible(true);
- }
-
- private void Scene5() {
- looseLabel.setVisible(true);
- restartGameBtn.setVisible(true);
- }
-
- private boolean IsValid(String w) {
- for (int i = 0; i < w.length(); i++) {
- if (!((w.charAt(i) >= 'A' && w.charAt(i) <= 'Z') || (w.charAt(i) >= 'a' && w.charAt(i) <= 'z'))) return false;
- }
- return true;
- }
-
- private void HandleButton(ActionEvent ae) {
- int letterPos = -1;
- Label l;
- int letterOcc;
- if (ae.getSource() == startGameBtn) {
- if (secretWord.getText().length() == 0 || !IsValid(secretWord.getText())) {
- Alert msg = new Alert(AlertType.ERROR);
- msg.setTitle("Error Message from Hangman");
- msg.setContentText("Please enter the Secret Word");
- msg.showAndWait();
- } else {
- word = secretWord.getText().toLowerCase();
- ScreenReset();
- Scene3();
- gameStart = true;
- }
- }
- if (gameStart) {
- if (ae.getSource() == alphabet[0]) {
- if (word.contains("a")) {
- letterOcc = LetterOccurenceTime(word, 'a');
- for (int i = 0; i < letterOcc; i++) {
- letterPos = LetterPos(word, 'a', letterPos + 1);
- l = new Label("A");
- l.setFont(font);
- l.setLayoutX(dashes[letterPos].getStartX());
- l.setLayoutY(dashes[letterPos].getStartY() - 30);
- root.getChildren().add(l);
- count++;
- lineStack.push(l);
- }
- alphabet[0].setDisable(true);
- letterPos = -1;
- } else {
- total_no_of_chances--;
- }
- }
- if (ae.getSource() == alphabet[1]) {
- if (word.contains("b")) {
- letterOcc = LetterOccurenceTime(word, 'b');
- for (int i = 0; i < letterOcc; i++) {
- letterPos = LetterPos(word, 'b', letterPos + 1);
- l = new Label("B");
- l.setFont(font);
- l.setLayoutX(dashes[letterPos].getStartX());
- l.setLayoutY(dashes[letterPos].getStartY() - 30);
- root.getChildren().add(l);
- count++;
- lineStack.push(l);
- }
- alphabet[1].setDisable(true);
- letterPos = -1;
- } else {
- total_no_of_chances--;
- }
- }
- if (ae.getSource() == alphabet[2]) {
- if (word.contains("c")) {
- letterOcc = LetterOccurenceTime(word, 'c');
- for (int i = 0; i < letterOcc; i++) {
- letterPos = LetterPos(word, 'c', letterPos + 1);
- l = new Label("C");
- l.setFont(font);
- l.setLayoutX(dashes[letterPos].getStartX());
- l.setLayoutY(dashes[letterPos].getStartY() - 30);
- root.getChildren().add(l);
- count++;
- lineStack.push(l);
- }
- alphabet[2].setDisable(true);
- letterPos = -1;
- } else {
- total_no_of_chances--;
- }
- }
- if (ae.getSource() == alphabet[3]) {
- if (word.contains("d")) {
- letterOcc = LetterOccurenceTime(word, 'd');
- for (int i = 0; i < letterOcc; i++) {
- letterPos = LetterPos(word, 'd', letterPos + 1);
- l = new Label("D");
- l.setFont(font);
- l.setLayoutX(dashes[letterPos].getStartX());
- l.setLayoutY(dashes[letterPos].getStartY() - 30);
- root.getChildren().add(l);
- count++;
- lineStack.push(l);
- }
- alphabet[3].setDisable(true);
- letterPos = -1;
- } else {
- total_no_of_chances--;
- }
- }
- if (ae.getSource() == alphabet[4]) {
- if (word.contains("e")) {
- letterOcc = LetterOccurenceTime(word, 'e');
- for (int i = 0; i < letterOcc; i++) {
- letterPos = LetterPos(word, 'e', letterPos + 1);
- l = new Label("E");
- l.setFont(font);
- l.setLayoutX(dashes[letterPos].getStartX());
- l.setLayoutY(dashes[letterPos].getStartY() - 30);
- root.getChildren().add(l);
- count++;
- lineStack.push(l);
- }
- alphabet[4].setDisable(true);
- letterPos = -1;
- } else {
- total_no_of_chances--;
- }
- }
- if (ae.getSource() == alphabet[5]) {
- if (word.contains("f")) {
- letterOcc = LetterOccurenceTime(word, 'f');
- for (int i = 0; i < letterOcc; i++) {
- letterPos = LetterPos(word, 'f', letterPos + 1);
- l = new Label("F");
- l.setFont(font);
- l.setLayoutX(dashes[letterPos].getStartX());
- l.setLayoutY(dashes[letterPos].getStartY() - 30);
- root.getChildren().add(l);
- count++;
- lineStack.push(l);
- }
- alphabet[5].setDisable(true);
- letterPos = -1;
- } else {
- total_no_of_chances--;
- }
- }
- if (ae.getSource() == alphabet[6]) {
- if (word.contains("g")) {
- letterOcc = LetterOccurenceTime(word, 'g');
- for (int i = 0; i < letterOcc; i++) {
- letterPos = LetterPos(word, 'g', letterPos + 1);
- l = new Label("G");
- l.setFont(font);
- l.setLayoutX(dashes[letterPos].getStartX());
- l.setLayoutY(dashes[letterPos].getStartY() - 30);
- root.getChildren().add(l);
- count++;
- lineStack.push(l);
- }
- alphabet[6].setDisable(true);
- letterPos = -1;
- } else {
- total_no_of_chances--;
- }
- }
- if (ae.getSource() == alphabet[7]) {
- if (word.contains("h")) {
- letterOcc = LetterOccurenceTime(word, 'h');
- for (int i = 0; i < letterOcc; i++) {
- letterPos = LetterPos(word, 'h', letterPos + 1);
- l = new Label("H");
- l.setFont(font);
- l.setLayoutX(dashes[letterPos].getStartX());
- l.setLayoutY(dashes[letterPos].getStartY() - 30);
- root.getChildren().add(l);
- count++;
- lineStack.push(l);
- }
- alphabet[7].setDisable(true);
- letterPos = -1;
- } else {
- total_no_of_chances--;
- }
- }
- if (ae.getSource() == alphabet[8]) {
- if (word.contains("i")) {
- letterOcc = LetterOccurenceTime(word, 'i');
- for (int i = 0; i < letterOcc; i++) {
- letterPos = LetterPos(word, 'i', letterPos + 1);
- l = new Label("I");
- l.setFont(font);
- l.setLayoutX(dashes[letterPos].getStartX());
- l.setLayoutY(dashes[letterPos].getStartY() - 30);
- root.getChildren().add(l);
- count++;
- lineStack.push(l);
- }
- alphabet[8].setDisable(true);
- letterPos = -1;
- } else {
- total_no_of_chances--;
- }
- }
- if (ae.getSource() == alphabet[9]) {
- if (word.contains("j")) {
- letterOcc = LetterOccurenceTime(word, 'j');
- for (int i = 0; i < letterOcc; i++) {
- letterPos = LetterPos(word, 'j', letterPos + 1);
- l = new Label("J");
- l.setFont(font);
- l.setLayoutX(dashes[letterPos].getStartX());
- l.setLayoutY(dashes[letterPos].getStartY() - 30);
- root.getChildren().add(l);
- count++;
- lineStack.push(l);
- }
- alphabet[9].setDisable(true);
- letterPos = -1;
- } else {
- total_no_of_chances--;
- }
- }
- if (ae.getSource() == alphabet[10]) {
- if (word.contains("k")) {
- letterOcc = LetterOccurenceTime(word, 'k');
- for (int i = 0; i < letterOcc; i++) {
- letterPos = LetterPos(word, 'k', letterPos + 1);
- l = new Label("K");
- l.setFont(font);
- l.setLayoutX(dashes[letterPos].getStartX());
- l.setLayoutY(dashes[letterPos].getStartY() - 30);
- root.getChildren().add(l);
- count++;
- lineStack.push(l);
- }
- alphabet[10].setDisable(true);
- letterPos = -1;
- } else {
- total_no_of_chances--;
- }
- }
- if (ae.getSource() == alphabet[11]) {
- if (word.contains("l")) {
- letterOcc = LetterOccurenceTime(word, 'l');
- for (int i = 0; i < letterOcc; i++) {
- letterPos = LetterPos(word, 'l', letterPos + 1);
- l = new Label("L");
- l.setFont(font);
- l.setLayoutX(dashes[letterPos].getStartX());
- l.setLayoutY(dashes[letterPos].getStartY() - 30);
- root.getChildren().add(l);
- count++;
- lineStack.push(l);
- }
- alphabet[11].setDisable(true);
- letterPos = -1;
- } else {
- total_no_of_chances--;
- }
- }
- if (ae.getSource() == alphabet[12]) {
- if (word.contains("m")) {
- letterOcc = LetterOccurenceTime(word, 'm');
- for (int i = 0; i < letterOcc; i++) {
- letterPos = LetterPos(word, 'm', letterPos + 1);
- l = new Label("M");
- l.setFont(font);
- l.setLayoutX(dashes[letterPos].getStartX());
- l.setLayoutY(dashes[letterPos].getStartY() - 30);
- root.getChildren().add(l);
- count++;
- lineStack.push(l);
- }
- alphabet[12].setDisable(true);
- letterPos = -1;
- } else {
- total_no_of_chances--;
- }
- }
- if (ae.getSource() == alphabet[13]) {
- if (word.contains("n")) {
- letterOcc = LetterOccurenceTime(word, 'n');
- for (int i = 0; i < letterOcc; i++) {
- letterPos = LetterPos(word, 'n', letterPos + 1);
- l = new Label("N");
- l.setFont(font);
- l.setLayoutX(dashes[letterPos].getStartX());
- l.setLayoutY(dashes[letterPos].getStartY() - 30);
- root.getChildren().add(l);
- count++;
- lineStack.push(l);
- }
- alphabet[13].setDisable(true);
- letterPos = -1;
- } else {
- total_no_of_chances--;
- }
- }
- if (ae.getSource() == alphabet[14]) {
- if (word.contains("o")) {
- letterOcc = LetterOccurenceTime(word, 'o');
- for (int i = 0; i < letterOcc; i++) {
- letterPos = LetterPos(word, 'o', letterPos + 1);
- l = new Label("O");
- l.setFont(font);
- l.setLayoutX(dashes[letterPos].getStartX());
- l.setLayoutY(dashes[letterPos].getStartY() - 30);
- root.getChildren().add(l);
- count++;
- lineStack.push(l);
- }
- alphabet[14].setDisable(true);
- letterPos = -1;
- } else {
- total_no_of_chances--;
- }
- }
- if (ae.getSource() == alphabet[15]) {
- if (word.contains("p")) {
- letterOcc = LetterOccurenceTime(word, 'p');
- for (int i = 0; i < letterOcc; i++) {
- letterPos = LetterPos(word, 'p', letterPos + 1);
- l = new Label("P");
- l.setFont(font);
- l.setLayoutX(dashes[letterPos].getStartX());
- l.setLayoutY(dashes[letterPos].getStartY() - 30);
- root.getChildren().add(l);
- count++;
- lineStack.push(l);
- }
- alphabet[15].setDisable(true);
- letterPos = -1;
- } else {
- total_no_of_chances--;
- }
- }
- if (ae.getSource() == alphabet[16]) {
- if (word.contains("q")) {
- letterOcc = LetterOccurenceTime(word, 'q');
- for (int i = 0; i < letterOcc; i++) {
- letterPos = LetterPos(word, 'q', letterPos + 1);
- l = new Label("Q");
- l.setFont(font);
- l.setLayoutX(dashes[letterPos].getStartX());
- l.setLayoutY(dashes[letterPos].getStartY() - 30);
- root.getChildren().add(l);
- count++;
- lineStack.push(l);
- }
- alphabet[16].setDisable(true);
- letterPos = -1;
- } else {
- total_no_of_chances--;
- }
- }
- if (ae.getSource() == alphabet[17]) {
- if (word.contains("r")) {
- letterOcc = LetterOccurenceTime(word, 'r');
- for (int i = 0; i < letterOcc; i++) {
- letterPos = LetterPos(word, 'r', letterPos + 1);
- l = new Label("R");
- l.setFont(font);
- l.setLayoutX(dashes[letterPos].getStartX());
- l.setLayoutY(dashes[letterPos].getStartY() - 30);
- root.getChildren().add(l);
- count++;
- lineStack.push(l);
- }
- alphabet[17].setDisable(true);
- letterPos = -1;
- } else {
- total_no_of_chances--;
- }
- }
- if (ae.getSource() == alphabet[18]) {
- if (word.contains("s")) {
- letterOcc = LetterOccurenceTime(word, 's');
- for (int i = 0; i < letterOcc; i++) {
- letterPos = LetterPos(word, 's', letterPos + 1);
- l = new Label("S");
- l.setFont(font);
- l.setLayoutX(dashes[letterPos].getStartX());
- l.setLayoutY(dashes[letterPos].getStartY() - 30);
- root.getChildren().add(l);
- count++;
- lineStack.push(l);
- }
- alphabet[18].setDisable(true);
- letterPos = -1;
- } else {
- total_no_of_chances--;
- }
- }
- if (ae.getSource() == alphabet[19]) {
- if (word.contains("t")) {
- letterOcc = LetterOccurenceTime(word, 't');
- for (int i = 0; i < letterOcc; i++) {
- letterPos = LetterPos(word, 't', letterPos + 1);
- l = new Label("T");
- l.setFont(font);
- l.setLayoutX(dashes[letterPos].getStartX());
- l.setLayoutY(dashes[letterPos].getStartY() - 30);
- root.getChildren().add(l);
- count++;
- lineStack.push(l);
- }
- alphabet[19].setDisable(true);
- letterPos = -1;
- } else {
- total_no_of_chances--;
- }
- }
- if (ae.getSource() == alphabet[20]) {
- if (word.contains("u")) {
- letterOcc = LetterOccurenceTime(word, 'u');
- for (int i = 0; i < letterOcc; i++) {
- letterPos = LetterPos(word, 'u', letterPos + 1);
- l = new Label("U");
- l.setFont(font);
- l.setLayoutX(dashes[letterPos].getStartX());
- l.setLayoutY(dashes[letterPos].getStartY() - 30);
- root.getChildren().add(l);
- count++;
- lineStack.push(l);
- }
- alphabet[20].setDisable(true);
- letterPos = -1;
- } else {
- total_no_of_chances--;
- }
- }
- if (ae.getSource() == alphabet[21]) {
- if (word.contains("v")) {
- letterOcc = LetterOccurenceTime(word, 'v');
- for (int i = 0; i < letterOcc; i++) {
- letterPos = LetterPos(word, 'v', letterPos + 1);
- l = new Label("V");
- l.setFont(font);
- l.setLayoutX(dashes[letterPos].getStartX());
- l.setLayoutY(dashes[letterPos].getStartY() - 30);
- root.getChildren().add(l);
- count++;
- lineStack.push(l);
- }
- alphabet[21].setDisable(true);
- letterPos = -1;
- } else {
- total_no_of_chances--;
- }
- }
- if (ae.getSource() == alphabet[22]) {
- if (word.contains("w")) {
- letterOcc = LetterOccurenceTime(word, 'w');
- for (int i = 0; i < letterOcc; i++) {
- letterPos = LetterPos(word, 'w', letterPos + 1);
- l = new Label("W");
- l.setFont(font);
- l.setLayoutX(dashes[letterPos].getStartX());
- l.setLayoutY(dashes[letterPos].getStartY() - 30);
- root.getChildren().add(l);
- count++;
- lineStack.push(l);
- }
- alphabet[22].setDisable(true);
- letterPos = -1;
- } else {
- total_no_of_chances--;
- }
- }
- if (ae.getSource() == alphabet[23]) {
- if (word.contains("x")) {
- letterOcc = LetterOccurenceTime(word, 'x');
- for (int i = 0; i < letterOcc; i++) {
- letterPos = LetterPos(word, 'x', letterPos + 1);
- l = new Label("X");
- l.setFont(font);
- l.setLayoutX(dashes[letterPos].getStartX());
- l.setLayoutY(dashes[letterPos].getStartY() - 30);
- root.getChildren().add(l);
- count++;
- lineStack.push(l);
- }
- alphabet[23].setDisable(true);
- letterPos = -1;
- } else {
- total_no_of_chances--;
- }
- }
- if (ae.getSource() == alphabet[24]) {
- if (word.contains("y")) {
- letterOcc = LetterOccurenceTime(word, 'y');
- for (int i = 0; i < letterOcc; i++) {
- letterPos = LetterPos(word, 'y', letterPos + 1);
- l = new Label("Y");
- l.setFont(font);
- l.setLayoutX(dashes[letterPos].getStartX());
- l.setLayoutY(dashes[letterPos].getStartY() - 30);
- root.getChildren().add(l);
- count++;
- lineStack.push(l);
- }
- alphabet[24].setDisable(true);
- letterPos = -1;
- } else {
- total_no_of_chances--;
- }
- }
- if (ae.getSource() == alphabet[25]) {
- if (word.contains("z")) {
- letterOcc = LetterOccurenceTime(word, 'z');
- for (int i = 0; i < letterOcc; i++) {
- letterPos = LetterPos(word, 'z', letterPos + 1);
- l = new Label("Z");
- l.setFont(font);
- l.setLayoutX(dashes[letterPos].getStartX());
- l.setLayoutY(dashes[letterPos].getStartY() - 30);
- root.getChildren().add(l);
- count++;
- lineStack.push(l);
- }
- alphabet[25].setDisable(true);
- letterPos = -1;
- } else {
- total_no_of_chances--;
- }
- }
- if (count == word.length() && total_no_of_chances > 0 && !gameOver) {
- ScreenReset();
- for (Line k: dashes) root.getChildren().remove(k);
- while (!lineStack.isEmpty()) {
- Label la = (Label) lineStack.pop();
- root.getChildren().remove(la);
- }
- Scene4();
- gameOver = true;
- }
- if (total_no_of_chances <= 0) {
- ScreenReset();
- for (Line k: dashes) root.getChildren().remove(k);
- while (!lineStack.isEmpty()) {
- Label la = (Label) lineStack.pop();
- root.getChildren().remove(la);
- }
- Scene5();
- gameOver = true;
- }
- if (ae.getSource() == restartGameBtn) {
- ScreenReset();
- ResetGame();
- Scene2();
- }
- chances.setText("Chances Left::" + total_no_of_chances);
- }
- }
-
- private int LetterPos(String word, char letter, int letterPos) {
- for (int i = letterPos; i < word.length(); i++) {
- if (word.charAt(i) == letter) return i;
- }
- return -1;
- }
-
- private int LetterOccurenceTime(String word, char letter) {
- int counter = 0;
- for (int i = 0; i < word.length(); i++) {
- if (word.charAt(i) == letter) counter++;
- }
- return counter;
- }
-
-
-
- public static void main(String[] args) {
- launch(args);
- }
- }
If you like the code, you can visit my blog at http://www.bakeyourcode.com for more interesting codes.