-
Notifications
You must be signed in to change notification settings - Fork 56
Desenvolvedor Estágio. #30
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
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0d82bf4
Adicionado arquivos iniciais do projeto.
leooresende01 d52ca28
Adicionado representação da posição de uma peça, tabulerio e moviment…
leooresende01 a40760d
Removida logica do jogo da classe maine adicionada em uma classe espe…
leooresende01 b9d989d
Adicionado testes.
leooresende01 3e35716
Merge branch 'hotfix/DEV20230702-1540'
leooresende01 c6f3475
Mudado o do tabuleiro para uma lista orientada a objetos.
leooresende01 d98e845
Criado model de Peca e mudado o array de botoes para uma lista orient…
leooresende01 9bdc521
Corrigido nomeclatura dos movimentos.
leooresende01 5905372
Corrigindo nomenclatura e testes.
leooresende01 5542736
Mais correcoes de nomenclatura.
leooresende01 24282da
Merge branch 'hotfix/DEV20230702-1610'
leooresende01 7436697
Algumas correções.
leooresende01 f14dbf8
Merge branch 'hotfix/DEV20230702-1924'
leooresende01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,52 @@ | ||
| /target/ | ||
| HELP.md | ||
| target/ | ||
| !.mvn/wrapper/maven-wrapper.jar | ||
| !**/src/main/**/target/ | ||
| !**/src/test/**/target/ | ||
|
|
||
| ### STS ### | ||
| .apt_generated | ||
| .classpath | ||
| .factorypath | ||
| .project | ||
| .settings | ||
| .springBeans | ||
| .sts4-cache | ||
|
|
||
| ### IntelliJ IDEA ### | ||
| .idea | ||
| *.iws | ||
| *.iml | ||
| *.ipr | ||
|
|
||
| ### NetBeans ### | ||
| /nbproject/private/ | ||
| /nbbuild/ | ||
| /dist/ | ||
| /nbdist/ | ||
| /.nb-gradle/ | ||
| build/ | ||
| !**/src/main/**/build/ | ||
| !**/src/test/**/build/ | ||
|
|
||
| ### VS Code ### | ||
| .vscode/ | ||
|
|
||
|
|
||
| ### Maven ### | ||
| target/ | ||
| pom.xml.tag | ||
| pom.xml.releaseBackup | ||
| pom.xml.versionsBackup | ||
| pom.xml.next | ||
| release.properties | ||
| dependency-reduced-pom.xml | ||
| buildNumber.properties | ||
| .mvn/ | ||
| .settings/ | ||
| baeldung.jks | ||
| baeldung.p12 | ||
| .mvn/timing.properties | ||
| .mvn/wrapper/maven-wrapper.jar | ||
| src/main/resources/application.properties |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,158 +1,12 @@ | ||
| 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 chat.gpt.ui.JogoDosOitoUI; | ||
|
|
||
| import javax.swing.JButton; | ||
| import javax.swing.JFrame; | ||
| import javax.swing.JLabel; | ||
| import javax.swing.JOptionPane; | ||
| public class JogoDosOito { | ||
|
|
||
| public class JogoDosOito extends JFrame implements KeyListener { | ||
| public static final String NOME_DO_JOGO = "Jogo dos Oito"; | ||
|
|
||
| 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(); | ||
| } | ||
| public static void main(String[] args) { | ||
| new JogoDosOitoUI(JogoDosOito.NOME_DO_JOGO).iniciarJogo(); | ||
| } | ||
| } |
6 changes: 6 additions & 0 deletions
6
jogo-oito/src/main/java/chat/gpt/infra/contratos/TabuleiroLaco.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package chat.gpt.infra.contratos; | ||
|
|
||
| public interface TabuleiroLaco { | ||
|
|
||
| void executar(Integer linha, Integer coluna); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package chat.gpt.infra.util; | ||
|
|
||
| import chat.gpt.infra.values.DirecaoMovimento; | ||
| import chat.gpt.model.PosicaoPeca; | ||
|
|
||
| public class Util { | ||
|
|
||
| public static Boolean movimentoEhInvalido(DirecaoMovimento pMovimento) { | ||
| DirecaoMovimento semMovimento = DirecaoMovimento.SEM_MOVIMENTO; | ||
| return pMovimento.getDeslocamentoVertical() == semMovimento.getDeslocamentoVertical() | ||
| && pMovimento.getDeslocamentoHorizontal() == semMovimento.getDeslocamentoHorizontal(); | ||
| } | ||
|
|
||
| public static Boolean posicaoEhInvalida(PosicaoPeca pMovimento) { | ||
| return pMovimento.getLinha() < 0 || pMovimento.getLinha() > 2 | ||
| || pMovimento.getColuna() < 0 || pMovimento.getColuna() > 2; | ||
| } | ||
| } | ||
45 changes: 45 additions & 0 deletions
45
jogo-oito/src/main/java/chat/gpt/infra/values/DirecaoMovimento.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package chat.gpt.infra.values; | ||
|
|
||
| import chat.gpt.model.PosicaoPeca; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| public enum DirecaoMovimento { | ||
| CIMA(1, 0), | ||
| BAIXO(-1, 0), | ||
| ESQUERDA(0, 1), | ||
| DIREITA(0, -1), | ||
| SEM_MOVIMENTO(0, 0); | ||
|
|
||
| private final Integer deslocamentoHorizontal; | ||
| private final Integer deslocamentoVertical; | ||
|
|
||
| DirecaoMovimento(Integer deslocamentoHorizontal, Integer deslocamentoVertical) { | ||
| this.deslocamentoHorizontal = deslocamentoHorizontal; | ||
| this.deslocamentoVertical = deslocamentoVertical; | ||
| } | ||
|
|
||
| public Integer getDeslocamentoHorizontal() { | ||
| return deslocamentoHorizontal; | ||
| } | ||
|
|
||
| public Integer getDeslocamentoVertical() { | ||
| return deslocamentoVertical; | ||
| } | ||
|
|
||
| public static DirecaoMovimento pegaPelaPosicaoPecaClicada(PosicaoPeca pVazia, PosicaoPeca pPecaClicada) { | ||
| Integer linha = pPecaClicada.pegaDistanciaLinhasPara(pVazia); | ||
| Integer coluna = pPecaClicada.pegaDistanciaColunasPara(pVazia); | ||
| return Arrays.stream(values()) | ||
| .filter(movimentoMouseValue -> movimentoMouseValue.deslocamentoHorizontal.equals(linha) && | ||
| movimentoMouseValue.deslocamentoVertical.equals(coluna)) | ||
| .findFirst().orElse(DirecaoMovimento.SEM_MOVIMENTO); | ||
| } | ||
|
|
||
| public static DirecaoMovimento pegaPeloTeclado(Integer teclaCode) { | ||
| return Arrays.stream(DirecaoMovimentoTeclado.values()) | ||
| .filter(movimentoTeclado -> movimentoTeclado.getTeclaCode().equals(teclaCode)) | ||
| .map(DirecaoMovimentoTeclado::getMovimento) | ||
| .findFirst().orElse(DirecaoMovimento.SEM_MOVIMENTO); | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
jogo-oito/src/main/java/chat/gpt/infra/values/DirecaoMovimentoTeclado.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package chat.gpt.infra.values; | ||
|
|
||
| import java.awt.event.KeyEvent; | ||
|
|
||
| public enum DirecaoMovimentoTeclado { | ||
| KEY_UP(KeyEvent.VK_UP, DirecaoMovimento.CIMA), | ||
| KEY_DOWN(KeyEvent.VK_DOWN, DirecaoMovimento.BAIXO), | ||
| KEY_LEFT(KeyEvent.VK_LEFT, DirecaoMovimento.ESQUERDA), | ||
| KEY_RIGHT(KeyEvent.VK_RIGHT, DirecaoMovimento.DIREITA); | ||
|
|
||
|
|
||
| private final DirecaoMovimento movimento; | ||
| private final Integer teclaCode; | ||
|
|
||
| DirecaoMovimentoTeclado(Integer teclaCode, DirecaoMovimento movimento) { | ||
| this.teclaCode = teclaCode; | ||
| this.movimento = movimento; | ||
| } | ||
|
|
||
| public DirecaoMovimento getMovimento() { | ||
| return movimento; | ||
| } | ||
|
|
||
| public Integer getTeclaCode() { | ||
| return teclaCode; | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
jogo-oito/src/main/java/chat/gpt/infra/values/TabuleiroPecas.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package chat.gpt.infra.values; | ||
|
|
||
| import chat.gpt.model.Peca; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| public enum TabuleiroPecas { | ||
| LINHA_UM(1, 2, 3), | ||
| LINHA_DOIS(4, 5, 6), | ||
| LINHA_TRES(7, 8, 0); | ||
|
|
||
|
|
||
| private final Peca pecaColuna1; | ||
| private final Peca pecaColuna2; | ||
| private final Peca pecaColuna3; | ||
|
|
||
| TabuleiroPecas(Integer coluna1, Integer coluna2, Integer coluna3) { | ||
| this.pecaColuna1 = new Peca(coluna1); | ||
| this.pecaColuna2 = new Peca(coluna2); | ||
| this.pecaColuna3 = new Peca(coluna3); | ||
| } | ||
|
|
||
| public static List<List<Peca>> pegaPecasTabuleiro() { | ||
| return new ArrayList<>(Arrays.stream(TabuleiroPecas.values()) | ||
| .map(tabuleiroPecas -> Arrays.asList(tabuleiroPecas.pecaColuna1, | ||
| tabuleiroPecas.pecaColuna2, tabuleiroPecas.pecaColuna3)) | ||
| .toList()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package chat.gpt.model; | ||
|
|
||
| public class Peca { | ||
|
|
||
| private final Integer valor; | ||
|
|
||
| public Peca(Integer valor) { | ||
| this.valor = valor; | ||
| } | ||
|
|
||
| public Integer getValor() { | ||
| return this.valor; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oi,
Obrigado por tentar resolver o desafio.
Já recebemos muitos pull request de candidatos com falta de disciplina em relação à POO.
Vc pode conferir um aqui #21
Atenciosamente
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Olá, muito obrigado por promover esse desafio, foi bem interressante ter tentado resolver ele, acho que valeu bastante a pena o tempo gasto.
Sobre a forma da comparação de objetos usando "==", a principio eu estava usando tipos primitivos, por conta de no android, ocorrer varios problemas quando se trata um wrapper como um tipo primitivo, ai pelo costume de programar pra essa plataforma eu acabei descuidando e usando alguns tipos primitivos.
Porém quando eu percebi eu já tinha feito o pull request, ai já era tarde, mas no ultimo commit eu ainda fiz a mudança pros wrappers, só que em alguns locais (como esse), eu deixei escapar.
Mas uma vez, muito obrigado pelo desafio e pelo feedback.