-
Notifications
You must be signed in to change notification settings - Fork 59
Melhoria no código original - Vaga estágio #5
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
vicvictor86
wants to merge
4
commits into
bempaggo:main
Choose a base branch
from
vicvictor86: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
4 commits
Select commit
Hold shift + click to select a range
2aba45b
Support to be capable of execute in eclipse IDE
vicvictor86 ca2c483
Merge pull request #1 from vicvictor86/dev-eclipse
vicvictor86 ef97e10
Removing the unused chat gpt code
vicvictor86 c9260e9
Merge pull request #2 from vicvictor86/dev-eclipse
vicvictor86 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
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,8 +1,11 @@ | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=18 | ||
org.eclipse.jdt.core.compiler.compliance=18 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 | ||
org.eclipse.jdt.core.compiler.compliance=17 | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning | ||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore | ||
org.eclipse.jdt.core.compiler.release=disabled | ||
org.eclipse.jdt.core.compiler.source=18 | ||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning | ||
org.eclipse.jdt.core.compiler.release=enabled | ||
org.eclipse.jdt.core.compiler.source=17 |
This file was deleted.
Oops, something went wrong.
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,11 @@ | ||
package victor; | ||
|
||
import victor.managers.ScreenManager; | ||
|
||
public class GameRun { | ||
|
||
public static void main(String[] args) { | ||
new ScreenManager(); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
jogo-oito/src/main/java/victor/entities/BoardPosition.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 victor.entities; | ||
|
||
public class BoardPosition { | ||
private final int xPosition; | ||
private final int yPosition; | ||
|
||
public BoardPosition(int xPosition, int yPosition) { | ||
this.xPosition = xPosition; | ||
this.yPosition = yPosition; | ||
} | ||
|
||
public int getXPosition() { | ||
return this.xPosition; | ||
} | ||
|
||
public int getYPosition() { | ||
return this.yPosition; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "BoardPosition { " + | ||
"xPosition = " + xPosition + | ||
", yPosition = " + yPosition + | ||
" }"; | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
jogo-oito/src/main/java/victor/managers/BoardManager.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,114 @@ | ||
package victor.managers; | ||
|
||
import java.util.*; | ||
import java.util.stream.IntStream; | ||
|
||
import victor.entities.BoardPosition; | ||
|
||
public class BoardManager { | ||
private final int boardSize; | ||
private final int[][] board; | ||
private final HashMap<String, BoardPosition> boardPositions = new HashMap<>(); | ||
|
||
public BoardManager(int boardSize) { | ||
this.boardSize = boardSize; | ||
this.board = new int[boardSize][boardSize]; | ||
|
||
resetBoard(); | ||
} | ||
|
||
public BoardPosition getBoardPosition(String boardValue) { | ||
return this.boardPositions.get(boardValue); | ||
} | ||
|
||
public int getBoardSize() { | ||
return this.boardSize; | ||
} | ||
|
||
public int getBoardValue(int xPosition, int yPosition) { | ||
return this.board[xPosition][yPosition]; | ||
} | ||
|
||
public BoardPosition getEmptyPositions() { | ||
return this.boardPositions.get("0"); | ||
} | ||
|
||
public void setBoardPositions(Integer boardValue, BoardPosition boardPosition) { | ||
board[boardPosition.getXPosition()][boardPosition.getYPosition()] = boardValue; | ||
boardPositions.put(boardValue.toString(), boardPosition); | ||
} | ||
|
||
public void resetBoard() { | ||
ArrayList<Integer> defaultValuesList = new ArrayList<>(); | ||
int totalBoardSize = this.boardSize * this.boardSize; | ||
|
||
IntStream.range(0, totalBoardSize).forEach(defaultValuesList::add); | ||
|
||
initializeBoardPositions(defaultValuesList); | ||
} | ||
|
||
private void initializeBoardPositions(ArrayList<Integer> defaultValues) { | ||
Random rand = new Random(); | ||
for (int i = 0; i < boardSize; i++) { | ||
for (int j = 0; j < boardSize; j++) { | ||
int randomChoice = rand.nextInt(defaultValues.size()); | ||
int boardValue = defaultValues.remove(randomChoice); | ||
|
||
board[i][j] = boardValue; | ||
boardPositions.put(Integer.toString(boardValue), new BoardPosition(i, j)); | ||
} | ||
} | ||
} | ||
|
||
private int safeSearchInBoard(int x, int y) { | ||
try { | ||
return this.getBoardValue(x, y); | ||
} catch (Exception e) { | ||
return -1; | ||
} | ||
} | ||
|
||
public boolean hasEmptySpaceInSurround(int xPosition, int yPosition) { | ||
boolean hasEmptyAbove = safeSearchInBoard(xPosition, yPosition + 1) == 0; | ||
boolean hasEmptyBelow = safeSearchInBoard(xPosition, yPosition - 1) == 0; | ||
boolean hasEmptyLeft = safeSearchInBoard(xPosition - 1, yPosition) == 0; | ||
boolean hasEmptyRight = safeSearchInBoard(xPosition + 1, yPosition) == 0; | ||
|
||
return hasEmptyBelow || hasEmptyLeft || hasEmptyAbove || hasEmptyRight; | ||
} | ||
|
||
public boolean isOrdered() { | ||
int highestValue = 0; | ||
for (int i = 0; i < boardSize; i++) { | ||
for (int j = 0; j < boardSize; j++) { | ||
int currentValue = board[i][j]; | ||
|
||
if (currentValue == 0 && (i != boardSize - 1 || j != boardSize - 1)) { | ||
return false; | ||
} | ||
|
||
if (currentValue != 0 && currentValue > highestValue) { | ||
highestValue = currentValue; | ||
} else { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public boolean changeButtonsPositions(int buttonXPosition, int buttonYPosition) { | ||
boolean hasEmptySpaceInSurround = this.hasEmptySpaceInSurround(buttonXPosition, buttonYPosition); | ||
|
||
if (hasEmptySpaceInSurround) { | ||
int value = this.getBoardValue(buttonXPosition, buttonYPosition); | ||
BoardPosition emptyPosition = this.getEmptyPositions(); | ||
|
||
this.setBoardPositions(value, emptyPosition); | ||
this.setBoardPositions(0, new BoardPosition(buttonXPosition, buttonYPosition)); | ||
} | ||
|
||
return hasEmptySpaceInSurround; | ||
} | ||
} |
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.
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.