diff --git a/jogo-oito/pom.xml b/jogo-oito/pom.xml index ebc4845..7d04663 100644 --- a/jogo-oito/pom.xml +++ b/jogo-oito/pom.xml @@ -14,10 +14,30 @@ maven-compiler-plugin 3.8.0 - 18 - 18 + 1.8 + 1.8 + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.2 + + + + junit + junit + 3.8.1 + test + jar + + + org.junit.jupiter + junit-jupiter + 5.8.0-M1 + pom.sha512 + + \ No newline at end of file diff --git a/jogo-oito/src/main/java/chat/gpt/JogoDosOito.java b/jogo-oito/src/main/java/chat/gpt/JogoDosOito.java index e51dd08..1c8b24b 100644 --- a/jogo-oito/src/main/java/chat/gpt/JogoDosOito.java +++ b/jogo-oito/src/main/java/chat/gpt/JogoDosOito.java @@ -6,6 +6,8 @@ import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; +import java.util.ArrayList; +import java.util.List; import javax.swing.JButton; import javax.swing.JFrame; @@ -14,145 +16,152 @@ public class JogoDosOito extends JFrame implements KeyListener { - private int[][] tabuleiro = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } }; - private JButton[][] botoes = new JButton[3][3]; - private JButton botaoReiniciar; - - public JogoDosOito() { - super("Jogo dos Oito"); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - setSize(300, 300); - setLayout(new GridLayout(4, 3)); - - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { - JButton botao = new JButton(); - botao.setFont(new Font("Arial", Font.BOLD, 36)); - botoes[i][j] = botao; - add(botao); - } - } - - botaoReiniciar = new JButton("Reiniciar"); - botaoReiniciar.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - reiniciarJogo(); - } - }); - add(new JLabel("")); - add(botaoReiniciar); - add(new JLabel("")); - - addKeyListener(this); - setFocusable(true); - atualizarTabuleiro(); - setVisible(true); - } - - public void keyPressed(KeyEvent e) { - int keyCode = e.getKeyCode(); - switch (keyCode) { - case KeyEvent.VK_UP: - mover(1, 0); - break; - case KeyEvent.VK_DOWN: - mover(-1, 0); - break; - case KeyEvent.VK_LEFT: - mover(0, 1); - break; - case KeyEvent.VK_RIGHT: - mover(0, -1); - break; - } - } - - public void keyTyped(KeyEvent e) { - } - - public void keyReleased(KeyEvent e) { - } - - private void mover(int linha, int coluna) { - int linhaVazia = -1; - int colunaVazia = -1; - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { - if (tabuleiro[i][j] == 0) { - linhaVazia = i; - colunaVazia = j; - } - } - } - int novaLinha = linhaVazia + linha; - int novaColuna = colunaVazia + coluna; - if (novaLinha < 0 || novaLinha > 2 || novaColuna < 0 || novaColuna > 2) { - // movimento inválido - return; - } - tabuleiro[linhaVazia][colunaVazia] = tabuleiro[novaLinha][novaColuna]; - tabuleiro[novaLinha][novaColuna] = 0; - atualizarTabuleiro(); - if (jogoConcluido()) { - JOptionPane.showMessageDialog(this, "Parabéns, você venceu!"); - reiniciarJogo(); - } - } - - public static void main(String[] args) { - new JogoDosOito(); - } - - private boolean jogoConcluido() { - int count = 1; - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { - if (tabuleiro[i][j] != count % 9) { - return false; - } - count++; - } - } - return true; - } - - private boolean movimentarPeca(int linha, int coluna) { - if (linha > 0 && tabuleiro[linha - 1][coluna] == 0) { - tabuleiro[linha - 1][coluna] = tabuleiro[linha][coluna]; - tabuleiro[linha][coluna] = 0; - return true; - } else if (linha < 2 && tabuleiro[linha + 1][coluna] == 0) { - tabuleiro[linha + 1][coluna] = tabuleiro[linha][coluna]; - tabuleiro[linha][coluna] = 0; - return true; - } else if (coluna > 0 && tabuleiro[linha][coluna - 1] == 0) { - tabuleiro[linha][coluna - 1] = tabuleiro[linha][coluna]; - tabuleiro[linha][coluna] = 0; - return true; - } else if (coluna < 2 && tabuleiro[linha][coluna + 1] == 0) { - tabuleiro[linha][coluna + 1] = tabuleiro[linha][coluna]; - tabuleiro[linha][coluna] = 0; - return true; - } - return false; - } - - private void atualizarTabuleiro() { - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { - JButton botao = botoes[i][j]; - int valor = tabuleiro[i][j]; - if (valor == 0) { - botao.setText(""); - } else { - botao.setText(String.valueOf(valor)); - } - } - } - } - - private void reiniciarJogo() { - tabuleiro = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } }; - atualizarTabuleiro(); - } + private Tabuleiro tabuleiro = new Tabuleiro(); + private JButton[][] botoes = new JButton[3][3]; + private List pecas; + private JButton botaoReiniciar; + + public JogoDosOito() { + super("Jogo dos Oito"); + this.pecas = new ArrayList(); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setSize(300, 300); + setLayout(new GridLayout(4, 3)); + + int count = 0; + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + JButton botao = new JButton(); + botao.setFont(new Font("Arial", Font.BOLD, 36)); + botoes[i][j] = botao; + add(botao); + if (count < 8) { + count++; + pecas.add(new Peca(tabuleiro.getValue(i, j), i, j)); + } + botoes[i][j].addActionListener((ActionEvent e) -> { + pecas.forEach(c -> { + if (e.getActionCommand().equals(String.valueOf(c.getNumber()))){ + mover(c.getNumX(), c.getNumY()); + } + }); + }); + } + } + + botaoReiniciar = new JButton("Reiniciar"); + botaoReiniciar.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + reiniciarJogo(); + } + }); + + add(new JLabel("")); + add(botaoReiniciar); + add(new JLabel("")); + + addKeyListener(this); + setFocusable(true); + atualizarTabuleiro(); + setVisible(true); + } + + private void mover(int linha, int coluna) { + int linhaVazia = -1; + int colunaVazia = -1; + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + if (tabuleiro.getValue(i, j) == 0) { + linhaVazia = i; + colunaVazia = j; + } + } + } + + if ((linhaVazia != linha || colunaVazia != coluna)) { + if (linhaVazia == linha) { + if ((Math.abs(coluna - colunaVazia) < 2)) { + movimentarPeca(linha, coluna, linhaVazia, colunaVazia); + } + } else if (colunaVazia == coluna) { + if (Math.abs(linha - linhaVazia) < 2) { + movimentarPeca(linha, coluna, linhaVazia, colunaVazia); + } + } + } + } + + public static void main(String[] args) { + new JogoDosOito(); + } + + public boolean jogoConcluido() { + int count = 1; + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + if (tabuleiro.getValue(i, j) != count % 9) { + return false; + } + count++; + } + } + return true; + } + + private void movimentarPeca(int linha, int coluna, int linhaVazia, int colunaVazia) { + tabuleiro.setValue(linhaVazia, colunaVazia, tabuleiro.getValue(linha, coluna)); + tabuleiro.setValue(linha, coluna, 0); + atualizarTabuleiro(); + //this.tabuleiro.ordenar(); + if (jogoConcluido()) { + JOptionPane.showMessageDialog(this, "Parabéns, você venceu!"); + reiniciarJogo(); + } + } + + private void atualizarTabuleiro() { + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + JButton botao = botoes[i][j]; + int valor = tabuleiro.getValue(i, j); + if (valor == 0) { + botao.setText(""); + } else { + botao.setText(String.valueOf(valor)); + } + botoes[i][j] = botao; + for (int e = 0; e < 8; e++) { + if (pecas.get(e).getNumber() == valor){ + pecas.get(e).setNumX(i); + pecas.get(e).setNumY(j); + pecas.get(e).setNumber(valor); + } + } + } + } + } + + private void reiniciarJogo() { + tabuleiro = new Tabuleiro(); + atualizarTabuleiro(); + } + + public void ordenar() { + this.tabuleiro.ordenar(); + } + + @Override + public void keyTyped(KeyEvent e) { + throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } + + @Override + public void keyPressed(KeyEvent e) { + throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } + + @Override + public void keyReleased(KeyEvent e) { + throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } } diff --git a/jogo-oito/src/main/java/chat/gpt/Peca.java b/jogo-oito/src/main/java/chat/gpt/Peca.java new file mode 100644 index 0000000..d495fbe --- /dev/null +++ b/jogo-oito/src/main/java/chat/gpt/Peca.java @@ -0,0 +1,45 @@ +/* + * 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 chat.gpt; + +/** + * + * @author diego + */ +public class Peca { + private int numX; + private int numY; + private int number; + + public Peca(int number, int x, int y) { + this.number = number; + this.numX = x; + this.numY = y; + } + + public void setNumX(int value){ + this.numX = value; + } + + public int getNumX(){ + return this.numX; + } + + public void setNumY(int value){ + this.numY = value; + } + + public int getNumY(){ + return this.numY; + } + + public void setNumber(int value){ + this.number = value; + } + + public int getNumber(){ + return this.number; + } +} diff --git a/jogo-oito/src/main/java/chat/gpt/Tabuleiro.java b/jogo-oito/src/main/java/chat/gpt/Tabuleiro.java new file mode 100644 index 0000000..78f8215 --- /dev/null +++ b/jogo-oito/src/main/java/chat/gpt/Tabuleiro.java @@ -0,0 +1,74 @@ +/* + * 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 chat.gpt; + +import java.util.Random; +import javax.swing.JButton; + +/** + * + * @author diego + */ +public class Tabuleiro { + + private int[][] tabuleiro; + + public Tabuleiro() { + tabuleiro = new int[][] { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } }; + this.embaralhar(); + } + + public int getValue(int x, int y){ + return this.tabuleiro[x][y]; + } + + public int[][] getTabuleiro(){ + return this.tabuleiro; + } + + public void setValue(int x, int y, int value) { + this.tabuleiro[x][y] = value; + } + + public void setTabuleiro( int[][] value){ + this.tabuleiro = value; + } + + public void embaralhar() { + //instância um objeto da classe Random usando o construtor básico + Random gerador = new Random(); + tabuleiro = new int[][] { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } }; + + //imprime sequência de 10 números inteiros aleatórios entre 0 e 25 + int count = 0; + int x = 0,y = 0, z = 0; + while (count < 8) { + int valor = gerador.nextInt(9); + int result = 0; + for(int i = 0; i < 3; i++) { + for(int j = 0; j < 3; j++) { + if(tabuleiro[i][j] == valor) + result = 1; + if(tabuleiro[i][j] == 0 && x == 0 && y == 0 && z == 0){ + x = i; + y = j; + z = 1; + } + } + } + if (result == 0) { + tabuleiro[x][y] = valor; + count++; + } + x = 0; + y = 0; + z = 0; + } + } + + public void ordenar() { + tabuleiro = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } }; + } +} diff --git a/jogo-oito/src/test/java/JogoDosOitoTest.java b/jogo-oito/src/test/java/JogoDosOitoTest.java new file mode 100644 index 0000000..1747757 --- /dev/null +++ b/jogo-oito/src/test/java/JogoDosOitoTest.java @@ -0,0 +1,31 @@ + +import chat.gpt.JogoDosOito; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/* + * 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 + */ + +/** + * + * @author diego + */ +public class JogoDosOitoTest { + + @Test + public void shouldReturnFalseWhenListUnordered() { + JogoDosOito jogo = new JogoDosOito(); + boolean value = jogo.jogoConcluido(); + Assertions.assertEquals(false, value); + } + + @Test + public void shouldReturnFalseWhenListOrdered() { + JogoDosOito jogo = new JogoDosOito(); + jogo.ordenar(); + boolean value = jogo.jogoConcluido(); + Assertions.assertEquals(true, value); + } +}