-
Notifications
You must be signed in to change notification settings - Fork 507
Expand file tree
/
Copy pathBoard.java
More file actions
38 lines (31 loc) · 908 Bytes
/
Board.java
File metadata and controls
38 lines (31 loc) · 908 Bytes
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
import java.util.Map;
import java.util.HashMap;
public class Board {
private int size;
private Map<Integer, Integer> snakes;
private Map<Integer, Integer> ladders;
public Board(int size) {
this.size = size;
this.snakes = new HashMap<>();
this.ladders = new HashMap<>();
}
public void addSnake(int head, int tail) {
snakes.put(head, tail);
}
public void addLadder(int start, int end) {
ladders.put(start, end);
}
public int getNewPosition(int currentPosition) {
while (snakes.containsKey(currentPosition) || ladders.containsKey(currentPosition)) {
if (snakes.containsKey(currentPosition)) {
currentPosition = snakes.get(currentPosition);
} else if (ladders.containsKey(currentPosition)) {
currentPosition = ladders.get(currentPosition);
}
}
return currentPosition;
}
public int getSize() {
return size;
}
}