-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.js
287 lines (257 loc) · 7.52 KB
/
main.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
window.onload = function() {
//init all global variables
var playerTotal = 0;
var computerTotal = 0;
var roundNumber = 1;
var slaveCard = document.getElementById('slave');
var citizenCards = document.getElementsByClassName('citizen');
var emperorCard = document.getElementById('emperor');
var play = document.getElementById('play');
var gameWrapper = document.getElementById('gameWrapper');
var endGameText = document.getElementById('endGameText');
var gameText = document.getElementById('gameText');
var endStats = document.getElementById('endStats');
var rules = document.getElementById('rules');
var intro = document.getElementById('introScreen');
var zawa = document.createElement('audio');
zawa.src = 'sound/zawa.mp3';
var draw = document.createElement('audio');
draw.src = 'sound/draw.wav';
var shuffle = document.createElement('audio');
shuffle.src = 'sound/shuffle.wav';
var cheer = document.createElement('audio');
cheer.src = 'sound/cheer.mp3';
var lose = document.createElement('audio');
lose.src = 'sound/pain.mp3';
var pick1;
var compDeck;
var count = 0;
var compNum = 0;
var hide = false;
//decides whether to show the emperor or slave card
var slaveOrEmperor = function() {
if (slave == true) {
slaveCard.style.display = 'inline';
emperorCard.style.display = 'none';
}
else {
slaveCard.style.display = 'none';
emperorCard.style.display = 'inline';
}
}
//changes value of slave based on round, 1-3 & 7-9 emperor, 4-6 & 10-12 slave
var whichRound = function() {
if ((roundNumber < 4)) {
slave = false;
return slave;
}
else if ((roundNumber > 6) && (10 > roundNumber)) {
slave = false;
return slave;
}
else if (roundNumber > 12) {
gameEnd();
}
else {
slave = true;
return slave;
}
}
//wow this function is out of control trash right now
//decides who wins based on which cards are picked, removes citizen cards played, resets hand with makeHand() on w/l
var whoWins = function() {
if (pick1 == undefined) {
gameText.innerHTML = 'Pick a card by clicking it!';
compNum--;
}
if (pick1 == 'citizen') {
citizenCards[count].style.display = 'none';
count++;
console.log(count);
}
draw.play();
pick2 = compDeck[compNum];
compNum++;
var winner;
switch (pick1) {
case 'emperor':
if (pick2 == 'slave') {
gameText.innerHTML ='computer plays ' + pick2 + ' computer wins this round...';
computerTotal = computerTotal + 3;
zawa.play();
endStats.innerHTML = ' You: ' + playerTotal + ' | ' + 'Enemy: ' + computerTotal;
roundNumber++;
makeHand();
break;
}
gameText.innerHTML = 'computer plays ' + pick2 + ', you win this round!';
playerTotal++;
endStats.innerHTML = 'You: ' + playerTotal + ' | ' + 'Enemy: ' + computerTotal;
roundNumber++;
makeHand();
break;
case 'slave':
if (pick2 == 'emperor') {
gameText.innerHTML = 'computer plays ' + pick2 + ', you win this round!';
playerTotal = playerTotal + 3;
roundNumber++;
endStats.innerHTML = 'You: ' + playerTotal + ' | ' + 'Enemy: ' + computerTotal;
makeHand();
break;
}
gameText.innerHTML = 'computer plays ' + pick2 + ', computer wins this round...';
computerTotal++;
roundNumber++;
zawa.play();
endStats.innerHTML = 'You: ' + playerTotal + ' | ' + 'Enemy: ' + computerTotal;
makeHand();
break;
case 'citizen':
if (pick2 == 'emperor') {
gameText.innerHTML = 'computer plays ' + pick2 + ', computer wins this round...';
computerTotal++;
roundNumber++;
zawa.play();
endStats.innerHTML = 'You: ' + playerTotal + ' | ' + 'Enemy: ' + computerTotal;
makeHand();
break;
}
else if (pick2 == 'slave') {
gameText.innerHTML = 'computer plays ' + pick2 + ', you win this round!';
roundNumber++;
playerTotal++;
endStats.innerHTML = 'You: ' + playerTotal + ' | ' + 'Enemy: ' + computerTotal;
makeHand();
break;
}
gameText.innerHTML = 'computer plays ' + pick2 + ', tie, play another card...';
zawa.play();
break;
}
pick1 = null;
resetStyles();
return;
}
//event listeners for play button, and picking the cards
play.addEventListener('click', function() {
console.log(whoWins());
})
slaveCard.addEventListener('click', function() {
pick1 = 'slave';
this.style.border = '3px solid orange';
this.style.width = '175px';
emperorCard.style.border = 'none';
emperorCard.style.width = '150px';
for (var i = 0; i < citizenCards.length; i++) {
citizenCards[i].style.border = 'none';
citizenCards[i].style.width = '150px';
}
})
emperorCard.addEventListener('click', function() {
pick1 = 'emperor';
this.style.border = '3px solid orange';
this.style.width = '175px';
slaveCard.style.border = 'none';
slaveCard.style.width = '150px';
for (var i = 0; i < citizenCards.length; i++) {
citizenCards[i].style.border = 'none';
citizenCards[i].style.width = '150px';
}
})
for (var i = 0; i < citizenCards.length; i++) {
citizenCards[i].addEventListener('click', function() {
slaveCard.style.border = 'none';
slaveCard.style.width = '150px';
emperorCard.style.border = 'none';
emperorCard.style.width = '150px';
for (var i = 0; i < citizenCards.length; i++) {
citizenCards[i].style.border = 'none';
citizenCards[i].style.width = '150px';
}
pick1 = 'citizen';
this.style.border = '3px solid orange';
this.style.width = '175px';
})
}
rules.addEventListener('click', function() {
if (hide == true) {
intro.style.display = 'block';
rules.innerHTML = 'Hide';
hide = false;
}
else {
intro.style.display = 'none';
rules.innerHTML = 'Show';
hide = true;
}
})
//resets the style of the cards after they are played
var resetStyles = function() {
slaveCard.style.border = 'none';
slaveCard.style.width = '150px';
emperorCard.style.border = 'none';
emperorCard.style.width = '150px';
for (var i = 0; i < citizenCards.length; i++) {
citizenCards[i].style.border = 'none';
citizenCards[i].style.width = '150px';
}
}
//makes the computers deck depending on if they are the emp or slave and then shuffles the array;
var computerDeck = function() {
compDeck = new Array(5);
if (slave == true) {
compDeck = ['emperor', 'citizen', 'citizen', 'citizen', 'citizen'];
compDeck.sort(function() { return 0.5 - Math.random() });
return compDeck;
}
else {
compDeck = ['slave', 'citizen', 'citizen', 'citizen', 'citizen'];
compDeck.sort(function() { return 0.5 - Math.random() });
return compDeck;
}
}
//makes hand with 4 citizens and either slave or emperor depending on the boolean value of slave
var makeHand = function() {
shuffle.play();
whichRound();
slaveOrEmperor();
computerDeck();
count = 0;
compNum = 0;
if (slave == true) {
slaveCard.style.display = 'inline';
for (var i = 0; i < citizenCards.length; i++) {
citizenCards[i].style.display = 'inline';
}
}
else {
emperorCard.style.display = 'inline';
for (var i = 0; i < citizenCards.length; i++) {
citizenCards[i].style.display = 'inline';
}
}
return;
}
//displays end of game screen and removes game
var gameEnd = function() {
endGameText.style.display = 'inline';
gameWrapper.style.display = 'none';
if (playerTotal < computerTotal) {
endGameText.innerHTML = 'The enemy wins with a score of ' + computerTotal + '...';
lose.play();
}
else if (playerTotal == computerTotal) {
endGameText.innerHTML = 'you tie with ' + playerTotal + '...';
zawa.play();
}
else {
endGameText.innerHTML = 'You win with a score of ' + playerTotal + '!';
cheer.play();
}
}
//first round init
endStats.innerHTML = ' You: ' + playerTotal + ' | ' + 'Enemy: ' + computerTotal;
slaveOrEmperor();
whichRound();
makeHand();
};