-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathGameBetter.java
More file actions
90 lines (73 loc) · 2.63 KB
/
GameBetter.java
File metadata and controls
90 lines (73 loc) · 2.63 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
package victor.training.trivia;
import java.util.ArrayList;
import java.util.List;
public class GameBetter implements IGame {
public static final int BOARD_SIZE = 12;
private final QuestionRepository questionRepository = new QuestionRepository();
private List<Player> players = new ArrayList<>();
private int[] places = new int[6];
private int[] purses = new int[6];
private boolean[] inPenaltyBox = new boolean[6];
private int currentPlayer = 0;
public void addPlayer(String playerName) {
players.add(new Player(playerName));
System.out.println(playerName + " was added");
System.out.println("They are player number " + players.size());
}
public void roll(int roll) {
System.out.println(currentPlayer().getName() + " is the current player");
System.out.println("They have rolled a " + roll);
if (currentPlayer().isInPenaltyBox()) {
if (roll % 2 == 0) {
System.out.println(currentPlayer().getName() + " is not getting out of the penalty box");
return;
}
currentPlayer().free();
System.out.println(currentPlayer().getName() + " is getting out of the penalty box");
}
currentPlayer().advance(roll);
System.out.println(currentPlayer().getName() + "'s new location is " + currentPlayer().getPlace());
System.out.println("The category is " + questionRepository.getCurrentCategory(currentPlayer().getPlace()).getLabel());
askQuestion();
}
public void correctAnswer() {
if (isGameOver()) {
return;
}
if (currentPlayer().isInPenaltyBox()) {
nextPlayer();
} else {
System.out.println("Answer was correct!!!!");
currentPlayer().reward();
System.out.println(currentPlayer().getName() + " now has " + currentPlayer().getPurse() + " Gold Coins.");
if (!isGameOver()) {
nextPlayer();
}
}
}
public void wrongAnswer() {
if (isGameOver()) {
return;
}
System.out.println("Question was incorrectly answered");
System.out.println(currentPlayer().getName() + " was sent to the penalty box");
currentPlayer().punish();
nextPlayer();
}
@Override
public boolean isGameOver() {
return currentPlayer().isWinner();
}
private Player currentPlayer() {
return players.get(currentPlayer);
}
private void askQuestion() {
System.out.println(questionRepository.nextQuestion(currentPlayer().getPlace()));
}
private void nextPlayer() {
currentPlayer++;
if (currentPlayer == players.size()) {
currentPlayer = 0;
}
}
}