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
24 changes: 22 additions & 2 deletions jogo-oito/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,30 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>18</source>
<target>18</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.0-M1</version>
<type>pom.sha512</type>
</dependency>
</dependencies>
</project>
291 changes: 150 additions & 141 deletions jogo-oito/src/main/java/chat/gpt/JogoDosOito.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Peca> pecas;
private JButton botaoReiniciar;

public JogoDosOito() {
super("Jogo dos Oito");
this.pecas = new ArrayList<Peca>();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
setLayout(new GridLayout(4, 3));

int count = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

A programação orientada a objetos eh um requisito para vaga (https://pt.stackoverflow.com/questions/274391/diferen%C3%A7a-entre-tipo-primitivo-e-objeto-em-java)

Caso interesse para aprofundar mais o conhecimento, veja o livro Gamma, Erich. Padrões de projetos: soluções reutilizáveis. Bookman editora, 2009.

Copy link
Author

Choose a reason for hiding this comment

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

Poderia me informar porque não está orientado a objetos? tem classes, tem objetos, os objetos possuem métodos e atribuições próprias, que são os conceitos base da orientação a objeto (classe e objeto).

Copy link
Contributor

Choose a reason for hiding this comment

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

int eh objeto?

Copy link
Contributor

Choose a reason for hiding this comment

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

Eu revisei este pull request (que não passou) #15 .
Talvez no comentários dela esclareça melhor.

Copy link
Author

Choose a reason for hiding this comment

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

Obrigado pelo comentário.

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