diff --git a/jogo-oito/src/main/java/facade/Controller.java b/jogo-oito/src/main/java/facade/Controller.java deleted file mode 100644 index 28c6df1b..00000000 --- a/jogo-oito/src/main/java/facade/Controller.java +++ /dev/null @@ -1,41 +0,0 @@ -package facade; - -import interfaces.Graph; -import interfaces.Vertex; -import java.util.List; -import util.Board; - -public class Controller { - - private final Graph board; - - public Controller() { - this.board = new Board(); - } - - public void feedback() { - this.board.feedback(); - } - - public void setting() { - this.board.setting(); - } - - public List getCells() { - return this.board.getCells(); - } - - public void swap(Integer keyCode) { - this.board.swap(keyCode); - } - - public Boolean checkGameOver() { - return this.board.checkGameOver(); - - } - - public void click(Integer cellValue) { - this.board.click(cellValue); - } - -} diff --git a/jogo-oito/src/main/java/facade/GameController.java b/jogo-oito/src/main/java/facade/GameController.java new file mode 100644 index 00000000..4ea21949 --- /dev/null +++ b/jogo-oito/src/main/java/facade/GameController.java @@ -0,0 +1,39 @@ +package facade; + +import java.util.List; + +import interfaces.GameGraph; +import interfaces.GameVertex; +import util.GameBoard; + +public class GameController { + private final GameGraph gameBoard; + + public GameController() { + this.gameBoard = new GameBoard(); + } + + public void provideFeedback() { + this.gameBoard.provideFeedback(); + } + + public void setupGame() { + this.gameBoard.setupGame(); + } + + public List getGameCells() { + return this.gameBoard.getGameCells(); + } + + public void performSwap(Integer keyCode) { + this.gameBoard.performSwap(keyCode); + } + + public Boolean isGameOver() { + return this.gameBoard.isGameOver(); + } + + public void processCellClick(Integer cellValue) { + this.gameBoard.processCellClick(cellValue); + } +} diff --git a/jogo-oito/src/main/java/interfaces/Edge.java b/jogo-oito/src/main/java/interfaces/Edge.java deleted file mode 100644 index 20e56cd0..00000000 --- a/jogo-oito/src/main/java/interfaces/Edge.java +++ /dev/null @@ -1,19 +0,0 @@ -/* - * 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; - -/** - * - * @author allen - */ -public interface Edge { - - Keyboard getKey(); - - Vertex getCell(); - -} diff --git a/jogo-oito/src/main/java/interfaces/GameEdge.java b/jogo-oito/src/main/java/interfaces/GameEdge.java new file mode 100644 index 00000000..5f398e87 --- /dev/null +++ b/jogo-oito/src/main/java/interfaces/GameEdge.java @@ -0,0 +1,9 @@ +package interfaces; + +import model.GameKeyboard; + +public interface GameEdge { + GameKeyboard getKeyboard(); + + GameVertex getCell(); +} diff --git a/jogo-oito/src/main/java/interfaces/GameGraph.java b/jogo-oito/src/main/java/interfaces/GameGraph.java new file mode 100644 index 00000000..139d4302 --- /dev/null +++ b/jogo-oito/src/main/java/interfaces/GameGraph.java @@ -0,0 +1,20 @@ +package interfaces; + +import java.util.List; + +public interface GameGraph { + + void provideFeedback(); + + void setupGame(); + + void performSwap(Integer keyCode); + + List getGameCells(); + + GameVertex getEmptyCell(); + + void processCellClick(Integer cellValue); + + Boolean isGameOver(); +} diff --git a/jogo-oito/src/main/java/interfaces/GameVertex.java b/jogo-oito/src/main/java/interfaces/GameVertex.java new file mode 100644 index 00000000..7a5c081e --- /dev/null +++ b/jogo-oito/src/main/java/interfaces/GameVertex.java @@ -0,0 +1,28 @@ +package interfaces; + +import java.util.List; + +import model.GameKeyboard; + +public interface GameVertex { + + void setValue(Integer value); + + Integer getValue(); + + void createHorizontalAdjacent(GameVertex cell); + + void createVerticalAdjacent(GameVertex cell); + + String valueToString(); + + GameEdge getAdjacentByKeyboard(GameKeyboard keyboard); + + GameVertex processCellClick(GameKeyboard keyboard); + + List getAdjacents(); + + void addAdjacent(GameEdge edge); + + GameVertex swapCells(Integer value); +} diff --git a/jogo-oito/src/main/java/interfaces/Graph.java b/jogo-oito/src/main/java/interfaces/Graph.java deleted file mode 100644 index e22aad8e..00000000 --- a/jogo-oito/src/main/java/interfaces/Graph.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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; - -/** - * - * @author allen - */ -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 deleted file mode 100644 index 4ff55e4b..00000000 --- a/jogo-oito/src/main/java/interfaces/Vertex.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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; - -/** - * - * @author allen - */ -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 deleted file mode 100644 index 6573c4f4..00000000 --- a/jogo-oito/src/main/java/model/Adjacent.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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 java.util.Objects; - -/** - * - * @author allen - */ -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 deleted file mode 100644 index 744d8d39..00000000 --- a/jogo-oito/src/main/java/model/Cell.java +++ /dev/null @@ -1,109 +0,0 @@ -package model; - -import interfaces.Edge; -import interfaces.Vertex; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.Optional; - -public class Cell implements Vertex { - - private Integer value; - private final List adjacents; - public static Integer content; - - public Cell(Integer value) { - this.value = value; - this.adjacents = new ArrayList<>(); - } - - public Cell() { - this.value = Cell.content++; - 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)); - cell.getAdjacents().add(new Adjacent(Keyboard.RIGHT, this)); - } - - @Override - public void creatingVerticalAdjacent(Vertex cell) { - this.adjacents.add(new Adjacent(Keyboard.UP, cell)); - cell.getAdjacents().add(new Adjacent(Keyboard.DOWN, this)); - } - - @Override - public String valueToText() { - return Optional.of(this.value) - .filter(value -> value != 0) - .map(String::valueOf) - .orElse(""); - } - - @Override - public Edge getAdjacentByKeyCode(Keyboard key) { - Adjacent edge = new Adjacent(key, null); - Integer indexEdge = this.adjacents.indexOf(edge); - return Optional.of(indexEdge) - .filter(index -> index != -1) - .map(this.adjacents::get) - .orElse(null); - } - - @Override - public Vertex click(Keyboard key) { - Edge adjacent = this.getAdjacentByKeyCode(key); - return this.movement(adjacent); - } - - private Vertex movement(Edge adjacent) { - return Optional.ofNullable(adjacent) - .map(Edge::getCell) - .map(this::swapCells) - .orElse(this); - } - - private Vertex swapCells(Vertex movementCell) { - this.setValue(movementCell.getValue()); - movementCell.setValue(0); - return movementCell; - } - - @Override - public Vertex swapCells(Integer value) { - return this.adjacents.stream() - .filter(adjacent -> Objects.equals(adjacent.getCell().getValue(), value)) - .findFirst() - .map(this::movement) - .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/GameAdjacent.java b/jogo-oito/src/main/java/model/GameAdjacent.java new file mode 100644 index 00000000..e4767992 --- /dev/null +++ b/jogo-oito/src/main/java/model/GameAdjacent.java @@ -0,0 +1,43 @@ +package model; + +import interfaces.GameEdge; +import interfaces.GameVertex; +import java.util.Objects; + +public class GameAdjacent implements GameEdge { + + private final GameKeyboard keyboard; + private final GameVertex cell; + + public GameAdjacent(GameKeyboard keyboard, GameVertex cell) { + this.keyboard = keyboard; + this.cell = cell; + } + + @Override + public GameKeyboard getKeyboard() { + return this.keyboard; + } + + @Override + public GameVertex getCell() { + return this.cell; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null || getClass() != obj.getClass()) { + return false; + } + GameAdjacent otherAdjacent = (GameAdjacent) obj; + return Objects.equals(keyboard, otherAdjacent.keyboard); + } + + @Override + public int hashCode() { + return Objects.hash(keyboard); + } +} diff --git a/jogo-oito/src/main/java/model/GameCell.java b/jogo-oito/src/main/java/model/GameCell.java new file mode 100644 index 00000000..2f985a29 --- /dev/null +++ b/jogo-oito/src/main/java/model/GameCell.java @@ -0,0 +1,110 @@ +package model; + +import interfaces.GameEdge; +import interfaces.GameVertex; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.Optional; + +public class GameCell implements GameVertex { + + private Integer value; + private final List adjacents; + public static Integer content; + + public GameCell(Integer value) { + this.value = value; + this.adjacents = new ArrayList<>(); + } + + public GameCell() { + this.value = GameCell.content++; + this.adjacents = new ArrayList<>(); + } + + @Override + public void setValue(Integer value) { + this.value = value; + } + + @Override + public Integer getValue() { + return this.value; + } + + @Override + public void createHorizontalAdjacent(GameVertex cell) { + this.adjacents.add(new GameAdjacent(GameKeyboard.LEFT, cell)); + cell.getAdjacents().add(new GameAdjacent(GameKeyboard.RIGHT, this)); + } + + @Override + public void createVerticalAdjacent(GameVertex cell) { + this.adjacents.add(new GameAdjacent(GameKeyboard.UP, cell)); + cell.getAdjacents().add(new GameAdjacent(GameKeyboard.DOWN, this)); + } + + @Override + public String valueToString() { + return Optional.ofNullable(this.value).filter(value -> value != 0).map(String::valueOf).orElse(""); + } + + @Override + public GameEdge getAdjacentByKeyboard(GameKeyboard key) { + return this.adjacents.stream().filter(adjacent -> Objects.equals(adjacent.getKeyboard(), key)).findFirst() + .orElse(null); + } + + @Override + public GameVertex processCellClick(GameKeyboard key) { + GameEdge adjacent = this.getAdjacentByKeyboard(key); + return this.movement(adjacent); + } + + private GameVertex movement(GameEdge adjacent) { + return Optional.ofNullable(adjacent).map(GameEdge::getCell).map(this::swapCells).orElse(this); + } + + private GameVertex swapCells(GameVertex movementCell) { + if (movementCell != null) { + Integer tempValue = this.value; + this.setValue(movementCell.getValue()); + movementCell.setValue(tempValue); + } + return movementCell; + } + + @Override + public GameVertex swapCells(Integer value) { + return this.adjacents.stream().filter(adjacent -> Objects.equals(adjacent.getCell().getValue(), value)) + .findFirst().map(this::movement).orElse(this); + } + + @Override + public List getAdjacents() { + return this.adjacents; + } + + @Override + public void addAdjacent(GameEdge edge) { + this.adjacents.add(edge); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null || getClass() != obj.getClass()) { + return false; + } + GameCell otherCell = (GameCell) obj; + return Objects.equals(value, otherCell.value); + } + + @Override + public int hashCode() { + return Objects.hash(value); + } +} diff --git a/jogo-oito/src/main/java/model/GameKeyboard.java b/jogo-oito/src/main/java/model/GameKeyboard.java new file mode 100644 index 00000000..68d56745 --- /dev/null +++ b/jogo-oito/src/main/java/model/GameKeyboard.java @@ -0,0 +1,28 @@ +package model; + +import java.awt.event.KeyEvent; +import java.util.Arrays; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +public enum GameKeyboard { + UP(KeyEvent.VK_UP), DOWN(KeyEvent.VK_DOWN), LEFT(KeyEvent.VK_LEFT), RIGHT(KeyEvent.VK_RIGHT); + + private final int keyCode; + + private static final Map KEY_MAP = Arrays.stream(GameKeyboard.values()) + .collect(Collectors.toMap(GameKeyboard::getKeyCode, Function.identity())); + + GameKeyboard(int keyCode) { + this.keyCode = keyCode; + } + + public int getKeyCode() { + return keyCode; + } + + public static GameKeyboard fromKeyCode(int keyCode) { + return KEY_MAP.get(keyCode); + } +} diff --git a/jogo-oito/src/main/java/model/GameMatrix.java b/jogo-oito/src/main/java/model/GameMatrix.java new file mode 100644 index 00000000..511ce879 --- /dev/null +++ b/jogo-oito/src/main/java/model/GameMatrix.java @@ -0,0 +1,69 @@ +package model; + +import interfaces.GameVertex; +import java.util.ArrayList; +import java.util.List; + +public final class GameMatrix { + + private final GameRow firstRow; + private final GameRow secondRow; + private final GameRow thirdRow; + public static List cells; + + public GameMatrix() { + GameMatrix.cells = new ArrayList<>(); + GameCell.content = 1; + this.firstRow = new GameRow(); + this.secondRow = new GameRow(); + this.thirdRow = new GameRow(); + this.defineAdjacent(); + } + + private void defineAdjacent() { + this.firstRow.initial.createVerticalAdjacent(this.secondRow.initial); + this.secondRow.initial.createVerticalAdjacent(this.thirdRow.initial); + + this.firstRow.center.createVerticalAdjacent(this.secondRow.center); + this.secondRow.center.createVerticalAdjacent(this.thirdRow.center); + + this.firstRow.last.createVerticalAdjacent(this.secondRow.last); + this.secondRow.last.createVerticalAdjacent(this.thirdRow.last); + + this.changePositionToValidateTemplate(); + } + + private void changePositionToValidateTemplate() { + this.thirdRow.last.setValue(0); + } + + public List getCells() { + return GameMatrix.cells; + } + + private final class GameRow { + + public final GameCell initial; + public final GameCell center; + public final GameCell last; + + public GameRow() { + this.initial = new GameCell(); + this.center = new GameCell(); + this.last = new GameCell(); + this.defineAdjacent(); + this.loadCells(); + } + + public void loadCells() { + GameMatrix.cells.add(this.initial); + GameMatrix.cells.add(this.center); + GameMatrix.cells.add(this.last); + } + + public void defineAdjacent() { + this.initial.createHorizontalAdjacent(this.center); + this.center.createHorizontalAdjacent(this.last); + } + } +} diff --git a/jogo-oito/src/main/java/model/Keyboard.java b/jogo-oito/src/main/java/model/Keyboard.java deleted file mode 100644 index ada8bb7a..00000000 --- a/jogo-oito/src/main/java/model/Keyboard.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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; -import java.util.Arrays; -import java.util.Map; -import java.util.function.Function; -import java.util.stream.Collectors; - -public enum Keyboard { - - UP(KeyEvent.VK_UP), - DOWN(KeyEvent.VK_DOWN), - LEFT(KeyEvent.VK_LEFT), - RIGHT(KeyEvent.VK_RIGHT); - - private final Integer value; - - private static final Map map = Arrays.stream(Keyboard.values()) - .collect(Collectors.toMap(Keyboard::getValue, Function.identity())); - - - Keyboard(Integer value) { - this.value = value; - } - - public Integer getValue() { - return value; - } - - public static Keyboard fromValue(Integer value) { - return map.get(value); - } - -} diff --git a/jogo-oito/src/main/java/model/Matrix.java b/jogo-oito/src/main/java/model/Matrix.java deleted file mode 100644 index 29586eaa..00000000 --- a/jogo-oito/src/main/java/model/Matrix.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * 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; -import java.util.ArrayList; -import java.util.List; - -/** - * - * @author allen - */ -public final class Matrix { - - private final Row firstRow; - private final Row secondRow; - private final Row thirdRow; - 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.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); - - 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/util/Board.java b/jogo-oito/src/main/java/util/GameBoard.java similarity index 52% rename from jogo-oito/src/main/java/util/Board.java rename to jogo-oito/src/main/java/util/GameBoard.java index e71e83ea..001b1609 100644 --- a/jogo-oito/src/main/java/util/Board.java +++ b/jogo-oito/src/main/java/util/GameBoard.java @@ -1,7 +1,5 @@ package util; -import interfaces.Graph; -import interfaces.Vertex; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -9,86 +7,83 @@ import java.util.List; import java.util.Optional; import java.util.stream.IntStream; -import model.Keyboard; -import model.Matrix; -public class Board implements Graph { +import interfaces.GameGraph; +import interfaces.GameVertex; +import model.GameKeyboard; +import model.GameMatrix; - private List cells; - private Vertex emptyCell; +public class GameBoard implements GameGraph { + + private List cells; + private GameVertex emptyCell; private Integer length; - private Matrix matrix; + private GameMatrix matrix; - public Board() { + public GameBoard() { } @Override - public void feedback() { - this.matrix = new Matrix(); + public void provideFeedback() { + this.matrix = new GameMatrix(); this.cells = this.matrix.getCells(); this.length = cells.size(); this.defineEmptyCell(); } @Override - public void setting() { - this.matrix = new Matrix(); + public void setupGame() { + this.matrix = new GameMatrix(); this.cells = this.matrix.getCells(); this.length = cells.size(); - this.shuffleCell(); + this.shuffleCells(); this.defineEmptyCell(); - } - private void shuffleCell() { + private void shuffleCells() { Iterator iterator = this.shuffleValues().iterator(); - this.cells.stream() - .forEach(vertex -> vertex.setValue(iterator.next())); + this.cells.forEach(cell -> cell.setValue(iterator.next())); } private List shuffleValues() { List values = new ArrayList<>(); this.cells.stream() - .map(Vertex::getValue) + .map(GameVertex::getValue) .forEach(values::add); Collections.shuffle(values); return values; } private void defineEmptyCell() { - Optional minCell = this.cells.stream() - .min(Comparator.comparing(cell -> cell.getValue())); - minCell.ifPresent(cell -> { - this.emptyCell = cell; - }); + Optional minCell = this.cells.stream() + .min(Comparator.comparing(GameVertex::getValue)); + minCell.ifPresent(cell -> this.emptyCell = cell); } @Override - public void click(Integer cellValue) { + public void processCellClick(Integer cellValue) { this.emptyCell = this.emptyCell.swapCells(cellValue); } @Override - public void swap(Integer keyCode) { - Keyboard key = Keyboard.fromValue(keyCode); - this.emptyCell = this.emptyCell.click(key); + public void performSwap(Integer keyCode) { + GameKeyboard key = GameKeyboard.fromKeyCode(keyCode); + this.emptyCell = this.emptyCell.processCellClick(key); } @Override - public List getCells() { + public List getGameCells() { return this.cells; } @Override - public Vertex getEmptyCell() { + public GameVertex getEmptyCell() { return this.emptyCell; } @Override - public Boolean checkGameOver() { + public Boolean isGameOver() { 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..ef3370b9 100644 --- a/jogo-oito/src/main/java/view/JogoDosOito.java +++ b/jogo-oito/src/main/java/view/JogoDosOito.java @@ -1,7 +1,5 @@ package view; -import facade.Controller; -import interfaces.Vertex; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; @@ -9,140 +7,141 @@ import java.awt.event.KeyListener; import java.util.ArrayList; import java.util.List; -import java.util.Optional; import java.util.stream.IntStream; + import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.SwingUtilities; +import facade.GameController; +import interfaces.GameVertex; + public class JogoDosOito extends JFrame implements KeyListener { - private final List buttons; - 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<>(); - } - - private void configureInterface() { - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - setSize(300, 300); - setLayout(new GridLayout(4, 3)); - setVisible(true); - addKeyListener(this); - 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) { - JButton button = new JButton(); - button.setFont(new Font("Arial", Font.BOLD, 36)); - button.setText(cell.valueToText()); - button.addActionListener((ActionEvent e) -> { - this.controller.click(this.textToValue(button.getText())); - this.updateBoard(); - this.checkGameOver(); - SwingUtilities.getRoot(button).requestFocus(); - }); - return button; - - } - - private void checkGameOver() { - Optional.ofNullable(this.controller.checkGameOver()) - .filter(Boolean::booleanValue) - .ifPresent(gameOver -> { - JOptionPane.showMessageDialog(this, "Parabéns, você venceu!"); - resetGame(); - }); - } - - 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) -> { - this.resetGame(); - SwingUtilities.getRoot(buttonReset).requestFocus(); - }); - return buttonReset; - } - - private JButton configFeedback() { - JButton buttonFeedback = new JButton("Gabarito"); - buttonFeedback.addActionListener((ActionEvent e) -> { - this.showFeedback(); - SwingUtilities.getRoot(buttonFeedback).requestFocus(); - }); - return buttonFeedback; - } - - - private void resetGame() { - this.controller.setting(); - this.updateBoard(); - } - - private void showFeedback() { - this.controller.feedback(); - this.updateBoard(); - } - - private void updateBoard() { - List cells = this.controller.getCells(); - IntStream.range(0, cells.size()) - .forEach(index -> { - JButton button = this.buttons.get(index); - button.setText(cells.get(index).valueToText()); - }); - } - - @Override - public void keyTyped(KeyEvent e) { - } - - @Override - public void keyPressed(KeyEvent e) { - this.controller.swap(e.getKeyCode()); - this.updateBoard(); - this.checkGameOver(); - } - - @Override - public void keyReleased(KeyEvent e) { - } - - public static void main(String[] args) { - JogoDosOito game = new JogoDosOito(); - game.createButtons(); - game.configMenu(); - game.configureInterface(); - - } + private final List buttons; + private final GameController controller; + private JButton reset; + private JButton feedback; + + public JogoDosOito() { + super("Jogo dos Oito"); + this.controller = new GameController(); + this.controller.setupGame(); + this.buttons = new ArrayList<>(); + } + + private void configureInterface() { + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setSize(300, 300); + setLayout(new GridLayout(4, 3)); + setVisible(true); + addKeyListener(this); + setFocusable(true); + } + + private void createButtons() { + this.controller.getGameCells().forEach(cell -> { + JButton button = this.configureButton(cell); + add(button); + buttons.add(button); + }); + } + + private Integer textToValue(String text) { + if (text != null && !text.isEmpty()) { + return Integer.valueOf(text); + } + JOptionPane.showMessageDialog(this, "Célula é vazia, por favor, selecione outra célula!", "Aviso", JOptionPane.ERROR_MESSAGE); + + return 0; + } + + + private JButton configureButton(GameVertex cell) { + JButton button = new JButton(); + button.setFont(new Font("Arial", Font.BOLD, 36)); + button.setText(cell.valueToString()); + button.addActionListener((ActionEvent e) -> { + this.controller.processCellClick(this.textToValue(button.getText())); + this.updateBoard(); + this.checkGameOver(); + SwingUtilities.getRoot(button).requestFocus(); + }); + return button; + } + + private void checkGameOver() { + if (this.controller.isGameOver()) { + JOptionPane.showMessageDialog(this, "Parabéns, você venceu!"); + resetGame(); + } + } + + private void configureMenu() { + this.reset = this.configureReset(); + this.feedback = this.configureFeedback(); + add(this.feedback); + add(this.reset); + add(new JLabel("")); + } + + private JButton configureReset() { + JButton buttonReset = new JButton("Reiniciar"); + buttonReset.addActionListener((ActionEvent e) -> { + this.resetGame(); + SwingUtilities.getRoot(buttonReset).requestFocus(); + }); + return buttonReset; + } + + private JButton configureFeedback() { + JButton buttonFeedback = new JButton("Gabarito"); + buttonFeedback.addActionListener((ActionEvent e) -> { + this.showFeedback(); + SwingUtilities.getRoot(buttonFeedback).requestFocus(); + }); + return buttonFeedback; + } + + private void resetGame() { + this.controller.setupGame(); + this.updateBoard(); + } + + private void showFeedback() { + this.controller.provideFeedback(); + this.updateBoard(); + } + + private void updateBoard() { + List cells = this.controller.getGameCells(); + IntStream.range(0, cells.size()).forEach(index -> { + JButton button = this.buttons.get(index); + button.setText(cells.get(index).valueToString()); + }); + } + + @Override + public void keyTyped(KeyEvent e) { + } + + @Override + public void keyPressed(KeyEvent e) { + this.controller.performSwap(e.getKeyCode()); + this.updateBoard(); + this.checkGameOver(); + } + + @Override + public void keyReleased(KeyEvent e) { + } + + public static void main(String[] args) { + JogoDosOito game = new JogoDosOito(); + game.createButtons(); + game.configureMenu(); + game.configureInterface(); + } } diff --git a/jogo-oito/src/test/java/game/AdjacentTest.java b/jogo-oito/src/test/java/game/AdjacentTest.java deleted file mode 100644 index 49dd2ac3..00000000 --- a/jogo-oito/src/test/java/game/AdjacentTest.java +++ /dev/null @@ -1,39 +0,0 @@ -package game; - -import model.Adjacent; -import model.Cell; -import model.Keyboard; -import interfaces.Edge; -import interfaces.Vertex; -import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; - -public class AdjacentTest { - - @Test - public void testGetKey() { - Edge edge = new Adjacent(Keyboard.RIGHT, null); - assertEquals(Keyboard.RIGHT, edge.getKey()); - } - - @Test - public void testGetCell() { - Vertex cell = new Cell(1); - Edge edge = new Adjacent(Keyboard.RIGHT, cell); - assertEquals(cell, edge.getCell()); - } - - @Test - 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); - - assertEquals(edge1.getCell().click(Keyboard.RIGHT), cell2); - assertEquals(edge2.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 deleted file mode 100644 index 7b39668f..00000000 --- a/jogo-oito/src/test/java/game/BoardTest.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * 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 game; - -/** - * - * @author allen - */ -import model.Cell; -import util.Board; -import interfaces.Edge; -import interfaces.Vertex; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; - -import java.util.List; -import model.Keyboard; -import org.junit.jupiter.api.Assertions; - -public class BoardTest { - - private Board board; - - @BeforeEach - public void setUp() { - board = new Board(); - board.feedback(); - } - - @Test - public void testSwap() { - Vertex emptyCell = board.getEmptyCell(); - Vertex cell = board.getCells().get(7); - Integer cellValue = cell.getValue(); - - board.click(cellValue); - - assertEquals(cellValue, emptyCell.getValue()); - assertEquals(0, cell.getValue()); - } - - @Test - public void testGetCells() { - List cells = board.getCells(); - - assertNotNull(cells); - assertEquals(9, cells.size()); - } - - @Test - public void testDefineCellRelationships() { - Vertex cell1 = board.getCells().get(1); - Vertex cell2 = board.getCells().get(2); - - Edge adjacent1 = cell1.getAdjacentByKeyCode(model.Keyboard.LEFT); - Edge adjacent2 = cell2.getAdjacentByKeyCode(model.Keyboard.RIGHT); - - assertEquals(cell2, adjacent1.getCell()); - assertEquals(cell1, adjacent2.getCell()); - } - - @Test - public void testClick() { - Vertex emptyCell = board.getEmptyCell(); - Vertex cell = board.getCells().get(7); - Integer cellValue = cell.getValue(); - - board.click(cellValue); - - assertEquals(cellValue, emptyCell.getValue()); - assertEquals(0, cell.getValue()); - } - - @Test - public void testCheckGameOver() { - Assertions.assertTrue(this.board.checkGameOver()); - board = new Board(); - board.setting(); - Assertions.assertFalse(board.checkGameOver()); - - } - - @Test - public void testCellCreatingHorizontalAdjacent() { - Vertex cell1 = new Cell(1); - Vertex cell2 = new Cell(2); - - cell1.creatingHorizontalAdjacent(cell2); - - Edge adjacent1 = cell1.getAdjacentByKeyCode(model.Keyboard.LEFT); - Edge adjacent2 = cell2.getAdjacentByKeyCode(model.Keyboard.RIGHT); - - assertEquals(cell2, adjacent1.getCell()); - assertEquals(cell1, adjacent2.getCell()); - } - - @Test - public void testCellCreatingVerticalAdjacent() { - Vertex cell1 = new Cell(1); - Vertex cell2 = new Cell(2); - - cell1.creatingVerticalAdjacent(cell2); - - Edge adjacent1 = cell1.getAdjacentByKeyCode(model.Keyboard.UP); - Edge adjacent2 = cell2.getAdjacentByKeyCode(model.Keyboard.DOWN); - - assertEquals(cell2, adjacent1.getCell()); - assertEquals(cell1, adjacent2.getCell()); - } - - @Test - public void testValueToText() { - Vertex cell = new Cell(0); - assertEquals("", cell.valueToText()); - - cell.setValue(5); - assertEquals("5", cell.valueToText()); - } - - @Test - public void testGetAdjacentByKeyCode() { - Vertex cell1 = new Cell(1); - Vertex cell2 = new Cell(2); - - cell1.creatingHorizontalAdjacent(cell2); - - Edge adjacent = cell1.getAdjacentByKeyCode(model.Keyboard.LEFT); - assertEquals(cell2, adjacent.getCell()); - } - - @Test - public void testClickDown() { - board.swap(Keyboard.DOWN.getValue()); - Assertions.assertEquals(5, board.getCells().indexOf(board.getEmptyCell())); - } - - @Test - public void testClickUp() { - board.swap(Keyboard.UP.getValue()); - Assertions.assertEquals(8, board.getCells().indexOf(board.getEmptyCell())); - } - - @Test - public void testClickRight() { - board.swap(Keyboard.RIGHT.getValue()); - Assertions.assertEquals(7, board.getCells().indexOf(board.getEmptyCell())); - } - - @Test - public void testClickLeft() { - board.swap(Keyboard.LEFT.getValue()); - Assertions.assertEquals(8, board.getCells().indexOf(board.getEmptyCell())); - } - -} diff --git a/jogo-oito/src/test/java/game/CellTest.java b/jogo-oito/src/test/java/game/CellTest.java deleted file mode 100644 index a2742248..00000000 --- a/jogo-oito/src/test/java/game/CellTest.java +++ /dev/null @@ -1,122 +0,0 @@ -/* - * 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 game; - -import interfaces.Edge; -import interfaces.Vertex; -import java.util.List; -import model.Adjacent; -import model.Cell; -import model.Keyboard; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.*; - -public class CellTest{ - - @Test - public void testSetValue() { - Vertex cell = new Cell(0); - cell.setValue(5); - assertEquals(5, cell.getValue()); - } - - @Test - public void testGetValue() { - Vertex cell = new Cell(10); - assertEquals(10, cell.getValue()); - } - - @Test - public void testCreatingHorizontalAdjacent() { - Vertex cell1 = new Cell(1); - Vertex cell2 = new Cell(2); - - cell1.creatingHorizontalAdjacent(cell2); - - assertEquals(1, cell1.getAdjacents().size()); - assertEquals(1, cell2.getAdjacents().size()); - } - - @Test - public void testCreatingVerticalAdjacent() { - Vertex cell1 = new Cell(1); - Vertex cell2 = new Cell(2); - - cell1.creatingVerticalAdjacent(cell2); - - assertEquals(1, cell1.getAdjacents().size()); - assertEquals(1, cell2.getAdjacents().size()); - } - - @Test - public void testValueToText() { - Vertex cell1 = new Cell(0); - Vertex cell2 = new Cell(5); - - assertEquals("", cell1.valueToText()); - assertEquals("5", cell2.valueToText()); - } - - @Test - public void testGetAdjacentByKeyCode() { - Vertex cell1 = new Cell(1); - Vertex cell2 = new Cell(2); - cell1.creatingHorizontalAdjacent(cell2); - - assertEquals(cell2, cell1.getAdjacentByKeyCode(Keyboard.LEFT).getCell()); - } - - @Test - public void testClick() { - Vertex cell1 = new Cell(1); - Vertex cell2 = new Cell(2); - cell1.creatingHorizontalAdjacent(cell2); - - assertEquals(cell2, cell1.click(Keyboard.LEFT)); - } - - @Test - public void testSwapCells() { - Vertex cell1 = new Cell(0); - Vertex cell2 = new Cell(2); - cell1.creatingHorizontalAdjacent(cell2); - - cell1.swapCells(cell2.getValue()); - - assertEquals(2, cell1.getValue()); - assertEquals(0, cell2.getValue()); - } - - @Test - public void testAddAdjacents() { - Vertex cell1 = new Cell(1); - Vertex cell2 = new Cell(2); - Edge edge = new Adjacent(Keyboard.RIGHT, cell2); - - cell1.addAdjacents(edge); - - List adjacents = cell1.getAdjacents(); - assertEquals(1, adjacents.size()); - assertEquals(edge, adjacents.get(0)); - } - - @Test - public void defineAdjacents() { - Vertex cell1 = new Cell(5); - Vertex cell2 = new Cell(10); - Vertex cell3 = new Cell(15); - Vertex cell4 = new Cell(20); - - cell1.creatingVerticalAdjacent(cell2); - cell1.creatingVerticalAdjacent(cell3); - cell1.creatingHorizontalAdjacent(cell4); - - List adjacents = cell1.getAdjacents(); - assertEquals(cell2, adjacents.get(0).getCell()); - assertEquals(cell3, adjacents.get(1).getCell()); - assertEquals(cell4, adjacents.get(2).getCell()); - } -} diff --git a/jogo-oito/src/test/java/game/GameAdjacentTest.java b/jogo-oito/src/test/java/game/GameAdjacentTest.java new file mode 100644 index 00000000..9cf6eca4 --- /dev/null +++ b/jogo-oito/src/test/java/game/GameAdjacentTest.java @@ -0,0 +1,40 @@ +package game; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +import interfaces.GameEdge; +import interfaces.GameVertex; +import model.GameAdjacent; +import model.GameCell; +import model.GameKeyboard; + +public class GameAdjacentTest { + + @Test + public void testGetKey() { + GameEdge edge = new GameAdjacent(GameKeyboard.RIGHT, null); + assertEquals(GameKeyboard.RIGHT, edge.getKeyboard()); + } + + @Test + public void testGetCell() { + GameVertex cell = new GameCell(1); + GameEdge edge = new GameAdjacent(GameKeyboard.RIGHT, cell); + assertEquals(cell, edge.getCell()); + } + + @Test + public void testEquals() { + GameVertex cell1 = new GameCell(1); + GameVertex cell2 = new GameCell(2); + + GameEdge edge1 = new GameAdjacent(GameKeyboard.RIGHT, cell2); + GameEdge edge2 = new GameAdjacent(GameKeyboard.LEFT, cell1); + + assertEquals(edge1.getCell().processCellClick(GameKeyboard.RIGHT), cell2); + assertEquals(edge2.getCell().processCellClick(GameKeyboard.LEFT), cell1); + } +} + diff --git a/jogo-oito/src/test/java/game/GameBoardTest.java b/jogo-oito/src/test/java/game/GameBoardTest.java new file mode 100644 index 00000000..61655ea9 --- /dev/null +++ b/jogo-oito/src/test/java/game/GameBoardTest.java @@ -0,0 +1,153 @@ +package game; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.util.List; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import interfaces.GameEdge; +import interfaces.GameVertex; +import model.GameCell; +import model.GameKeyboard; +import util.GameBoard; + +public class GameBoardTest { + + private GameBoard board; + + @BeforeEach + public void setUp() { + board = new GameBoard(); + board.provideFeedback(); + } + + @Test + public void testSwap() { + GameVertex emptyCell = board.getEmptyCell(); + GameVertex cell = board.getGameCells().get(7); + Integer cellValue = cell.getValue(); + + board.processCellClick(cellValue); + + assertEquals(cellValue, emptyCell.getValue()); + assertEquals(0, cell.getValue()); + } + + @Test + public void testGetCells() { + List cells = board.getGameCells(); + + assertNotNull(cells); + assertEquals(9, cells.size()); + } + + @Test + public void testDefineCellRelationships() { + GameVertex cell1 = board.getGameCells().get(1); + GameVertex cell2 = board.getGameCells().get(2); + + GameEdge adjacent1 = cell1.getAdjacentByKeyboard(GameKeyboard.LEFT); + GameEdge adjacent2 = cell2.getAdjacentByKeyboard(GameKeyboard.RIGHT); + + assertEquals(cell2, adjacent1.getCell()); + assertEquals(cell1, adjacent2.getCell()); + } + + @Test + public void testClick() { + GameVertex emptyCell = board.getEmptyCell(); + GameVertex cell = board.getGameCells().get(7); + Integer cellValue = cell.getValue(); + + board.processCellClick(cellValue); + + assertEquals(cellValue, emptyCell.getValue()); + assertEquals(0, cell.getValue()); + } + + @Test + public void testCheckGameOver() { + Assertions.assertTrue(board.isGameOver()); + board = new GameBoard(); + board.setupGame(); + Assertions.assertFalse(board.isGameOver()); + + } + + @Test + public void testCellCreatingHorizontalAdjacent() { + GameVertex cell1 = new GameCell(1); + GameVertex cell2 = new GameCell(2); + + cell1.createHorizontalAdjacent(cell2); + + GameEdge adjacent1 = cell1.getAdjacentByKeyboard(GameKeyboard.LEFT); + GameEdge adjacent2 = cell2.getAdjacentByKeyboard(GameKeyboard.RIGHT); + + assertEquals(cell2, adjacent1.getCell()); + assertEquals(cell1, adjacent2.getCell()); + } + + @Test + public void testCellCreatingVerticalAdjacent() { + GameVertex cell1 = new GameCell(1); + GameVertex cell2 = new GameCell(2); + + cell1.createVerticalAdjacent(cell2); + + GameEdge adjacent1 = cell1.getAdjacentByKeyboard(GameKeyboard.UP); + GameEdge adjacent2 = cell2.getAdjacentByKeyboard(GameKeyboard.DOWN); + + assertEquals(cell2, adjacent1.getCell()); + assertEquals(cell1, adjacent2.getCell()); + } + + + @Test + public void testValueToText() { + GameVertex cell = new GameCell(0); + assertEquals("", cell.valueToString()); + + cell.setValue(5); + assertEquals("5", cell.valueToString()); + } + + @Test + public void testGetAdjacentByKeyCode() { + GameVertex cell1 = new GameCell(1); + GameVertex cell2 = new GameCell(2); + + cell1.createHorizontalAdjacent(cell2); + + GameEdge adjacent = cell1.getAdjacentByKeyboard(GameKeyboard.LEFT); + assertEquals(cell2, adjacent.getCell()); + } + + @Test + public void testClickDown() { + board.performSwap(GameKeyboard.DOWN.getKeyCode()); + Assertions.assertEquals(5, board.getGameCells().indexOf(board.getEmptyCell())); + } + + @Test + public void testClickUp() { + board.performSwap(GameKeyboard.UP.getKeyCode()); + Assertions.assertEquals(8, board.getGameCells().indexOf(board.getEmptyCell())); + } + + @Test + public void testClickRight() { + board.performSwap(GameKeyboard.RIGHT.getKeyCode()); + Assertions.assertEquals(7, board.getGameCells().indexOf(board.getEmptyCell())); + } + + @Test + public void testClickLeft() { + board.performSwap(GameKeyboard.LEFT.getKeyCode()); + Assertions.assertEquals(8, board.getGameCells().indexOf(board.getEmptyCell())); + } +} diff --git a/jogo-oito/src/test/java/game/GameCellTest.java b/jogo-oito/src/test/java/game/GameCellTest.java new file mode 100644 index 00000000..e147213c --- /dev/null +++ b/jogo-oito/src/test/java/game/GameCellTest.java @@ -0,0 +1,119 @@ +package game; + +import interfaces.GameEdge; +import interfaces.GameVertex; +import model.GameAdjacent; +import model.GameCell; +import model.GameKeyboard; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +public class GameCellTest { + + @Test + public void testSetValue() { + GameVertex cell = new GameCell(0); + cell.setValue(5); + assertEquals(5, cell.getValue()); + } + + @Test + public void testGetValue() { + GameVertex cell = new GameCell(10); + assertEquals(10, cell.getValue()); + } + + @Test + public void testCreatingHorizontalAdjacent() { + GameVertex cell1 = new GameCell(1); + GameVertex cell2 = new GameCell(2); + + cell1.createHorizontalAdjacent(cell2); + + assertEquals(1, cell1.getAdjacents().size()); + assertEquals(1, cell2.getAdjacents().size()); + } + + @Test + public void testCreatingVerticalAdjacent() { + GameVertex cell1 = new GameCell(1); + GameVertex cell2 = new GameCell(2); + + cell1.createVerticalAdjacent(cell2); + + assertEquals(1, cell1.getAdjacents().size()); + assertEquals(1, cell2.getAdjacents().size()); + } + + @Test + public void testValueToText() { + GameVertex cell1 = new GameCell(0); + GameVertex cell2 = new GameCell(5); + + assertEquals("", cell1.valueToString()); + assertEquals("5", cell2.valueToString()); + } + + @Test + public void testGetAdjacentByKeyCode() { + GameVertex cell1 = new GameCell(1); + GameVertex cell2 = new GameCell(2); + cell1.createHorizontalAdjacent(cell2); + + assertEquals(cell2, cell1.processCellClick(GameKeyboard.LEFT).getAdjacents()); + } + + @Test + public void testClick() { + GameVertex cell1 = new GameCell(1); + GameVertex cell2 = new GameCell(2); + cell1.createHorizontalAdjacent(cell2); + + assertEquals(cell2, cell1.processCellClick(GameKeyboard.LEFT)); + } + + @Test + public void testSwapCells() { + GameVertex cell1 = new GameCell(0); + GameVertex cell2 = new GameCell(2); + cell1.createHorizontalAdjacent(cell2); + + cell1.swapCells(cell2.getValue()); + + assertEquals(2, cell1.getValue()); + assertEquals(0, cell2.getValue()); + } + + @Test + public void testAddAdjacents() { + GameVertex cell1 = new GameCell(1); + GameVertex cell2 = new GameCell(2); + GameEdge edge = new GameAdjacent(GameKeyboard.RIGHT, cell2); + + cell1.addAdjacent(edge); + + List adjacents = cell1.getAdjacents(); + assertEquals(1, adjacents.size()); + assertEquals(edge, adjacents.get(0)); + } + + @Test + public void defineAdjacents() { + GameVertex cell1 = new GameCell(5); + GameVertex cell2 = new GameCell(10); + GameVertex cell3 = new GameCell(15); + GameVertex cell4 = new GameCell(20); + + cell1.createVerticalAdjacent(cell2); + cell1.createVerticalAdjacent(cell3); + cell1.createHorizontalAdjacent(cell4); + + List adjacents = cell1.getAdjacents(); + assertEquals(cell2, adjacents.get(0).getCell()); + assertEquals(cell3, adjacents.get(1).getCell()); + assertEquals(cell4, adjacents.get(2).getCell()); + } +}