-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathGame.php
More file actions
175 lines (137 loc) · 3.73 KB
/
Game.php
File metadata and controls
175 lines (137 loc) · 3.73 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace trivia;
function echoln($string)
{
echo $string . "\n";
}
class Game
{
var $players;
var $places;
var $board;
var $purses;
var $inPenaltyBox;
var $popQuestions;
var $scienceQuestions;
var $sportsQuestions;
var $rockQuestions;
var $currentPlayer = 0;
var $isGettingOutOfPenaltyBox;
function __construct()
{
$this->board = new GameBoard();
$this->players = array();
$this->places = array(0);
$this->purses = array(0);
$this->inPenaltyBox = array(0);
}
function createRockQuestion($index)
{
return "Rock Question " . $index;
}
function isPlayable()
{
return ($this->getPlayerCount() >= 2);
}
function addPlayer($playerName)
{
array_push($this->players, $playerName);
$this->places[$this->getPlayerCount()] = 0;
$this->purses[$this->getPlayerCount()] = 0;
$this->inPenaltyBox[$this->getPlayerCount()] = false;
echoln($playerName . " was added");
echoln("They are player number " . count($this->players));
return true;
}
function getPlayerCount()
{
return count($this->players);
}
function rollDice($roll)
{
echoln($this->players[$this->currentPlayer] . " is the current player");
echoln("They have rolled a " . $roll);
if ($this->inPenaltyBox[$this->currentPlayer]) {
$this->isRollOddAndDoesOtherThings($roll);
} else {
$this->movePlaces($roll);
}
}
function wasCorrectlyAnswered()
{
if ($this->inPenaltyBox[$this->currentPlayer]) {
if ($this->isGettingOutOfPenaltyBox) {
echoln("Answer was correct!!!!");
$this->getAnswerOutcome('correct');
$winner = $this->didPlayerWin();
$this->currentPlayer++;
if ($this->currentPlayer == count($this->players)) $this->currentPlayer = 0;
return $winner;
} else {
$this->currentPlayer++;
if ($this->currentPlayer == count($this->players)) $this->currentPlayer = 0;
return true;
}
} else {
echoln("Answer was corrent!!!!");
$this->getAnswerOutcome('correct');
$winner = $this->didPlayerWin();
$this->nextPlayer();
return $winner;
}
}
function wasNotCorrectlyAnswered()
{
$this->getAnswerOutcome('incorrect');
$this->inPenaltyBox[$this->currentPlayer] = true;
$this->nextPlayer();
return true;
}
function didPlayerWin()
{
return !($this->purses[$this->currentPlayer] == 6);
}
function nextPlayer()
{
$this->currentPlayer++;
if ($this->currentPlayer == count($this->players)) $this->currentPlayer = 0;
}
function getAnswerOutcome($answer)
{
if ($answer === "correct") {
$this->purses[$this->currentPlayer]++;
echoln($this->players[$this->currentPlayer]
. " now has "
. $this->purses[$this->currentPlayer]
. " Gold Coins.");
} else {
echoln("Question was incorrectly answered");
echoln($this->players[$this->currentPlayer] . " was sent to the penalty box");
}
}
function movePlaces($places)
{
$this->places[$this->currentPlayer] = $this->currentPlayerPlace() + $places;
if ($this->places[$this->currentPlayer] > 11) $this->places[$this->currentPlayer] = $this->currentPlayerPlace() - 12;
echoln($this->players[$this->currentPlayer]
. "'s new location is "
. $this->currentPlayerPlace());
echoln("The category is " . $this->board->getcurrentCategory($this->currentPlayerPlace()));
$this->board->askQuestion($this->currentPlayerPlace());
}
function currentPlayerPlace()
{
return $this->places[$this->currentPlayer];
}
function isRollOddAndDoesOtherThings($roll)
{
if ($roll % 2 != 0) {
$this->isGettingOutOfPenaltyBox = true;
echoln($this->players[$this->currentPlayer] . " is getting out of the penalty box");
$this->movePlaces($roll);
} else {
echoln($this->players[$this->currentPlayer] . " is not getting out of the penalty box");
$this->isGettingOutOfPenaltyBox = false;
}
}
}