diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..19b3f53
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+eight_game/target/*
diff --git a/jogo-oito/.classpath b/eight_game/.classpath
similarity index 56%
rename from jogo-oito/.classpath
rename to eight_game/.classpath
index 1d97b2f..851d83e 100644
--- a/jogo-oito/.classpath
+++ b/eight_game/.classpath
@@ -6,22 +6,36 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
+
+
diff --git a/eight_game/.idea/.gitignore b/eight_game/.idea/.gitignore
new file mode 100644
index 0000000..26d3352
--- /dev/null
+++ b/eight_game/.idea/.gitignore
@@ -0,0 +1,3 @@
+# Default ignored files
+/shelf/
+/workspace.xml
diff --git a/eight_game/.idea/codeStyles/codeStyleConfig.xml b/eight_game/.idea/codeStyles/codeStyleConfig.xml
new file mode 100644
index 0000000..a55e7a1
--- /dev/null
+++ b/eight_game/.idea/codeStyles/codeStyleConfig.xml
@@ -0,0 +1,5 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/eight_game/.idea/compiler.xml b/eight_game/.idea/compiler.xml
new file mode 100644
index 0000000..4eecdf8
--- /dev/null
+++ b/eight_game/.idea/compiler.xml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/eight_game/.idea/discord.xml b/eight_game/.idea/discord.xml
new file mode 100644
index 0000000..8cf359d
--- /dev/null
+++ b/eight_game/.idea/discord.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/eight_game/.idea/encodings.xml b/eight_game/.idea/encodings.xml
new file mode 100644
index 0000000..aa00ffa
--- /dev/null
+++ b/eight_game/.idea/encodings.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/eight_game/.idea/jarRepositories.xml b/eight_game/.idea/jarRepositories.xml
new file mode 100644
index 0000000..712ab9d
--- /dev/null
+++ b/eight_game/.idea/jarRepositories.xml
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/eight_game/.idea/misc.xml b/eight_game/.idea/misc.xml
new file mode 100644
index 0000000..7499f4f
--- /dev/null
+++ b/eight_game/.idea/misc.xml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/eight_game/.idea/vcs.xml b/eight_game/.idea/vcs.xml
new file mode 100644
index 0000000..6c0b863
--- /dev/null
+++ b/eight_game/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/jogo-oito/.project b/eight_game/.project
similarity index 91%
rename from jogo-oito/.project
rename to eight_game/.project
index 71ae64e..b086d3a 100644
--- a/jogo-oito/.project
+++ b/eight_game/.project
@@ -1,6 +1,6 @@
- jogo-do-oito-do-estagiario-gpt
+ eight_game
diff --git a/eight_game/.settings/org.eclipse.core.resources.prefs b/eight_game/.settings/org.eclipse.core.resources.prefs
new file mode 100644
index 0000000..abdea9a
--- /dev/null
+++ b/eight_game/.settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,4 @@
+eclipse.preferences.version=1
+encoding//src/main/java=UTF-8
+encoding//src/main/resources=UTF-8
+encoding/=UTF-8
diff --git a/jogo-oito/.settings/org.eclipse.jdt.core.prefs b/eight_game/.settings/org.eclipse.jdt.core.prefs
similarity index 55%
rename from jogo-oito/.settings/org.eclipse.jdt.core.prefs
rename to eight_game/.settings/org.eclipse.jdt.core.prefs
index bc0c2ff..b5490a0 100644
--- a/jogo-oito/.settings/org.eclipse.jdt.core.prefs
+++ b/eight_game/.settings/org.eclipse.jdt.core.prefs
@@ -1,8 +1,8 @@
eclipse.preferences.version=1
-org.eclipse.jdt.core.compiler.codegen.targetPlatform=18
-org.eclipse.jdt.core.compiler.compliance=18
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
+org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
-org.eclipse.jdt.core.compiler.release=disabled
-org.eclipse.jdt.core.compiler.source=18
+org.eclipse.jdt.core.compiler.release=enabled
+org.eclipse.jdt.core.compiler.source=11
diff --git a/jogo-oito/.settings/org.eclipse.m2e.core.prefs b/eight_game/.settings/org.eclipse.m2e.core.prefs
similarity index 100%
rename from jogo-oito/.settings/org.eclipse.m2e.core.prefs
rename to eight_game/.settings/org.eclipse.m2e.core.prefs
diff --git a/eight_game/nbactions.xml b/eight_game/nbactions.xml
new file mode 100644
index 0000000..00d47f1
--- /dev/null
+++ b/eight_game/nbactions.xml
@@ -0,0 +1,30 @@
+
+
+
+ run
+
+ jar
+
+
+ clean
+ javafx:run
+
+
+
+ jlink
+
+ clean
+ javafx:jlink
+
+
+
+ debug
+
+ clean
+ javafx:run@debug
+
+
+ true
+
+
+
diff --git a/eight_game/pom.xml b/eight_game/pom.xml
new file mode 100644
index 0000000..8d1173c
--- /dev/null
+++ b/eight_game/pom.xml
@@ -0,0 +1,71 @@
+
+ 4.0.0
+ com.j0suetm
+ eight_game
+ 0.0.1-SNAPSHOT
+
+ UTF-8
+ 11
+ 11
+
+
+
+ org.openjfx
+ javafx-controls
+ 21-ea+17
+
+
+ org.openjfx
+ javafx-base
+ 21-ea+21
+
+
+ org.openjfx
+ javafx-media
+ 21-ea+21
+
+
+ org.junit.jupiter
+ junit-jupiter-api
+ 5.10.0-M1
+ test
+
+
+ org.testfx
+ testfx-junit5
+ 4.0.16-alpha
+ test
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.8.0
+
+ 11
+
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+ 2.22.1
+
+
+ --add-exports javafx.graphics/com.sun.javafx.application=ALL-UNNAMED
+
+
+
+
+ org.openjfx
+ javafx-maven-plugin
+ 0.0.4
+
+ com.j0suetm.eight_game.App
+
+
+
+
+
diff --git a/eight_game/src/main/java/com/j0suetm/eight_game/App.java b/eight_game/src/main/java/com/j0suetm/eight_game/App.java
new file mode 100644
index 0000000..00e6171
--- /dev/null
+++ b/eight_game/src/main/java/com/j0suetm/eight_game/App.java
@@ -0,0 +1,24 @@
+/**
+ * file: App.java
+ * author: Josué Teodoro Moreira
+ * date: Jun 16, 2023
+ */
+
+package com.j0suetm.eight_game;
+
+import javafx.application.Application;
+import javafx.stage.Stage;
+
+public class App extends Application {
+ private Game game;
+
+ @Override
+ public void start(Stage stage) {
+ this.game = new Game("Jogo do 8 - Josué Teodoro", 600, 640, true);
+ game.show(stage);
+ }
+
+ public static void main(String[] args) {
+ launch(args);
+ }
+}
\ No newline at end of file
diff --git a/eight_game/src/main/java/com/j0suetm/eight_game/Game.java b/eight_game/src/main/java/com/j0suetm/eight_game/Game.java
new file mode 100644
index 0000000..7a9a532
--- /dev/null
+++ b/eight_game/src/main/java/com/j0suetm/eight_game/Game.java
@@ -0,0 +1,95 @@
+/**
+ * file: Game.java
+ * author: Josué Teodoro Moreira
+ * date: Jun 16, 2023
+ */
+
+package com.j0suetm.eight_game;
+
+import com.j0suetm.eight_game.components.Board;
+import com.j0suetm.eight_game.components.RestartButton;
+
+import javafx.geometry.Point2D;
+import javafx.geometry.Pos;
+import javafx.scene.Scene;
+import javafx.scene.layout.StackPane;
+import javafx.stage.Stage;
+
+public class Game {
+ private Board board;
+ private String title;
+ private Point2D size;
+ private RestartButton restartButton;
+ private Stage stage;
+ private Scene scene;
+ private StackPane stackPane;
+
+ public Game(String title, int width, int height, boolean shouldShuffle) {
+ this.title = title;
+ this.size = new Point2D(width, height);
+
+ this.board = new Board(width, height - 40, shouldShuffle);
+ this.restartButton = new RestartButton((int)this.size.getX(), 40);
+ this.restartButton.addEventHandler(this.board);
+
+ this.stackPane = new StackPane(this.restartButton.getButton(), this.board.getGridPane());
+ this.stackPane.setAlignment(Pos.BOTTOM_CENTER);
+
+ this.scene = new Scene(this.stackPane, this.size.getX(),this.size.getY());
+ }
+
+ public void show(Stage stage) {
+ this.stage = stage;
+ this.stage.setTitle(this.title);
+ this.stage.setScene(this.scene);
+ this.stage.show();
+ }
+
+ public Scene getScene() {
+ return scene;
+ }
+
+ public void setScene(Scene scene) {
+ this.scene = scene;
+ }
+
+ public StackPane getStackPane() {
+ return stackPane;
+ }
+
+ public void setStackPane(StackPane stackPane) {
+ this.stackPane = stackPane;
+ }
+
+ public Board getBoard() {
+ return board;
+ }
+
+ public void setBoard(Board board) {
+ this.board = board;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ public Point2D getSize() {
+ return size;
+ }
+
+ public void setSize(Point2D size) {
+ this.size = size;
+ }
+
+ public RestartButton getRestartButton() {
+ return restartButton;
+ }
+
+ public void setRestartButton(RestartButton restartButton) {
+ this.restartButton = restartButton;
+ }
+}
diff --git a/eight_game/src/main/java/com/j0suetm/eight_game/components/Board.java b/eight_game/src/main/java/com/j0suetm/eight_game/components/Board.java
new file mode 100644
index 0000000..aa36478
--- /dev/null
+++ b/eight_game/src/main/java/com/j0suetm/eight_game/components/Board.java
@@ -0,0 +1,167 @@
+/**
+ * file: components/Board.java
+ * author: Josué Teodoro Moreira
+ * date: Jun 16, 2023
+ */
+
+package com.j0suetm.eight_game.components;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import javafx.event.EventHandler;
+import javafx.geometry.Point2D;
+import javafx.geometry.Pos;
+import javafx.scene.control.Alert;
+import javafx.scene.control.Alert.AlertType;
+import javafx.scene.input.KeyCode;
+import javafx.scene.input.KeyEvent;
+import javafx.scene.input.MouseEvent;
+import javafx.scene.layout.GridPane;
+
+public class Board {
+ private GridPane gridPane;
+ private final Point2D gridSize = new Point2D(3, 3);
+ private List tiles;
+ private Point2D size;
+ private Point2D blankTile;
+
+ public Board(int width, int height, boolean shouldShuffle) {
+ this.size = new Point2D(width, height);
+ this.gridPane = new GridPane();
+ this.gridPane.setMinSize(this.size.getX(), this.size.getY());
+ this.gridPane.setAlignment(Pos.TOP_CENTER);
+ this.blankTile = new Point2D(2, 2);
+
+ this.buildTiles();
+ if (shouldShuffle)
+ this.shuffleTiles();
+ }
+
+ private void buildTiles() {
+ Point2D defaultTileSize =
+ new Point2D(this.size.getX() / this.gridSize.getX(),
+ this.size.getY() / this.gridSize.getY());
+
+ this.tiles = new ArrayList(8);
+ // utilizo for somente para ter um número crescente, para dar nome
+ // aos `tiles` sem precisar de fazer um por um.
+ for (int i = 0; i < 8; ++i) {
+ Tile tile = new Tile(Integer.toString(i + 1), defaultTileSize);
+ this.gridPane.add(tile.getButton(), 0, 0);
+ tile.moveToRelativePosition(i);
+ this.addEventHandlerToTile(tile);
+
+ this.tiles.add(tile);
+ }
+ }
+
+ private void addEventHandlerToTile(Tile tile) {
+ tile.getButton().addEventFilter(MouseEvent.MOUSE_CLICKED,
+ new EventHandler() {
+ @Override
+ public void handle(MouseEvent event) {
+ moveTileToBlankSpot(tile);
+ checkSuccess();
+ }
+ }
+ );
+
+ tile.getButton().addEventFilter(KeyEvent.KEY_PRESSED,
+ new EventHandler() {
+ @Override
+ public void handle(KeyEvent event) {
+ KeyCode code = event.getCode();
+ if (!tile.getMovementMap().containsKey(code))
+ return;
+
+ TileMovement movement = tile.getMovementMap().get(code);
+ Point2D newPosition = tile.getPosition().add(movement.getDirection());
+ if (!newPosition.equals(blankTile))
+ return;
+
+ blankTile = tile.getPosition();
+ tile.move(movement);
+ checkSuccess();
+ }
+ }
+ );
+ }
+
+ private void moveTileToBlankSpot(Tile tile) {
+ Point2D whereCanMove = whereCanTileMove(tile);
+ if (whereCanMove.equals(tile.getPosition()))
+ return;
+
+ blankTile = tile.getPosition();
+ tile.moveToPosition(whereCanMove);
+ }
+
+ private Point2D whereCanTileMove(Tile tile) {
+ Point2D tilePos = tile.getPosition();
+
+ // vertical ou horizontal
+ boolean isAlignedV = tilePos.getX() == this.blankTile.getX();
+ boolean isAlignedH = tilePos.getY() == this.blankTile.getY();
+
+ Point2D newPosition = tilePos;
+ Point2D distance = new Point2D(0, 0);
+ if (!isAlignedH && !isAlignedV) {
+ return tilePos;
+ } else if (isAlignedH) {
+ distance = new Point2D((int)(blankTile.getX() - tilePos.getX()), 0);
+ } else if (isAlignedV) {
+ distance = new Point2D(0, (int)(blankTile.getY() - tilePos.getY()));
+ }
+
+ if (distance.getX() > 1 || distance.getY() > 1)
+ return tilePos;
+
+ newPosition = newPosition.add(distance);
+
+ return newPosition;
+ }
+
+ // não executa caso pelo menos uma `tile` estiver fora de seu lugar
+ private void checkSuccess() {
+ if (!this.isComplete())
+ return;
+
+ Alert alert = new Alert(AlertType.INFORMATION);
+ alert.setTitle("Parabéns!!");
+ alert.setHeaderText(null);
+ alert.setContentText("Parabéns!! Você ganhou!");
+
+ alert.showAndWait();
+ }
+
+ public boolean isComplete() {
+ for (Tile tile : this.tiles) {
+ String tileText = tile.getButton().getText();
+ String tileRelativePosition = Integer.toString(tiles.indexOf(tile) + 1);
+ if (!tileText.equals(tileRelativePosition)) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ public void shuffleTiles() {
+ Collections.shuffle(this.tiles);
+ for (Tile tile : this.tiles)
+ tile.moveToRelativePosition(this.tiles.indexOf(tile));
+ }
+
+ public GridPane getGridPane() {
+ return this.gridPane;
+ }
+
+ public List getTiles() {
+ return this.tiles;
+ }
+
+ public Tile getTileAt(int position) {
+ return this.tiles.get(position);
+ }
+}
diff --git a/eight_game/src/main/java/com/j0suetm/eight_game/components/Movable.java b/eight_game/src/main/java/com/j0suetm/eight_game/components/Movable.java
new file mode 100644
index 0000000..de73ef3
--- /dev/null
+++ b/eight_game/src/main/java/com/j0suetm/eight_game/components/Movable.java
@@ -0,0 +1,15 @@
+/**
+ * file: components/Movable.java
+ * author: Josué Teodoro Moreira
+ * date: Jun 17, 2023
+ */
+
+package com.j0suetm.eight_game.components;
+
+import javafx.geometry.Point2D;
+
+public interface Movable {
+ public void move(TileMovement direction);
+ public void moveToPosition(Point2D newPosition);
+ public void moveToRelativePosition(int relativePosition);
+}
diff --git a/eight_game/src/main/java/com/j0suetm/eight_game/components/RestartButton.java b/eight_game/src/main/java/com/j0suetm/eight_game/components/RestartButton.java
new file mode 100644
index 0000000..07fc688
--- /dev/null
+++ b/eight_game/src/main/java/com/j0suetm/eight_game/components/RestartButton.java
@@ -0,0 +1,28 @@
+package com.j0suetm.eight_game.components;
+
+import javafx.event.ActionEvent;
+import javafx.event.EventHandler;
+import javafx.scene.control.Button;
+
+public class RestartButton {
+ private Button button;
+
+ public RestartButton(int width, int height) {
+ this.button = new Button("Reiniciar");
+ this.button.setMinSize(width, height);
+ }
+
+ public void addEventHandler(Board board) {
+ this.button.setOnAction(new EventHandler() {
+ @Override
+ public void handle(ActionEvent evt) {
+ board.shuffleTiles();
+ }
+ }
+ );
+ }
+
+ public Button getButton() {
+ return this.button;
+ }
+}
diff --git a/eight_game/src/main/java/com/j0suetm/eight_game/components/Tile.java b/eight_game/src/main/java/com/j0suetm/eight_game/components/Tile.java
new file mode 100644
index 0000000..2f7aa4f
--- /dev/null
+++ b/eight_game/src/main/java/com/j0suetm/eight_game/components/Tile.java
@@ -0,0 +1,67 @@
+/**
+ * file: components/Tile.java
+ * author: Josué Teodoro Moreira
+ * date: Jun 16, 2023
+ */
+
+package com.j0suetm.eight_game.components;
+
+import java.util.HashMap;
+
+import javafx.geometry.Point2D;
+import javafx.scene.control.Button;
+import javafx.scene.input.KeyCode;
+import javafx.scene.layout.GridPane;
+
+public class Tile implements Movable {
+ private Button button;
+ Point2D position;
+ private HashMap movementMap;
+
+ public Tile(String text, Point2D size) {
+ this.button = new Button(text);
+ this.button.setMinSize(size.getX(), size.getY());
+
+ this.movementMap = new HashMap(4);
+ this.movementMap.put(KeyCode.LEFT, TileMovement.LEFT);
+ this.movementMap.put(KeyCode.RIGHT, TileMovement.RIGHT);
+ this.movementMap.put(KeyCode.UP, TileMovement.UP);
+ this.movementMap.put(KeyCode.DOWN, TileMovement.DOWN);
+ }
+
+ @Override
+ public void move(TileMovement direction) {
+ this.position = this.position.add(direction.getDirection());
+ this.updateGridConstraints();
+ }
+
+ @Override
+ public void moveToPosition(Point2D newPosition) {
+ this.position = newPosition;
+ this.updateGridConstraints();
+ }
+
+ // A posicao é relativa à posição na Array de Tiles
+ // na Board, que é convertida aqui em em coluna e linha.
+ @Override
+ public void moveToRelativePosition(int relativePosition) {
+ this.position = new Point2D((relativePosition + 3) % 3, relativePosition / 3);
+ this.updateGridConstraints();
+ }
+
+ private void updateGridConstraints() {
+ GridPane.setConstraints(this.button, (int)this.position.getX(), (int)this.position.getY());
+ }
+
+ public Button getButton() {
+ return this.button;
+ }
+
+ public Point2D getPosition() {
+ return this.position;
+ }
+
+ public HashMap getMovementMap() {
+ return this.movementMap;
+ }
+}
diff --git a/eight_game/src/main/java/com/j0suetm/eight_game/components/TileMovement.java b/eight_game/src/main/java/com/j0suetm/eight_game/components/TileMovement.java
new file mode 100644
index 0000000..4a2898a
--- /dev/null
+++ b/eight_game/src/main/java/com/j0suetm/eight_game/components/TileMovement.java
@@ -0,0 +1,26 @@
+/**
+ * file: components/TileMovement.java
+ * author: Josué Teodoro Moreira
+ * date: Jun 17, 2023
+ */
+
+package com.j0suetm.eight_game.components;
+
+import javafx.geometry.Point2D;
+
+public enum TileMovement {
+ LEFT(new Point2D(-1, 0)),
+ RIGHT(new Point2D(1, 0)),
+ UP(new Point2D(0, -1)),
+ DOWN(new Point2D(0, 1));
+
+ private Point2D direction;
+
+ TileMovement(Point2D direction) {
+ this.direction = direction;
+ }
+
+ public Point2D getDirection () {
+ return this.direction;
+ }
+}
diff --git a/eight_game/src/main/java/module-info.java b/eight_game/src/main/java/module-info.java
new file mode 100644
index 0000000..600edae
--- /dev/null
+++ b/eight_game/src/main/java/module-info.java
@@ -0,0 +1,4 @@
+module com.j0suetm.eight_game {
+ requires javafx.controls;
+ exports com.j0suetm.eight_game;
+}
\ No newline at end of file
diff --git a/jogo-oito/.gitignore b/jogo-oito/.gitignore
deleted file mode 100644
index b83d222..0000000
--- a/jogo-oito/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
-/target/
diff --git a/jogo-oito/.settings/org.eclipse.core.resources.prefs b/jogo-oito/.settings/org.eclipse.core.resources.prefs
deleted file mode 100644
index 99f26c0..0000000
--- a/jogo-oito/.settings/org.eclipse.core.resources.prefs
+++ /dev/null
@@ -1,2 +0,0 @@
-eclipse.preferences.version=1
-encoding/=UTF-8
diff --git a/jogo-oito/pom.xml b/jogo-oito/pom.xml
deleted file mode 100644
index ebc4845..0000000
--- a/jogo-oito/pom.xml
+++ /dev/null
@@ -1,23 +0,0 @@
-
- 4.0.0
- com.bempaggo.jogo
- jogo-do-oito-do-estagiario-gpt
- 0.0.1-SNAPSHOT
-
- UTF-8
-
-
- rest-unity-test
-
-
- org.apache.maven.plugins
- maven-compiler-plugin
- 3.8.0
-
- 18
- 18
-
-
-
-
-
\ No newline at end of file
diff --git a/jogo-oito/src/main/java/chat/gpt/JogoDosOito.java b/jogo-oito/src/main/java/chat/gpt/JogoDosOito.java
deleted file mode 100644
index e51dd08..0000000
--- a/jogo-oito/src/main/java/chat/gpt/JogoDosOito.java
+++ /dev/null
@@ -1,158 +0,0 @@
-package chat.gpt;
-
-import java.awt.Font;
-import java.awt.GridLayout;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
-import java.awt.event.KeyEvent;
-import java.awt.event.KeyListener;
-
-import javax.swing.JButton;
-import javax.swing.JFrame;
-import javax.swing.JLabel;
-import javax.swing.JOptionPane;
-
-public class JogoDosOito extends JFrame implements KeyListener {
-
- private int[][] tabuleiro = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } };
- private JButton[][] botoes = new JButton[3][3];
- private JButton botaoReiniciar;
-
- public JogoDosOito() {
- super("Jogo dos Oito");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setSize(300, 300);
- setLayout(new GridLayout(4, 3));
-
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- JButton botao = new JButton();
- botao.setFont(new Font("Arial", Font.BOLD, 36));
- botoes[i][j] = botao;
- add(botao);
- }
- }
-
- botaoReiniciar = new JButton("Reiniciar");
- botaoReiniciar.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- reiniciarJogo();
- }
- });
- add(new JLabel(""));
- add(botaoReiniciar);
- add(new JLabel(""));
-
- addKeyListener(this);
- setFocusable(true);
- atualizarTabuleiro();
- setVisible(true);
- }
-
- public void keyPressed(KeyEvent e) {
- int keyCode = e.getKeyCode();
- switch (keyCode) {
- case KeyEvent.VK_UP:
- mover(1, 0);
- break;
- case KeyEvent.VK_DOWN:
- mover(-1, 0);
- break;
- case KeyEvent.VK_LEFT:
- mover(0, 1);
- break;
- case KeyEvent.VK_RIGHT:
- mover(0, -1);
- break;
- }
- }
-
- public void keyTyped(KeyEvent e) {
- }
-
- public void keyReleased(KeyEvent e) {
- }
-
- private void mover(int linha, int coluna) {
- int linhaVazia = -1;
- int colunaVazia = -1;
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- if (tabuleiro[i][j] == 0) {
- linhaVazia = i;
- colunaVazia = j;
- }
- }
- }
- int novaLinha = linhaVazia + linha;
- int novaColuna = colunaVazia + coluna;
- if (novaLinha < 0 || novaLinha > 2 || novaColuna < 0 || novaColuna > 2) {
- // movimento inválido
- return;
- }
- tabuleiro[linhaVazia][colunaVazia] = tabuleiro[novaLinha][novaColuna];
- tabuleiro[novaLinha][novaColuna] = 0;
- atualizarTabuleiro();
- if (jogoConcluido()) {
- JOptionPane.showMessageDialog(this, "Parabéns, você venceu!");
- reiniciarJogo();
- }
- }
-
- public static void main(String[] args) {
- new JogoDosOito();
- }
-
- private boolean jogoConcluido() {
- int count = 1;
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- if (tabuleiro[i][j] != count % 9) {
- return false;
- }
- count++;
- }
- }
- return true;
- }
-
- private boolean movimentarPeca(int linha, int coluna) {
- if (linha > 0 && tabuleiro[linha - 1][coluna] == 0) {
- tabuleiro[linha - 1][coluna] = tabuleiro[linha][coluna];
- tabuleiro[linha][coluna] = 0;
- return true;
- } else if (linha < 2 && tabuleiro[linha + 1][coluna] == 0) {
- tabuleiro[linha + 1][coluna] = tabuleiro[linha][coluna];
- tabuleiro[linha][coluna] = 0;
- return true;
- } else if (coluna > 0 && tabuleiro[linha][coluna - 1] == 0) {
- tabuleiro[linha][coluna - 1] = tabuleiro[linha][coluna];
- tabuleiro[linha][coluna] = 0;
- return true;
- } else if (coluna < 2 && tabuleiro[linha][coluna + 1] == 0) {
- tabuleiro[linha][coluna + 1] = tabuleiro[linha][coluna];
- tabuleiro[linha][coluna] = 0;
- return true;
- }
- return false;
- }
-
- private void atualizarTabuleiro() {
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- JButton botao = botoes[i][j];
- int valor = tabuleiro[i][j];
- if (valor == 0) {
- botao.setText("");
- } else {
- botao.setText(String.valueOf(valor));
- }
- }
- }
- }
-
- private void reiniciarJogo() {
- tabuleiro = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } };
- atualizarTabuleiro();
- }
-}