├── .project ├── README.md └── jogo-oito ├── .classpath ├── .gitignore ├── .project ├── .settings ├── org.eclipse.core.resources.prefs ├── org.eclipse.jdt.core.prefs └── org.eclipse.m2e.core.prefs ├── pom.xml └── src ├── main └── java │ ├── facade │ └── Controller.java │ ├── interfaces │ ├── Edge.java │ ├── Graph.java │ └── Vertex.java │ ├── model │ ├── Adjacent.java │ ├── Cell.java │ ├── Keyboard.java │ └── Matrix.java │ ├── util │ └── Board.java │ └── view │ └── JogoDosOito.java └── test └── java └── game ├── AdjacentTest.java ├── BoardTest.java └── CellTest.java /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | senior-developer-challenge 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [Encerrado] _[REMOTO] Desafio para vaga de desenvolvedor [Sênior, em processo de contratação]._ 2 | 3 | ![image](https://user-images.githubusercontent.com/5315184/228086966-4b4b41cc-276c-4d04-96b8-5d37317a89e8.png) 4 | 5 | A Bempaggo é uma empresa do setor de pagamentos. 6 | 7 | ## Informações sobre a vaga: 8 | 9 | -> Local de trabalho 100% REMOTO. 10 | 11 | -> Vaga para trabalho remoto (Residir no Brasil por mais de 6 meses no ano). 12 | 13 | -> R$ 10K (inicial) - Desenvolvedor Sênior (em processo de contrataçâo). 14 | 15 | -> R$ 5K (inicial) - Desenvolvedor Pleno. 16 | 17 | -> R$ 2K (inicial) - Desenvolvedor Estágio. 18 | 19 | -> + Benefícios. 20 | 21 | 22 | ## Principais tecnologias que utilizamos: 23 | 24 | -> Banco de Dados (Postgresql e Mysql). 25 | 26 | -> Backend -> Java (ORM, jakarta.ws.rs [jersey]), Rust. 27 | 28 | -> Frontend -> JavaScript, TypeScript, Vue, CSS e HTML. 29 | 30 | -> Testes automatizados-> unidade e aceitação (JUnit). 31 | 32 | -> Github e AWS. 33 | 34 | -> Desenvolvimento dirigido por testes (TDD), destacando a programação em par e assegurar primeiro. 35 | 36 | 37 | ## Requisito da vaga 38 | 39 | -> Domínio do paradigma de programação orientada aos objetos (POO). 40 | 41 | ## 42 | # INSTRUÇÕES: 43 | 44 | ## 1 - Crie um fork do projeto. 45 | __https://github.com/bempaggo/developer-challenge__ 46 | 47 | ## 2 - Melhore o nosso projeto inicial. 48 | 49 | 50 | _Esperamos que você seja melhor que o ChatGPT e do @AllenHichard ._ 51 | 52 | _Valorizamos código com qualidade, ausência de dívidas técnicas..._ 53 | 54 | _Mostre para nós o teu potencial por meio do código._ 55 | 56 | _Aplicar programação orientada aos objetos é uma exigência para esta vaga._ 57 | 58 | _Precisamos jogar o jogo com mouse._ 59 | 60 | ## 2.1 Histórico do desenvolvimento 61 | 1° - Chat Gpt gerou um código Java para um jogo do 8, o código aparentava estruturas monolíticas. 62 | 63 | 2° - Allan incluiu uma versão Orientada Objetos, contudo com muito código funcional. 64 | 65 | 3° - Estamos agoardando ansiosos por adicionar mais nomes de desenvolvedores ou desenvolvedoras aqui. 66 | 67 | ## 2.2 Melhorias 68 | Gostariamos de uma versão tendendo à POO pura. Onde pode melhorar? 69 | São diversos pontos, alguns mais evidentes: Melhorar as abstrações, incluido nomes e conceitos do jogo de 8. Reduzir o volume de código. Aplicar Padrões de Projetos de Software. 70 | 71 | ## 3 - Faça um pull request ao nosso projeto. 72 | 73 | _Indique o nível da vaga na mensagem de pull request._ 74 | 75 | _Sugerimos que capriche sempre que pedir um pull request, reveja o que os outros já fizeram isso ajuda a revisão._ 76 | 77 | 78 | ## 4 - Aguarde uma revisão. 79 | 80 | _Caso rejeitado, apontaremos apenas um problema._ 81 | 82 | ## 5 - O que é avaliado no desafio? 83 | 84 | -> Avaliamos o domínio de Programação Orientada aos Objetos. 85 | 86 | ### Dicas do avaliador: 87 | 88 | -> A empresa utiliza essencialmente Java e TypeScript como linguagem. 89 | 90 | -> Orienta-se utilização da linguagem que domine para resolver o desafio (especialmente se quiser concorrer à vaga de estágio. Eu não tenho preferência por linguagem). 91 | 92 | -> Neste momento, a quantidade de tecnologias que têm contato/domine/goste é indiferente. 93 | 94 | -> Disciplinas de: Matemática, Português, Inglês e Física são desejáveis. 95 | 96 | -> Programação Orientada aos Objetos não se refere às tecnologias. 97 | 98 | -> Caso utilize testes automatizados, a chance do candidato ter o _pull request_ aceito aumenta significativamente. 99 | 100 | -> Respire. Leia atentamente a Seção 5. 101 | 102 | -> [Se liga ](https://refactoring.guru/)https://refactoring.guru/ 103 | 104 | 105 | 106 | -------------------------------------------------------------------------------- /jogo-oito/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /jogo-oito/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /jogo-oito/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | jogo-do-oito-do-estagiario-gpt 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /jogo-oito/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding/=UTF-8 3 | -------------------------------------------------------------------------------- /jogo-oito/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=18 3 | org.eclipse.jdt.core.compiler.compliance=18 4 | org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled 5 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 6 | org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore 7 | org.eclipse.jdt.core.compiler.release=disabled 8 | org.eclipse.jdt.core.compiler.source=18 9 | -------------------------------------------------------------------------------- /jogo-oito/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /jogo-oito/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | com.bempaggo.jogo 4 | jogo-do-oito-do-estagiario-gpt 5 | 0.0.1-SNAPSHOT 6 | 7 | UTF-8 8 | 9 | 10 | rest-unity-test 11 | 12 | 13 | org.apache.maven.plugins 14 | maven-compiler-plugin 15 | 3.8.0 16 | 17 | 18 18 | 18 19 | 20 | 21 | 22 | org.apache.maven.plugins 23 | maven-surefire-plugin 24 | 2.22.0 25 | 26 | 27 | 28 | 29 | 30 | org.junit.jupiter 31 | junit-jupiter-api 32 | 5.6.0 33 | test 34 | 35 | 36 | org.junit.jupiter 37 | junit-jupiter-params 38 | 5.6.0 39 | test 40 | 41 | 42 | org.junit.jupiter 43 | junit-jupiter-engine 44 | 5.6.0 45 | test 46 | 47 | 48 | junit 49 | junit 50 | 4.4 51 | test 52 | jar 53 | 54 | 55 | -------------------------------------------------------------------------------- /jogo-oito/src/main/java/facade/Controller.java: -------------------------------------------------------------------------------- 1 | package facade; 2 | 3 | import interfaces.Graph; 4 | import interfaces.Vertex; 5 | import java.util.List; 6 | import util.Board; 7 | 8 | public class Controller { 9 | 10 | private final Graph board; 11 | 12 | public Controller() { 13 | this.board = new Board(); 14 | } 15 | 16 | public void feedback() { 17 | this.board.feedback(); 18 | } 19 | 20 | public void setting() { 21 | this.board.setting(); 22 | } 23 | 24 | public List getCells() { 25 | return this.board.getCells(); 26 | } 27 | 28 | public void swap(Integer keyCode) { 29 | this.board.swap(keyCode); 30 | } 31 | 32 | public Boolean checkGameOver() { 33 | return this.board.checkGameOver(); 34 | 35 | } 36 | 37 | public void click(Integer cellValue) { 38 | this.board.click(cellValue); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /jogo-oito/src/main/java/interfaces/Edge.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license 3 | * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template 4 | */ 5 | package interfaces; 6 | 7 | import model.Keyboard; 8 | 9 | /** 10 | * 11 | * @author allen 12 | */ 13 | public interface Edge { 14 | 15 | Keyboard getKey(); 16 | 17 | Vertex getCell(); 18 | 19 | } 20 | -------------------------------------------------------------------------------- /jogo-oito/src/main/java/interfaces/Graph.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license 3 | * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template 4 | */ 5 | package interfaces; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * 11 | * @author allen 12 | */ 13 | public interface Graph { 14 | 15 | void feedback(); 16 | 17 | void setting(); 18 | 19 | void swap(Integer keyCode); 20 | 21 | List getCells(); 22 | 23 | Vertex getEmptyCell(); 24 | 25 | void click(Integer cellValue); 26 | 27 | Boolean checkGameOver(); 28 | } 29 | -------------------------------------------------------------------------------- /jogo-oito/src/main/java/interfaces/Vertex.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license 3 | * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template 4 | */ 5 | package interfaces; 6 | 7 | import java.util.List; 8 | import model.Keyboard; 9 | 10 | /** 11 | * 12 | * @author allen 13 | */ 14 | public interface Vertex { 15 | 16 | void setValue(Integer value); 17 | 18 | Integer getValue(); 19 | 20 | void creatingHorizontalAdjacent(Vertex cell); 21 | 22 | void creatingVerticalAdjacent(Vertex cell); 23 | 24 | String valueToText(); 25 | 26 | Edge getAdjacentByKeyCode(Keyboard key); 27 | 28 | Vertex click(Keyboard key); 29 | 30 | List getAdjacents(); 31 | 32 | void addAdjacents(Edge edge); 33 | 34 | Vertex swapCells(Integer value); 35 | 36 | } 37 | -------------------------------------------------------------------------------- /jogo-oito/src/main/java/model/Adjacent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license 3 | * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template 4 | */ 5 | package model; 6 | 7 | import interfaces.Edge; 8 | import interfaces.Vertex; 9 | import java.util.Objects; 10 | 11 | /** 12 | * 13 | * @author allen 14 | */ 15 | public class Adjacent implements Edge{ 16 | 17 | private final Keyboard key; 18 | private final Vertex cell; 19 | 20 | public Adjacent(Keyboard key, Vertex cell) { 21 | this.key = key; 22 | this.cell = cell; 23 | } 24 | 25 | @Override 26 | public Keyboard getKey() { 27 | return this.key; 28 | } 29 | 30 | @Override 31 | public Vertex getCell() { 32 | return this.cell; 33 | } 34 | 35 | @Override 36 | public boolean equals(Object obj) { 37 | return Objects.equals(((Adjacent) obj).getKey(), this.getKey()); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /jogo-oito/src/main/java/model/Cell.java: -------------------------------------------------------------------------------- 1 | package model; 2 | 3 | import interfaces.Edge; 4 | import interfaces.Vertex; 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.Objects; 8 | import java.util.Optional; 9 | 10 | public class Cell implements Vertex { 11 | 12 | private Integer value; 13 | private final List adjacents; 14 | public static Integer content; 15 | 16 | public Cell(Integer value) { 17 | this.value = value; 18 | this.adjacents = new ArrayList<>(); 19 | } 20 | 21 | public Cell() { 22 | this.value = Cell.content++; 23 | this.adjacents = new ArrayList<>(); 24 | } 25 | 26 | @Override 27 | public void setValue(Integer value) { 28 | this.value = value; 29 | } 30 | 31 | @Override 32 | public Integer getValue() { 33 | return this.value; 34 | } 35 | 36 | @Override 37 | public void creatingHorizontalAdjacent(Vertex cell) { 38 | this.adjacents.add(new Adjacent(Keyboard.LEFT, cell)); 39 | cell.getAdjacents().add(new Adjacent(Keyboard.RIGHT, this)); 40 | } 41 | 42 | @Override 43 | public void creatingVerticalAdjacent(Vertex cell) { 44 | this.adjacents.add(new Adjacent(Keyboard.UP, cell)); 45 | cell.getAdjacents().add(new Adjacent(Keyboard.DOWN, this)); 46 | } 47 | 48 | @Override 49 | public String valueToText() { 50 | return Optional.of(this.value) 51 | .filter(value -> value != 0) 52 | .map(String::valueOf) 53 | .orElse(""); 54 | } 55 | 56 | @Override 57 | public Edge getAdjacentByKeyCode(Keyboard key) { 58 | Adjacent edge = new Adjacent(key, null); 59 | Integer indexEdge = this.adjacents.indexOf(edge); 60 | return Optional.of(indexEdge) 61 | .filter(index -> index != -1) 62 | .map(this.adjacents::get) 63 | .orElse(null); 64 | } 65 | 66 | @Override 67 | public Vertex click(Keyboard key) { 68 | Edge adjacent = this.getAdjacentByKeyCode(key); 69 | return this.movement(adjacent); 70 | } 71 | 72 | private Vertex movement(Edge adjacent) { 73 | return Optional.ofNullable(adjacent) 74 | .map(Edge::getCell) 75 | .map(this::swapCells) 76 | .orElse(this); 77 | } 78 | 79 | private Vertex swapCells(Vertex movementCell) { 80 | this.setValue(movementCell.getValue()); 81 | movementCell.setValue(0); 82 | return movementCell; 83 | } 84 | 85 | @Override 86 | public Vertex swapCells(Integer value) { 87 | return this.adjacents.stream() 88 | .filter(adjacent -> Objects.equals(adjacent.getCell().getValue(), value)) 89 | .findFirst() 90 | .map(this::movement) 91 | .orElse(this); 92 | } 93 | 94 | @Override 95 | public List getAdjacents() { 96 | return this.adjacents; 97 | } 98 | 99 | @Override 100 | public void addAdjacents(Edge edge) { 101 | this.adjacents.add(edge); 102 | } 103 | 104 | @Override 105 | public boolean equals(Object obj) { 106 | return Objects.equals(this.value, ((Cell) obj).value); 107 | } 108 | 109 | } 110 | -------------------------------------------------------------------------------- /jogo-oito/src/main/java/model/Keyboard.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license 3 | * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template 4 | */ 5 | package model; 6 | 7 | import java.awt.event.KeyEvent; 8 | import java.util.Arrays; 9 | import java.util.Map; 10 | import java.util.function.Function; 11 | import java.util.stream.Collectors; 12 | 13 | public enum Keyboard { 14 | 15 | UP(KeyEvent.VK_UP), 16 | DOWN(KeyEvent.VK_DOWN), 17 | LEFT(KeyEvent.VK_LEFT), 18 | RIGHT(KeyEvent.VK_RIGHT); 19 | 20 | private final Integer value; 21 | 22 | private static final Map map = Arrays.stream(Keyboard.values()) 23 | .collect(Collectors.toMap(Keyboard::getValue, Function.identity())); 24 | 25 | 26 | Keyboard(Integer value) { 27 | this.value = value; 28 | } 29 | 30 | public Integer getValue() { 31 | return value; 32 | } 33 | 34 | public static Keyboard fromValue(Integer value) { 35 | return map.get(value); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /jogo-oito/src/main/java/model/Matrix.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license 3 | * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template 4 | */ 5 | package model; 6 | 7 | import interfaces.Vertex; 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | /** 12 | * 13 | * @author allen 14 | */ 15 | public final class Matrix { 16 | 17 | private final Row firstRow; 18 | private final Row secondRow; 19 | private final Row thirdRow; 20 | public static List cells; 21 | 22 | 23 | public Matrix() { 24 | Matrix.cells = new ArrayList<>(); 25 | Cell.content = 1; 26 | this.firstRow = new Row(); 27 | this.secondRow = new Row(); 28 | this.thirdRow = new Row(); 29 | this.defineAdjacent(); 30 | } 31 | 32 | private void defineAdjacent() { 33 | this.firstRow.initial.creatingVerticalAdjacent(secondRow.initial); 34 | this.secondRow.initial.creatingVerticalAdjacent(thirdRow.initial); 35 | 36 | this.firstRow.center.creatingVerticalAdjacent(secondRow.center); 37 | this.secondRow.center.creatingVerticalAdjacent(thirdRow.center); 38 | 39 | this.firstRow.last.creatingVerticalAdjacent(secondRow.last); 40 | this.secondRow.last.creatingVerticalAdjacent(thirdRow.last); 41 | 42 | this.changePositionToValidateTemplate(); 43 | } 44 | 45 | private void changePositionToValidateTemplate(){ 46 | this.thirdRow.last.setValue(0); 47 | } 48 | 49 | 50 | public List getCells() { 51 | return Matrix.cells; 52 | } 53 | 54 | private final class Row { 55 | 56 | public final Cell initial; 57 | public final Cell center; 58 | public final Cell last; 59 | 60 | public Row() { 61 | this.initial = new Cell(); 62 | this.center = new Cell(); 63 | this.last = new Cell(); 64 | this.defineAdjacent(); 65 | this.loadCells(); 66 | } 67 | 68 | public void loadCells() { 69 | Matrix.cells.add(this.initial); 70 | Matrix.cells.add(this.center); 71 | Matrix.cells.add(this.last); 72 | } 73 | 74 | public void defineAdjacent() { 75 | this.initial.creatingHorizontalAdjacent(this.center); 76 | this.center.creatingHorizontalAdjacent(this.last); 77 | } 78 | 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /jogo-oito/src/main/java/util/Board.java: -------------------------------------------------------------------------------- 1 | package util; 2 | 3 | import interfaces.Graph; 4 | import interfaces.Vertex; 5 | import java.util.ArrayList; 6 | import java.util.Collections; 7 | import java.util.Comparator; 8 | import java.util.Iterator; 9 | import java.util.List; 10 | import java.util.Optional; 11 | import java.util.stream.IntStream; 12 | import model.Keyboard; 13 | import model.Matrix; 14 | 15 | public class Board implements Graph { 16 | 17 | private List cells; 18 | private Vertex emptyCell; 19 | private Integer length; 20 | private Matrix matrix; 21 | 22 | public Board() { 23 | } 24 | 25 | @Override 26 | public void feedback() { 27 | this.matrix = new Matrix(); 28 | this.cells = this.matrix.getCells(); 29 | this.length = cells.size(); 30 | this.defineEmptyCell(); 31 | } 32 | 33 | @Override 34 | public void setting() { 35 | this.matrix = new Matrix(); 36 | this.cells = this.matrix.getCells(); 37 | this.length = cells.size(); 38 | this.shuffleCell(); 39 | this.defineEmptyCell(); 40 | 41 | } 42 | 43 | private void shuffleCell() { 44 | Iterator iterator = this.shuffleValues().iterator(); 45 | this.cells.stream() 46 | .forEach(vertex -> vertex.setValue(iterator.next())); 47 | } 48 | 49 | private List shuffleValues() { 50 | List values = new ArrayList<>(); 51 | this.cells.stream() 52 | .map(Vertex::getValue) 53 | .forEach(values::add); 54 | Collections.shuffle(values); 55 | return values; 56 | } 57 | 58 | private void defineEmptyCell() { 59 | Optional minCell = this.cells.stream() 60 | .min(Comparator.comparing(cell -> cell.getValue())); 61 | minCell.ifPresent(cell -> { 62 | this.emptyCell = cell; 63 | }); 64 | } 65 | 66 | @Override 67 | public void click(Integer cellValue) { 68 | this.emptyCell = this.emptyCell.swapCells(cellValue); 69 | } 70 | 71 | @Override 72 | public void swap(Integer keyCode) { 73 | Keyboard key = Keyboard.fromValue(keyCode); 74 | this.emptyCell = this.emptyCell.click(key); 75 | } 76 | 77 | @Override 78 | public List getCells() { 79 | return this.cells; 80 | } 81 | 82 | @Override 83 | public Vertex getEmptyCell() { 84 | return this.emptyCell; 85 | } 86 | 87 | @Override 88 | public Boolean checkGameOver() { 89 | return IntStream.range(0, this.length) 90 | .allMatch(index -> this.cells.get(index).getValue() == (index + 1) % this.length); 91 | 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /jogo-oito/src/main/java/view/JogoDosOito.java: -------------------------------------------------------------------------------- 1 | package view; 2 | 3 | import facade.Controller; 4 | import interfaces.Vertex; 5 | import java.awt.Font; 6 | import java.awt.GridLayout; 7 | import java.awt.event.ActionEvent; 8 | import java.awt.event.KeyEvent; 9 | import java.awt.event.KeyListener; 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | import java.util.Optional; 13 | import java.util.stream.IntStream; 14 | import javax.swing.JButton; 15 | import javax.swing.JFrame; 16 | import javax.swing.JLabel; 17 | import javax.swing.JOptionPane; 18 | import javax.swing.SwingUtilities; 19 | 20 | public class JogoDosOito extends JFrame implements KeyListener { 21 | 22 | private final List buttons; 23 | private final Controller controller; 24 | private JButton reset; 25 | private JButton feedback; 26 | 27 | public JogoDosOito() { 28 | super("Jogo dos Oito"); 29 | this.controller = new Controller(); 30 | this.controller.setting(); 31 | this.buttons = new ArrayList<>(); 32 | } 33 | 34 | private void configureInterface() { 35 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 36 | setSize(300, 300); 37 | setLayout(new GridLayout(4, 3)); 38 | setVisible(true); 39 | addKeyListener(this); 40 | setFocusable(true); 41 | } 42 | 43 | private void createButtons() { 44 | this.controller.getCells().forEach(cell -> { 45 | JButton button = this.configButton(cell); 46 | add(button); 47 | buttons.add(button); 48 | }); 49 | } 50 | 51 | private Integer textToValue(String text) { 52 | return Optional.ofNullable(text) 53 | .map(Integer::valueOf) 54 | .orElse(0); 55 | } 56 | 57 | private JButton configButton(Vertex cell) { 58 | JButton button = new JButton(); 59 | button.setFont(new Font("Arial", Font.BOLD, 36)); 60 | button.setText(cell.valueToText()); 61 | button.addActionListener((ActionEvent e) -> { 62 | this.controller.click(this.textToValue(button.getText())); 63 | this.updateBoard(); 64 | this.checkGameOver(); 65 | SwingUtilities.getRoot(button).requestFocus(); 66 | }); 67 | return button; 68 | 69 | } 70 | 71 | private void checkGameOver() { 72 | Optional.ofNullable(this.controller.checkGameOver()) 73 | .filter(Boolean::booleanValue) 74 | .ifPresent(gameOver -> { 75 | JOptionPane.showMessageDialog(this, "Parabéns, você venceu!"); 76 | resetGame(); 77 | }); 78 | } 79 | 80 | private void configMenu() { 81 | this.reset = this.configReset(); 82 | this.feedback = this.configFeedback(); 83 | add(this.feedback); 84 | add(this.reset); 85 | add(new JLabel("")); 86 | } 87 | 88 | private JButton configReset() { 89 | JButton buttonReset = new JButton("Reiniciar"); 90 | buttonReset.addActionListener((ActionEvent e) -> { 91 | this.resetGame(); 92 | SwingUtilities.getRoot(buttonReset).requestFocus(); 93 | }); 94 | return buttonReset; 95 | } 96 | 97 | private JButton configFeedback() { 98 | JButton buttonFeedback = new JButton("Gabarito"); 99 | buttonFeedback.addActionListener((ActionEvent e) -> { 100 | this.showFeedback(); 101 | SwingUtilities.getRoot(buttonFeedback).requestFocus(); 102 | }); 103 | return buttonFeedback; 104 | } 105 | 106 | 107 | private void resetGame() { 108 | this.controller.setting(); 109 | this.updateBoard(); 110 | } 111 | 112 | private void showFeedback() { 113 | this.controller.feedback(); 114 | this.updateBoard(); 115 | } 116 | 117 | private void updateBoard() { 118 | List cells = this.controller.getCells(); 119 | IntStream.range(0, cells.size()) 120 | .forEach(index -> { 121 | JButton button = this.buttons.get(index); 122 | button.setText(cells.get(index).valueToText()); 123 | }); 124 | } 125 | 126 | @Override 127 | public void keyTyped(KeyEvent e) { 128 | } 129 | 130 | @Override 131 | public void keyPressed(KeyEvent e) { 132 | this.controller.swap(e.getKeyCode()); 133 | this.updateBoard(); 134 | this.checkGameOver(); 135 | } 136 | 137 | @Override 138 | public void keyReleased(KeyEvent e) { 139 | } 140 | 141 | public static void main(String[] args) { 142 | JogoDosOito game = new JogoDosOito(); 143 | game.createButtons(); 144 | game.configMenu(); 145 | game.configureInterface(); 146 | 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /jogo-oito/src/test/java/game/AdjacentTest.java: -------------------------------------------------------------------------------- 1 | package game; 2 | 3 | import model.Adjacent; 4 | import model.Cell; 5 | import model.Keyboard; 6 | import interfaces.Edge; 7 | import interfaces.Vertex; 8 | import org.junit.jupiter.api.Test; 9 | import static org.junit.jupiter.api.Assertions.*; 10 | 11 | public class AdjacentTest { 12 | 13 | @Test 14 | public void testGetKey() { 15 | Edge edge = new Adjacent(Keyboard.RIGHT, null); 16 | assertEquals(Keyboard.RIGHT, edge.getKey()); 17 | } 18 | 19 | @Test 20 | public void testGetCell() { 21 | Vertex cell = new Cell(1); 22 | Edge edge = new Adjacent(Keyboard.RIGHT, cell); 23 | assertEquals(cell, edge.getCell()); 24 | } 25 | 26 | @Test 27 | public void testEquals() { 28 | Vertex cell1 = new Cell(1); 29 | Vertex cell2 = new Cell(2); 30 | 31 | Edge edge1 = new Adjacent(Keyboard.RIGHT, cell2); 32 | Edge edge2 = new Adjacent(Keyboard.LEFT, cell1); 33 | 34 | assertEquals(edge1.getCell().click(Keyboard.RIGHT), cell2); 35 | assertEquals(edge2.getCell().click(Keyboard.LEFT), cell1); 36 | 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /jogo-oito/src/test/java/game/BoardTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license 3 | * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template 4 | */ 5 | package game; 6 | 7 | /** 8 | * 9 | * @author allen 10 | */ 11 | import model.Cell; 12 | import util.Board; 13 | import interfaces.Edge; 14 | import interfaces.Vertex; 15 | import org.junit.jupiter.api.BeforeEach; 16 | import org.junit.jupiter.api.Test; 17 | import static org.junit.jupiter.api.Assertions.*; 18 | 19 | import java.util.List; 20 | import model.Keyboard; 21 | import org.junit.jupiter.api.Assertions; 22 | 23 | public class BoardTest { 24 | 25 | private Board board; 26 | 27 | @BeforeEach 28 | public void setUp() { 29 | board = new Board(); 30 | board.feedback(); 31 | } 32 | 33 | @Test 34 | public void testSwap() { 35 | Vertex emptyCell = board.getEmptyCell(); 36 | Vertex cell = board.getCells().get(7); 37 | Integer cellValue = cell.getValue(); 38 | 39 | board.click(cellValue); 40 | 41 | assertEquals(cellValue, emptyCell.getValue()); 42 | assertEquals(0, cell.getValue()); 43 | } 44 | 45 | @Test 46 | public void testGetCells() { 47 | List cells = board.getCells(); 48 | 49 | assertNotNull(cells); 50 | assertEquals(9, cells.size()); 51 | } 52 | 53 | @Test 54 | public void testDefineCellRelationships() { 55 | Vertex cell1 = board.getCells().get(1); 56 | Vertex cell2 = board.getCells().get(2); 57 | 58 | Edge adjacent1 = cell1.getAdjacentByKeyCode(model.Keyboard.LEFT); 59 | Edge adjacent2 = cell2.getAdjacentByKeyCode(model.Keyboard.RIGHT); 60 | 61 | assertEquals(cell2, adjacent1.getCell()); 62 | assertEquals(cell1, adjacent2.getCell()); 63 | } 64 | 65 | @Test 66 | public void testClick() { 67 | Vertex emptyCell = board.getEmptyCell(); 68 | Vertex cell = board.getCells().get(7); 69 | Integer cellValue = cell.getValue(); 70 | 71 | board.click(cellValue); 72 | 73 | assertEquals(cellValue, emptyCell.getValue()); 74 | assertEquals(0, cell.getValue()); 75 | } 76 | 77 | @Test 78 | public void testCheckGameOver() { 79 | Assertions.assertTrue(this.board.checkGameOver()); 80 | board = new Board(); 81 | board.setting(); 82 | Assertions.assertFalse(board.checkGameOver()); 83 | 84 | } 85 | 86 | @Test 87 | public void testCellCreatingHorizontalAdjacent() { 88 | Vertex cell1 = new Cell(1); 89 | Vertex cell2 = new Cell(2); 90 | 91 | cell1.creatingHorizontalAdjacent(cell2); 92 | 93 | Edge adjacent1 = cell1.getAdjacentByKeyCode(model.Keyboard.LEFT); 94 | Edge adjacent2 = cell2.getAdjacentByKeyCode(model.Keyboard.RIGHT); 95 | 96 | assertEquals(cell2, adjacent1.getCell()); 97 | assertEquals(cell1, adjacent2.getCell()); 98 | } 99 | 100 | @Test 101 | public void testCellCreatingVerticalAdjacent() { 102 | Vertex cell1 = new Cell(1); 103 | Vertex cell2 = new Cell(2); 104 | 105 | cell1.creatingVerticalAdjacent(cell2); 106 | 107 | Edge adjacent1 = cell1.getAdjacentByKeyCode(model.Keyboard.UP); 108 | Edge adjacent2 = cell2.getAdjacentByKeyCode(model.Keyboard.DOWN); 109 | 110 | assertEquals(cell2, adjacent1.getCell()); 111 | assertEquals(cell1, adjacent2.getCell()); 112 | } 113 | 114 | @Test 115 | public void testValueToText() { 116 | Vertex cell = new Cell(0); 117 | assertEquals("", cell.valueToText()); 118 | 119 | cell.setValue(5); 120 | assertEquals("5", cell.valueToText()); 121 | } 122 | 123 | @Test 124 | public void testGetAdjacentByKeyCode() { 125 | Vertex cell1 = new Cell(1); 126 | Vertex cell2 = new Cell(2); 127 | 128 | cell1.creatingHorizontalAdjacent(cell2); 129 | 130 | Edge adjacent = cell1.getAdjacentByKeyCode(model.Keyboard.LEFT); 131 | assertEquals(cell2, adjacent.getCell()); 132 | } 133 | 134 | @Test 135 | public void testClickDown() { 136 | board.swap(Keyboard.DOWN.getValue()); 137 | Assertions.assertEquals(5, board.getCells().indexOf(board.getEmptyCell())); 138 | } 139 | 140 | @Test 141 | public void testClickUp() { 142 | board.swap(Keyboard.UP.getValue()); 143 | Assertions.assertEquals(8, board.getCells().indexOf(board.getEmptyCell())); 144 | } 145 | 146 | @Test 147 | public void testClickRight() { 148 | board.swap(Keyboard.RIGHT.getValue()); 149 | Assertions.assertEquals(7, board.getCells().indexOf(board.getEmptyCell())); 150 | } 151 | 152 | @Test 153 | public void testClickLeft() { 154 | board.swap(Keyboard.LEFT.getValue()); 155 | Assertions.assertEquals(8, board.getCells().indexOf(board.getEmptyCell())); 156 | } 157 | 158 | } 159 | -------------------------------------------------------------------------------- /jogo-oito/src/test/java/game/CellTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license 3 | * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template 4 | */ 5 | package game; 6 | 7 | import interfaces.Edge; 8 | import interfaces.Vertex; 9 | import java.util.List; 10 | import model.Adjacent; 11 | import model.Cell; 12 | import model.Keyboard; 13 | import org.junit.jupiter.api.Test; 14 | 15 | import static org.junit.jupiter.api.Assertions.*; 16 | 17 | public class CellTest{ 18 | 19 | @Test 20 | public void testSetValue() { 21 | Vertex cell = new Cell(0); 22 | cell.setValue(5); 23 | assertEquals(5, cell.getValue()); 24 | } 25 | 26 | @Test 27 | public void testGetValue() { 28 | Vertex cell = new Cell(10); 29 | assertEquals(10, cell.getValue()); 30 | } 31 | 32 | @Test 33 | public void testCreatingHorizontalAdjacent() { 34 | Vertex cell1 = new Cell(1); 35 | Vertex cell2 = new Cell(2); 36 | 37 | cell1.creatingHorizontalAdjacent(cell2); 38 | 39 | assertEquals(1, cell1.getAdjacents().size()); 40 | assertEquals(1, cell2.getAdjacents().size()); 41 | } 42 | 43 | @Test 44 | public void testCreatingVerticalAdjacent() { 45 | Vertex cell1 = new Cell(1); 46 | Vertex cell2 = new Cell(2); 47 | 48 | cell1.creatingVerticalAdjacent(cell2); 49 | 50 | assertEquals(1, cell1.getAdjacents().size()); 51 | assertEquals(1, cell2.getAdjacents().size()); 52 | } 53 | 54 | @Test 55 | public void testValueToText() { 56 | Vertex cell1 = new Cell(0); 57 | Vertex cell2 = new Cell(5); 58 | 59 | assertEquals("", cell1.valueToText()); 60 | assertEquals("5", cell2.valueToText()); 61 | } 62 | 63 | @Test 64 | public void testGetAdjacentByKeyCode() { 65 | Vertex cell1 = new Cell(1); 66 | Vertex cell2 = new Cell(2); 67 | cell1.creatingHorizontalAdjacent(cell2); 68 | 69 | assertEquals(cell2, cell1.getAdjacentByKeyCode(Keyboard.LEFT).getCell()); 70 | } 71 | 72 | @Test 73 | public void testClick() { 74 | Vertex cell1 = new Cell(1); 75 | Vertex cell2 = new Cell(2); 76 | cell1.creatingHorizontalAdjacent(cell2); 77 | 78 | assertEquals(cell2, cell1.click(Keyboard.LEFT)); 79 | } 80 | 81 | @Test 82 | public void testSwapCells() { 83 | Vertex cell1 = new Cell(0); 84 | Vertex cell2 = new Cell(2); 85 | cell1.creatingHorizontalAdjacent(cell2); 86 | 87 | cell1.swapCells(cell2.getValue()); 88 | 89 | assertEquals(2, cell1.getValue()); 90 | assertEquals(0, cell2.getValue()); 91 | } 92 | 93 | @Test 94 | public void testAddAdjacents() { 95 | Vertex cell1 = new Cell(1); 96 | Vertex cell2 = new Cell(2); 97 | Edge edge = new Adjacent(Keyboard.RIGHT, cell2); 98 | 99 | cell1.addAdjacents(edge); 100 | 101 | List adjacents = cell1.getAdjacents(); 102 | assertEquals(1, adjacents.size()); 103 | assertEquals(edge, adjacents.get(0)); 104 | } 105 | 106 | @Test 107 | public void defineAdjacents() { 108 | Vertex cell1 = new Cell(5); 109 | Vertex cell2 = new Cell(10); 110 | Vertex cell3 = new Cell(15); 111 | Vertex cell4 = new Cell(20); 112 | 113 | cell1.creatingVerticalAdjacent(cell2); 114 | cell1.creatingVerticalAdjacent(cell3); 115 | cell1.creatingHorizontalAdjacent(cell4); 116 | 117 | List adjacents = cell1.getAdjacents(); 118 | assertEquals(cell2, adjacents.get(0).getCell()); 119 | assertEquals(cell3, adjacents.get(1).getCell()); 120 | assertEquals(cell4, adjacents.get(2).getCell()); 121 | } 122 | } 123 | --------------------------------------------------------------------------------