/
gamestate.js
75 lines (62 loc) · 2.03 KB
/
gamestate.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
/**
* Is used to track the current state of the game
* @author Nate Celeste NTC14, Noah Crowley NWC17
*/
function GameState() {
this.board = new Board(this);
this.ghostBoard = new Board(this);
this.currentPlayerID = "player1";
this.isGameGoing = false;
this.players = [];
this.createPlayers();
document.addEventListener("spaceChangedEvent", function(e) {
this.ghostBoard.getSpace(e.detail.column, e.detail.row).playerID = e.detail.playerID;
}.bind(this));
}
/**
* Creates the players, Player1 and Player2, and stores them in the _players Map.
*/
GameState.prototype.createPlayers = function () {
this.players["player1"] = new HumanPlayer("player1");
this.players["player2"] = new ComputerPlayer(this, "player2");
};
/**
* Starts the game and fires the gameStarted event
*/
GameState.prototype.startGame = function () {
this.isGameGoing = true;
document.dispatchEvent(new CustomEvent("gameStartedEvent"));
};
/**
* Ends the game and fires the gameEnded event
* @param winnerPlayerID The PlayerID of the player who won the game
*/
GameState.prototype.endGame = function (winnerPlayerID) {
this.isGameGoing = false;
var event = new CustomEvent("gameOverEvent", {"detail": winnerPlayerID});
document.dispatchEvent(event);
};
/**
* Sets the current player to the player who is next up.
*/
GameState.prototype.goToNextPlayer = function () {
var winningPlayer = this.board.checkForWinner();
if (winningPlayer === "none") {
if(this.currentPlayerID === "player1")
this.currentPlayerID = "player2";
else
this.currentPlayerID = "player1";
var event = new CustomEvent("playerChangedEvent", {"detail": this.currentPlayerID});
document.dispatchEvent(event);
}
else{
this.currentPlayerID = "none";
this.endGame(winningPlayer);
}
};
GameState.prototype.toString = function () {
var output = "";
output += this.currentPlayerID + "\n";
output += this.board.toString() + "\n";
return output;
};