-
Notifications
You must be signed in to change notification settings - Fork 56
Refactor: Jogo dos oito [Estágio] #20
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
aleviannaf
wants to merge
5
commits into
bempaggo:main
Choose a base branch
from
aleviannaf:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ceb9b95
refactor: refatorei o codigo principal aplicando poo para a class Tab…
aleviannaf fcf035c
refactor: desacoplei a logica do tabuleiro
aleviannaf 81b4f5e
refactor: separando a responsabilidade de inicialização
aleviannaf 1fa7363
feat: gerador de numeros aleatorios para o tabuleiro
aleviannaf 23303ef
refactor
aleviannaf 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
30 changes: 30 additions & 0 deletions
30
jogo-oito/src/main/java/chat/gpt/GeradorAleatorioTabuleiro.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,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; | ||
| } | ||
|
|
||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package chat.gpt; | ||
|
|
||
| public class Start { | ||
| public static void main(String[] args) { | ||
| JogoDosOito jogo = new JogoDosOito(); | ||
|
|
||
| jogo.montarJanela(); | ||
| } | ||
| } |
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,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(); | ||
| } | ||
| } |
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.
Ola Alexandre.
Veja a revisão deste pull request anterior.
#13
Obrigado por tentar resolver o desafio.