-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
95 lines (86 loc) · 3.26 KB
/
Game.java
File metadata and controls
95 lines (86 loc) · 3.26 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
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
import java.util.ArrayList;
import java.util.ArrayDeque;
import java.util.List;
import java.util.Optional;
import java.util.Queue;
public class Game {
private final Queue<Player> turnOrder = new ArrayDeque<>();
private final Board board;
private final Dice dice;
private final StartStrategy startStrategy;
private final EndStrategy endStrategy;
private final boolean requireExactToFinish = true;
public Game(List<Player> players, List<Entity> entities, int boardSize, int diceSides,
StartStrategy startStrategy, EndStrategy endStrategy) {
players.forEach(turnOrder::offer);
this.board = Board.getInstance(boardSize, entities);
this.dice = Dice.getInstance(diceSides);
this.startStrategy = startStrategy;
this.endStrategy = endStrategy;
}
public void play() {
// this will basically give all the entities the starting position = 0
startStrategy.apply(turnOrder);
// this will just get the last cell
int last = board.getLastCell();
while (true) {
// this is just turn wise playing
Player p = turnOrder.poll();
int roll = dice.roll();
// sets the new position according to new dice value
p.moveForward(roll, last, requireExactToFinish);
// this checks if there is any snake or ladder at the new position and changes position accordingly
applyEntities(p);
// for now the end strategy is just first player to reach end wins
System.out.println(p.getName() + " rolled " + roll + " -> " + p.getPosition());
if (endStrategy.checkWinCondition(p, last)) {
System.out.println(p.getName() + " wins!");
break;
}
turnOrder.offer(p);
}
}
private void applyEntities(Player player) {
// this itration and maxItration is nothing, jist to avoid infinite loops
int itration = 0;
int maxItration = 20;
while (itration < maxItration) {
Optional<Entity> entitiesOpt = board.getEntityAt(player.getPosition());
if (entitiesOpt.isEmpty())
break;
Entity entity = entitiesOpt.get();
int from = player.getPosition();
int to = entity.apply(from);
if (to == from)
break;
player.setPosition(to);
itration++;
}
}
public static void main(String[] args) {
List<Player> players = new ArrayList<>();
players.add(new Human("Riya"));
players.add(new Bot("Bot1"));
players.add(new Bot("Bot2"));
players.add(new Human("Rushikesh"));
List<Entity> entities = new ArrayList<>();
Snake s1 = new Snake();
s1.start = 14;
s1.end = 7;
entities.add(s1);
Snake s2 = new Snake();
s2.start = 98;
s2.end = 79;
entities.add(s2);
Ladder l1 = new Ladder();
l1.start = 3;
l1.end = 22;
entities.add(l1);
Ladder l2 = new Ladder();
l2.start = 5;
l2.end = 8;
entities.add(l2);
Game game = new Game(players, entities, 10, 6, new DefaultStartStrategy(), new DefaultEndStrategy());
game.play();
}
}