Skip to content

Instantly share code, notes, and snippets.

@Da9el00
Created July 6, 2022 17:34
Show Gist options
  • Save Da9el00/1b5199491460f433ba15f0eff772d0cc to your computer and use it in GitHub Desktop.
Save Da9el00/1b5199491460f433ba15f0eff772d0cc to your computer and use it in GitHub Desktop.
Memory game in JavaFX
package com.example.memorygame;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class Application extends javafx.application.Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(Application.class.getResource("view.fxml"));
Scene scene = new Scene(fxmlLoader.load());
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
import javafx.animation.KeyFrame;
import javafx.animation.PauseTransition;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Control;
import javafx.scene.text.Text;
import javafx.util.Duration;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.ResourceBundle;
public class Controller implements Initializable {
ArrayList<String> possibleButtons = new ArrayList<>(Arrays.asList("button0", "button1", "button2", "button3", "button4",
"button5", "button6", "button7", "button8"));
ArrayList<Button> buttons = new ArrayList<>();
ArrayList<String> pattern = new ArrayList<>();
int patternOrder = 0;
Random random = new Random();
int counter = 0;
int turn = 1;
@FXML
private Button button0;
@FXML
private Button button1;
@FXML
private Button button2;
@FXML
private Button button3;
@FXML
private Button button4;
@FXML
private Button button5;
@FXML
private Button button6;
@FXML
private Button button7;
@FXML
private Button button8;
@FXML
private Text text;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
buttons.addAll(Arrays.asList(button0, button1, button2, button3, button4,
button5, button6, button7, button8));
}
@FXML
void buttonClicked(ActionEvent event) {
if(((Control)event.getSource()).getId().equals(pattern.get(counter))){
text.setText("Correct " + counter);
Button button = buttons.get(getIndexOfButton(event));
changeButtonColor(button, "-fx-base: lightgreen");
counter++;
} else {
Button button = buttons.get(getIndexOfButton(event));
changeButtonColor(button, "-fx-base: red");
text.setText("Wrong");
return;
}
if(counter == turn){
nextTurn();
}
}
@FXML
void start(ActionEvent event) {
pattern.clear();
pattern.add(possibleButtons.get(random.nextInt(possibleButtons.size())));
showPattern();
System.out.println(pattern);
counter = 0;
turn = 1;
}
private void nextTurn(){
counter = 0;
turn++;
pattern.add(possibleButtons.get(random.nextInt(possibleButtons.size())));
showPattern();
System.out.println(pattern);
}
private int getIndexOfButton(ActionEvent event){
String buttonId = ((Control)event.getSource()).getId();
return Integer.parseInt(buttonId.substring(buttonId.length() -1));
}
private int getIndexOfButton(String button){
return Integer.parseInt(button.substring(button.length() -1));
}
private void showPattern(){
PauseTransition pause = new PauseTransition(
Duration.seconds(1));
pause.setOnFinished(e -> {
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), event -> {
showNext();
}));
timeline.setCycleCount(pattern.size());
timeline.play();
});
pause.play();
}
private void showNext(){
Button button = buttons.get(getIndexOfButton(pattern.get(patternOrder)));
changeButtonColor(button, "-fx-base: gray");
patternOrder++;
if(patternOrder == turn){
patternOrder = 0;
}
}
private void changeButtonColor(Button button, String color){
button.setStyle(color);
PauseTransition pause = new PauseTransition(
Duration.seconds(0.5));
pause.setOnFinished(e -> {
button.setStyle(null);
});
pause.play();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?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="com.example.memorygame.Controller">
<children>
<VBox layoutX="150.0" layoutY="50.0" prefHeight="300.0" prefWidth="300.0">
<children>
<HBox prefHeight="100.0" prefWidth="300.0">
<children>
<Button fx:id="button0" mnemonicParsing="false" onAction="#buttonClicked" prefHeight="100.0" prefWidth="100.0" />
<Button fx:id="button1" layoutX="10.0" layoutY="10.0" mnemonicParsing="false" onAction="#buttonClicked" prefHeight="100.0" prefWidth="100.0" />
<Button fx:id="button2" layoutX="235.0" layoutY="10.0" mnemonicParsing="false" onAction="#buttonClicked" prefHeight="100.0" prefWidth="100.0" />
</children>
</HBox>
<HBox layoutX="10.0" layoutY="10.0" prefHeight="100.0" prefWidth="300.0">
<children>
<Button fx:id="button3" mnemonicParsing="false" onAction="#buttonClicked" prefHeight="100.0" prefWidth="100.0" />
<Button fx:id="button4" layoutX="10.0" layoutY="10.0" mnemonicParsing="false" onAction="#buttonClicked" prefHeight="100.0" prefWidth="100.0" />
<Button fx:id="button5" layoutX="110.0" layoutY="10.0" mnemonicParsing="false" onAction="#buttonClicked" prefHeight="100.0" prefWidth="100.0" />
</children>
</HBox>
<HBox layoutX="10.0" layoutY="110.0" prefHeight="100.0" prefWidth="300.0">
<children>
<Button fx:id="button6" mnemonicParsing="false" onAction="#buttonClicked" prefHeight="100.0" prefWidth="100.0" />
<Button fx:id="button7" layoutX="10.0" layoutY="10.0" mnemonicParsing="false" onAction="#buttonClicked" prefHeight="100.0" prefWidth="100.0" />
<Button fx:id="button8" layoutX="110.0" layoutY="10.0" mnemonicParsing="false" onAction="#buttonClicked" prefHeight="100.0" prefWidth="100.0" />
</children>
</HBox>
</children>
</VBox>
<Button layoutX="280.0" layoutY="361.0" mnemonicParsing="false" onAction="#start" text="Start" />
<Text fx:id="text" layoutX="55.0" layoutY="205.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Text" />
</children>
</AnchorPane>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment