Skip to content

Aprimoramento do jogo dos Oito - [Estágio] #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

170 changes: 14 additions & 156 deletions jogo-oito/src/main/java/chat/gpt/JogoDosOito.java
Original file line number Diff line number Diff line change
@@ -1,158 +1,16 @@
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();
}
}
import chat.gpt.controller.GameController;
import chat.gpt.model.GameModel;
import chat.gpt.view.GameView;

public class JogoDosOito {
public static void main(String[] args) {
// Cria instancia do GameView
GameView view = new GameView();
// Cria instancia do GameModel e passa view como parametro
GameModel model = new GameModel(view);
// Cria instancia do GameController passando model e view como parametro
GameController controller = new GameController(model, view);
}
}
131 changes: 131 additions & 0 deletions jogo-oito/src/main/java/chat/gpt/controller/GameController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package chat.gpt.controller;

import java.awt.event.*;
import javax.swing.*;

import chat.gpt.model.GameModel;
import chat.gpt.view.GameView;

public class GameController implements KeyListener, ActionListener, MouseListener {

private GameModel model;
private GameView view;

// Construtor responsável por iniciar e configurar o controller do jogo
public GameController(GameModel model, GameView view) {
this.model = model;
this.view = view;

// Adiciona ouvinte de eventos do teclado
this.view.addKeyListener(this);
// Adiciona view do botão de reiniciar e o metodo adiciona o proprio controlador como ouvinte
this.view.getBotaoReiniciar().addActionListener(this);

// Captura eventos do mouse nos botões individuais e responda
for (int i = 0; i < 3; i++) {
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.

for (int j = 0; j < 3; j++) {
JButton button = this.view.getBotoes(i, j);
button.addMouseListener(this);
}
}

// Atualiza a exibição do jogo
this.atualizarView();
}

// Metodo que lida com ações de click reiniciando
@Override
public void actionPerformed(ActionEvent e) {
// Chama o metodo reiniciar quando clicado
this.model.reiniciar();
// Atualiza a interface após clicado
this.atualizarView();
}

// Metodo que é chamado quando as teclas (Setas) do teclado é pressionada
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();

switch (keyCode) {
// Case obtém a tecla pressionada
case KeyEvent.VK_UP:
// Com base na tecla pressionada realiza a ação de mover, do metodo mover
this.model.mover(1, 0);
break;
case KeyEvent.VK_DOWN:
this.model.mover(-1, 0);
break;
case KeyEvent.VK_LEFT:
this.model.mover(0, 1);
break;
case KeyEvent.VK_RIGHT:
this.model.mover(0, -1);
break;
}
this.atualizarView();
}


@Override
public void keyTyped(KeyEvent e) {
// Sem ações
}

@Override
public void keyReleased(KeyEvent e) {
// Sem ações
}

// Atualiza a exibição do jogo
private void atualizarView() {
// Recebe matriz do jogo atual
int[][] tabuleiro = model.getTabuleiro();
// Chamada do metodo para atualizar o tabuleiro
view.atualizarTabuleiro(tabuleiro);
// repaint redesenha a tela e exibe as atualizações feitas
view.repaint();
}

// Acionado quando um botão do mouse é clicado
@Override
public void mouseClicked(MouseEvent e) {
// Armazena o botao cliclado na variavel botaoClicado
JButton botaoClicado = (JButton) e.getSource();
int linha = -1;
int coluna = -1;
// Encontra a linha e a coluna do botão clicado
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (this.view.getBotoes(i, j) == botaoClicado) {
linha = i;
coluna = j;
}
}
}
// Move o número para o quadrado vazio
this.model.mover(linha - this.model.getLinhaVazia(), coluna - this.model.getColunaVazia());
// Atualiza a exibição do jogo
this.atualizarView();
}

@Override
public void mousePressed(MouseEvent e) {
// Sem ações
}

@Override
public void mouseReleased(MouseEvent e) {
// Sem ações
}

@Override
public void mouseEntered(MouseEvent e) {
// Sem ações
}

@Override
public void mouseExited(MouseEvent e) {
// Sem ações
}
}
Loading