forked from marrerom/Web-Teaching
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
126 lines (103 loc) · 4.02 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
/* every game has two players, identified by their WebSocket */
var game = function (gameID) {
this.playerA = null;
this.playerB = null;
this.id = gameID;
this.wordToGuess = null; //first player to join the game, can set the word
this.gameState = "0 JOINT"; //"A" means A won, "B" means B won, "ABORTED" means the game was aborted
};
/*
* The game can be in a number of different states.
*/
game.prototype.transitionStates = {};
game.prototype.transitionStates["0 JOINT"] = 0;
game.prototype.transitionStates["1 JOINT"] = 1;
game.prototype.transitionStates["2 JOINT"] = 2;
game.prototype.transitionStates["CHAR GUESSED"] = 3;
game.prototype.transitionStates["A"] = 4; //A won
game.prototype.transitionStates["B"] = 5; //B won
game.prototype.transitionStates["ABORTED"] = 6;
/*
* Not all game states can be transformed into each other;
* the matrix contains the valid transitions.
* They are checked each time a state change is attempted.
*/
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], //CHAR GUESSED
[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);
}
};
game.prototype.setWord = function (w) {
console.assert(typeof w == "string", "%s: Expecting a string, got a %s", arguments.callee.name, typeof w);
//two possible options for the current game state:
//1 JOINT, 2 JOINT
if (this.gameState != "1 JOINT" && this.gameState != "2 JOINT") {
return new Error("Trying to set word, but game status is %s", this.gameState);
}
this.wordToGuess = w;
};
game.prototype.getWord = function(){
return this.wordToGuess;
};
game.prototype.hasTwoConnectedPlayers = function () {
return (this.gameState == "2 JOINT");
};
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;