Hangman - A Classic Game Developed In JavaFX

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 hangman in JavaFX. You can see the source code below.
  1. /* 
  2.  * To change this license header, choose License Headers in Project Properties. 
  3.  * To change this template file, choose Tools | Templates 
  4.  * and open the template in the editor. 
  5.  */  
  6. package hangman;  
  7. import java.util.Arrays;  
  8. import java.util.Stack;  
  9. import javafx.application.Application;  
  10. import javafx.event.ActionEvent;  
  11. import javafx.scene.Group;  
  12. import javafx.scene.Scene;  
  13. import javafx.scene.control.Alert;  
  14. import javafx.scene.control.Alert.AlertType;  
  15. import javafx.scene.control.Button;  
  16. import javafx.scene.control.Label;  
  17. import javafx.scene.control.TextField;  
  18. import javafx.scene.image.Image;  
  19. import javafx.scene.image.ImageView;  
  20. import javafx.scene.paint.Color;  
  21. import javafx.scene.shape.Line;  
  22. import javafx.scene.text.Font;  
  23. import javafx.stage.Stage;  
  24. /** 
  25.  * 
  26.  * @author Compaq 
  27.  */  
  28. public class Hangman extends Application {  
  29.     //UI components used in the code  
  30.     private Image gameLogo, gameImage, playBtnImg, exitBtnImg;  
  31.     private ImageView iv, iv2;  
  32.     private Button playBtn, exitBtn, startGameBtn, restartGameBtn;  
  33.     private Label label1, winLabel, looseLabel, chances, label2;  
  34.     private String word = "";  
  35.     private TextField secretWord;  
  36.     private Button[] alphabet;  
  37.     private Font font, font2;  
  38.     private final char alph = 'A';  
  39.     private Line[] dashes;  
  40.     private Group root;  
  41.     private int total_no_of_chances = 8;  
  42.     private final Stack lineStack = new Stack();  
  43.     private boolean gameOver = false, gameStart = false;  
  44.     int count = 0;  
  45.     @Override  
  46.     public void start(Stage primaryStage) {  
  47.         //Font used in the code  
  48.         font = new Font("Poor Richard", 22);  
  49.         font2 = new Font("Poor Richard", 40);  
  50.         //Game logo used in the code  
  51.         gameLogo = new Image(Hangman.class.getClassLoader().getResource("Images/logo.png").toString());  
  52.         //UI Settings  
  53.         iv = new ImageView();  
  54.         iv.setImage(gameLogo);  
  55.         iv.setLayoutX(15);  
  56.         iv.setLayoutY(15);  
  57.         gameImage = new Image(Hangman.class.getClassLoader().getResource("Images/hanging.gif-c200").toString());  
  58.         iv2 = new ImageView(gameImage);  
  59.         iv2.setLayoutX(270);  
  60.         iv2.setLayoutY(290);  
  61.         iv2.setFitHeight(200);  
  62.         iv2.setFitWidth(200);  
  63.         playBtnImg = new Image(Hangman.class.getClassLoader().getResource("Images/playbutton.png").toString());  
  64.         playBtn = new Button(""new ImageView(playBtnImg));  
  65.         playBtn.setLayoutX(50);  
  66.         playBtn.setLayoutY(270);  
  67.         restartGameBtn = new Button("Restart Game");  
  68.         restartGameBtn.setFont(font);  
  69.         restartGameBtn.setLayoutX(220);  
  70.         restartGameBtn.setLayoutY(270);  
  71.         restartGameBtn.setVisible(false);  
  72.         exitBtnImg = new Image(Hangman.class.getClassLoader().getResource("Images/exitbutton.png").toString());  
  73.         exitBtn = new Button(""new ImageView(exitBtnImg));  
  74.         exitBtn.setLayoutX(50);  
  75.         exitBtn.setLayoutY(360);  
  76.         label1 = new Label("Enter the word while the other player turn around");  
  77.         label1.setFont(font);  
  78.         label1.setLayoutX(50);  
  79.         label1.setLayoutY(100);  
  80.         label1.setVisible(false);  
  81.         winLabel = new Label("You Win");  
  82.         winLabel.setFont(font2);  
  83.         winLabel.setLayoutX(230);  
  84.         winLabel.setLayoutY(100);  
  85.         winLabel.setVisible(false);  
  86.         looseLabel = new Label("You Loose");  
  87.         looseLabel.setFont(font2);  
  88.         looseLabel.setLayoutX(220);  
  89.         looseLabel.setLayoutY(100);  
  90.         looseLabel.setVisible(false);  
  91.         chances = new Label("Chances Left::" + total_no_of_chances);  
  92.         chances.setFont(font2);  
  93.         chances.setLayoutX(350);  
  94.         chances.setLayoutY(400);  
  95.         chances.setVisible(false);  
  96.         label2 = new Label("Guess The Word");  
  97.         label2.setFont(font2);  
  98.         label2.setLayoutX(220);  
  99.         label2.setLayoutY(150);  
  100.         label2.setVisible(false);  
  101.         secretWord = new TextField();  
  102.         secretWord.setFont(font);  
  103.         secretWord.setPrefWidth(350);  
  104.         secretWord.setLayoutX(80);  
  105.         secretWord.setLayoutY(150);  
  106.         secretWord.setVisible(false);  
  107.         startGameBtn = new Button(""new ImageView(new Image(Hangman.class.getClassLoader().getResource("Images/startbutton.png").toString())));  
  108.         startGameBtn.setLayoutX(120);  
  109.         startGameBtn.setLayoutY(200);  
  110.         startGameBtn.setVisible(false);  
  111.         alphabet = new Button[26];  
  112.         for (int i = 0, j = 0; i < alphabet.length; i++) {  
  113.             alphabet[i] = new Button("" + (char)(alph + i));  
  114.             alphabet[i].setFont(font);  
  115.             alphabet[i].setVisible(false);  
  116.             if (i <= 13) {  
  117.                 alphabet[i].setLayoutX(20 + i * 40);  
  118.                 alphabet[i].setLayoutY(10);  
  119.             } else {  
  120.                 alphabet[i].setLayoutX(20 + j * 40);  
  121.                 alphabet[i].setLayoutY(50);  
  122.                 j++;  
  123.             }  
  124.         }  
  125.         dashes = new Line[100];  
  126.         playBtn.setOnAction((ActionEvent ae) - > {  
  127.             ScreenReset();  
  128.             Scene2();  
  129.         });  
  130.         exitBtn.setOnAction((ActionEvent ae) - > {  
  131.             System.exit(0);  
  132.         });  
  133.         startGameBtn.setOnAction((e) - > HandleButton(e));  
  134.         restartGameBtn.setOnAction((e) - > HandleButton(e));  
  135.         for (Button alphabet1: alphabet) {  
  136.             alphabet1.setOnAction((e) - > HandleButton(e));  
  137.         }  
  138.         //adding all the components to the root  
  139.         root = new Group();  
  140.         root.getChildren().add(iv);  
  141.         root.getChildren().add(iv2);  
  142.         root.getChildren().add(playBtn);  
  143.         root.getChildren().add(exitBtn);  
  144.         root.getChildren().add(label1);  
  145.         root.getChildren().add(secretWord);  
  146.         root.getChildren().add(startGameBtn);  
  147.         root.getChildren().addAll(Arrays.asList(alphabet));  
  148.         root.getChildren().add(winLabel);  
  149.         root.getChildren().add(restartGameBtn);  
  150.         root.getChildren().add(looseLabel);  
  151.         root.getChildren().add(chances);  
  152.         root.getChildren().add(label2);  
  153.         Scene scene = new Scene(root, 620, 500);  
  154.         scene.setFill(Color.LIGHTGREEN);  
  155.         primaryStage.setTitle("Hangman");  
  156.         primaryStage.setScene(scene);  
  157.         primaryStage.setResizable(false);  
  158.         primaryStage.show();  
  159.     }  
  160.     //Method to reset the game  
  161.     private void ResetGame() {  
  162.         secretWord.clear();  
  163.         word = "";  
  164.         for (Button b: alphabet) b.setDisable(false);  
  165.         total_no_of_chances = 8;  
  166.         gameOver = false;  
  167.         count = 0;  
  168.     }  
  169.     //method to rest the Screen  
  170.     private void ScreenReset() {  
  171.         iv.setVisible(false);  
  172.         iv2.setVisible(false);  
  173.         playBtn.setVisible(false);  
  174.         exitBtn.setVisible(false);  
  175.         label1.setVisible(false);  
  176.         secretWord.setVisible(false);  
  177.         startGameBtn.setVisible(false);  
  178.         for (Button alphabet1: alphabet) {  
  179.             alphabet1.setVisible(false);  
  180.         }  
  181.         restartGameBtn.setVisible(false);  
  182.         winLabel.setVisible(false);  
  183.         looseLabel.setVisible(false);  
  184.         chances.setVisible(false);  
  185.         label2.setVisible(false);  
  186.     }  
  187.     //Scene 2  
  188.     private void Scene2() {  
  189.         label1.setVisible(true);  
  190.         secretWord.setVisible(true);  
  191.         startGameBtn.setVisible(true);  
  192.     }  
  193.     //Scene 3  
  194.     private void Scene3() {  
  195.         for (Button alphabet1: alphabet) {  
  196.             alphabet1.setVisible(true);  
  197.         }  
  198.         for (int i = 0; i < word.length(); i++) {  
  199.             dashes[i] = new Line(10 + (i * 30 + 10), 250, (10 + (i + 1) * 30), 250);  
  200.         }  
  201.         for (int i = 0; i < word.length(); i++) {  
  202.             root.getChildren().add(dashes[i]);  
  203.         }  
  204.         chances.setVisible(true);  
  205.         label2.setVisible(true);  
  206.     }  
  207.     //Scene 4  
  208.     private void Scene4() {  
  209.         winLabel.setVisible(true);  
  210.         restartGameBtn.setVisible(true);  
  211.     }  
  212.     //Scene 5  
  213.     private void Scene5() {  
  214.         looseLabel.setVisible(true);  
  215.         restartGameBtn.setVisible(true);  
  216.     }  
  217.     //Method to check whehter the word entered is valid  
  218.     private boolean IsValid(String w) {  
  219.         for (int i = 0; i < w.length(); i++) {  
  220.             if (!((w.charAt(i) >= 'A' && w.charAt(i) <= 'Z') || (w.charAt(i) >= 'a' && w.charAt(i) <= 'z'))) return false;  
  221.         }  
  222.         return true;  
  223.     }  
  224.     //Method to handle the buttons used in the code  
  225.     private void HandleButton(ActionEvent ae) {  
  226.         int letterPos = -1;  
  227.         Label l;  
  228.         int letterOcc;  
  229.         if (ae.getSource() == startGameBtn) {  
  230.             if (secretWord.getText().length() == 0 || !IsValid(secretWord.getText())) {  
  231.                 Alert msg = new Alert(AlertType.ERROR);  
  232.                 msg.setTitle("Error Message from Hangman");  
  233.                 msg.setContentText("Please enter the Secret Word");  
  234.                 msg.showAndWait();  
  235.             } else {  
  236.                 word = secretWord.getText().toLowerCase();  
  237.                 ScreenReset();  
  238.                 Scene3();  
  239.                 gameStart = true;  
  240.             }  
  241.         }  
  242.         if (gameStart) {  
  243.             if (ae.getSource() == alphabet[0]) {  
  244.                 if (word.contains("a")) {  
  245.                     letterOcc = LetterOccurenceTime(word, 'a');  
  246.                     for (int i = 0; i < letterOcc; i++) {  
  247.                         letterPos = LetterPos(word, 'a', letterPos + 1);  
  248.                         l = new Label("A");  
  249.                         l.setFont(font);  
  250.                         l.setLayoutX(dashes[letterPos].getStartX());  
  251.                         l.setLayoutY(dashes[letterPos].getStartY() - 30);  
  252.                         root.getChildren().add(l);  
  253.                         count++;  
  254.                         lineStack.push(l);  
  255.                     }  
  256.                     alphabet[0].setDisable(true);  
  257.                     letterPos = -1;  
  258.                 } else {  
  259.                     total_no_of_chances--;  
  260.                 }  
  261.             }  
  262.             if (ae.getSource() == alphabet[1]) {  
  263.                 if (word.contains("b")) {  
  264.                     letterOcc = LetterOccurenceTime(word, 'b');  
  265.                     for (int i = 0; i < letterOcc; i++) {  
  266.                         letterPos = LetterPos(word, 'b', letterPos + 1);  
  267.                         l = new Label("B");  
  268.                         l.setFont(font);  
  269.                         l.setLayoutX(dashes[letterPos].getStartX());  
  270.                         l.setLayoutY(dashes[letterPos].getStartY() - 30);  
  271.                         root.getChildren().add(l);  
  272.                         count++;  
  273.                         lineStack.push(l);  
  274.                     }  
  275.                     alphabet[1].setDisable(true);  
  276.                     letterPos = -1;  
  277.                 } else {  
  278.                     total_no_of_chances--;  
  279.                 }  
  280.             }  
  281.             if (ae.getSource() == alphabet[2]) {  
  282.                 if (word.contains("c")) {  
  283.                     letterOcc = LetterOccurenceTime(word, 'c');  
  284.                     for (int i = 0; i < letterOcc; i++) {  
  285.                         letterPos = LetterPos(word, 'c', letterPos + 1);  
  286.                         l = new Label("C");  
  287.                         l.setFont(font);  
  288.                         l.setLayoutX(dashes[letterPos].getStartX());  
  289.                         l.setLayoutY(dashes[letterPos].getStartY() - 30);  
  290.                         root.getChildren().add(l);  
  291.                         count++;  
  292.                         lineStack.push(l);  
  293.                     }  
  294.                     alphabet[2].setDisable(true);  
  295.                     letterPos = -1;  
  296.                 } else {  
  297.                     total_no_of_chances--;  
  298.                 }  
  299.             }  
  300.             if (ae.getSource() == alphabet[3]) {  
  301.                 if (word.contains("d")) {  
  302.                     letterOcc = LetterOccurenceTime(word, 'd');  
  303.                     for (int i = 0; i < letterOcc; i++) {  
  304.                         letterPos = LetterPos(word, 'd', letterPos + 1);  
  305.                         l = new Label("D");  
  306.                         l.setFont(font);  
  307.                         l.setLayoutX(dashes[letterPos].getStartX());  
  308.                         l.setLayoutY(dashes[letterPos].getStartY() - 30);  
  309.                         root.getChildren().add(l);  
  310.                         count++;  
  311.                         lineStack.push(l);  
  312.                     }  
  313.                     alphabet[3].setDisable(true);  
  314.                     letterPos = -1;  
  315.                 } else {  
  316.                     total_no_of_chances--;  
  317.                 }  
  318.             }  
  319.             if (ae.getSource() == alphabet[4]) {  
  320.                 if (word.contains("e")) {  
  321.                     letterOcc = LetterOccurenceTime(word, 'e');  
  322.                     for (int i = 0; i < letterOcc; i++) {  
  323.                         letterPos = LetterPos(word, 'e', letterPos + 1);  
  324.                         l = new Label("E");  
  325.                         l.setFont(font);  
  326.                         l.setLayoutX(dashes[letterPos].getStartX());  
  327.                         l.setLayoutY(dashes[letterPos].getStartY() - 30);  
  328.                         root.getChildren().add(l);  
  329.                         count++;  
  330.                         lineStack.push(l);  
  331.                     }  
  332.                     alphabet[4].setDisable(true);  
  333.                     letterPos = -1;  
  334.                 } else {  
  335.                     total_no_of_chances--;  
  336.                 }  
  337.             }  
  338.             if (ae.getSource() == alphabet[5]) {  
  339.                 if (word.contains("f")) {  
  340.                     letterOcc = LetterOccurenceTime(word, 'f');  
  341.                     for (int i = 0; i < letterOcc; i++) {  
  342.                         letterPos = LetterPos(word, 'f', letterPos + 1);  
  343.                         l = new Label("F");  
  344.                         l.setFont(font);  
  345.                         l.setLayoutX(dashes[letterPos].getStartX());  
  346.                         l.setLayoutY(dashes[letterPos].getStartY() - 30);  
  347.                         root.getChildren().add(l);  
  348.                         count++;  
  349.                         lineStack.push(l);  
  350.                     }  
  351.                     alphabet[5].setDisable(true);  
  352.                     letterPos = -1;  
  353.                 } else {  
  354.                     total_no_of_chances--;  
  355.                 }  
  356.             }  
  357.             if (ae.getSource() == alphabet[6]) {  
  358.                 if (word.contains("g")) {  
  359.                     letterOcc = LetterOccurenceTime(word, 'g');  
  360.                     for (int i = 0; i < letterOcc; i++) {  
  361.                         letterPos = LetterPos(word, 'g', letterPos + 1);  
  362.                         l = new Label("G");  
  363.                         l.setFont(font);  
  364.                         l.setLayoutX(dashes[letterPos].getStartX());  
  365.                         l.setLayoutY(dashes[letterPos].getStartY() - 30);  
  366.                         root.getChildren().add(l);  
  367.                         count++;  
  368.                         lineStack.push(l);  
  369.                     }  
  370.                     alphabet[6].setDisable(true);  
  371.                     letterPos = -1;  
  372.                 } else {  
  373.                     total_no_of_chances--;  
  374.                 }  
  375.             }  
  376.             if (ae.getSource() == alphabet[7]) {  
  377.                 if (word.contains("h")) {  
  378.                     letterOcc = LetterOccurenceTime(word, 'h');  
  379.                     for (int i = 0; i < letterOcc; i++) {  
  380.                         letterPos = LetterPos(word, 'h', letterPos + 1);  
  381.                         l = new Label("H");  
  382.                         l.setFont(font);  
  383.                         l.setLayoutX(dashes[letterPos].getStartX());  
  384.                         l.setLayoutY(dashes[letterPos].getStartY() - 30);  
  385.                         root.getChildren().add(l);  
  386.                         count++;  
  387.                         lineStack.push(l);  
  388.                     }  
  389.                     alphabet[7].setDisable(true);  
  390.                     letterPos = -1;  
  391.                 } else {  
  392.                     total_no_of_chances--;  
  393.                 }  
  394.             }  
  395.             if (ae.getSource() == alphabet[8]) {  
  396.                 if (word.contains("i")) {  
  397.                     letterOcc = LetterOccurenceTime(word, 'i');  
  398.                     for (int i = 0; i < letterOcc; i++) {  
  399.                         letterPos = LetterPos(word, 'i', letterPos + 1);  
  400.                         l = new Label("I");  
  401.                         l.setFont(font);  
  402.                         l.setLayoutX(dashes[letterPos].getStartX());  
  403.                         l.setLayoutY(dashes[letterPos].getStartY() - 30);  
  404.                         root.getChildren().add(l);  
  405.                         count++;  
  406.                         lineStack.push(l);  
  407.                     }  
  408.                     alphabet[8].setDisable(true);  
  409.                     letterPos = -1;  
  410.                 } else {  
  411.                     total_no_of_chances--;  
  412.                 }  
  413.             }  
  414.             if (ae.getSource() == alphabet[9]) {  
  415.                 if (word.contains("j")) {  
  416.                     letterOcc = LetterOccurenceTime(word, 'j');  
  417.                     for (int i = 0; i < letterOcc; i++) {  
  418.                         letterPos = LetterPos(word, 'j', letterPos + 1);  
  419.                         l = new Label("J");  
  420.                         l.setFont(font);  
  421.                         l.setLayoutX(dashes[letterPos].getStartX());  
  422.                         l.setLayoutY(dashes[letterPos].getStartY() - 30);  
  423.                         root.getChildren().add(l);  
  424.                         count++;  
  425.                         lineStack.push(l);  
  426.                     }  
  427.                     alphabet[9].setDisable(true);  
  428.                     letterPos = -1;  
  429.                 } else {  
  430.                     total_no_of_chances--;  
  431.                 }  
  432.             }  
  433.             if (ae.getSource() == alphabet[10]) {  
  434.                 if (word.contains("k")) {  
  435.                     letterOcc = LetterOccurenceTime(word, 'k');  
  436.                     for (int i = 0; i < letterOcc; i++) {  
  437.                         letterPos = LetterPos(word, 'k', letterPos + 1);  
  438.                         l = new Label("K");  
  439.                         l.setFont(font);  
  440.                         l.setLayoutX(dashes[letterPos].getStartX());  
  441.                         l.setLayoutY(dashes[letterPos].getStartY() - 30);  
  442.                         root.getChildren().add(l);  
  443.                         count++;  
  444.                         lineStack.push(l);  
  445.                     }  
  446.                     alphabet[10].setDisable(true);  
  447.                     letterPos = -1;  
  448.                 } else {  
  449.                     total_no_of_chances--;  
  450.                 }  
  451.             }  
  452.             if (ae.getSource() == alphabet[11]) {  
  453.                 if (word.contains("l")) {  
  454.                     letterOcc = LetterOccurenceTime(word, 'l');  
  455.                     for (int i = 0; i < letterOcc; i++) {  
  456.                         letterPos = LetterPos(word, 'l', letterPos + 1);  
  457.                         l = new Label("L");  
  458.                         l.setFont(font);  
  459.                         l.setLayoutX(dashes[letterPos].getStartX());  
  460.                         l.setLayoutY(dashes[letterPos].getStartY() - 30);  
  461.                         root.getChildren().add(l);  
  462.                         count++;  
  463.                         lineStack.push(l);  
  464.                     }  
  465.                     alphabet[11].setDisable(true);  
  466.                     letterPos = -1;  
  467.                 } else {  
  468.                     total_no_of_chances--;  
  469.                 }  
  470.             }  
  471.             if (ae.getSource() == alphabet[12]) {  
  472.                 if (word.contains("m")) {  
  473.                     letterOcc = LetterOccurenceTime(word, 'm');  
  474.                     for (int i = 0; i < letterOcc; i++) {  
  475.                         letterPos = LetterPos(word, 'm', letterPos + 1);  
  476.                         l = new Label("M");  
  477.                         l.setFont(font);  
  478.                         l.setLayoutX(dashes[letterPos].getStartX());  
  479.                         l.setLayoutY(dashes[letterPos].getStartY() - 30);  
  480.                         root.getChildren().add(l);  
  481.                         count++;  
  482.                         lineStack.push(l);  
  483.                     }  
  484.                     alphabet[12].setDisable(true);  
  485.                     letterPos = -1;  
  486.                 } else {  
  487.                     total_no_of_chances--;  
  488.                 }  
  489.             }  
  490.             if (ae.getSource() == alphabet[13]) {  
  491.                 if (word.contains("n")) {  
  492.                     letterOcc = LetterOccurenceTime(word, 'n');  
  493.                     for (int i = 0; i < letterOcc; i++) {  
  494.                         letterPos = LetterPos(word, 'n', letterPos + 1);  
  495.                         l = new Label("N");  
  496.                         l.setFont(font);  
  497.                         l.setLayoutX(dashes[letterPos].getStartX());  
  498.                         l.setLayoutY(dashes[letterPos].getStartY() - 30);  
  499.                         root.getChildren().add(l);  
  500.                         count++;  
  501.                         lineStack.push(l);  
  502.                     }  
  503.                     alphabet[13].setDisable(true);  
  504.                     letterPos = -1;  
  505.                 } else {  
  506.                     total_no_of_chances--;  
  507.                 }  
  508.             }  
  509.             if (ae.getSource() == alphabet[14]) {  
  510.                 if (word.contains("o")) {  
  511.                     letterOcc = LetterOccurenceTime(word, 'o');  
  512.                     for (int i = 0; i < letterOcc; i++) {  
  513.                         letterPos = LetterPos(word, 'o', letterPos + 1);  
  514.                         l = new Label("O");  
  515.                         l.setFont(font);  
  516.                         l.setLayoutX(dashes[letterPos].getStartX());  
  517.                         l.setLayoutY(dashes[letterPos].getStartY() - 30);  
  518.                         root.getChildren().add(l);  
  519.                         count++;  
  520.                         lineStack.push(l);  
  521.                     }  
  522.                     alphabet[14].setDisable(true);  
  523.                     letterPos = -1;  
  524.                 } else {  
  525.                     total_no_of_chances--;  
  526.                 }  
  527.             }  
  528.             if (ae.getSource() == alphabet[15]) {  
  529.                 if (word.contains("p")) {  
  530.                     letterOcc = LetterOccurenceTime(word, 'p');  
  531.                     for (int i = 0; i < letterOcc; i++) {  
  532.                         letterPos = LetterPos(word, 'p', letterPos + 1);  
  533.                         l = new Label("P");  
  534.                         l.setFont(font);  
  535.                         l.setLayoutX(dashes[letterPos].getStartX());  
  536.                         l.setLayoutY(dashes[letterPos].getStartY() - 30);  
  537.                         root.getChildren().add(l);  
  538.                         count++;  
  539.                         lineStack.push(l);  
  540.                     }  
  541.                     alphabet[15].setDisable(true);  
  542.                     letterPos = -1;  
  543.                 } else {  
  544.                     total_no_of_chances--;  
  545.                 }  
  546.             }  
  547.             if (ae.getSource() == alphabet[16]) {  
  548.                 if (word.contains("q")) {  
  549.                     letterOcc = LetterOccurenceTime(word, 'q');  
  550.                     for (int i = 0; i < letterOcc; i++) {  
  551.                         letterPos = LetterPos(word, 'q', letterPos + 1);  
  552.                         l = new Label("Q");  
  553.                         l.setFont(font);  
  554.                         l.setLayoutX(dashes[letterPos].getStartX());  
  555.                         l.setLayoutY(dashes[letterPos].getStartY() - 30);  
  556.                         root.getChildren().add(l);  
  557.                         count++;  
  558.                         lineStack.push(l);  
  559.                     }  
  560.                     alphabet[16].setDisable(true);  
  561.                     letterPos = -1;  
  562.                 } else {  
  563.                     total_no_of_chances--;  
  564.                 }  
  565.             }  
  566.             if (ae.getSource() == alphabet[17]) {  
  567.                 if (word.contains("r")) {  
  568.                     letterOcc = LetterOccurenceTime(word, 'r');  
  569.                     for (int i = 0; i < letterOcc; i++) {  
  570.                         letterPos = LetterPos(word, 'r', letterPos + 1);  
  571.                         l = new Label("R");  
  572.                         l.setFont(font);  
  573.                         l.setLayoutX(dashes[letterPos].getStartX());  
  574.                         l.setLayoutY(dashes[letterPos].getStartY() - 30);  
  575.                         root.getChildren().add(l);  
  576.                         count++;  
  577.                         lineStack.push(l);  
  578.                     }  
  579.                     alphabet[17].setDisable(true);  
  580.                     letterPos = -1;  
  581.                 } else {  
  582.                     total_no_of_chances--;  
  583.                 }  
  584.             }  
  585.             if (ae.getSource() == alphabet[18]) {  
  586.                 if (word.contains("s")) {  
  587.                     letterOcc = LetterOccurenceTime(word, 's');  
  588.                     for (int i = 0; i < letterOcc; i++) {  
  589.                         letterPos = LetterPos(word, 's', letterPos + 1);  
  590.                         l = new Label("S");  
  591.                         l.setFont(font);  
  592.                         l.setLayoutX(dashes[letterPos].getStartX());  
  593.                         l.setLayoutY(dashes[letterPos].getStartY() - 30);  
  594.                         root.getChildren().add(l);  
  595.                         count++;  
  596.                         lineStack.push(l);  
  597.                     }  
  598.                     alphabet[18].setDisable(true);  
  599.                     letterPos = -1;  
  600.                 } else {  
  601.                     total_no_of_chances--;  
  602.                 }  
  603.             }  
  604.             if (ae.getSource() == alphabet[19]) {  
  605.                 if (word.contains("t")) {  
  606.                     letterOcc = LetterOccurenceTime(word, 't');  
  607.                     for (int i = 0; i < letterOcc; i++) {  
  608.                         letterPos = LetterPos(word, 't', letterPos + 1);  
  609.                         l = new Label("T");  
  610.                         l.setFont(font);  
  611.                         l.setLayoutX(dashes[letterPos].getStartX());  
  612.                         l.setLayoutY(dashes[letterPos].getStartY() - 30);  
  613.                         root.getChildren().add(l);  
  614.                         count++;  
  615.                         lineStack.push(l);  
  616.                     }  
  617.                     alphabet[19].setDisable(true);  
  618.                     letterPos = -1;  
  619.                 } else {  
  620.                     total_no_of_chances--;  
  621.                 }  
  622.             }  
  623.             if (ae.getSource() == alphabet[20]) {  
  624.                 if (word.contains("u")) {  
  625.                     letterOcc = LetterOccurenceTime(word, 'u');  
  626.                     for (int i = 0; i < letterOcc; i++) {  
  627.                         letterPos = LetterPos(word, 'u', letterPos + 1);  
  628.                         l = new Label("U");  
  629.                         l.setFont(font);  
  630.                         l.setLayoutX(dashes[letterPos].getStartX());  
  631.                         l.setLayoutY(dashes[letterPos].getStartY() - 30);  
  632.                         root.getChildren().add(l);  
  633.                         count++;  
  634.                         lineStack.push(l);  
  635.                     }  
  636.                     alphabet[20].setDisable(true);  
  637.                     letterPos = -1;  
  638.                 } else {  
  639.                     total_no_of_chances--;  
  640.                 }  
  641.             }  
  642.             if (ae.getSource() == alphabet[21]) {  
  643.                 if (word.contains("v")) {  
  644.                     letterOcc = LetterOccurenceTime(word, 'v');  
  645.                     for (int i = 0; i < letterOcc; i++) {  
  646.                         letterPos = LetterPos(word, 'v', letterPos + 1);  
  647.                         l = new Label("V");  
  648.                         l.setFont(font);  
  649.                         l.setLayoutX(dashes[letterPos].getStartX());  
  650.                         l.setLayoutY(dashes[letterPos].getStartY() - 30);  
  651.                         root.getChildren().add(l);  
  652.                         count++;  
  653.                         lineStack.push(l);  
  654.                     }  
  655.                     alphabet[21].setDisable(true);  
  656.                     letterPos = -1;  
  657.                 } else {  
  658.                     total_no_of_chances--;  
  659.                 }  
  660.             }  
  661.             if (ae.getSource() == alphabet[22]) {  
  662.                 if (word.contains("w")) {  
  663.                     letterOcc = LetterOccurenceTime(word, 'w');  
  664.                     for (int i = 0; i < letterOcc; i++) {  
  665.                         letterPos = LetterPos(word, 'w', letterPos + 1);  
  666.                         l = new Label("W");  
  667.                         l.setFont(font);  
  668.                         l.setLayoutX(dashes[letterPos].getStartX());  
  669.                         l.setLayoutY(dashes[letterPos].getStartY() - 30);  
  670.                         root.getChildren().add(l);  
  671.                         count++;  
  672.                         lineStack.push(l);  
  673.                     }  
  674.                     alphabet[22].setDisable(true);  
  675.                     letterPos = -1;  
  676.                 } else {  
  677.                     total_no_of_chances--;  
  678.                 }  
  679.             }  
  680.             if (ae.getSource() == alphabet[23]) {  
  681.                 if (word.contains("x")) {  
  682.                     letterOcc = LetterOccurenceTime(word, 'x');  
  683.                     for (int i = 0; i < letterOcc; i++) {  
  684.                         letterPos = LetterPos(word, 'x', letterPos + 1);  
  685.                         l = new Label("X");  
  686.                         l.setFont(font);  
  687.                         l.setLayoutX(dashes[letterPos].getStartX());  
  688.                         l.setLayoutY(dashes[letterPos].getStartY() - 30);  
  689.                         root.getChildren().add(l);  
  690.                         count++;  
  691.                         lineStack.push(l);  
  692.                     }  
  693.                     alphabet[23].setDisable(true);  
  694.                     letterPos = -1;  
  695.                 } else {  
  696.                     total_no_of_chances--;  
  697.                 }  
  698.             }  
  699.             if (ae.getSource() == alphabet[24]) {  
  700.                 if (word.contains("y")) {  
  701.                     letterOcc = LetterOccurenceTime(word, 'y');  
  702.                     for (int i = 0; i < letterOcc; i++) {  
  703.                         letterPos = LetterPos(word, 'y', letterPos + 1);  
  704.                         l = new Label("Y");  
  705.                         l.setFont(font);  
  706.                         l.setLayoutX(dashes[letterPos].getStartX());  
  707.                         l.setLayoutY(dashes[letterPos].getStartY() - 30);  
  708.                         root.getChildren().add(l);  
  709.                         count++;  
  710.                         lineStack.push(l);  
  711.                     }  
  712.                     alphabet[24].setDisable(true);  
  713.                     letterPos = -1;  
  714.                 } else {  
  715.                     total_no_of_chances--;  
  716.                 }  
  717.             }  
  718.             if (ae.getSource() == alphabet[25]) {  
  719.                 if (word.contains("z")) {  
  720.                     letterOcc = LetterOccurenceTime(word, 'z');  
  721.                     for (int i = 0; i < letterOcc; i++) {  
  722.                         letterPos = LetterPos(word, 'z', letterPos + 1);  
  723.                         l = new Label("Z");  
  724.                         l.setFont(font);  
  725.                         l.setLayoutX(dashes[letterPos].getStartX());  
  726.                         l.setLayoutY(dashes[letterPos].getStartY() - 30);  
  727.                         root.getChildren().add(l);  
  728.                         count++;  
  729.                         lineStack.push(l);  
  730.                     }  
  731.                     alphabet[25].setDisable(true);  
  732.                     letterPos = -1;  
  733.                 } else {  
  734.                     total_no_of_chances--;  
  735.                 }  
  736.             }  
  737.             if (count == word.length() && total_no_of_chances > 0 && !gameOver) {  
  738.                 ScreenReset();  
  739.                 for (Line k: dashes) root.getChildren().remove(k);  
  740.                 while (!lineStack.isEmpty()) {  
  741.                     Label la = (Label) lineStack.pop();  
  742.                     root.getChildren().remove(la);  
  743.                 }  
  744.                 Scene4();  
  745.                 gameOver = true;  
  746.             }  
  747.             if (total_no_of_chances <= 0) {  
  748.                 ScreenReset();  
  749.                 for (Line k: dashes) root.getChildren().remove(k);  
  750.                 while (!lineStack.isEmpty()) {  
  751.                     Label la = (Label) lineStack.pop();  
  752.                     root.getChildren().remove(la);  
  753.                 }  
  754.                 Scene5();  
  755.                 gameOver = true;  
  756.             }  
  757.             if (ae.getSource() == restartGameBtn) {  
  758.                 ScreenReset();  
  759.                 ResetGame();  
  760.                 Scene2();  
  761.             }  
  762.             chances.setText("Chances Left::" + total_no_of_chances);  
  763.         }  
  764.     }  
  765.     //Method to find the position of the letter  
  766.     private int LetterPos(String word, char letter, int letterPos) {  
  767.         for (int i = letterPos; i < word.length(); i++) {  
  768.             if (word.charAt(i) == letter) return i;  
  769.         }  
  770.         return -1;  
  771.     }  
  772.     //Method to count the number of times the letter occur  
  773.     private int LetterOccurenceTime(String word, char letter) {  
  774.         int counter = 0;  
  775.         for (int i = 0; i < word.length(); i++) {  
  776.             if (word.charAt(i) == letter) counter++;  
  777.         }  
  778.         return counter;  
  779.     }  
  780.     /** 
  781.      * @param args the command line arguments 
  782.      */  
  783.     public static void main(String[] args) {  
  784.         launch(args);  
  785.     }  
  786. }  
If you like the code, you can visit my blog at http://www.bakeyourcode.com for more interesting codes.
Ebook Download
View all
Learn
View all