Skip to content

Instantly share code, notes, and snippets.

@Da9el00
Last active May 7, 2022 04:22
Show Gist options
  • Save Da9el00/5ec5a347af23ed2c73852f7954bda0db to your computer and use it in GitHub Desktop.
Save Da9el00/5ec5a347af23ed2c73852f7954bda0db to your computer and use it in GitHub Desktop.
JavaFX and Scene Builder - Hangman tutorial - Controller
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
private Text hangmanTextArea;
@FXML
private TextField guess;
@FXML
private Text textForWord;
@FXML
private Text endOfGameText;
private String word;
private StringBuilder secretWord = new StringBuilder();
private int livesPos = 0;
ArrayList<String> hangManLives = new ArrayList<>(Arrays.asList(
"""
+---+
| |
|
|
|
|
=========""",
"""
+---+
| |
O |
|
|
|
=========""",
"""
+---+
| |
O |
| |
|
|
=========""",
"""
+---+
| |
O |
/| |
|
|
=========""",
"""
+---+
| |
O |
/|\\ |
|
|
=========""",
"""
+---+
| |
O |
/|\\ |
/ |
|
=========""",
"""
+---+
| |
O |
/|\\ |
/ \\ |
|
========="""
));
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
hangmanTextArea.setText(hangManLives.get(livesPos));
}
@FXML
void getTextInput(ActionEvent event) {
if(word == null){
word = guess.getText();
setupWord();
guess.clear();
} else{
playTurn();
}
}
public void setupWord(){
int wordLength = word.length();
secretWord.append("*".repeat(wordLength));
textForWord.setText(String.valueOf(secretWord));
}
public void playTurn(){
String guess = this.guess.getText();
ArrayList<Integer> positions = new ArrayList<>();
char[] wordChars = word.toCharArray();
char letterGuess = guess.charAt(0);
if(word.equals(guess)){
endOfGameText.setText("You won!!");
return;
}
if(word.contains(guess)){
for (int i = 0; i < word.length(); i++) {
if(wordChars[i] == letterGuess){
positions.add(i);
}
}
positions.forEach(pos ->{
secretWord.setCharAt(pos,letterGuess);
});
textForWord.setText(String.valueOf(secretWord));
} else {
hangmanTextArea.setText(hangManLives.get(++livesPos));
if(livesPos == 6){
endOfGameText.setText("You LOST!!");
}
}
}
@FXML
void reset(ActionEvent event) {
word = null;
secretWord.setLength(0);
livesPos = 0;
hangmanTextArea.setText(hangManLives.get(0));
endOfGameText.setText("");
}
}
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<TextField fx:id="guess" layoutX="333.0" layoutY="76.0" />
<Text layoutX="333.0" layoutY="61.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Text Input" />
<Button layoutX="333.0" layoutY="114.0" mnemonicParsing="false" onAction="#getTextInput" text="Enter" />
<Text fx:id="textForWord" layoutX="319.0" layoutY="200.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Input a word" wrappingWidth="149.0">
<font>
<Font size="40.0" />
</font>
</Text>
<Text fx:id="hangmanTextArea" layoutX="49.0" layoutY="70.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Text">
<font>
<Font name="Monospaced Regular" size="32.0" />
</font>
</Text>
<Text fx:id="endOfGameText" layoutX="264.0" layoutY="345.0" strokeType="OUTSIDE" strokeWidth="0.0">
<font>
<Font size="39.0" />
</font>
</Text>
<Button layoutX="523.0" layoutY="355.0" mnemonicParsing="false" onAction="#reset" text="Reset" />
</children>
</AnchorPane>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment