-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
159 lines (126 loc) · 4.8 KB
/
game.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
var game = function(gameID) {
this.playerA = null;
this.playerB = null;
this.id = gameID;
this.boardA = null; //both players set matrix
this.boardB = null;
this.shoot = null;
this.gameState = "0 JOINT"; //"A" means A won, "B" means B won, "ABORTED" means the game was aborted
};
game.prototype.transitionStates = {};
game.prototype.transitionStates["0 JOINT"] = 0;
game.prototype.transitionStates["1 JOINT"] = 1;
game.prototype.transitionStates["2 JOINT"] = 2;
// for characters change for matrix
game.prototype.transitionStates["SHOOT"] = 3; //change for ship
game.prototype.transitionStates["A"] = 4; //A won
game.prototype.transitionStates["B"] = 5; //B won
game.prototype.transitionStates["ABORTED"] = 6;
game.prototype.transitionMatrix = [
[0, 1, 0, 0, 0, 0, 0], //0 JOINT
[1, 0, 1, 0, 0, 0, 0], //1 JOINT
[0, 0, 0, 1, 0, 0, 1], //2 JOINT (note: once we have two players, there is no way back!)
[0, 0, 0, 1, 1, 1, 1], //SHOOT
[0, 0, 0, 0, 0, 0, 0], //A WON
[0, 0, 0, 0, 0, 0, 0], //B WON
[0, 0, 0, 0, 0, 0, 0] //ABORTED
];
game.prototype.isValidTransition = function(from, to) {
console.assert(typeof from == "string", "%s: Expecting a string, got a %s", arguments.callee.name, typeof from);
console.assert(typeof to == "string", "%s: Expecting a string, got a %s", arguments.callee.name, typeof to);
console.assert(from in game.prototype.transitionStates == true, "%s: Expecting %s to be a valid transition state", arguments.callee.name, from);
console.assert(to in game.prototype.transitionStates == true, "%s: Expecting %s to be a valid transition state", arguments.callee.name, to);
let i, j;
if (!(from in game.prototype.transitionStates)) {
return false;
} else {
i = game.prototype.transitionStates[from];
}
if (!(to in game.prototype.transitionStates)) {
return false;
} else {
j = game.prototype.transitionStates[to];
}
return (game.prototype.transitionMatrix[i][j] > 0);
};
game.prototype.isValidState = function(s) {
return (s in game.prototype.transitionStates);
};
game.prototype.setStatus = function(w) {
console.assert(typeof w == "string", "%s: Expecting a string, got a %s", arguments.callee.name, typeof w);
if (game.prototype.isValidState(w) && game.prototype.isValidTransition(this.gameState, w)) {
this.gameState = w;
console.log("[STATUS] %s", this.gameState);
} else {
return new Error("Impossible status change from %s to %s", this.gameState, w);
}
};
//SET BOARD FOR A
game.prototype.setBoardA = function(w) {
console.assert(Array.isArray(w), "%s: Expecting array, got %s", arguments.callee.name, typeof w);
if (this.gameState != "1 JOINT" && this.gameState != "2 JOINT") {
return new Error("Trying to set ships, but game status is %s", this.gameState);
}
this.boardA = w;
};
//SET BOARD FOR B
game.prototype.setBoardB = function(w) {
console.assert(Array.isArray(w), "%s: Expecting array, got %s", arguments.callee.name, typeof w);
if (this.gameState != "1 JOINT" && this.gameState != "2 JOINT") {
return new Error("Trying to set ships, but game status is %s", this.gameState);
}
this.boardB = w;
};
//GET BOARD A
game.prototype.getBoardA = function() {
return this.boardA;
}
//GET BOARD B
game.prototype.getBoardB = function() {
return this.boardB;
}
// game.prototype.setShoot=funciton(w)
// {
// console.assert(typeof w=="string", "%s: Expecting a string, got a %s", arguments.callee.name,typeof w);
//
// }
//
// game.prototype.getShoot=function(){
// return this.shoot;
// }
//CHANGE STATUS
game.prototype.setStatus = function(w) {
console.assert(typeof w == "string", "%s: Expecting a string, got a %s", arguments.callee.name, typeof w);
if (game.prototype.isValidState(w) && game.prototype.isValidTransition(this.gameState, w)) {
this.gameState = w;
console.log("[STATUS] %s", this.gameState);
} else {
return new Error("Impossible status change from %s to %s", this.gameState, w);
}
};
//CHACK TWO PLAYERS CONNECTED?
game.prototype.hasTwoConnectedPlayers = function() {
return (this.gameState == "2 JOINT");
};
//ADD PLAYERS
game.prototype.addPlayer = function(p) {
console.assert(p instanceof Object, "%s: Expecting an object (WebSocket), got a %s", arguments.callee.name, typeof p);
if (this.gameState != "0 JOINT" && this.gameState != "1 JOINT") {
return new Error("Invalid call to addPlayer, current state is %s", this.gameState);
}
/*
* revise the game state
*/
var error = this.setStatus("1 JOINT");
if (error instanceof Error) {
this.setStatus("2 JOINT");
}
if (this.playerA == null) {
this.playerA = p;
return "A";
} else {
this.playerB = p;
return "B";
}
};
module.exports = game;