-
Notifications
You must be signed in to change notification settings - Fork 514
Expand file tree
/
Copy pathGame.java
More file actions
49 lines (36 loc) · 1014 Bytes
/
Copy pathGame.java
File metadata and controls
49 lines (36 loc) · 1014 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
39
40
41
42
43
44
45
46
47
48
49
package practice.snakes.and.ladders;
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class Game {
List<Player> players;
Board board;
boolean gameCompleted;
public Game() {
players = new ArrayList<>();
board = new Board();
gameCompleted = false;
}
public void addPlayer(String name) {
players.add(new Player(name, 0));
}
public void startGame() {
board.addSnakesAndLadders();
while (!gameCompleted) {
for (Player player: players) {
int diceRoll = (int)(Math.random() * 6) + 1;
int initialPosition = player.getPosition();
player.moveToPosition(board, diceRoll);
System.out.println(player.getName() +" rolled a "+diceRoll+" and moved from "+initialPosition+" to "+player.getPosition());
if (player.getPosition() == Board.BOARD_SIZE) {
System.out.println(player.getName()+" wins the game");
gameCompleted = true;
break;
}
}
}
}
}