Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions jogo-oito/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,35 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
11 changes: 11 additions & 0 deletions jogo-oito/.project
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,15 @@
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
<filteredResources>
<filter>
<id>1689274720542</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
2 changes: 2 additions & 0 deletions jogo-oito/.settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/test/java=UTF-8
encoding/<project>=UTF-8
2 changes: 2 additions & 0 deletions jogo-oito/.settings/org.eclipse.jdt.apt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
org.eclipse.jdt.apt.aptEnabled=false
1 change: 1 addition & 0 deletions jogo-oito/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions jogo-oito/src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import view.JogoDosOito;

public class Main {

public static void main(String[] args) {
JogoDosOito game = new JogoDosOito();
}

}
8 changes: 4 additions & 4 deletions jogo-oito/src/main/java/facade/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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();

}

Expand Down
5 changes: 5 additions & 0 deletions jogo-oito/src/main/java/interfaces/DefinableAdjacent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package interfaces;

public interface DefinableAdjacent {
void defineAdjacent();
}
2 changes: 1 addition & 1 deletion jogo-oito/src/main/java/interfaces/Graph.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ public interface Graph {

void click(Integer cellValue);

Boolean checkGameOver();
Boolean checkVictory();
}
67 changes: 35 additions & 32 deletions jogo-oito/src/main/java/model/Matrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package model;

import interfaces.DefinableAdjacent;
import interfaces.Vertex;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -12,69 +13,71 @@
*
* @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<Row> rows;
private final int NUMBER_OF_ROWS = 3;
public static List<Vertex> 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++){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Boa tarde,

Nossa ideia é utilizar POO para fugir destes blocos aninhados de for...
o que são i e 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);
}


public List<Vertex> getCells() {
return Matrix.cells;
}

private final class Row {
public List<Row> getRows() {
return this.rows;
}

public final Cell initial;
public final Cell center;
public final Cell last;
private final class Row implements DefinableAdjacent{

private List<Vertex> 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);
}
}

}
}
2 changes: 1 addition & 1 deletion jogo-oito/src/main/java/util/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
24 changes: 10 additions & 14 deletions jogo-oito/src/main/java/view/JogoDosOito.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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!");
Expand Down Expand Up @@ -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();

}
}
4 changes: 2 additions & 2 deletions jogo-oito/src/test/java/game/BoardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());

}

Expand Down
31 changes: 31 additions & 0 deletions jogo-oito/src/test/java/model/MatrixTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}