Skip to content

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
wants to merge 4 commits 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
5 changes: 3 additions & 2 deletions jogo-oito/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-18">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
Expand Down
13 changes: 8 additions & 5 deletions jogo-oito/.settings/org.eclipse.jdt.core.prefs
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
158 changes: 0 additions & 158 deletions jogo-oito/src/main/java/chat/gpt/JogoDosOito.java

This file was deleted.

11 changes: 11 additions & 0 deletions jogo-oito/src/main/java/victor/GameRun.java
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 jogo-oito/src/main/java/victor/entities/BoardPosition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package victor.entities;

public class BoardPosition {
private final int xPosition;
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.

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 jogo-oito/src/main/java/victor/managers/BoardManager.java
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;
}
}
Loading