-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathGame.java
115 lines (90 loc) · 2.45 KB
/
Game.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
package snakeAndLadder;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import snakeAndLadder.entities.Board;
import snakeAndLadder.entities.Player;
import snakeAndLadder.services.DiceService;
public class Game {
Board board;
Queue<Player> players;
Queue<Player> winners;
int diceCount;
int dimention;
int size;
public Game(int dimention, int diceCount) {
this.size = dimention*dimention;
this.dimention = dimention;
this.diceCount = diceCount;
board = new Board(dimention);
players = new LinkedList<Player>();
winners = new LinkedList<Player>();
}
public void launch() {
this.board.print();
while (players.size() > 1) {
Player currPlayer = players.poll();
System.out.println();
System.out.println(currPlayer.getUserName()+"'s turn.");
System.out.println("Press 'r' to roll the dice.");
Scanner sc = new Scanner(System.in);
char c = sc.next().charAt(0);
makeMove(currPlayer);
if (currPlayer.getPosition() == size) {
System.out.println(currPlayer.getUserName() + " won!!!");
winners.add(currPlayer);
} else {
players.add(currPlayer);
}
printPositions();
}
}
private void printPositions() {
for(Player player : players) {
System.out.println(player.getUserName()+":"+player.getPosition());
}
}
private void makeMove(Player player) {
int currPosition = player.getPosition();
int move = DiceService.roll(this.diceCount);
//if move is 6 then you get another chance
//What to do in case of multiple dices?
System.out.println("You got: "+move);
int finalPos = currPosition + move ;
if(finalPos <= size) {
if(board.hasSnakeOrLadder(finalPos)) {
System.out.println(board.getEntity(finalPos).getEncounterMessage());
finalPos = board.getEntity(finalPos).getEnd();
}
System.out.println("Taking you to "+finalPos);
}else {
System.out.println("Try again in the next turn.");
finalPos = currPosition;
}
player.setPosition(finalPos);
}
public Queue<Player> getPlayers() {
return players;
}
public void addPlayer(Player player) {
this.players.add(player);
}
public Queue<Player> getWinners() {
return winners;
}
public void setWinners(Queue<Player> winners) {
this.winners = winners;
}
public int getDiceCount() {
return diceCount;
}
public void setDiceCount(int diceCount) {
this.diceCount = diceCount;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
}