diff --git a/jogo-oito/.classpath b/jogo-oito/.classpath
index 1d97b2f..d149464 100644
--- a/jogo-oito/.classpath
+++ b/jogo-oito/.classpath
@@ -23,5 +23,35 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/jogo-oito/.project b/jogo-oito/.project
index 71ae64e..119be93 100644
--- a/jogo-oito/.project
+++ b/jogo-oito/.project
@@ -20,4 +20,15 @@
org.eclipse.jdt.core.javanature
org.eclipse.m2e.core.maven2Nature
+
+
+ 1689274720542
+
+ 30
+
+ org.eclipse.core.resources.regexFilterMatcher
+ node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__
+
+
+
diff --git a/jogo-oito/.settings/org.eclipse.core.resources.prefs b/jogo-oito/.settings/org.eclipse.core.resources.prefs
index 99f26c0..f9fe345 100644
--- a/jogo-oito/.settings/org.eclipse.core.resources.prefs
+++ b/jogo-oito/.settings/org.eclipse.core.resources.prefs
@@ -1,2 +1,4 @@
eclipse.preferences.version=1
+encoding//src/main/java=UTF-8
+encoding//src/test/java=UTF-8
encoding/=UTF-8
diff --git a/jogo-oito/.settings/org.eclipse.jdt.apt.core.prefs b/jogo-oito/.settings/org.eclipse.jdt.apt.core.prefs
new file mode 100644
index 0000000..d4313d4
--- /dev/null
+++ b/jogo-oito/.settings/org.eclipse.jdt.apt.core.prefs
@@ -0,0 +1,2 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.apt.aptEnabled=false
diff --git a/jogo-oito/.settings/org.eclipse.jdt.core.prefs b/jogo-oito/.settings/org.eclipse.jdt.core.prefs
index bc0c2ff..7127a22 100644
--- a/jogo-oito/.settings/org.eclipse.jdt.core.prefs
+++ b/jogo-oito/.settings/org.eclipse.jdt.core.prefs
@@ -4,5 +4,6 @@ org.eclipse.jdt.core.compiler.compliance=18
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.processAnnotations=disabled
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=18
diff --git a/jogo-oito/src/main/java/Main.java b/jogo-oito/src/main/java/Main.java
new file mode 100644
index 0000000..b7416c4
--- /dev/null
+++ b/jogo-oito/src/main/java/Main.java
@@ -0,0 +1,9 @@
+import view.JogoDosOito;
+
+public class Main {
+
+ public static void main(String[] args) {
+ JogoDosOito game = new JogoDosOito();
+ }
+
+}
diff --git a/jogo-oito/src/main/java/facade/Controller.java b/jogo-oito/src/main/java/facade/Controller.java
index 28c6df1..d61cbf3 100644
--- a/jogo-oito/src/main/java/facade/Controller.java
+++ b/jogo-oito/src/main/java/facade/Controller.java
@@ -9,8 +9,8 @@ public class Controller {
private final Graph board;
- public Controller() {
- this.board = new Board();
+ public Controller(Graph graph) {
+ this.board = graph;
}
public void feedback() {
@@ -29,8 +29,8 @@ public void swap(Integer keyCode) {
this.board.swap(keyCode);
}
- public Boolean checkGameOver() {
- return this.board.checkGameOver();
+ public Boolean checkVicory() {
+ return this.board.checkVictory();
}
diff --git a/jogo-oito/src/main/java/interfaces/DefinableAdjacent.java b/jogo-oito/src/main/java/interfaces/DefinableAdjacent.java
new file mode 100644
index 0000000..3e29d83
--- /dev/null
+++ b/jogo-oito/src/main/java/interfaces/DefinableAdjacent.java
@@ -0,0 +1,5 @@
+package interfaces;
+
+public interface DefinableAdjacent {
+ void defineAdjacent();
+}
diff --git a/jogo-oito/src/main/java/interfaces/Graph.java b/jogo-oito/src/main/java/interfaces/Graph.java
index e22aad8..8530ef9 100644
--- a/jogo-oito/src/main/java/interfaces/Graph.java
+++ b/jogo-oito/src/main/java/interfaces/Graph.java
@@ -24,5 +24,5 @@ public interface Graph {
void click(Integer cellValue);
- Boolean checkGameOver();
+ Boolean checkVictory();
}
diff --git a/jogo-oito/src/main/java/model/Matrix.java b/jogo-oito/src/main/java/model/Matrix.java
index 29586ea..f5d9d13 100644
--- a/jogo-oito/src/main/java/model/Matrix.java
+++ b/jogo-oito/src/main/java/model/Matrix.java
@@ -4,6 +4,7 @@
*/
package model;
+import interfaces.DefinableAdjacent;
import interfaces.Vertex;
import java.util.ArrayList;
import java.util.List;
@@ -12,38 +13,37 @@
*
* @author allen
*/
-public final class Matrix {
+public final class Matrix implements DefinableAdjacent{
- private final Row firstRow;
- private final Row secondRow;
- private final Row thirdRow;
+ private List rows;
+ private final int NUMBER_OF_ROWS = 3;
public static List cells;
-
public Matrix() {
Matrix.cells = new ArrayList<>();
Cell.content = 1;
- this.firstRow = new Row();
- this.secondRow = new Row();
- this.thirdRow = new Row();
+ this.rows = new ArrayList<>();
+ for(int i =0; i < NUMBER_OF_ROWS; i++){
+ this.rows.add(new Row());
+ }
this.defineAdjacent();
}
- 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);
-
+ public void defineAdjacent() {
+ for(int i = 0; i < NUMBER_OF_ROWS - 1; i++){
+ Row upRow = rows.get(i);
+ Row downRow = rows.get(i + 1);
+ for(int j = 0; j < upRow.cells.size();j++){
+ Vertex upCell = upRow.cells.get(j);
+ Vertex downCell = downRow.cells.get(j);
+ upCell.creatingVerticalAdjacent(downCell);
+ }
+ }
this.changePositionToValidateTemplate();
}
private void changePositionToValidateTemplate(){
- this.thirdRow.last.setValue(0);
+ this.rows.get(NUMBER_OF_ROWS - 1).cells.get(2).setValue(0);
}
@@ -51,30 +51,33 @@ public List getCells() {
return Matrix.cells;
}
- private final class Row {
+ public List getRows() {
+ return this.rows;
+ }
- public final Cell initial;
- public final Cell center;
- public final Cell last;
+ private final class Row implements DefinableAdjacent{
+ private List cells;
+ private final int NUMBER_OF_CELLS = 3;
public Row() {
- this.initial = new Cell();
- this.center = new Cell();
- this.last = new Cell();
+ this.cells = new ArrayList<>();
+ for(int i = 0; i < NUMBER_OF_CELLS; i++){
+ this.cells.add(new Cell());
+ }
this.defineAdjacent();
this.loadCells();
}
public void loadCells() {
- Matrix.cells.add(this.initial);
- Matrix.cells.add(this.center);
- Matrix.cells.add(this.last);
+ Matrix.cells.addAll(this.cells);
}
public void defineAdjacent() {
- this.initial.creatingHorizontalAdjacent(this.center);
- this.center.creatingHorizontalAdjacent(this.last);
+ for(int i = 0; i < NUMBER_OF_CELLS - 1; i++){
+ Vertex leftCell = this.cells.get(i);
+ Vertex rightCell = this.cells.get(i + 1);
+ leftCell.creatingHorizontalAdjacent(rightCell);
+ }
}
-
}
}
diff --git a/jogo-oito/src/main/java/util/Board.java b/jogo-oito/src/main/java/util/Board.java
index e71e83e..8b1ad74 100644
--- a/jogo-oito/src/main/java/util/Board.java
+++ b/jogo-oito/src/main/java/util/Board.java
@@ -85,7 +85,7 @@ public Vertex getEmptyCell() {
}
@Override
- public Boolean checkGameOver() {
+ public Boolean checkVictory() {
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 2b68755..f8ca69f 100644
--- a/jogo-oito/src/main/java/view/JogoDosOito.java
+++ b/jogo-oito/src/main/java/view/JogoDosOito.java
@@ -2,6 +2,8 @@
import facade.Controller;
import interfaces.Vertex;
+import util.Board;
+
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
@@ -26,9 +28,12 @@ public class JogoDosOito extends JFrame implements KeyListener {
public JogoDosOito() {
super("Jogo dos Oito");
- this.controller = new Controller();
+ this.controller = new Controller(new Board());
this.controller.setting();
this.buttons = new ArrayList<>();
+ createButtons();
+ configMenu();
+ configureInterface();
}
private void configureInterface() {
@@ -61,15 +66,14 @@ private JButton configButton(Vertex cell) {
button.addActionListener((ActionEvent e) -> {
this.controller.click(this.textToValue(button.getText()));
this.updateBoard();
- this.checkGameOver();
+ this.checkVictory();
SwingUtilities.getRoot(button).requestFocus();
});
return button;
-
}
- private void checkGameOver() {
- Optional.ofNullable(this.controller.checkGameOver())
+ private void checkVictory() {
+ Optional.ofNullable(this.controller.checkVicory())
.filter(Boolean::booleanValue)
.ifPresent(gameOver -> {
JOptionPane.showMessageDialog(this, "ParabĂ©ns, vocĂȘ venceu!");
@@ -131,18 +135,10 @@ public void keyTyped(KeyEvent e) {
public void keyPressed(KeyEvent e) {
this.controller.swap(e.getKeyCode());
this.updateBoard();
- this.checkGameOver();
+ this.checkVictory();
}
@Override
public void keyReleased(KeyEvent e) {
}
-
- public static void main(String[] args) {
- JogoDosOito game = new JogoDosOito();
- game.createButtons();
- game.configMenu();
- game.configureInterface();
-
- }
}
diff --git a/jogo-oito/src/test/java/game/BoardTest.java b/jogo-oito/src/test/java/game/BoardTest.java
index 7b39668..14c346a 100644
--- a/jogo-oito/src/test/java/game/BoardTest.java
+++ b/jogo-oito/src/test/java/game/BoardTest.java
@@ -76,10 +76,10 @@ public void testClick() {
@Test
public void testCheckGameOver() {
- Assertions.assertTrue(this.board.checkGameOver());
+ Assertions.assertTrue(this.board.checkVictory());
board = new Board();
board.setting();
- Assertions.assertFalse(board.checkGameOver());
+ Assertions.assertFalse(board.checkVictory());
}
diff --git a/jogo-oito/src/test/java/model/MatrixTest.java b/jogo-oito/src/test/java/model/MatrixTest.java
new file mode 100644
index 0000000..4bf1308
--- /dev/null
+++ b/jogo-oito/src/test/java/model/MatrixTest.java
@@ -0,0 +1,31 @@
+package model;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import interfaces.Vertex;
+
+public class MatrixTest {
+
+ private Matrix matrix;
+
+ @BeforeEach
+ void setup(){
+ matrix = new Matrix();
+ }
+
+ @Test
+ void getCells_ReturnListOfCells(){
+ int numberOfCellsExpected = 9;
+ Assertions.assertFalse(matrix.getCells().isEmpty());
+ Assertions.assertEquals(numberOfCellsExpected, matrix.getCells().size());
+ }
+
+ @Test
+ void defineAdjacent() {
+ int numberofRowsExpected = 3;
+ Assertions.assertNotNull(matrix.getRows());
+ Assertions.assertTrue(matrix.getRows().size() == numberofRowsExpected);
+ }
+}