Skip to content

Feature/add harmful item to the board #40

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 5 commits into
base: master
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
57 changes: 56 additions & 1 deletion src/main/java/pl/nogacz/snake/board/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,19 @@ public class Board {
private PawnClass snakeHeadClass = new PawnClass(Pawn.SNAKE_HEAD);
private PawnClass snakeBodyClass = new PawnClass(Pawn.SNAKE_BODY);
private PawnClass foodClass = new PawnClass(Pawn.FOOD);
private PawnClass harmfulItemClass = new PawnClass(Pawn.HARMFUL_ITEM);

private ArrayList<Coordinates> snakeTail = new ArrayList<>();

private Coordinates harmfulItemCoordinates;
private boolean isHarmfulItemThere = false;
private final int SECOND = 1000;
private double start;
private double end;
private int appearTime;
private int disappearTime;
private boolean areRandomTimesAssigned = false;

public Board(Design design) {
this.design = design;

Expand All @@ -51,9 +61,10 @@ private void addStartEntity() {
board.put(new Coordinates(i, 0), new PawnClass(Pawn.BRICK));
board.put(new Coordinates(i, 21), new PawnClass(Pawn.BRICK));
}

addEat();
displayAllImage();
start = System.currentTimeMillis();
addHarmFulItemInRandomTimes();
}

private void checkMap() {
Expand Down Expand Up @@ -96,6 +107,17 @@ private void moveSnakeHead(Coordinates coordinates) {
snakeHeadCoordinates = coordinates;

addEat();

} else if (getPawn(coordinates).getPawn().isHarmfulItem()) {
board.remove(snakeHeadCoordinates);
board.put(coordinates, snakeHeadClass);
snakeHeadCoordinates = coordinates;
if (tailLength > 0) {
moveSnakeBody();
board.remove(snakeTail.get(0));
snakeTail.remove(snakeTail.get(0));
tailLength--;
}
} else {
isEndGame = true;

Expand All @@ -114,6 +136,7 @@ private void moveSnakeHead(Coordinates coordinates) {
}
}
}
addHarmFulItemInRandomTimes();
}

private void moveSnakeBody() {
Expand Down Expand Up @@ -145,6 +168,38 @@ private void addEat() {

board.put(foodCoordinates, foodClass);
}

private void addHarmFulItemInRandomTimes(){
if (!areRandomTimesAssigned) {
do {
appearTime = random.nextInt(10);
disappearTime = random.nextInt(10);
} while (appearTime == 0 || disappearTime == 0);
areRandomTimesAssigned = true;

}

end = System.currentTimeMillis();
if (!isHarmfulItemThere) {
if (end - start > SECOND * appearTime) {
do {
harmfulItemCoordinates = new Coordinates(random.nextInt(21), random.nextInt(21));
} while(isFieldNotNull(harmfulItemCoordinates));

board.put(harmfulItemCoordinates, harmfulItemClass);
isHarmfulItemThere = true;
start = System.currentTimeMillis();
}
} else {
if (end - start > SECOND * disappearTime) {
board.remove(harmfulItemCoordinates);
isHarmfulItemThere = false;
design.removePawn(harmfulItemCoordinates);
areRandomTimesAssigned = false;
start = System.currentTimeMillis();
}
}
}

private void mapTask() {
Task<Void> task = new Task<Void>() {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/pl/nogacz/snake/pawn/Pawn.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
public enum Pawn {
FOOD,
HARMFUL_ITEM,
BRICK,
SNAKE_HEAD,
SNAKE_BODY;
Expand All @@ -13,6 +14,10 @@ public boolean isFood() {
return this == FOOD;
}

public boolean isHarmfulItem() {
return this == HARMFUL_ITEM;
}

public boolean isHead() {
return this == SNAKE_HEAD;
}
Expand Down
Binary file added src/main/resources/HARMFUL_ITEM.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.