-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamestate.js
82 lines (61 loc) · 1.65 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
76
77
78
79
80
81
82
var game = function (gameID) {
this.playerA;
this.playerB;
this.id = gameID;
this.playerAhits = {};
this.playerBhits = {};
this.playerAField;
this.playerBField;
this.gameState = "0 JOINT";
}
game.prototype.setState = function(state) {
if(state == "A"){
this.gameState = "1 JOINT";
}
else if(state == "B")
this.gameState = "2 JOINT";
else{
this.gameState = "0 JOINT";
}
}
game.prototype.setplayerAfield = function(ships) {
this.playerAField = ships;
}
game.prototype.setplayerBfield = function(ships) {
this.playerBField = ships;
}
game.prototype.getplayerAField = function(){
return this.playerAField;
}
game.prototype.getplayerBField = function(){
return this.playerBField;
}
game.prototype.playerAgotAHit = function(gridspot){
if( this.playerBField.includes(gridspot) ){
if(!this.playerAhits.includes(gridspot))
this.playerAhits.push(gridspot);
}
}
game.prototype.playerBgotAHit = function(gridspot){
if( this.playerAField.includes(gridspot) ){
if(!this.playerBhits.includes(gridspot))
this.playerBhits.push(gridspot);
}
}
game.prototype.hasTwoConnectedPlayers = function () {
return (this.gameState == "2 JOINT");
};
game.prototype.addPlayer = function (p) {
// if(this.gameState != "0 JOINT" && this.gameState != "1 JOINT"){
// return new Error("game full");
// }
if(this.gameState == "0 JOINT"){
this.playerA = p;
return "A";
}
else{
this.playerB = p;
return "B";
}
};
module.exports = game;