From 95bd250e3a867836839864c8d8fed153fa7673ba Mon Sep 17 00:00:00 2001 From: EduardoYucho Date: Tue, 10 Oct 2023 01:07:54 -0300 Subject: [PATCH 1/2] branch_1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Alteração na formatação do código para deixa-lo padronizado com igualmente. --- .../src/main/java/facade/Controller.java | 45 ++- jogo-oito/src/main/java/interfaces/Edge.java | 6 +- jogo-oito/src/main/java/interfaces/Graph.java | 16 +- .../src/main/java/interfaces/Vertex.java | 24 +- jogo-oito/src/main/java/model/Adjacent.java | 48 ++-- jogo-oito/src/main/java/model/Cell.java | 181 ++++++------ jogo-oito/src/main/java/model/Keyboard.java | 31 +-- jogo-oito/src/main/java/model/Matrix.java | 99 ++++--- jogo-oito/src/main/java/util/Board.java | 148 +++++----- jogo-oito/src/main/java/view/JogoDosOito.java | 245 ++++++++--------- .../src/test/java/game/AdjacentTest.java | 50 ++-- jogo-oito/src/test/java/game/BoardTest.java | 259 +++++++++--------- jogo-oito/src/test/java/game/CellTest.java | 210 +++++++------- 13 files changed, 662 insertions(+), 700 deletions(-) diff --git a/jogo-oito/src/main/java/facade/Controller.java b/jogo-oito/src/main/java/facade/Controller.java index 28c6df1b..ea3d0af1 100644 --- a/jogo-oito/src/main/java/facade/Controller.java +++ b/jogo-oito/src/main/java/facade/Controller.java @@ -7,35 +7,34 @@ public class Controller { - private final Graph board; + private final Graph board; - public Controller() { - this.board = new Board(); - } - - public void feedback() { - this.board.feedback(); - } + public Controller() { + this.board = new Board(); + } - public void setting() { - this.board.setting(); - } + public void feedback() { + this.board.feedback(); + } - public List getCells() { - return this.board.getCells(); - } + public void setting() { + this.board.setting(); + } - public void swap(Integer keyCode) { - this.board.swap(keyCode); - } + public List getCells() { + return this.board.getCells(); + } - public Boolean checkGameOver() { - return this.board.checkGameOver(); + 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); - } + } + public void click(Integer cellValue) { + this.board.click(cellValue); + } } diff --git a/jogo-oito/src/main/java/interfaces/Edge.java b/jogo-oito/src/main/java/interfaces/Edge.java index 20e56cd0..bdd76ba7 100644 --- a/jogo-oito/src/main/java/interfaces/Edge.java +++ b/jogo-oito/src/main/java/interfaces/Edge.java @@ -12,8 +12,8 @@ */ public interface Edge { - Keyboard getKey(); + Keyboard getKey(); + + Vertex getCell(); - Vertex getCell(); - } diff --git a/jogo-oito/src/main/java/interfaces/Graph.java b/jogo-oito/src/main/java/interfaces/Graph.java index e22aad8e..f44a9223 100644 --- a/jogo-oito/src/main/java/interfaces/Graph.java +++ b/jogo-oito/src/main/java/interfaces/Graph.java @@ -12,17 +12,17 @@ */ public interface Graph { - void feedback(); - - void setting(); + void feedback(); - void swap(Integer keyCode); + void setting(); - List getCells(); + void swap(Integer keyCode); - Vertex getEmptyCell(); + List getCells(); - void click(Integer cellValue); + Vertex getEmptyCell(); - Boolean checkGameOver(); + 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..70fca4e6 100644 --- a/jogo-oito/src/main/java/interfaces/Vertex.java +++ b/jogo-oito/src/main/java/interfaces/Vertex.java @@ -13,24 +13,24 @@ */ public interface Vertex { - void setValue(Integer value); + void setValue(Integer value); - Integer getValue(); + Integer getValue(); - void creatingHorizontalAdjacent(Vertex cell); + void creatingHorizontalAdjacent(Vertex cell); - void creatingVerticalAdjacent(Vertex cell); + void creatingVerticalAdjacent(Vertex cell); - String valueToText(); + String valueToText(); - Edge getAdjacentByKeyCode(Keyboard key); + Edge getAdjacentByKeyCode(Keyboard key); - Vertex click(Keyboard key); + Vertex click(Keyboard key); - List getAdjacents(); - - void addAdjacents(Edge edge); - - Vertex swapCells(Integer value); + 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..3b265faf 100644 --- a/jogo-oito/src/main/java/model/Adjacent.java +++ b/jogo-oito/src/main/java/model/Adjacent.java @@ -12,29 +12,29 @@ * * @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()); - } +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..129a0136 100644 --- a/jogo-oito/src/main/java/model/Cell.java +++ b/jogo-oito/src/main/java/model/Cell.java @@ -9,101 +9,88 @@ 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); - } - + 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/Keyboard.java b/jogo-oito/src/main/java/model/Keyboard.java index ada8bb7a..e2d1cab2 100644 --- a/jogo-oito/src/main/java/model/Keyboard.java +++ b/jogo-oito/src/main/java/model/Keyboard.java @@ -12,27 +12,22 @@ public enum Keyboard { - UP(KeyEvent.VK_UP), - DOWN(KeyEvent.VK_DOWN), - LEFT(KeyEvent.VK_LEFT), - RIGHT(KeyEvent.VK_RIGHT); + UP(KeyEvent.VK_UP), DOWN(KeyEvent.VK_DOWN), LEFT(KeyEvent.VK_LEFT), RIGHT(KeyEvent.VK_RIGHT); - private final Integer value; + 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; + } - Keyboard(Integer value) { - this.value = value; - } - - public Integer getValue() { - return value; - } - - public static Keyboard fromValue(Integer value) { - return map.get(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 index 29586eaa..6fcd6fcf 100644 --- a/jogo-oito/src/main/java/model/Matrix.java +++ b/jogo-oito/src/main/java/model/Matrix.java @@ -14,67 +14,64 @@ */ public final class Matrix { - private final Row firstRow; - private final Row secondRow; - private final Row thirdRow; - public static List cells; + 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(); + } - 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); - 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.center.creatingVerticalAdjacent(secondRow.center); - this.secondRow.center.creatingVerticalAdjacent(thirdRow.center); + this.firstRow.last.creatingVerticalAdjacent(secondRow.last); + this.secondRow.last.creatingVerticalAdjacent(thirdRow.last); - this.firstRow.last.creatingVerticalAdjacent(secondRow.last); - this.secondRow.last.creatingVerticalAdjacent(thirdRow.last); - - this.changePositionToValidateTemplate(); - } - - private void changePositionToValidateTemplate(){ - this.thirdRow.last.setValue(0); - } - + this.changePositionToValidateTemplate(); + } - public List getCells() { - return Matrix.cells; - } + private void changePositionToValidateTemplate() { + this.thirdRow.last.setValue(0); + } - private final class Row { + public List getCells() { + return Matrix.cells; + } - public final Cell initial; - public final Cell center; - public final Cell last; + private final class Row { - public Row() { - this.initial = new Cell(); - this.center = new Cell(); - this.last = new Cell(); - this.defineAdjacent(); - this.loadCells(); - } + public final Cell initial; + public final Cell center; + public final Cell last; - public void loadCells() { - Matrix.cells.add(this.initial); - Matrix.cells.add(this.center); - Matrix.cells.add(this.last); - } + public Row() { + this.initial = new Cell(); + this.center = new Cell(); + this.last = new Cell(); + this.defineAdjacent(); + this.loadCells(); + } - public void defineAdjacent() { - this.initial.creatingHorizontalAdjacent(this.center); - this.center.creatingHorizontalAdjacent(this.last); - } + 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..5bc982f5 100644 --- a/jogo-oito/src/main/java/util/Board.java +++ b/jogo-oito/src/main/java/util/Board.java @@ -14,81 +14,75 @@ public class Board implements Graph { - private List cells; - private Vertex emptyCell; - private Integer length; - private Matrix matrix; - - public Board() { - } - - @Override - public void feedback() { - this.matrix = new Matrix(); - this.cells = this.matrix.getCells(); - this.length = cells.size(); - this.defineEmptyCell(); - } - - @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())); - } - - private List shuffleValues() { - List values = new ArrayList<>(); - this.cells.stream() - .map(Vertex::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; - }); - } - - @Override - public void click(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); - } - - @Override - public List getCells() { - return this.cells; - } - - @Override - public Vertex getEmptyCell() { - return this.emptyCell; - } - - @Override - public Boolean checkGameOver() { - return IntStream.range(0, this.length) - .allMatch(index -> this.cells.get(index).getValue() == (index + 1) % this.length); - - } - + private List cells; + private Vertex emptyCell; + private Integer length; + private Matrix matrix; + + public Board() { + } + + @Override + public void feedback() { + this.matrix = new Matrix(); + this.cells = this.matrix.getCells(); + this.length = cells.size(); + this.defineEmptyCell(); + } + + @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())); + } + + private List shuffleValues() { + List values = new ArrayList<>(); + this.cells.stream().map(Vertex::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; + }); + } + + @Override + public void click(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); + } + + @Override + public List getCells() { + return this.cells; + } + + @Override + public Vertex getEmptyCell() { + return this.emptyCell; + } + + @Override + 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..694a3279 100644 --- a/jogo-oito/src/main/java/view/JogoDosOito.java +++ b/jogo-oito/src/main/java/view/JogoDosOito.java @@ -19,130 +19,123 @@ 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 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(); + } } diff --git a/jogo-oito/src/test/java/game/AdjacentTest.java b/jogo-oito/src/test/java/game/AdjacentTest.java index 49dd2ac3..591926a4 100644 --- a/jogo-oito/src/test/java/game/AdjacentTest.java +++ b/jogo-oito/src/test/java/game/AdjacentTest.java @@ -10,30 +10,28 @@ 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); - - } - + @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 index 7b39668f..389bd995 100644 --- a/jogo-oito/src/test/java/game/BoardTest.java +++ b/jogo-oito/src/test/java/game/BoardTest.java @@ -22,137 +22,136 @@ public class BoardTest { - private Board board; + private Board board; - @BeforeEach - public void setUp() { - board = new Board(); - board.feedback(); - } + @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())); - } + @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 index a2742248..b9e64992 100644 --- a/jogo-oito/src/test/java/game/CellTest.java +++ b/jogo-oito/src/test/java/game/CellTest.java @@ -14,109 +14,109 @@ 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()); - } +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()); + } } From 2ed523ae305b79f90b75061d96b6da43672a4201 Mon Sep 17 00:00:00 2001 From: EduardoYucho Date: Wed, 11 Oct 2023 00:22:30 -0300 Subject: [PATCH 2/2] branch_1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Código refatorado para aprimorar as orientações a objetos. Classes e métodos alterados para seguirem a mesma linha de programação, e, deixa-lo mais coerente com o solicitado. --- .../src/main/java/facade/Controller.java | 40 ----- .../src/main/java/facade/GameController.java | 39 +++++ jogo-oito/src/main/java/interfaces/Edge.java | 19 --- .../src/main/java/interfaces/GameEdge.java | 9 + .../src/main/java/interfaces/GameGraph.java | 20 +++ .../src/main/java/interfaces/GameVertex.java | 28 ++++ jogo-oito/src/main/java/interfaces/Graph.java | 28 ---- .../src/main/java/interfaces/Vertex.java | 36 ---- jogo-oito/src/main/java/model/Adjacent.java | 40 ----- jogo-oito/src/main/java/model/Cell.java | 96 ----------- .../src/main/java/model/GameAdjacent.java | 43 +++++ jogo-oito/src/main/java/model/GameCell.java | 110 ++++++++++++ .../src/main/java/model/GameKeyboard.java | 28 ++++ jogo-oito/src/main/java/model/GameMatrix.java | 69 ++++++++ jogo-oito/src/main/java/model/Keyboard.java | 33 ---- jogo-oito/src/main/java/model/Matrix.java | 77 --------- jogo-oito/src/main/java/util/Board.java | 88 ---------- jogo-oito/src/main/java/util/GameBoard.java | 89 ++++++++++ jogo-oito/src/main/java/view/JogoDosOito.java | 58 ++++--- .../src/test/java/game/AdjacentTest.java | 37 ----- jogo-oito/src/test/java/game/BoardTest.java | 157 ------------------ jogo-oito/src/test/java/game/CellTest.java | 122 -------------- .../src/test/java/game/GameAdjacentTest.java | 40 +++++ .../src/test/java/game/GameBoardTest.java | 153 +++++++++++++++++ .../src/test/java/game/GameCellTest.java | 119 +++++++++++++ 25 files changed, 779 insertions(+), 799 deletions(-) delete mode 100644 jogo-oito/src/main/java/facade/Controller.java create mode 100644 jogo-oito/src/main/java/facade/GameController.java delete mode 100644 jogo-oito/src/main/java/interfaces/Edge.java create mode 100644 jogo-oito/src/main/java/interfaces/GameEdge.java create mode 100644 jogo-oito/src/main/java/interfaces/GameGraph.java create mode 100644 jogo-oito/src/main/java/interfaces/GameVertex.java delete mode 100644 jogo-oito/src/main/java/interfaces/Graph.java delete mode 100644 jogo-oito/src/main/java/interfaces/Vertex.java delete mode 100644 jogo-oito/src/main/java/model/Adjacent.java delete mode 100644 jogo-oito/src/main/java/model/Cell.java create mode 100644 jogo-oito/src/main/java/model/GameAdjacent.java create mode 100644 jogo-oito/src/main/java/model/GameCell.java create mode 100644 jogo-oito/src/main/java/model/GameKeyboard.java create mode 100644 jogo-oito/src/main/java/model/GameMatrix.java delete mode 100644 jogo-oito/src/main/java/model/Keyboard.java delete mode 100644 jogo-oito/src/main/java/model/Matrix.java delete mode 100644 jogo-oito/src/main/java/util/Board.java create mode 100644 jogo-oito/src/main/java/util/GameBoard.java delete mode 100644 jogo-oito/src/test/java/game/AdjacentTest.java delete mode 100644 jogo-oito/src/test/java/game/BoardTest.java delete mode 100644 jogo-oito/src/test/java/game/CellTest.java create mode 100644 jogo-oito/src/test/java/game/GameAdjacentTest.java create mode 100644 jogo-oito/src/test/java/game/GameBoardTest.java create mode 100644 jogo-oito/src/test/java/game/GameCellTest.java 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 ea3d0af1..00000000 --- a/jogo-oito/src/main/java/facade/Controller.java +++ /dev/null @@ -1,40 +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 bdd76ba7..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 f44a9223..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 70fca4e6..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 3b265faf..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 129a0136..00000000 --- a/jogo-oito/src/main/java/model/Cell.java +++ /dev/null @@ -1,96 +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 e2d1cab2..00000000 --- a/jogo-oito/src/main/java/model/Keyboard.java +++ /dev/null @@ -1,33 +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 6fcd6fcf..00000000 --- a/jogo-oito/src/main/java/model/Matrix.java +++ /dev/null @@ -1,77 +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/Board.java deleted file mode 100644 index 5bc982f5..00000000 --- a/jogo-oito/src/main/java/util/Board.java +++ /dev/null @@ -1,88 +0,0 @@ -package util; - -import interfaces.Graph; -import interfaces.Vertex; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.Iterator; -import java.util.List; -import java.util.Optional; -import java.util.stream.IntStream; -import model.Keyboard; -import model.Matrix; - -public class Board implements Graph { - - private List cells; - private Vertex emptyCell; - private Integer length; - private Matrix matrix; - - public Board() { - } - - @Override - public void feedback() { - this.matrix = new Matrix(); - this.cells = this.matrix.getCells(); - this.length = cells.size(); - this.defineEmptyCell(); - } - - @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())); - } - - private List shuffleValues() { - List values = new ArrayList<>(); - this.cells.stream().map(Vertex::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; - }); - } - - @Override - public void click(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); - } - - @Override - public List getCells() { - return this.cells; - } - - @Override - public Vertex getEmptyCell() { - return this.emptyCell; - } - - @Override - 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/util/GameBoard.java b/jogo-oito/src/main/java/util/GameBoard.java new file mode 100644 index 00000000..001b1609 --- /dev/null +++ b/jogo-oito/src/main/java/util/GameBoard.java @@ -0,0 +1,89 @@ +package util; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.Iterator; +import java.util.List; +import java.util.Optional; +import java.util.stream.IntStream; + +import interfaces.GameGraph; +import interfaces.GameVertex; +import model.GameKeyboard; +import model.GameMatrix; + +public class GameBoard implements GameGraph { + + private List cells; + private GameVertex emptyCell; + private Integer length; + private GameMatrix matrix; + + public GameBoard() { + } + + @Override + public void provideFeedback() { + this.matrix = new GameMatrix(); + this.cells = this.matrix.getCells(); + this.length = cells.size(); + this.defineEmptyCell(); + } + + @Override + public void setupGame() { + this.matrix = new GameMatrix(); + this.cells = this.matrix.getCells(); + this.length = cells.size(); + this.shuffleCells(); + this.defineEmptyCell(); + } + + private void shuffleCells() { + Iterator iterator = this.shuffleValues().iterator(); + this.cells.forEach(cell -> cell.setValue(iterator.next())); + } + + private List shuffleValues() { + List values = new ArrayList<>(); + this.cells.stream() + .map(GameVertex::getValue) + .forEach(values::add); + Collections.shuffle(values); + return values; + } + + private void defineEmptyCell() { + Optional minCell = this.cells.stream() + .min(Comparator.comparing(GameVertex::getValue)); + minCell.ifPresent(cell -> this.emptyCell = cell); + } + + @Override + public void processCellClick(Integer cellValue) { + this.emptyCell = this.emptyCell.swapCells(cellValue); + } + + @Override + public void performSwap(Integer keyCode) { + GameKeyboard key = GameKeyboard.fromKeyCode(keyCode); + this.emptyCell = this.emptyCell.processCellClick(key); + } + + @Override + public List getGameCells() { + return this.cells; + } + + @Override + public GameVertex getEmptyCell() { + return this.emptyCell; + } + + @Override + 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 694a3279..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,25 +7,28 @@ 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 final GameController controller; private JButton reset; private JButton feedback; public JogoDosOito() { super("Jogo dos Oito"); - this.controller = new Controller(); - this.controller.setting(); + this.controller = new GameController(); + this.controller.setupGame(); this.buttons = new ArrayList<>(); } @@ -41,47 +42,52 @@ private void configureInterface() { } private void createButtons() { - this.controller.getCells().forEach(cell -> { - JButton button = this.configButton(cell); + this.controller.getGameCells().forEach(cell -> { + JButton button = this.configureButton(cell); add(button); buttons.add(button); }); } private Integer textToValue(String text) { - return Optional.ofNullable(text).map(Integer::valueOf).orElse(0); + 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 configButton(Vertex cell) { + + private JButton configureButton(GameVertex cell) { JButton button = new JButton(); button.setFont(new Font("Arial", Font.BOLD, 36)); - button.setText(cell.valueToText()); + button.setText(cell.valueToString()); button.addActionListener((ActionEvent e) -> { - this.controller.click(this.textToValue(button.getText())); + this.controller.processCellClick(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 -> { + if (this.controller.isGameOver()) { JOptionPane.showMessageDialog(this, "Parabéns, você venceu!"); resetGame(); - }); + } } - private void configMenu() { - this.reset = this.configReset(); - this.feedback = this.configFeedback(); + private void configureMenu() { + this.reset = this.configureReset(); + this.feedback = this.configureFeedback(); add(this.feedback); add(this.reset); add(new JLabel("")); } - private JButton configReset() { + private JButton configureReset() { JButton buttonReset = new JButton("Reiniciar"); buttonReset.addActionListener((ActionEvent e) -> { this.resetGame(); @@ -90,7 +96,7 @@ private JButton configReset() { return buttonReset; } - private JButton configFeedback() { + private JButton configureFeedback() { JButton buttonFeedback = new JButton("Gabarito"); buttonFeedback.addActionListener((ActionEvent e) -> { this.showFeedback(); @@ -100,20 +106,20 @@ private JButton configFeedback() { } private void resetGame() { - this.controller.setting(); + this.controller.setupGame(); this.updateBoard(); } private void showFeedback() { - this.controller.feedback(); + this.controller.provideFeedback(); this.updateBoard(); } private void updateBoard() { - List cells = this.controller.getCells(); + List cells = this.controller.getGameCells(); IntStream.range(0, cells.size()).forEach(index -> { JButton button = this.buttons.get(index); - button.setText(cells.get(index).valueToText()); + button.setText(cells.get(index).valueToString()); }); } @@ -123,7 +129,7 @@ public void keyTyped(KeyEvent e) { @Override public void keyPressed(KeyEvent e) { - this.controller.swap(e.getKeyCode()); + this.controller.performSwap(e.getKeyCode()); this.updateBoard(); this.checkGameOver(); } @@ -135,7 +141,7 @@ public void keyReleased(KeyEvent e) { public static void main(String[] args) { JogoDosOito game = new JogoDosOito(); game.createButtons(); - game.configMenu(); + 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 591926a4..00000000 --- a/jogo-oito/src/test/java/game/AdjacentTest.java +++ /dev/null @@ -1,37 +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 389bd995..00000000 --- a/jogo-oito/src/test/java/game/BoardTest.java +++ /dev/null @@ -1,157 +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 b9e64992..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()); + } +}