This repository has been archived by the owner on Sep 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBattleship.js
519 lines (445 loc) · 18.5 KB
/
Battleship.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
Players = new Mongo.Collection("Players");
Game = new Mongo.Collection("Game");
Board = new Mongo.Collection("Board");
var numberRows = 20;
var numberColumns = 20;
if (Meteor.isClient) {
Meteor.subscribe("players");
Meteor.subscribe("game");
var boardHandle = Meteor.subscribe("board");
Tracker.autorun(function() {
// Every time the Board dataset is updated, go through here and redraw the board for this player
if(boardHandle.ready()) {
var playerBoardDocument = Board.findOne({forPlayer: Meteor.userId()});
if(playerBoardDocument != null) {
var cells = playerBoardDocument.boardCells;
for(var i = 0; i < numberRows; i++) {
for(var j = 0; j < numberColumns; j++) {
var cellSelectorString = 'td[data-row="' + i + '"][data-col="' + j + '"]';
if(cells[i][j].isShip) { // This cell contains a ship that belongs to this player
$(cellSelectorString).removeClass(); // Clear existing classes
if(cells[i][j].isHit) { // Own ship is hit
$(cellSelectorString).addClass("own-ship-hit-cell");
} else { // Own ship is not hit
$(cellSelectorString).addClass("own-ship-cell");
}
} else if(cells[i][j].isHit) { // Player hit a ship in this cell
$(cellSelectorString).addClass("hit-shot-cell");
} else if(cells[i][j].isMiss) {
$(cellSelectorString).addClass("missed-shot-cell");
}
}
}
} else { // Dataset not available clear board (triggered at end of game, if triggered in middle, data not lost, can be redrawn)
for(var i = 0; i < numberRows; i++) {
for(var j = 0; j < numberColumns; j++) {
$('td[data-row="' + i + '"][data-col="' + j + '"]').removeClass();
}
}
}
}
});
Template.joinGame.helpers({
// Return if the game is started or not
gameBegun: function() {
return Game.findOne({field: "gameStarted"}).value;
}
});
Template.statusBar.helpers({
// Return the status of the game
status: function() {
return Game.findOne({field: "status"}).value;
},
cssClass: function() {
return Game.findOne({field: "status"}).cssClass;
}
});
// Provides values to the players template
Template.players.helpers({
// Return all the players
players: function () {
return Players.find({});
}
});
// Handles events from within the players template
Template.joinGame.events({
'click #join_game': function() {
Meteor.call("addPlayer");
}
});
Template.board.events({
'click td': function(event) {
var isTurn = Players.findOne({ player: Meteor.userId() }).isTurn; // Ascertain it is this player's turn
if(isTurn) {
var clickedRow = parseInt(event.target.attributes["data-row"].value);
var clickedColumn = parseInt(event.target.attributes["data-col"].value);
Meteor.call("fireShot", clickedRow, clickedColumn);
}
}
});
// Dynamically generate the battleship board
Template.board.rendered = function() {
var tableHtml;
for(var i = 0; i < numberRows; i++) {
tableHtml += "<tr>";
for(var j = 0; j < numberColumns; j++) {
tableHtml += '<td data-row="' + i + '" data-col="' + j + '" class="fire-shot">' + i + j + '</td>'
}
tableHtml += "</tr>";
}
$("#game_board").append(tableHtml);
};
Template.board.helpers({
// Return board layout
rows: function () {
return Game.findOne({field: "board"}).numberRows;
}
});
Template.board.helpers({
// Return board layout
columns: function () {
return Game.findOne({field: "board"}).numberColumns;
}
});
// Set up accounts to require a username only, not an email address
Accounts.ui.config({
passwordSignupFields: "USERNAME_ONLY"
});
}
// Server-only code
if (Meteor.isServer) {
Meteor.startup(function () {
// TODO move these to a place where they are called each time a new game is started?
// Clear players on startup
Players.remove({});
// Clear game information
Game.remove({});
Board.remove({});
});
}
Meteor.startup(function() {
Meteor.call("initialize");
});
// Identifier for players, incremented as they are added.
// Tracks with the current number of players because it starts at 0 and is incremented after each one is added.
var playerNumber = 0;
// Flag indicating whether a player has been added in the last time interval, referenced in periodicStartGameCheck
var playerAdded = false;
// Keeps track of the index of the player whose turn it is
var activePlayerNumber;
var shotsFired = false;
var initialized = false;
// Methods called from the client side, but run on the server side for
var turnTimeout; // Variable for the timeout function
Meteor.methods({
// Add a player
addPlayer: function() {
Players.insert({
player: Meteor.userId(),
displayName: Meteor.user().username,
playerNumber: playerNumber,
isTurn: false,
inGame: true,
score: 0
});
playerNumber++;
playerAdded = true;
if(Meteor.isClient) {
$("#join_game").prop("disabled",true);
}
// Build board for this player
Meteor.call("setUpBoard");
// Player added, should we start the game?
// There must be at least two players.
// Once two players are ready to start the game, start a timer to check if anyone joins in a set amount of time since the last player joined.
// Since this block is run every time a player joins, the timer resets after every new player joins.
if (Meteor.isServer) { // Only run this check on the server, not the client-side simulation
if(playerNumber > 1) { // There must be at least two players
playerAdded = false; // Clear the flag that was just set telling that a player was added. It will be set to true again if a player joins in the given interval.
Meteor.setTimeout(function() { // After ten seconds check if another player has been added
if(!playerAdded || playerNumber > 5) { // Player was not added in time interval since last player was added or the max number of players has been reached; start the game.
// Time to start the game
// Give the first turn to the first player who joined
Players.update({playerNumber: 0}, {$set: { isTurn: true }});
activePlayerNumber = 0;
// Commented out because for some reason this timeout isn't getting cancelled.
// As a consequence the first user has however long they like to kick off the game.
/*turnTimeout = Meteor.setTimeout(function() { // Timeout for first turn needs to be set up manually because there's not a preceding turn triggering it
if(!shotsFired) {
console.log("shotsfired " +shotsFired);
advanceToNextPlayer();
}
}, 15000); // 15 seconds max per turn (including for this, the first turn)*/
// Indicate that the game has started
Game.update({field: "gameStarted"}, {$set: { value: true }});
Game.update({field: "status"}, {$set: { value: "Game in progress", cssClass: "alert alert-success"}});
}
}, 15000);
}
}
},
// Player fires a shot on his turn
fireShot: function(clickedRow, clickedColumn) {
// Make sure player is not firing on own ship or somewhere they have already fired
var personalCells = Board.findOne({forPlayer: Meteor.userId()}).boardCells;
if(personalCells[clickedRow][clickedColumn].isShip) { // Firing on ship, return
return;
} else if(personalCells[clickedRow][clickedColumn].isHit || personalCells[clickedRow][clickedColumn].isMiss) { // Firing on somewhere already fired on, return
return;
}
if(Meteor.isServer) {
// Get the boards for every player as an array (note the array is non-reactive, that's why we have to do the Board.update statements)
var boards = Board.find().fetch();
// Check in the board of each player (including the board of the shooter) to see if there is a ship there
var hitAShip = false; // Flag indicating whether we hit a ship or not, used in updating shooter's board
for(var boardNumber = 0; boardNumber < boards.length; boardNumber++) { // Loop through the boards of every player
var boardOwner = boards[boardNumber].forPlayer; // Get the owner of this board
var boardCells = boards[boardNumber].boardCells; // Get the cells of this board
if(boardCells[clickedRow][clickedColumn].isShip) { // There is a ship in clicked cell
hitAShip = true;
if(!boardCells[clickedRow][clickedColumn].isHit) { // Ship not already hit, award points and update as hit for player who owns the ship
var currentScore = Players.findOne({ player: Meteor.userId() }).score;
Players.update({player: Meteor.userId()}, {$set: { score: currentScore + 1 }});
boardCells[clickedRow][clickedColumn].isHit = true;
Board.update({forPlayer: boardOwner}, {$set: {boardCells: boardCells}});
}
// Make sure player should remain in game
// Should remain in game if still has at least one ship on board that isn't sunk
// If there is one square that isShip == true and isHit == false, the player is still in the game
// If not, remove the player from the game
var playerHitStillInGame = false;
for(var i = 0; i < boardCells.length; i++) {
for(var j = 0; j < boardCells[0].length; j++) {
if(boardCells[i][j].isShip && !boardCells[i][j].isHit) {
playerHitStillInGame = true;
}
}
}
if(!playerHitStillInGame) {
Players.update({player: boardOwner}, {$set: {inGame: false}});
}
break; // Found that there was a ship hit, no need to keep looping
}
}
// Now update board of player who fired the shot based on whether the shot was a hit or miss
if(hitAShip) { // Indicate hit to player who fired the shot
personalCells[clickedRow][clickedColumn].isHit = true;
Board.update({forPlayer: Meteor.userId()}, {$set: {boardCells: personalCells}});
} else { // Indicate miss to player who fired the shot
personalCells[clickedRow][clickedColumn].isMiss = true;
Board.update({forPlayer: Meteor.userId()}, {$set: {boardCells: personalCells}});
}
// Advance to next player's turn
shotsFired = true;
advanceToNextPlayer();
}
},
initialize: function() {
if(!initialized) { // Protects from refreshes
initialized = true;
// Set game as not started
Game.insert({
field: "gameStarted",
value: false
});
Game.insert({
field: "status",
value: "Waiting for players to join...",
cssClass: "alert alert-warning"
});
// Build board
Game.insert({
field: "board",
numberRows: 20,
numberColumns: 20
});
playerNumber = 0;
playerAdded = false;
}
},
// Set up the board for each player once they have joined the game
// Each player has their own board in the dataset; there is no global board
// Not responsible for overlapping ships, this should be handled in generateShip
setUpBoard: function() {
if(Meteor.isServer) {
var numberRows = Game.findOne({field: "board"}).numberRows;
var numberColumns = Game.findOne({field: "board"}).numberColumns;
// Make an empty board
var emptyCells = [];
for(var i = 0; i < numberRows; i++) {
emptyCells[i] = []
for(var j = 0; j < numberColumns; j++) {
// Add a cell obect to this 2D array
emptyCells[i].push({
isShip: false, // Indicates if there is a ship in this spot, that is, one of this player's own ships
shipType: "", // Indicates the type of ship if there is a ship in this spot, therefore it is only set for this player's own ships
isHit: false, // Indicates if there is a hit in this square, whether on this player's own ship or on another player's ship
isMiss: false // Indicates if the player has shot this square but it has not hit any other player's ships
});
}
}
Board.insert({
forPlayer: Meteor.userId(),
boardCells: emptyCells
});
generateShip(2, "Destroyer", numberRows, numberColumns);
generateShip(3, "Submarine", numberRows, numberColumns);
generateShip(3, "Cruiser", numberRows, numberColumns);
generateShip(4, "Battleship", numberRows, numberColumns);
generateShip(5, "Carrier", numberRows, numberColumns);
}
}
});
function generateShip(shipLength, shipType, numberRows, numberColumns) {
// Generate random starting cell and direction
var randomRow = Math.floor((Math.random() * numberRows));
var randomColumn = Math.floor((Math.random() * numberColumns));
var randomDirection = Math.floor((Math.random() * 4)); // Random direction of four directions: 0 == North, 1 == East, 2 == South, 3 == West
// See if ship can exist in randomly selected spot
// Get current board for this player
var boardCells = Board.findOne({forPlayer: Meteor.userId()}).boardCells;
// Check if generated start spot is already on an existing ship
if(boardCells[randomRow][randomColumn].isShip) {
generateShip(shipLength, shipType, numberRows, numberColumns);
return;
}
// Check if flows over edge of board
if(randomDirection == 0 && randomRow - shipLength < 0) { // Flows over north (top) edge, regenerate
generateShip(shipLength, shipType, numberRows, numberColumns);
return;
} else if(randomDirection == 1 && randomColumn + shipLength > numberColumns - 1) { // Flows over east (right) edge, regenerate
generateShip(shipLength, shipType, numberRows, numberColumns);
return;
} else if(randomDirection == 2 && randomRow + shipLength > numberRows - 1) { // Flows over south (bottom) edge, regenerate
generateShip(shipLength, shipType, numberRows, numberColumns);
return;
} else if(randomDirection == 3 && randomColumn - shipLength < 0) { // Flows over west (left) edge, regenerate
generateShip(shipLength, shipType, numberRows, numberColumns);
return;
}
// Check if would occupy spot of existing ship
// Must check in every player's board, including one's own
var boards = Board.find().fetch();
for(var boardNumber = 0; boardNumber < boards.length; boardNumber++) {
var playerBoard = boards[boardNumber].boardCells; // The board we are currently checking
if(randomDirection == 0) { // Check northward for length of ship
for(var i = 1; i < shipLength; i++) {
if(playerBoard[randomRow - i][randomColumn].isShip) {
generateShip(shipLength, shipType, numberRows, numberColumns);
return;
}
}
} else if(randomDirection == 1) { // Check eastward for length of ship
for(var i = 1; i < shipLength; i++) {
if(playerBoard[randomRow][randomColumn + i].isShip) {
generateShip(shipLength, shipType, numberRows, numberColumns);
return;
}
}
} else if(randomDirection == 2) { // Check southward for length of ship
for(var i = 1; i < shipLength; i++) {
if(playerBoard[randomRow + i][randomColumn].isShip) {
generateShip(shipLength, shipType, numberRows, numberColumns);
return;
}
}
} else if(randomDirection == 3) { // Check westward for length of ship
for(var i = 1; i < shipLength; i++) {
if(playerBoard[randomRow][randomColumn - i].isShip) {
generateShip(shipLength, shipType, numberRows, numberColumns);
return;
}
}
}
}
// We have now checked that the generated ship does not flow off the board or overlap with any other existing ships.
// Therefore, place it on the board.
if(randomDirection == 0) { // Place northward for length of ship
for(var i = 0; i < shipLength; i++) {
boardCells[randomRow - i][randomColumn].isShip = true;
boardCells[randomRow - i][randomColumn].shipType = shipType;
}
} else if(randomDirection == 1) { // Place eastward for length of ship
for(var i = 0; i < shipLength; i++) {
boardCells[randomRow][randomColumn + i].isShip = true;
boardCells[randomRow][randomColumn + i].shipType = shipType;
}
} else if(randomDirection == 2) { // Place southward for length of ship
for(var i = 0; i < shipLength; i++) {
boardCells[randomRow + i][randomColumn].isShip = true;
boardCells[randomRow + i][randomColumn].shipType = shipType;
}
} else if(randomDirection == 3) { // Place westward for length of ship
for(var i = 0; i < shipLength; i++) {
boardCells[randomRow][randomColumn - i].isShip = true;
boardCells[randomRow][randomColumn - i].shipType = shipType;
}
}
// Put the generated ship in the current player's board
Board.update({forPlayer: Meteor.userId()}, {$set: {boardCells: boardCells}});
}
// Advance to the next turn
// Players have a set time to fire a shot or they forfeit their turn.
var lastPlayerSequenceNumberToFire;
function advanceToNextPlayer() {
// Note player who last fired; if it becomes this player's turn again the game is over
lastPlayerSequenceNumberToFire = activePlayerNumber;
// Set isTurn to false for player who just fired
Players.update({playerNumber: activePlayerNumber}, {$set: { isTurn: false }});
// Set isTurn to true for next player in sequence who is still in the game
var foundNextPlayer = false;
while(!foundNextPlayer) {
// Increment activePlayerNumber to next player in sequence
if(activePlayerNumber == playerNumber - 1) { // Reached last player in sequence; loop to start
activePlayerNumber = 0;
} else {
activePlayerNumber++;
}
// Check if the next player is in the game (hasn't had all their ships eliminated)
if(Players.findOne({playerNumber: activePlayerNumber}).inGame) {
foundNextPlayer = true;
// Check if the found next player is the same as the last player to fire, indicating the end of the game
if(activePlayerNumber == lastPlayerSequenceNumberToFire) {
Game.update({field: "status"}, {$set: { value: "Game over", cssClass: "alert alert-info"}});
Meteor.clearTimeout(turnTimeout); // Remove exiting time out; game is over
Meteor.setTimeout(function() {
reset();
}, 30000); // Reset game 15 seconds after it ends
return;
}
}
}
Players.update({playerNumber: activePlayerNumber}, {$set: { isTurn: true }});
// If player does not fire in certain amount of time, advance to next player
Meteor.clearTimeout(turnTimeout); // Stop running the previous timeout, we're starting a new one for the next player
if (Meteor.isServer) { // Only run this check on the server, not the client-side simulation
shotsFired = false; // Clear the shotsFired flag, it will be reset to true if the player whose turn it is fires a shot before the timeout
turnTimeout = Meteor.setTimeout(function() {
if(!shotsFired) {
advanceToNextPlayer();
}
}, 15000); // 15 seconds max per turn
}
}
function reset() {
// Clear players on startup
Players.remove({});
// Clear game information
Game.remove({});
Board.remove({});
initialized = false;
Meteor.call("initialize");
}
// Publish access to datastores
if(Meteor.isServer) {
Meteor.publish("players", function () {
return Players.find();
});
Meteor.publish("game", function () {
return Game.find();
});
// Publish to each player only their own board
Meteor.publish("board", function () {
return Board.find({forPlayer: this.userId});
});
}