From 021330354478070d149e674087581a12c2edc022 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Sun, 25 Jun 2023 16:26:29 -0300 Subject: [PATCH] commit final developer-challenge --- jogo-oito/.classpath | 32 +++- .../org.eclipse.core.resources.prefs | 1 + .../.settings/org.eclipse.jdt.apt.core.prefs | 2 + .../.settings/org.eclipse.jdt.core.prefs | 7 +- jogo-oito/pom.xml | 18 +- .../src/main/java/chat/gpt/JogoDosOito.java | 158 ------------------ .../main/java/chat/gpt/gui/JogoDosOito.java | 153 +++++++++++++++++ .../main/java/chat/gpt/model/Tabuleiro.java | 74 ++++++++ .../src/main/java/chat/gpt/model/Tecla.java | 25 +++ 9 files changed, 305 insertions(+), 165 deletions(-) create mode 100644 jogo-oito/.settings/org.eclipse.jdt.apt.core.prefs delete mode 100644 jogo-oito/src/main/java/chat/gpt/JogoDosOito.java create mode 100644 jogo-oito/src/main/java/chat/gpt/gui/JogoDosOito.java create mode 100644 jogo-oito/src/main/java/chat/gpt/model/Tabuleiro.java create mode 100644 jogo-oito/src/main/java/chat/gpt/model/Tecla.java diff --git a/jogo-oito/.classpath b/jogo-oito/.classpath index 1d97b2f..823648c 100644 --- a/jogo-oito/.classpath +++ b/jogo-oito/.classpath @@ -13,7 +13,7 @@ - + @@ -23,5 +23,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/jogo-oito/.settings/org.eclipse.core.resources.prefs b/jogo-oito/.settings/org.eclipse.core.resources.prefs index 99f26c0..e9441bb 100644 --- a/jogo-oito/.settings/org.eclipse.core.resources.prefs +++ b/jogo-oito/.settings/org.eclipse.core.resources.prefs @@ -1,2 +1,3 @@ eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 encoding/=UTF-8 diff --git a/jogo-oito/.settings/org.eclipse.jdt.apt.core.prefs b/jogo-oito/.settings/org.eclipse.jdt.apt.core.prefs new file mode 100644 index 0000000..d4313d4 --- /dev/null +++ b/jogo-oito/.settings/org.eclipse.jdt.apt.core.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.apt.aptEnabled=false diff --git a/jogo-oito/.settings/org.eclipse.jdt.core.prefs b/jogo-oito/.settings/org.eclipse.jdt.core.prefs index bc0c2ff..c9d1e55 100644 --- a/jogo-oito/.settings/org.eclipse.jdt.core.prefs +++ b/jogo-oito/.settings/org.eclipse.jdt.core.prefs @@ -1,8 +1,9 @@ eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.targetPlatform=18 -org.eclipse.jdt.core.compiler.compliance=18 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 +org.eclipse.jdt.core.compiler.compliance=17 org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore +org.eclipse.jdt.core.compiler.processAnnotations=disabled org.eclipse.jdt.core.compiler.release=disabled -org.eclipse.jdt.core.compiler.source=18 +org.eclipse.jdt.core.compiler.source=17 diff --git a/jogo-oito/pom.xml b/jogo-oito/pom.xml index ebc4845..17e0e83 100644 --- a/jogo-oito/pom.xml +++ b/jogo-oito/pom.xml @@ -1,4 +1,6 @@ - + 4.0.0 com.bempaggo.jogo jogo-do-oito-do-estagiario-gpt @@ -6,6 +8,16 @@ UTF-8 + + + + org.projectlombok + lombok + 1.18.28 + provided + + + rest-unity-test @@ -14,8 +26,8 @@ maven-compiler-plugin 3.8.0 - 18 - 18 + 17 + 17 diff --git a/jogo-oito/src/main/java/chat/gpt/JogoDosOito.java b/jogo-oito/src/main/java/chat/gpt/JogoDosOito.java deleted file mode 100644 index e51dd08..0000000 --- a/jogo-oito/src/main/java/chat/gpt/JogoDosOito.java +++ /dev/null @@ -1,158 +0,0 @@ -package chat.gpt; - -import java.awt.Font; -import java.awt.GridLayout; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.KeyEvent; -import java.awt.event.KeyListener; - -import javax.swing.JButton; -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JOptionPane; - -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(); - } -} diff --git a/jogo-oito/src/main/java/chat/gpt/gui/JogoDosOito.java b/jogo-oito/src/main/java/chat/gpt/gui/JogoDosOito.java new file mode 100644 index 0000000..eee9886 --- /dev/null +++ b/jogo-oito/src/main/java/chat/gpt/gui/JogoDosOito.java @@ -0,0 +1,153 @@ +package chat.gpt.gui; + +import javax.swing.*; + +import chat.gpt.model.Tabuleiro; +import chat.gpt.model.Tecla; + +import java.awt.*; +import java.awt.event.*; + +public class JogoDosOito extends JFrame { + private static final long serialVersionUID = 1L; + private Tabuleiro tabuleiro; + private JButton[][] botoes; + + public JogoDosOito() { + super("Jogo dos Oito"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setSize(300, 300); + setLayout(new GridLayout(4, 3)); + + tabuleiro = new Tabuleiro(); + botoes = new JButton[3][3]; + + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + JButton botao = criarBotao(i, j); + botoes[i][j] = botao; + add(botao); + } + } + + add(new JLabel("")); + JButton botaoReiniciar = new JButton("Reiniciar"); + botaoReiniciar.addActionListener(e -> { + tabuleiro.reiniciarJogo(); + atualizarTabuleiro(); + }); + add(botaoReiniciar); + add(new JLabel("")); + + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + JButton botao = botoes[i][j]; + botao.addKeyListener(new KeyAdapter() { + @Override + public void keyPressed(KeyEvent e) { + int keyCode = e.getKeyCode(); + moverTeclaPorTeclado(keyCode); + } + }); + + botao.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + moverTeclaPorMouse(botao); + } + }); + } + } + + atualizarTabuleiro(); + setVisible(true); + } + + private JButton criarBotao(int linha, int coluna) { + JButton botao = new JButton(); + botao.setFont(new Font("Arial", Font.BOLD, 36)); + return botao; + } + + private void atualizarTabuleiro() { + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + JButton botao = botoes[i][j]; + Tecla tecla = tabuleiro.getTecla(i, j); + if (tecla.ehVazia()) { + botao.setText(""); + } else { + botao.setText(String.valueOf(tecla.getValor())); + } + } + } + } + + private void moverTeclaPorMouse(JButton botao) { + int linhaVazia = tabuleiro.getLinhaVazia(); + int colunaVazia = tabuleiro.getColunaVazia(); + int linhaBotao = -1; + int colunaBotao = -1; + + // Encontra a posição do botão clicado no tabuleiro + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + if (botoes[i][j] == botao) { + linhaBotao = i; + colunaBotao = j; + break; + } + } + } + + // Calcula a diferença entre as posições do botão e da tecla vazia + int diferencaLinha = linhaBotao - linhaVazia; + int diferencaColuna = colunaBotao - colunaVazia; + + // Verifica se o movimento é válido e move a tecla no tabuleiro + if ((Math.abs(diferencaLinha) == 1 && diferencaColuna == 0) + || (Math.abs(diferencaColuna) == 1 && diferencaLinha == 0)) { + tabuleiro.movimentarTecla(diferencaLinha, diferencaColuna); + atualizarTabuleiro(); + + // Verifica se o jogo foi concluído + if (tabuleiro.jogoConcluido()) { + JOptionPane.showMessageDialog(this, "Parabéns, você venceu!"); + tabuleiro.reiniciarJogo(); + atualizarTabuleiro(); + } + } + } + + private void moverTeclaPorTeclado(int direcao) { + int linha = 0; + int coluna = 0; + + switch (direcao) { + case KeyEvent.VK_UP: + linha = -1; + break; + case KeyEvent.VK_DOWN: + linha = 1; + break; + case KeyEvent.VK_LEFT: + coluna = -1; + break; + case KeyEvent.VK_RIGHT: + coluna = 1; + break; + } + + tabuleiro.movimentarTecla(linha, coluna); + atualizarTabuleiro(); + if (tabuleiro.jogoConcluido()) { + JOptionPane.showMessageDialog(this, "Parabéns, você venceu!"); + tabuleiro.reiniciarJogo(); + atualizarTabuleiro(); + } + } + + public static void main(String[] args) { + SwingUtilities.invokeLater(JogoDosOito::new); + } +} diff --git a/jogo-oito/src/main/java/chat/gpt/model/Tabuleiro.java b/jogo-oito/src/main/java/chat/gpt/model/Tabuleiro.java new file mode 100644 index 0000000..b69ad0b --- /dev/null +++ b/jogo-oito/src/main/java/chat/gpt/model/Tabuleiro.java @@ -0,0 +1,74 @@ +package chat.gpt.model; + +import lombok.Getter; + +@Getter +public class Tabuleiro { + private Tecla[][] teclas; + private int linhaVazia; + private int colunaVazia; + + public Tabuleiro() { + teclas = new Tecla[3][3]; + linhaVazia = 2; + colunaVazia = 2; + inicializarTeclas(); + } + + private void inicializarTeclas() { + int valor = 1; + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + teclas[i][j] = new Tecla(valor, i, j); + valor++; + } + } + teclas[2][2].setValor(0); + } + + public Tecla getTecla(int linha, int coluna) { + return teclas[linha][coluna]; + } + + public void movimentarTecla(int linha, int coluna) { + int novaLinha = linhaVazia + linha; + int novaColuna = colunaVazia + coluna; + + if (novaLinha >= 0 && novaLinha < 3 && novaColuna >= 0 && novaColuna < 3) { + Tecla teclaVazia = teclas[linhaVazia][colunaVazia]; + Tecla teclaMovida = teclas[novaLinha][novaColuna]; + teclaVazia.movimentar(linha, coluna); + teclaMovida.movimentar(-linha, -coluna); + teclas[linhaVazia][colunaVazia] = teclaMovida; + teclas[novaLinha][novaColuna] = teclaVazia; + linhaVazia = novaLinha; + colunaVazia = novaColuna; + } + } + + public boolean jogoConcluido() { + int valor = 1; + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + Tecla tecla = teclas[i][j]; + if (i == 2 && j == 2) { + if (tecla.getValor() != 0) { + return false; + } + } else { + if (tecla.getValor() != valor) { + return false; + } + valor++; + } + } + } + return true; + } + + public void reiniciarJogo() { + inicializarTeclas(); + linhaVazia = 2; + colunaVazia = 2; + } +} diff --git a/jogo-oito/src/main/java/chat/gpt/model/Tecla.java b/jogo-oito/src/main/java/chat/gpt/model/Tecla.java new file mode 100644 index 0000000..ea058c6 --- /dev/null +++ b/jogo-oito/src/main/java/chat/gpt/model/Tecla.java @@ -0,0 +1,25 @@ +package chat.gpt.model; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@AllArgsConstructor +public class Tecla { + + private int valor; + private int posicaoX; + private int posicaoY; + + public boolean ehVazia() { + return valor == 0; + } + + public void movimentar(int linha, int coluna) { + this.posicaoX += linha; + this.posicaoY += coluna; + } +} +