diff --git a/jogo-oito/src/main/java/chat/gpt/GeradorAleatorioTabuleiro.java b/jogo-oito/src/main/java/chat/gpt/GeradorAleatorioTabuleiro.java new file mode 100644 index 0000000..5a81a51 --- /dev/null +++ b/jogo-oito/src/main/java/chat/gpt/GeradorAleatorioTabuleiro.java @@ -0,0 +1,30 @@ +package chat.gpt; + +import java.util.Random; + +public class GeradorAleatorioTabuleiro { + private int[][] tabuleiro; + private Random random; + + public GeradorAleatorioTabuleiro() { + tabuleiro = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 0}}; + random = new Random(); + } + + public int[][] gerarNumeros() { + int numMovimentos = 100; + for (int i = 0; i < numMovimentos; i++) { + int linha1 = random.nextInt(3); + int coluna1 = random.nextInt(3); + int linha2 = random.nextInt(3); + int coluna2 = random.nextInt(3); + + int temp = tabuleiro[linha1][coluna1]; + tabuleiro[linha1][coluna1] = tabuleiro[linha2][coluna2]; + tabuleiro[linha2][coluna2] = temp; + } + + return tabuleiro; + } + +} diff --git a/jogo-oito/src/main/java/chat/gpt/JogoDosOito.java b/jogo-oito/src/main/java/chat/gpt/JogoDosOito.java index e51dd08..8aadd3e 100644 --- a/jogo-oito/src/main/java/chat/gpt/JogoDosOito.java +++ b/jogo-oito/src/main/java/chat/gpt/JogoDosOito.java @@ -14,145 +14,75 @@ 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 JButton[][] botoes = new JButton[3][3]; + private JButton botaoReiniciar; + private Tabuleiro tabuleiroObjeto; + + public JogoDosOito() { + super("Jogo dos Oito"); + } + + public void montarJanela() { + tabuleiroObjeto = new Tabuleiro(botoes); + 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) { + tabuleiroObjeto.reiniciarJogo(); + } + }); + add(new JLabel("")); + add(botaoReiniciar); + add(new JLabel("")); + + addKeyListener(this); + setFocusable(true); + + tabuleiroObjeto.atualizarTabuleiro(); + setVisible(true); + } + + private void mover(int linha, int coluna) { + tabuleiroObjeto.moverPeca(linha, coluna); + if (tabuleiroObjeto.jogoConcluido()) { + JOptionPane.showMessageDialog(this, "Parabéns, você venceu!"); + tabuleiroObjeto.reiniciarJogo(); + } + } + + 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) { + } } diff --git a/jogo-oito/src/main/java/chat/gpt/Start.java b/jogo-oito/src/main/java/chat/gpt/Start.java new file mode 100644 index 0000000..e84a5fa --- /dev/null +++ b/jogo-oito/src/main/java/chat/gpt/Start.java @@ -0,0 +1,9 @@ +package chat.gpt; + +public class Start { + public static void main(String[] args) { + JogoDosOito jogo = new JogoDosOito(); + + jogo.montarJanela(); + } +} \ No newline at end of file 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..f558a40 --- /dev/null +++ b/jogo-oito/src/main/java/chat/gpt/Tabuleiro.java @@ -0,0 +1,71 @@ +package chat.gpt; + +import javax.swing.JButton; + +public class Tabuleiro { + + private int[][] tabuleiro; + private JButton[][] botoes; + private GeradorAleatorioTabuleiro tab = new GeradorAleatorioTabuleiro(); + + public Tabuleiro(JButton[][] botoes) { + this.tabuleiro = tab.gerarNumeros(); + this.botoes = botoes; + } + + public void setTabuleiro(int[][] tabuleiro) { + this.tabuleiro = tabuleiro; + } + + public 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)); + } + } + } + } + + public void reiniciarJogo() { + int[][] newTabuleiro = tab.gerarNumeros(); + setTabuleiro(newTabuleiro); + atualizarTabuleiro(); + } + + public 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; + } + + public void moverPeca(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; + + tabuleiro[linhaVazia][colunaVazia] = tabuleiro[novaLinha][novaColuna]; + tabuleiro[novaLinha][novaColuna] = 0; + atualizarTabuleiro(); + } +}