-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
203 lines (163 loc) · 5.54 KB
/
game.js
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
exports = typeof window !== "undefined" && window !== null ? window : global;
exports.Game = function() {
var players = [];
var places = [];
var purses = [];
var inPenaltyBox = [];
var popQuestions = [];
var scienceQuestions = [];
var sportsQuestions = [];
var rockQuestions = [];
var currentPlayer = 0;
var isGettingOutOfPenaltyBox = false;
var didPlayerWin = function() {
return (purses[currentPlayer] !== 6);
};
var currentCategory = function() {
var popPos = [0, 4, 8];
var sciencePos = [1, 5, 9];
var sportsPos = [2, 6, 10];
var PlayerPos = parseInt(places[currentPlayer]);
if (popPos.indexOf(PlayerPos) > -1) { return "Pop"; }
if (sciencePos.indexOf(PlayerPos) > -1) { return "Science"; }
if (sportsPos.indexOf(PlayerPos) > -1) { return "Sports"; }
return "Rock";
};
// This will enable us to add question whenever the question bank is empty
this.createQuestion = function(index, category) {
return category.trim() + " Question " + index;
};
/*
Keeping this function even if it's a duplicate because it existed as an
interface from the begging
So may be there is another client using it.
*/
this.createRockQuestion = function(index) {
return this.createQuestion(index, "Rock");
};
for (var i = 0; i < 50; i++) {
popQuestions.push(this.createQuestion(i, "Pop"));
scienceQuestions.push(this.createQuestion(i, "Science"));
sportsQuestions.push(this.createQuestion(i, "Sports"));
rockQuestions.push(this.createQuestion(i, "Rock"));
}
this.isPlayable = function(howManyPlayers) {
if (!howManyPlayers) {
return this.howManyPlayers >= 2;
}
return howManyPlayers >= 2;
};
this.add = function(playerName) {
players.push(playerName);
places[this.howManyPlayers() - 1] = 0;
purses[this.howManyPlayers() - 1] = 0;
inPenaltyBox[this.howManyPlayers() - 1] = false;
console.log(playerName + " was added");
console.log("They are player number " + players.length);
return true;
};
this.howManyPlayers = function() {
return players.length;
};
var askQuestion = function() {
var category = currentCategory();
switch (category) {
case "Pop":
if (popQuestions.length === 0) {
this.createQuestion(0, "Pop");
}
console.log(popQuestions.shift());
break;
case "Science":
if (scienceQuestions.length === 0) {
this.createQuestion(0, "Science");
}
console.log(scienceQuestions.shift());
break;
case "Sports":
if (sportsQuestions.length === 0) {
this.createQuestion(0, "Sports");
}
console.log(sportsQuestions.shift());
break;
case "Rock":
if (rockQuestions.length === 0) {
this.createQuestion(0, "Rock");
}
console.log(rockQuestions.shift());
break;
default:
console.error("Could not find a question for the current category");
}
};
this.roll = function(roll) {
console.log(players[currentPlayer] + " is the current player");
console.log("They have rolled a " + roll);
if (inPenaltyBox[currentPlayer]) {
if (roll % 2 !== 0) {
isGettingOutOfPenaltyBox = true;
console.log(players[currentPlayer] + " is getting out of the penalty box");
places[currentPlayer] = places[currentPlayer] + roll;
if (places[currentPlayer] > 11) {
places[currentPlayer] = places[currentPlayer] - 12;
}
console.log(players[currentPlayer] + "'s new location is " + places[currentPlayer]);
console.log("The category is " + currentCategory());
askQuestion();
} else {
console.log(players[currentPlayer] + " is not getting out of the penalty box");
isGettingOutOfPenaltyBox = false;
}
} else {
places[currentPlayer] = places[currentPlayer] + roll;
if (places[currentPlayer] > 11) {
places[currentPlayer] = places[currentPlayer] - 12;
}
console.log(players[currentPlayer] + "'s new location is " + places[currentPlayer]);
console.log("The category is " + currentCategory());
askQuestion();
}
};
var correctAnswer = function() {
console.log("Answer was correct!!!!");
purses[currentPlayer] += 1;
console.log(players[currentPlayer] + " now has " + purses[currentPlayer] + " Gold Coins.");
var winner = didPlayerWin();
currentPlayer += 1;
if (currentPlayer === players.length) { currentPlayer = 0; }
return winner;
};
this.wasCorrectlyAnswered = function() {
if (inPenaltyBox[currentPlayer] && !isGettingOutOfPenaltyBox) {
currentPlayer += 1;
if (currentPlayer === players.length) { currentPlayer = 0; }
return true;
} else {
return correctAnswer();
}
};
this.wrongAnswer = function() {
console.log("Question was incorrectly answered");
console.log(players[currentPlayer] + " was sent to the penalty box");
inPenaltyBox[currentPlayer] = true;
currentPlayer += 1;
if (currentPlayer === players.length) { currentPlayer = 0; }
return true;
};
this.play = function(playersCount, diceRoll, randomNo) {
var notAWinner = false;
// Add new players
for (var i = 0; i < playersCount; i += 1) {
var playerName = "player_" + i;
this.add(playerName);
}
do {
this.roll(diceRoll);
if (Math.floor(randomNo * 10) === 7) {
notAWinner = this.wrongAnswer();
} else {
notAWinner = this.wasCorrectlyAnswered();
}
} while (notAWinner);
};
};