forked from learning-zone/java-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameBoard.java
More file actions
66 lines (60 loc) · 2.48 KB
/
GameBoard.java
File metadata and controls
66 lines (60 loc) · 2.48 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
package SnakeNLadder;
import java.util.List;
import java.util.Map;
import java.util.Queue;
class GameBoard {
private Dice dice;
private Queue<Player> nextTurn;
private List<Jumper> snakes;
private List<Jumper> ladders;
private Map<String,Integer> playersCurrentPosition;
int boardSize;
GameBoard(Dice dice, Queue<Player> nextTurn, List<Jumper> snakes, List<Jumper> ladders, Map<String,Integer> playersCurrentPosition, int boardSize) {
this.dice = dice;
this.nextTurn = nextTurn;
this.snakes = snakes;
this.ladders = ladders;
this.playersCurrentPosition = playersCurrentPosition;
this.boardSize = boardSize;
}
void startGame(){
while(nextTurn.size()>1) {
Player player = nextTurn.poll();
int currentPosition = playersCurrentPosition.get(player.getPlayerName());
int diceValue = dice.rollDice();
int nextCell = currentPosition + diceValue;
if (nextCell > boardSize) nextTurn.offer(player); //
else if (nextCell == boardSize) {
System.out.println(player.getPlayerName() + " won the game");
}
else {
int[] nextPosition = new int[1];
boolean[] b = new boolean[1];
nextPosition[0] = nextCell;
snakes.forEach(v -> {
if (v.startPoint == nextCell) {
nextPosition[0] = v.endPoint;
}
});
if (nextPosition[0] != nextCell)
System.out.println(player.getPlayerName() + " Bitten by Snake present at: " + nextCell);
ladders.forEach(v -> {
if (v.startPoint == nextCell) {
nextPosition[0] = v.endPoint;
b[0] = true;
}
});
if (nextPosition[0] != nextCell && b[0])
System.out.println(player.getPlayerName() + " Got ladder present at: " + nextCell);
if (nextPosition[0] == boardSize) {
System.out.println(player.getPlayerName() + " won the game");
}
else {
playersCurrentPosition.put(player.getPlayerName(), nextPosition[0]);
System.out.println(player.getPlayerName() + " is at position " + nextPosition[0]);
nextTurn.offer(player);
}
}
}
}
}