-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgLogic.js
344 lines (231 loc) · 8.49 KB
/
gLogic.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// global variables
let startofgame = true; //tells whether players have initialized their positions or not.
let playcount = 0; //helps in player initialization. never used again.
let maxplayers = 2; // stores the number of players playing.
let turn = -1; // tracks whose turn it is to play.
let invalidmove = false; // for status printing?.
let shiftdone = false; // for not allowing multiple bomb shifts.
let lastplayerfallen = false; // for delaying shifting of bombs when last player steps into a bomb field.
var count = 0;
// various arrays used to store data .
var players = []; //stores player objects.
var colourarray = ["url(playerred.svg)","url(playerblue.svg)","url(playergreen.svg)","url(playeryellow.svg)"]; //stores player icons.
var deatharray = []; //stores the field block ids which cause death (bomb present).
var playernames = []; // stores player names entered at the start.
//for tranistion from number of players to enter player names.
function toSecond(s)
{
document.getElementById("pre").style.display = 'none';
document.getElementById("mid").style.display = 'flex';
maxplayers = parseInt(s);
var currentplayers = document.getElementById("mid").getElementsByClassName("flexentry");
for(let i=0 ; i<maxplayers ; i++)
{
currentplayers[i].style.display = "flex";
}
}
//for transition from enter player names to actual game.
function startgame()
{
for(let i=26; i<26+maxplayers ; i++)
{
playernames[i-26] = document.getElementById(i.toString()).value;
}
document.getElementById("mid").style.display = 'none';
document.getElementById("fin").style.display = 'flex';
}
// this function tells whether a field is currently occupied by another player or not.
function occupied(x)
{
var occup = false;
if(startofgame)
for(let i=0 ; i<playcount ; i++)
{
if(players[i].currentbox == x)
occup = true;
}
else
{
for(let i=0 ; i<maxplayers ; i++)
{
if(players[i].currentbox == x)
occup = true;
}
}
return occup;
}
// class for the player objects. very useful
class Player {
constructor(pname , pnumber , currentbox , playercolour)
{
this.pname = pname;
this.pnumber = pnumber;
this.currentbox = currentbox;
this.playercolour = playercolour;
}
// this class method tells whether a player can make a certain move or not.
validateMove(m) {
if(( Math.abs(m - this.currentbox) == 1 || Math.abs(m-this.currentbox) == 5 ) && !occupied(m))
return true;
else
return false;
}
}
// shifts the location of the bombs to one field below. (UNDER IMPLEMENTATION)
function shiftBombs()
{
//this loop changes the bomb icon position and death array numbers
for(let i=0 ; i<deatharray.length ; i++)
{
document.getElementById(deatharray[i]).getElementsByClassName("bombs")[0].style.display = "none";
if(deatharray[i]!=20)
deatharray[i] = (deatharray[i] + 5)%25;
else
{
deatharray[i] = 25;
}
document.getElementById(deatharray[i]).getElementsByClassName("bombs")[0].style.display = "block";
}
//checks whether any falling bomb has caused any death
for(let i=0 ; i<maxplayers ; i++)
{
if(deatharray.includes(players[i].currentbox))
{
setTimeout(() => {
if(maxplayers!=1) {
let deadplayer = players[i].pname;
document.getElementById(players[i].currentbox).style.backgroundImage = "none";
alert(players[i].pname+" died. trash gameplay.");
maxplayers--;
players.splice(i,1);
playernames.splice(i,1);
colourarray.splice(i,1);
turn = (turn-1)%maxplayers;
}
if(maxplayers==1){
setTimeout(() => { let winplayer = players[0].pname;
if(confirm(winplayer+" has won the game. The game has ended. Press Ok to restart"))
{
location.reload();
return;
}}, 300);
}
}, 300);
}
}
}
// main functionality of the game
function clicked(x)
{
// position initialization
if(startofgame==true)
{
if(playcount == 0 || !occupied(x)) {
document.getElementById("stat").value = playernames[playcount]+" has chosen their starting field.";
players[playcount] = new Player(playernames[playcount],playcount+1,0,colourarray[playcount],false);
players[playcount].currentbox = x;
document.getElementById(x).style.backgroundImage = colourarray[playcount];
const animate1=[
{backgroundSize:'0px'},
{backgrounSize:'50px'}
];
const animate2={
duration:250,
iteration:1
}
document.getElementById(x).animate(animate1,animate2);
playcount+=1;
if(playcount == maxplayers)
startofgame = false;
}
else
{
document.getElementById("stat").value = "Chosen field is already occupied.";
}
}
// normal gameplay mechanics
else
{
invalidmove = false;
shiftdone = false;
lastplayerfallen = false;
turn = (turn+1)%maxplayers;
//bomb placement
if(players[turn].currentbox == x && !deatharray.includes(x))
{
if(turn!= maxplayers-1)
{
document.getElementById(x).getElementsByClassName("bombs")[0].style.display = "block";
deatharray.push(x);
}
else
{
shiftBombs();
document.getElementById(x).getElementsByClassName("bombs")[0].style.display = "block";
deatharray.push(x);
shiftdone = true;
}
}
//moves player
else if(players[turn].validateMove(x))
{
document.getElementById(players[turn].currentbox).style.backgroundImage = "none";
document.getElementById(x).style.backgroundImage = colourarray[turn];
const animate1=[
{backgroundSize:'0px'},
{backgrounSize:'50px'}
];
const animate2={
duration:250,
iteration:1
}
document.getElementById(x).animate(animate1,animate2);
players[turn].currentbox = x;
//if moved into a bomb placed field
if(deatharray.includes(x))
{
if(turn == maxplayers-1)
lastplayerfallen = true;
setTimeout(() => {
if(maxplayers!=1) {
let deadplayer = players[turn].pname;
document.getElementById(x).style.backgroundImage = "none";
alert(players[turn].pname+" died. trash gameplay.");
maxplayers--;
players.splice(turn,1);
playernames.splice(turn,1);
colourarray.splice(turn,1);
turn = (turn-1)%maxplayers;
}
if(maxplayers==1){
setTimeout(() => { let winplayer = players[0].pname;
if(confirm(winplayer+" has won the game. The game has ended. Press Ok to restart"))
{
location.reload();
return;
}}, 300);
}
}, 300);
}
document.getElementById("stat").value = playernames[turn]+" moved";
if(turn==maxplayers-1 && !shiftdone)
{
if(!lastplayerfallen)
shiftBombs();
else
{
setTimeout(() => { shiftBombs(); }, 300);
}
}
}
//balancing an invalid move
else
{ invalidmove = true;
document.getElementById("stat").value = "invalid move by "+playernames[turn];
turn = turn+maxplayers-1;
}
if(!invalidmove)
document.getElementById("stat").value = playernames[(turn+1)%maxplayers]+"'s turn";
}
}
// setTimeout(() => { }, 300);