-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
152 lines (126 loc) · 4.36 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
exports.Game = function () {
const plyrs = new Array();
const places = new Array(6);
const purses = new Array(6);
var inPenaltyBox = new Array(6);
const popQuestions = new Array();
const scienceQuestions = new Array();
const sportsQuestions = new Array();
const rockQuestions = new Array();
var curPlayer = 0;
let isGettingOutOfPenaltyBox = false;
var didPlayerWin = function () {
return !(purses[curPlayer] == 6);
};
var currentCategory = function () {
if (places[curPlayer] == 0) return "Pop";
if (places[curPlayer] == 4) return "Pop";
if (places[curPlayer] == 8) return "Pop";
if (places[curPlayer] == 1) return "Science";
if (places[curPlayer] == 5) return "Science";
if (places[curPlayer] == 9) return "Science";
if (places[curPlayer] == 2) return "Sports";
if (places[curPlayer] == 6) return "Sports";
if (places[curPlayer] == 10) return "Sports";
return "Rock";
};
this.createRockQuestion = function (idx) {
return "Rock Question " + idx;
};
for (var i = 0; i < 50; i++) {
popQuestions.push("Pop Question " + i);
scienceQuestions.push("Science Question " + i);
sportsQuestions.push("Sports Question " + i);
rockQuestions.push(this.createRockQuestion(i));
}
this.isPlayable = function (howManyPlayers) {
return howManyPlayers >= 2;
};
this.add = function (playerName) {
plyrs.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 " + plyrs.length);
return true;
};
this.howManyPlayers = function () {
return plyrs.length;
};
var askQuestion = function () {
if (currentCategory() == "Pop") console.log(popQuestions.shift());
if (currentCategory() == "Science") console.log(scienceQuestions.shift());
if (currentCategory() == "Sports") console.log(sportsQuestions.shift());
if (currentCategory() == "Rock") console.log(rockQuestions.shift());
};
this.roll = function (roll) {
console.log(plyrs[curPlayer] + " is the current player");
console.log("They have rolled a " + roll);
if (inPenaltyBox[curPlayer]) {
if (roll % 2 != 0) {
isGettingOutOfPenaltyBox = true;
console.log(plyrs[curPlayer] + " is getting out of the penalty box");
places[curPlayer] = places[curPlayer] + roll;
if (places[curPlayer] > 11) {
places[curPlayer] = places[curPlayer] - 12;
}
console.log(
plyrs[curPlayer] + "'s new location is " + places[curPlayer]
);
console.log("The category is " + currentCategory());
askQuestion();
} else {
console.log(
plyrs[curPlayer] + " is not getting out of the penalty box"
);
isGettingOutOfPenaltyBox = false;
}
} else {
places[curPlayer] = places[curPlayer] + roll;
if (places[curPlayer] > 11) {
places[curPlayer] = places[curPlayer] - 12;
}
console.log(plyrs[curPlayer] + "'s new location is " + places[curPlayer]);
console.log("The category is " + currentCategory());
askQuestion();
}
};
this.wasCorrectlyAnswered = function () {
if (inPenaltyBox[curPlayer]) {
if (isGettingOutOfPenaltyBox) {
console.log("Answer was correct!!!!");
purses[curPlayer] += 1;
console.log(
plyrs[curPlayer] + " now has " + purses[curPlayer] + " Gold Coins."
);
var winner = didPlayerWin();
curPlayer += 1;
if (curPlayer == plyrs.length) curPlayer = 0;
return winner;
} else {
curPlayer += 1;
if (curPlayer == plyrs.length) curPlayer = 0;
return true;
}
} else {
console.log("Answer was correct!!!!");
purses[curPlayer] += 1;
console.log(
plyrs[curPlayer] + " now has " + purses[curPlayer] + " Gold Coins."
);
var winner = didPlayerWin();
curPlayer += 1;
if (curPlayer == plyrs.length) curPlayer = 0;
return winner;
}
};
this.wrongAnswer = function () {
console.log("Question was incorrectly answered");
console.log(plyrs[curPlayer] + " was sent to the penalty box");
inPenaltyBox[curPlayer] = true;
curPlayer += 1;
if (curPlayer == plyrs.length) curPlayer = 0;
return true;
};
};