-
Notifications
You must be signed in to change notification settings - Fork 513
Expand file tree
/
Copy pathBoard.java
More file actions
42 lines (36 loc) · 1.13 KB
/
Copy pathBoard.java
File metadata and controls
42 lines (36 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Board {
private final Map<Integer, Integer> snakes = new HashMap<>();
private final Map<Integer, Integer> ladders = new HashMap<>();
private final int boardSize;
public Board(List<Snake> snakeList, List<Ladder> ladderList, int boardSize){
this.boardSize = boardSize;
for(Snake s : snakeList){
snakes.put(s.getHead(), s.getTail());
}
for(Ladder l : ladderList){
ladders.put(l.getStart(), l.getEnd());
}
}
public int getBoardSize() {
return boardSize;
}
public int getNextPosition(int currentPosition) {
int newPos = currentPosition;
//handle snake and ladder logic
boolean moved;
do {
moved = false;
if(ladders.containsKey(newPos)){
newPos = ladders.get(newPos);
moved = true;
} else if(snakes.containsKey(newPos)){
newPos = snakes.get(newPos);
moved = true;
}
} while (moved);
return newPos;
}
}