From 1855248e99c4fac82de9a2bec4ad8e4e39ba0ec7 Mon Sep 17 00:00:00 2001
From: Joao Quintino <>
Date: Mon, 9 Oct 2023 23:31:20 -0300
Subject: [PATCH] Ajustes e melhorias do codigo
---
jogo-oito/pom.xml | 65 +++++++++-------
.../src/main/java/facade/Controller.java | 14 ++--
jogo-oito/src/main/java/interfaces/Edge.java | 10 +--
jogo-oito/src/main/java/interfaces/Graph.java | 14 +---
.../src/main/java/interfaces/Vertex.java | 18 +----
jogo-oito/src/main/java/model/Adjacent.java | 38 +++------
jogo-oito/src/main/java/model/Cell.java | 31 +++-----
jogo-oito/src/main/java/model/Keyboard.java | 15 ++--
jogo-oito/src/main/java/model/Matrix.java | 45 ++---------
jogo-oito/src/main/java/model/Row.java | 28 +++++++
jogo-oito/src/main/java/util/Board.java | 17 ++--
jogo-oito/src/main/java/view/JogoDosOito.java | 77 ++++++++-----------
.../src/test/java/game/AdjacentTest.java | 8 +-
jogo-oito/src/test/java/game/BoardTest.java | 2 +-
14 files changed, 153 insertions(+), 229 deletions(-)
create mode 100644 jogo-oito/src/main/java/model/Row.java
diff --git a/jogo-oito/pom.xml b/jogo-oito/pom.xml
index e11ec3a9..9da2777f 100644
--- a/jogo-oito/pom.xml
+++ b/jogo-oito/pom.xml
@@ -1,31 +1,38 @@
-
- 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
-
-
-
- org.apache.maven.plugins
- maven-surefire-plugin
- 2.22.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
+
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+ 2.22.0
+
+
+
+
+
+ org.projectlombok
+ lombok
+ 1.18.30
+ provided
+
org.junit.jupiter
junit-jupiter-api
@@ -52,4 +59,4 @@
jar
-
\ No newline at end of file
+
diff --git a/jogo-oito/src/main/java/facade/Controller.java b/jogo-oito/src/main/java/facade/Controller.java
index 28c6df1b..4a062d42 100644
--- a/jogo-oito/src/main/java/facade/Controller.java
+++ b/jogo-oito/src/main/java/facade/Controller.java
@@ -3,16 +3,21 @@
import interfaces.Graph;
import interfaces.Vertex;
import java.util.List;
+
import util.Board;
+/**
+ * Classe que representa o controlador do jogo.
+ *
+ * @author quintino
+ */
public class Controller {
-
- private final Graph board;
+ private final Graph board = new Board();
public Controller() {
- this.board = new Board();
+ this.setting();
}
-
+
public void feedback() {
this.board.feedback();
}
@@ -31,7 +36,6 @@ public void swap(Integer keyCode) {
public Boolean checkGameOver() {
return this.board.checkGameOver();
-
}
public void click(Integer cellValue) {
diff --git a/jogo-oito/src/main/java/interfaces/Edge.java b/jogo-oito/src/main/java/interfaces/Edge.java
index 20e56cd0..e103208d 100644
--- a/jogo-oito/src/main/java/interfaces/Edge.java
+++ b/jogo-oito/src/main/java/interfaces/Edge.java
@@ -1,19 +1,13 @@
-/*
- * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
- * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
- */
package interfaces;
import model.Keyboard;
/**
+ * Interface que representa uma aresta do jogo.
*
- * @author allen
+ * @author quintino
*/
public interface Edge {
-
Keyboard getKey();
-
Vertex getCell();
-
}
diff --git a/jogo-oito/src/main/java/interfaces/Graph.java b/jogo-oito/src/main/java/interfaces/Graph.java
index e22aad8e..67e329d9 100644
--- a/jogo-oito/src/main/java/interfaces/Graph.java
+++ b/jogo-oito/src/main/java/interfaces/Graph.java
@@ -1,28 +1,18 @@
-/*
- * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
- * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
- */
package interfaces;
import java.util.List;
/**
+ * Interface que representa o grafo do jogo.
*
- * @author allen
+ * @author quintino
*/
public interface Graph {
-
void feedback();
-
void setting();
-
void swap(Integer keyCode);
-
List getCells();
-
Vertex getEmptyCell();
-
void click(Integer cellValue);
-
Boolean checkGameOver();
}
diff --git a/jogo-oito/src/main/java/interfaces/Vertex.java b/jogo-oito/src/main/java/interfaces/Vertex.java
index 4ff55e4b..2837676a 100644
--- a/jogo-oito/src/main/java/interfaces/Vertex.java
+++ b/jogo-oito/src/main/java/interfaces/Vertex.java
@@ -1,36 +1,22 @@
-/*
- * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
- * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
- */
package interfaces;
import java.util.List;
import model.Keyboard;
/**
+ * Interface que representa um vértice do jogo.
*
- * @author allen
+ * @author quintino
*/
public interface Vertex {
-
void setValue(Integer value);
-
Integer getValue();
-
void creatingHorizontalAdjacent(Vertex cell);
-
void creatingVerticalAdjacent(Vertex cell);
-
String valueToText();
-
Edge getAdjacentByKeyCode(Keyboard key);
-
Vertex click(Keyboard key);
-
List getAdjacents();
-
void addAdjacents(Edge edge);
-
Vertex swapCells(Integer value);
-
}
diff --git a/jogo-oito/src/main/java/model/Adjacent.java b/jogo-oito/src/main/java/model/Adjacent.java
index 6573c4f4..f8ce92f8 100644
--- a/jogo-oito/src/main/java/model/Adjacent.java
+++ b/jogo-oito/src/main/java/model/Adjacent.java
@@ -1,40 +1,22 @@
-/*
- * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
- * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
- */
package model;
import interfaces.Edge;
import interfaces.Vertex;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
import java.util.Objects;
/**
+ * Classe que representa uma aresta do jogo.
*
- * @author allen
+ * @author quintino
*/
-public class Adjacent implements Edge{
-
+@Data
+@AllArgsConstructor
+@EqualsAndHashCode
+public class Adjacent implements Edge {
private final Keyboard key;
private final Vertex cell;
-
- public Adjacent(Keyboard key, Vertex cell) {
- this.key = key;
- this.cell = cell;
- }
-
- @Override
- public Keyboard getKey() {
- return this.key;
- }
-
- @Override
- public Vertex getCell() {
- return this.cell;
- }
-
- @Override
- public boolean equals(Object obj) {
- return Objects.equals(((Adjacent) obj).getKey(), this.getKey());
- }
-
}
diff --git a/jogo-oito/src/main/java/model/Cell.java b/jogo-oito/src/main/java/model/Cell.java
index 744d8d39..1c1bc036 100644
--- a/jogo-oito/src/main/java/model/Cell.java
+++ b/jogo-oito/src/main/java/model/Cell.java
@@ -2,13 +2,22 @@
import interfaces.Edge;
import interfaces.Vertex;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
+/**
+ * Classe que representa uma célula do jogo.
+ *
+ * @author quintino
+ */
+@Data
+@EqualsAndHashCode
public class Cell implements Vertex {
-
private Integer value;
private final List adjacents;
public static Integer content;
@@ -23,16 +32,6 @@ public Cell() {
this.adjacents = new ArrayList<>();
}
- @Override
- public void setValue(Integer value) {
- this.value = value;
- }
-
- @Override
- public Integer getValue() {
- return this.value;
- }
-
@Override
public void creatingHorizontalAdjacent(Vertex cell) {
this.adjacents.add(new Adjacent(Keyboard.LEFT, cell));
@@ -91,19 +90,9 @@ public Vertex swapCells(Integer value) {
.orElse(this);
}
- @Override
- public List getAdjacents() {
- return this.adjacents;
- }
-
@Override
public void addAdjacents(Edge edge) {
this.adjacents.add(edge);
}
- @Override
- public boolean equals(Object obj) {
- return Objects.equals(this.value, ((Cell) obj).value);
- }
-
}
diff --git a/jogo-oito/src/main/java/model/Keyboard.java b/jogo-oito/src/main/java/model/Keyboard.java
index ada8bb7a..b433fb0d 100644
--- a/jogo-oito/src/main/java/model/Keyboard.java
+++ b/jogo-oito/src/main/java/model/Keyboard.java
@@ -1,7 +1,3 @@
-/*
- * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
- * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
- */
package model;
import java.awt.event.KeyEvent;
@@ -10,8 +6,12 @@
import java.util.function.Function;
import java.util.stream.Collectors;
+/**
+ * Enum que representa as teclas do teclado.
+ *
+ * @author quintino
+ */
public enum Keyboard {
-
UP(KeyEvent.VK_UP),
DOWN(KeyEvent.VK_DOWN),
LEFT(KeyEvent.VK_LEFT),
@@ -19,9 +19,8 @@ public enum Keyboard {
private final Integer value;
- private static final Map map = Arrays.stream(Keyboard.values())
- .collect(Collectors.toMap(Keyboard::getValue, Function.identity()));
-
+ private static final Map map =
+ Arrays.stream(Keyboard.values()).collect(Collectors.toMap(Keyboard::getValue, Function.identity()));
Keyboard(Integer value) {
this.value = value;
diff --git a/jogo-oito/src/main/java/model/Matrix.java b/jogo-oito/src/main/java/model/Matrix.java
index 29586eaa..6650fd42 100644
--- a/jogo-oito/src/main/java/model/Matrix.java
+++ b/jogo-oito/src/main/java/model/Matrix.java
@@ -1,7 +1,3 @@
-/*
- * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
- * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
- */
package model;
import interfaces.Vertex;
@@ -9,19 +5,17 @@
import java.util.List;
/**
+ * Classe que representa a matriz do jogo.
*
- * @author allen
+ * @author quintino
*/
public final class Matrix {
-
private final Row firstRow;
private final Row secondRow;
private final Row thirdRow;
- public static List cells;
-
+ public static List cells = new ArrayList<>();
public Matrix() {
- Matrix.cells = new ArrayList<>();
Cell.content = 1;
this.firstRow = new Row();
this.secondRow = new Row();
@@ -32,49 +26,20 @@ public Matrix() {
private void defineAdjacent() {
this.firstRow.initial.creatingVerticalAdjacent(secondRow.initial);
this.secondRow.initial.creatingVerticalAdjacent(thirdRow.initial);
-
this.firstRow.center.creatingVerticalAdjacent(secondRow.center);
this.secondRow.center.creatingVerticalAdjacent(thirdRow.center);
-
this.firstRow.last.creatingVerticalAdjacent(secondRow.last);
this.secondRow.last.creatingVerticalAdjacent(thirdRow.last);
-
this.changePositionToValidateTemplate();
}
-
+
private void changePositionToValidateTemplate(){
this.thirdRow.last.setValue(0);
}
-
+
public List getCells() {
return Matrix.cells;
}
- private final class Row {
-
- public final Cell initial;
- public final Cell center;
- public final Cell last;
-
- public Row() {
- this.initial = new Cell();
- this.center = new Cell();
- this.last = new Cell();
- this.defineAdjacent();
- this.loadCells();
- }
-
- public void loadCells() {
- Matrix.cells.add(this.initial);
- Matrix.cells.add(this.center);
- Matrix.cells.add(this.last);
- }
-
- public void defineAdjacent() {
- this.initial.creatingHorizontalAdjacent(this.center);
- this.center.creatingHorizontalAdjacent(this.last);
- }
-
- }
}
diff --git a/jogo-oito/src/main/java/model/Row.java b/jogo-oito/src/main/java/model/Row.java
new file mode 100644
index 00000000..7fcf7efd
--- /dev/null
+++ b/jogo-oito/src/main/java/model/Row.java
@@ -0,0 +1,28 @@
+package model;
+
+/**
+ * Classe que representa a linha da matriz do jogo.
+ *
+ * @author quintino
+ */
+public class Row {
+ public final Cell initial = new Cell();
+ public final Cell center = new Cell();
+ public final Cell last = new Cell();
+
+ public Row() {
+ this.defineAdjacent();
+ this.loadCells();
+ }
+
+ public void loadCells() {
+ Matrix.cells.add(this.initial);
+ Matrix.cells.add(this.center);
+ Matrix.cells.add(this.last);
+ }
+
+ public void defineAdjacent() {
+ this.initial.creatingHorizontalAdjacent(this.center);
+ this.center.creatingHorizontalAdjacent(this.last);
+ }
+}
diff --git a/jogo-oito/src/main/java/util/Board.java b/jogo-oito/src/main/java/util/Board.java
index e71e83ea..adf73df1 100644
--- a/jogo-oito/src/main/java/util/Board.java
+++ b/jogo-oito/src/main/java/util/Board.java
@@ -12,15 +12,16 @@
import model.Keyboard;
import model.Matrix;
+/**
+ * Classe que representa o tabuleiro do jogo.
+ *
+ * @author quintino
+ */
public class Board implements Graph {
-
private List cells;
private Vertex emptyCell;
private Integer length;
- private Matrix matrix;
-
- public Board() {
- }
+ private Matrix matrix = new Matrix();
@Override
public void feedback() {
@@ -32,18 +33,15 @@ public void feedback() {
@Override
public void setting() {
- this.matrix = new Matrix();
this.cells = this.matrix.getCells();
this.length = cells.size();
this.shuffleCell();
this.defineEmptyCell();
-
}
private void shuffleCell() {
Iterator iterator = this.shuffleValues().iterator();
- this.cells.stream()
- .forEach(vertex -> vertex.setValue(iterator.next()));
+ this.cells.forEach(vertex -> vertex.setValue(iterator.next()));
}
private List shuffleValues() {
@@ -88,7 +86,6 @@ public Vertex getEmptyCell() {
public Boolean checkGameOver() {
return IntStream.range(0, this.length)
.allMatch(index -> this.cells.get(index).getValue() == (index + 1) % this.length);
-
}
}
diff --git a/jogo-oito/src/main/java/view/JogoDosOito.java b/jogo-oito/src/main/java/view/JogoDosOito.java
index 2b687559..21461b3d 100644
--- a/jogo-oito/src/main/java/view/JogoDosOito.java
+++ b/jogo-oito/src/main/java/view/JogoDosOito.java
@@ -17,18 +17,23 @@
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
+/**
+ * Classe responsável por gerenciar a interface gráfica do jogo.
+ *
+ * @author quintino
+ */
public class JogoDosOito extends JFrame implements KeyListener {
-
- private final List buttons;
+ private final List buttons = new ArrayList<>();
private final Controller controller;
- private JButton reset;
- private JButton feedback;
public JogoDosOito() {
super("Jogo dos Oito");
this.controller = new Controller();
- this.controller.setting();
- this.buttons = new ArrayList<>();
+ this.controller.getCells().forEach(this::configButton);
+ this.configReset();
+ this.configFeedback();
+ add(new JLabel(""));
+ this.configureInterface();
}
private void configureInterface() {
@@ -40,21 +45,13 @@ private void configureInterface() {
setFocusable(true);
}
- private void createButtons() {
- this.controller.getCells().forEach(cell -> {
- JButton button = this.configButton(cell);
- add(button);
- buttons.add(button);
- });
- }
-
private Integer textToValue(String text) {
return Optional.ofNullable(text)
.map(Integer::valueOf)
.orElse(0);
}
- private JButton configButton(Vertex cell) {
+ private void configButton(Vertex cell) {
JButton button = new JButton();
button.setFont(new Font("Arial", Font.BOLD, 36));
button.setText(cell.valueToText());
@@ -64,8 +61,8 @@ private JButton configButton(Vertex cell) {
this.checkGameOver();
SwingUtilities.getRoot(button).requestFocus();
});
- return button;
-
+ add(button);
+ buttons.add(button);
}
private void checkGameOver() {
@@ -77,30 +74,23 @@ private void checkGameOver() {
});
}
- private void configMenu() {
- this.reset = this.configReset();
- this.feedback = this.configFeedback();
- add(this.feedback);
- add(this.reset);
- add(new JLabel(""));
- }
-
- private JButton configReset() {
- JButton buttonReset = new JButton("Reiniciar");
- buttonReset.addActionListener((ActionEvent e) -> {
+ private void configReset() {
+ JButton button = new JButton("Reiniciar");
+ button.addActionListener((ActionEvent e) -> {
this.resetGame();
- SwingUtilities.getRoot(buttonReset).requestFocus();
+ SwingUtilities.getRoot(button).requestFocus();
});
- return buttonReset;
+ add(button);
}
-
- private JButton configFeedback() {
- JButton buttonFeedback = new JButton("Gabarito");
- buttonFeedback.addActionListener((ActionEvent e) -> {
- this.showFeedback();
- SwingUtilities.getRoot(buttonFeedback).requestFocus();
+
+ private void configFeedback() {
+ JButton button = new JButton("Gabarito");
+ button.addActionListener((ActionEvent e) -> {
+ this.controller.feedback();
+ this.updateBoard();
+ SwingUtilities.getRoot(button).requestFocus();
});
- return buttonFeedback;
+ add(button);
}
@@ -108,11 +98,6 @@ private void resetGame() {
this.controller.setting();
this.updateBoard();
}
-
- private void showFeedback() {
- this.controller.feedback();
- this.updateBoard();
- }
private void updateBoard() {
List cells = this.controller.getCells();
@@ -125,6 +110,7 @@ private void updateBoard() {
@Override
public void keyTyped(KeyEvent e) {
+ //TODO document why this method is empty
}
@Override
@@ -136,13 +122,10 @@ public void keyPressed(KeyEvent e) {
@Override
public void keyReleased(KeyEvent e) {
+ //TODO document why this method is empty
}
public static void main(String[] args) {
- JogoDosOito game = new JogoDosOito();
- game.createButtons();
- game.configMenu();
- game.configureInterface();
-
+ new JogoDosOito();
}
}
diff --git a/jogo-oito/src/test/java/game/AdjacentTest.java b/jogo-oito/src/test/java/game/AdjacentTest.java
index 49dd2ac3..a59bab20 100644
--- a/jogo-oito/src/test/java/game/AdjacentTest.java
+++ b/jogo-oito/src/test/java/game/AdjacentTest.java
@@ -28,11 +28,11 @@ public void testEquals() {
Vertex cell1 = new Cell(1);
Vertex cell2 = new Cell(2);
- Edge edge1 = new Adjacent(Keyboard.RIGHT, cell2);
- Edge edge2 = new Adjacent(Keyboard.LEFT, cell1);
+ Edge edge = new Adjacent(Keyboard.RIGHT, cell2);
+ Edge edge12 = new Adjacent(Keyboard.LEFT, cell1);
- assertEquals(edge1.getCell().click(Keyboard.RIGHT), cell2);
- assertEquals(edge2.getCell().click(Keyboard.LEFT), cell1);
+ assertEquals(edge.getCell().click(Keyboard.RIGHT), cell2);
+ assertEquals(edge12.getCell().click(Keyboard.LEFT), cell1);
}
diff --git a/jogo-oito/src/test/java/game/BoardTest.java b/jogo-oito/src/test/java/game/BoardTest.java
index 7b39668f..bdb8b99b 100644
--- a/jogo-oito/src/test/java/game/BoardTest.java
+++ b/jogo-oito/src/test/java/game/BoardTest.java
@@ -130,7 +130,7 @@ public void testGetAdjacentByKeyCode() {
Edge adjacent = cell1.getAdjacentByKeyCode(model.Keyboard.LEFT);
assertEquals(cell2, adjacent.getCell());
}
-
+
@Test
public void testClickDown() {
board.swap(Keyboard.DOWN.getValue());