diff --git a/games/tic-tac-toe-js/index.html b/games/tic-tac-toe-js/index.html
index 6df9733..97d3c4f 100644
--- a/games/tic-tac-toe-js/index.html
+++ b/games/tic-tac-toe-js/index.html
@@ -8,12 +8,12 @@
diff --git a/games/tic-tac-toe-js/js/GGS.js b/games/tic-tac-toe-js/js/GGS.js
index 1b92ff7..7b9d54b 100644
--- a/games/tic-tac-toe-js/js/GGS.js
+++ b/games/tic-tac-toe-js/js/GGS.js
@@ -1,4 +1,4 @@
-function GGSI(game_name) {
+function GGS(game_name) {
var world = new Storage(game_name, "world");
this.__defineGetter__("world", function(){
return world;
@@ -11,6 +11,6 @@ function GGSI(game_name) {
var game_n = game_name;
this.__defineGetter__("users", function(){
- return GameServerI.users(game_n);
+ return GameServer.users(game_n);
});
}
diff --git a/games/tic-tac-toe-js/js/GameServer.js b/games/tic-tac-toe-js/js/GameServer.js
index dc752b8..208cb73 100644
--- a/games/tic-tac-toe-js/js/GameServer.js
+++ b/games/tic-tac-toe-js/js/GameServer.js
@@ -55,7 +55,7 @@ GameServer.prototype.callCommand = function(game_name, client, command, attrs) {
if(typeof game.instance.userCommand == "function") {
var user = null;
for (var i=0; i < game.users.length; i++) {
- if (game.users[i].client == client) {
+ if (game.users[i].isClient(client)) {
user = game.users[i];
}
}
@@ -129,7 +129,7 @@ GameServer.prototype.clear = function(game_name, type) {
} else throw "GGS: Unknown game " + game_name
}
-var GameServerI = new GameServer();
+var GameServer = new GameServer();
@@ -143,9 +143,11 @@ function User(id, client) {
return {
id: id,
- client: client,
sendCommand: function(command, args) {
client.commandCalled(command, args);
+ },
+ isClient: function(c) {
+ return c == client;
}
}
}
@@ -159,24 +161,24 @@ function Storage(game_name, type) {
return {
setItem: function(key, value) {
- GameServerI.set(self.gameName, self.type, key, value);
+ GameServer.set(self.gameName, self.type, key, value);
},
getItem: function(key) {
- return GameServerI.get(self.gameName, self.type, key);
+ return GameServer.get(self.gameName, self.type, key);
},
key: function(position) {
- return GameServerI.key(self.gameName, self.type, position);
+ return GameServer.key(self.gameName, self.type, position);
},
length: {
get: function() {
- return GameServerI.length(self.gameName, self.type);
+ return GameServer.length(self.gameName, self.type);
}
},
removeItem: function(key) {
- GameServerI.remove(self.gameName, self.type, key);
+ GameServer.remove(self.gameName, self.type, key);
},
clear: function() {
- GameServerI.clear(self.gameName, self.type);
+ GameServer.clear(self.gameName, self.type);
}
}
} else throw "GGS: No such storage available " + type;
diff --git a/games/tic-tac-toe-js/js/TicTacToe.js b/games/tic-tac-toe-js/js/TicTacToe.js
index 2d47974..f935939 100644
--- a/games/tic-tac-toe-js/js/TicTacToe.js
+++ b/games/tic-tac-toe-js/js/TicTacToe.js
@@ -1,8 +1,31 @@
-function TicTacToe() {};
-TicTacToe.prototype.init = function() {
+/*
+This code is public domain, it's just a example how a game
+on our Generic Game Server could look like.
+API:
+
+GGS.world // webstorage interface
+GGS.localStorage // webstorage interface
+http://dev.w3.org/html5/webstorage/
+
+GGS.users is a array with User objects:
+User {
+ id // integer attribute
+ sendCommand: function(command, args)
}
+There are optional and mandatory methods a Game should implement:
+
+Game main() // m - should return a instance of the game, is called at startup
+userCommand(user, command, args) // m - is called when a client sends a message
+userAllowed(user) // o - should return a bolean, is called when a new is added
+userAdded(user) // o - is called when a client was added
+
+*/
+
+
+function TicTacToe() {};
+
TicTacToe.prototype.userAllowed = function(user) {
if(GGS.users.length <= 2) return true
else return false;
@@ -58,8 +81,10 @@ TicTacToe.prototype.userCommand = function(user, command, args) {
var gameBoard = JSON.parse(GGS.world.getItem("game_board"));
if (gameBoard[props.x][props.y] == 0) {
+
gameBoard[props.x][props.y] = p;
- GGS.world.setItem("game_board", JSON.stringify(gameBoard))
+ GGS.world.setItem("game_board", JSON.stringify(gameBoard));
+
if (this.checkIfWon(p, gameBoard)) {
if (p == 1) {
this.getUser(p1_id).sendCommand("winner", "You win!");
@@ -92,44 +117,36 @@ TicTacToe.prototype.checkIfWon = function(player, gameBoard) {
var rows = gameBoard.length;
for (i = 0; i < rows; ++i) {
- for (j = 0; j < rows; ++j) {
- if (gameBoard[i][j] != player) {
+ for (j = 0; j < rows; ++j)
+ if (gameBoard[i][j] != player)
break;
- }
- }
- if (j == rows) {
- return true;
- }
- for (j = 0; j < rows; ++j) {
- if (gameBoard[j][i] != player) {
+ if (j == rows)
+ return true;
+
+ for (j = 0; j < rows; ++j)
+ if (gameBoard[j][i] != player)
break;
- }
- }
- if (j == rows) {
+
+ if (j == rows)
return true;
- }
}
// Now check diagnols
- for (i = 0; i < rows; ++i) {
- if (gameBoard[i][i] != player) {
+ for (i = 0; i < rows; ++i)
+ if (gameBoard[i][i] != player)
break;
- }
- }
+
- if (i == rows) {
+ if (i == rows)
return true;
- }
- for (i = 0; i < rows; ++i) {
- if (gameBoard[i][rows - i - 1] != player) {
+ for (i = 0; i < rows; ++i)
+ if (gameBoard[i][rows - i - 1] != player)
break;
- }
- }
- if (i == rows) {
+
+ if (i == rows)
return true;
- }
return false;
}
diff --git a/games/tic-tac-toe-js/js/TicTacToeClient.js b/games/tic-tac-toe-js/js/TicTacToeClient.js
index 444f921..80f8ef1 100644
--- a/games/tic-tac-toe-js/js/TicTacToeClient.js
+++ b/games/tic-tac-toe-js/js/TicTacToeClient.js
@@ -1,4 +1,6 @@
// This first version doesn't take networking into account yet.
+// Only tested in FireFox, Safari, Chrome and Opera
+// Will not work in IE <9 because getters and setters are used.
function TicTacToeClient(container, server) {
this.server = server;
@@ -6,7 +8,6 @@ function TicTacToeClient(container, server) {
this.init();
}
-
TicTacToeClient.prototype.init = function() {
this.rows = 3;
this.spots = [];