Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions jogo-oito/src/main/java/chat/gpt/GeradorAleatorioTabuleiro.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package chat.gpt;

import java.util.Random;

public class GeradorAleatorioTabuleiro {
private int[][] tabuleiro;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ola Alexandre.
Veja a revisão deste pull request anterior.
#13

Obrigado por tentar resolver o desafio.

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;
}

}
212 changes: 71 additions & 141 deletions jogo-oito/src/main/java/chat/gpt/JogoDosOito.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}
}
9 changes: 9 additions & 0 deletions jogo-oito/src/main/java/chat/gpt/Start.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package chat.gpt;

public class Start {
public static void main(String[] args) {
JogoDosOito jogo = new JogoDosOito();

jogo.montarJanela();
}
}
71 changes: 71 additions & 0 deletions jogo-oito/src/main/java/chat/gpt/Tabuleiro.java
Original file line number Diff line number Diff line change
@@ -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();
}
}