-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakeAndLadder.java
139 lines (116 loc) · 4.06 KB
/
SnakeAndLadder.java
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package games;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class SnakeAndLadder {
private int playerCount;
private int games;
private boolean newBoardEveryTime;
private int[] gamesWon;
private Board currentBoard;
private Random rand = new Random();
public SnakeAndLadder(int playerCount, int games, boolean newBoardEveryTime) {
this.playerCount = playerCount;
this.games = games;
this.newBoardEveryTime = newBoardEveryTime;
this.gamesWon = new int[playerCount];
for (int i = 0; i < playerCount; i++) {
gamesWon[i] = 0;
}
this.currentBoard = null;
}
private int getRandom(int min, int max) {
return rand.nextInt((max - min) + 1) + min;
}
public int[] execute() {
for (int i = 0; i < games; i++) {
Board board = getBoard();
int won = -1;
;
while (won == -1) {
won = board.run();
}
gamesWon[won]++;
}
return gamesWon;
}
private Board getBoard() {
if (null == currentBoard || newBoardEveryTime) {
currentBoard = new Board(playerCount);
} else {
currentBoard.reset();
}
return currentBoard;
}
public class Board {
private Map<Integer, Integer> snakes = new HashMap<>();
private Map<Integer, Integer> ladders = new HashMap<>();
private Map<Integer, Integer> playerLocationMap = new HashMap<>();
private int snakeCount = 10;
private int ladderCount = 10;
private int playerCount;
private int playerTurn;
public Board(int playerCount) {
this.playerCount = playerCount;
for (int i = 0; i < snakeCount; i++) {
int[] snake = getSnake();
if (!snakes.containsKey(snake[0])) {
snakes.put(snake[0], snake[1]);
}
}
for (int i = 0; i < ladderCount; i++) {
int[] ladder = getLadder();
if (snakes.values().stream().anyMatch(v -> v.equals(ladder[0])) || snakes.containsKey(ladder[1])) {
continue;
}
}
reset();
}
public int run() {
int winner = -1;
if (playerLocationMap.get(playerTurn) != 101) {
int diceCount = getRandom(1, 6);
int newLocation = playerLocationMap.get(playerTurn) + diceCount;
if (newLocation == 101) {
winner = playerTurn;
} else if (newLocation > 101) {
newLocation -= diceCount;
}
while (snakes.containsKey(newLocation)) {
newLocation = snakes.get(newLocation);
}
while (ladders.containsKey(newLocation)) {
newLocation = ladders.get(newLocation);
}
// System.out.println("Player: " + playerTurn +
// ", dice: " + diceCount +
// ", previousLocation: " + playerLocationMap.get(playerTurn) +
// ", newLocation: " + newLocation);
playerLocationMap.put(playerTurn, newLocation);
}
playerTurn = (playerTurn + 1) % playerCount;
return winner;
}
private void reset() {
for (int i = 0; i < playerCount; i++) {
playerLocationMap.put(i, 0);
}
playerTurn = 0;
}
private int[] getSnake() {
int head = getRandom(11, 100);
int tail = getRandom(1, (head - (head % 10)));
int[] result = new int[2];
result[0] = head;
result[1] = tail;
return result;
}
private int[] getLadder() {
int[] snake = getSnake();
int[] result = new int[2];
result[0] = snake[1];
result[1] = snake[0];
return result;
}
}
}