Created
September 3, 2022 08:15
-
-
Save Da9el00/e481f0556adc1780a975e1aa1a047fb8 to your computer and use it in GitHub Desktop.
JavaFX memory matching game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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.Font?> | |
<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.memorymatchinggame.HelloController"> | |
<children> | |
<VBox layoutX="160.0" layoutY="60.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"> | |
<font> | |
<Font size="32.0" /> | |
</font> | |
</Button> | |
<Button fx:id="button1" layoutX="10.0" layoutY="10.0" mnemonicParsing="false" onAction="#buttonClicked" prefHeight="100.0" prefWidth="100.0"> | |
<font> | |
<Font size="32.0" /> | |
</font> | |
</Button> | |
<Button fx:id="button2" layoutX="235.0" layoutY="10.0" mnemonicParsing="false" onAction="#buttonClicked" prefHeight="100.0" prefWidth="100.0"> | |
<font> | |
<Font size="32.0" /> | |
</font> | |
</Button> | |
</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"> | |
<font> | |
<Font size="32.0" /> | |
</font> | |
</Button> | |
<Button fx:id="button4" layoutX="10.0" layoutY="10.0" mnemonicParsing="false" onAction="#buttonClicked" prefHeight="100.0" prefWidth="100.0"> | |
<font> | |
<Font size="32.0" /> | |
</font> | |
</Button> | |
<Button fx:id="button5" layoutX="110.0" layoutY="10.0" mnemonicParsing="false" onAction="#buttonClicked" prefHeight="100.0" prefWidth="100.0"> | |
<font> | |
<Font size="32.0" /> | |
</font> | |
</Button> | |
</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"> | |
<font> | |
<Font size="32.0" /> | |
</font> | |
</Button> | |
<Button fx:id="button7" layoutX="10.0" layoutY="10.0" mnemonicParsing="false" onAction="#buttonClicked" prefHeight="100.0" prefWidth="100.0"> | |
<font> | |
<Font size="32.0" /> | |
</font> | |
</Button> | |
<Button layoutX="110.0" layoutY="10.0" mnemonicParsing="false" prefHeight="100.0" prefWidth="100.0" style="-fx-background-color: #c61a09;" /> | |
</children> | |
</HBox> | |
</children> | |
</VBox> | |
</children> | |
</AnchorPane> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.memorymatchinggame; | |
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.Scene; | |
import javafx.stage.Stage; | |
import java.io.IOException; | |
public class HelloApplication extends Application { | |
@Override | |
public void start(Stage stage) throws IOException { | |
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml")); | |
Scene scene = new Scene(fxmlLoader.load()); | |
stage.setTitle("Memory Game"); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
public static void main(String[] args) { | |
launch(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.memorymatchinggame; | |
import javafx.animation.KeyFrame; | |
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.util.Duration; | |
import java.net.URL; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.ResourceBundle; | |
public class HelloController implements Initializable { | |
ArrayList<Button> buttons = new ArrayList<>(); | |
MemoryGame memoryGame = new MemoryGame(); | |
@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; | |
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1.5), e -> hideButtons())); | |
private boolean firstButtonClicked = false; | |
private int firstButtonIndex; | |
private int secondButtonIndex; | |
private boolean match; | |
@Override | |
public void initialize(URL url, ResourceBundle resourceBundle) { | |
memoryGame.setupGame(); | |
buttons.addAll(Arrays.asList(button0, button1, button2, button3, button4, | |
button5, button6, button7)); | |
} | |
@FXML | |
void buttonClicked(ActionEvent event) { | |
if(!firstButtonClicked){ | |
//If next turn is started before old buttons are hidden | |
if(!match){ | |
hideButtons(); | |
timeline.stop(); | |
} | |
match = false; | |
firstButtonClicked = true; | |
//Get clicked button memory letter | |
String buttonId = ((Control)event.getSource()).getId(); | |
firstButtonIndex = Integer.parseInt(buttonId.substring(buttonId.length() - 1)); | |
//Change clicked button text | |
buttons.get(firstButtonIndex).setText(memoryGame.getOptionAtIndex(firstButtonIndex)); | |
return; | |
} | |
//Get clicked button memory letter | |
String buttonId = ((Control)event.getSource()).getId(); | |
secondButtonIndex = Integer.parseInt(buttonId.substring(buttonId.length() - 1)); | |
//Change clicked button text | |
buttons.get(secondButtonIndex).setText(memoryGame.getOptionAtIndex(secondButtonIndex)); | |
firstButtonClicked = false; | |
//Check if the two clicked button match | |
if(memoryGame.checkTwoPositions(firstButtonIndex,secondButtonIndex)){ | |
System.out.println("Match"); | |
match = true; | |
return; | |
} | |
timeline.play(); | |
} | |
private void hideButtons(){ | |
buttons.get(firstButtonIndex).setText(""); | |
buttons.get(secondButtonIndex).setText(""); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.memorymatchinggame; | |
import java.util.*; | |
public class MemoryGame { | |
private int turns = 0; | |
private int points = 0; | |
private final int boardLength = 3; | |
private final int boardSize = boardLength * boardLength; | |
private final Random random = new Random(); | |
private final ArrayList<String> memoryBoard = new ArrayList<>(Arrays.asList("", "", "", "", "", "", "", "")); | |
private final ArrayList<String> memoryOptions = new ArrayList<>(Arrays.asList("a", "a", "b", "b", "c", "c", "d", "d")); | |
public void setupGame(){ | |
setupMemoryBoard(); | |
System.out.println(memoryBoard); | |
} | |
public String getOptionAtIndex(int index){ | |
return memoryBoard.get(index); | |
} | |
private void setupMemoryBoard(){ | |
for (int i = 0; i < boardSize - 1; i++) { | |
String memoryOption = memoryOptions.get(i); | |
int position = random.nextInt(boardSize - 1); | |
while (!Objects.equals(memoryBoard.get(position), "")){ | |
position = random.nextInt(boardSize - 1); | |
} | |
memoryBoard.set(position, memoryOption); | |
} | |
} | |
public boolean checkTwoPositions(int firstIndex, int secondIndex){ | |
return memoryBoard.get(firstIndex).equals(memoryBoard.get(secondIndex)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment