Added python tictactoe client
This commit is contained in:
parent
9efd739cbb
commit
f81fec6b9c
6 changed files with 706 additions and 1 deletions
156
games/tic-tac-toe-python/server.js
Normal file
156
games/tic-tac-toe-python/server.js
Normal file
|
@ -0,0 +1,156 @@
|
|||
function playerCommand(player_id, command, args) {
|
||||
if (commaned == "hi") {
|
||||
hi(player_id);
|
||||
} else if (command == "set") {
|
||||
move(player_id, args);
|
||||
} else if (command == "new") {
|
||||
newGame();
|
||||
}
|
||||
}
|
||||
|
||||
var ROWS = 3;
|
||||
|
||||
function hi(player_id) {
|
||||
var p1_id = GGS.localStorage.getItem("p1_id");
|
||||
var p2_id = GGS.localStorage.getItem("p2_id");
|
||||
if (p1_id == "") {
|
||||
GGS.localStorage.setItem("p1_id", player_id);
|
||||
GGS.sendCommand(player_id, "welcome", "1");
|
||||
} else if (p2_id == "") {
|
||||
GGS.localStorage.setItem("p2_id", player_id);
|
||||
GGS.sendCommand(player_id, "welcome", "2");
|
||||
} else {
|
||||
GGS.sendCommand(player_id, "not_welcome", "Already have 2 players on this table");
|
||||
}
|
||||
}
|
||||
|
||||
function move(player_id, args) {
|
||||
var nextPlayer = GGS.localStorage.getItem("next_player");
|
||||
var p1_id = GGS.localStorage.getItem("p1_id");
|
||||
var p2_id = GGS.localStorage.getItem("p2_id");
|
||||
var valid = false;
|
||||
|
||||
if(nextPlayer == 1 && player_id == p1_id) {
|
||||
valid = true;
|
||||
} else if (nextPlayer == 2 && player_id == p2_id) {
|
||||
valid = true;
|
||||
}
|
||||
|
||||
if (valid) {
|
||||
|
||||
var p = nextPlayer;
|
||||
var props = JSON.parse(args);
|
||||
var gameBoard = JSON.parse(GGS.localStorage.getItem("game_board"));
|
||||
|
||||
if (gameBoard[props.x][props.y] == 0) {
|
||||
|
||||
gameBoard[props.x][props.y] = p;
|
||||
GGS.localStorage.setItem("game_board", JSON.stringify(gameBoard));
|
||||
GGS.sendCommandToAll("game_board", boardAsString(gameBoard));
|
||||
|
||||
if (this.checkIfWon(p, gameBoard)) {
|
||||
if (p == 1) {
|
||||
GGS.sendCommand(p1_id, "winner", "You win!");
|
||||
GGS.sendCommand(p2_id, "loser", "You lose!");
|
||||
} else {
|
||||
GGS.sendCommand(p1_id, "loser", "You lose!");
|
||||
GGS.sendCommand(p2_id, "winner", "You win!");
|
||||
}
|
||||
}
|
||||
|
||||
if (nextPlayer == 1) {
|
||||
GGS.localStorage.setItem("next_player", 2);
|
||||
GGS.sendCommand(p1_id, "yourturn", "");
|
||||
} else {
|
||||
GGS.localStorage.setItem("next_player", 1);
|
||||
GGS.sendCommand(p2_id, "yourturn", "");
|
||||
}
|
||||
|
||||
} else {
|
||||
GGS.sendCommand(plaer_id, "warning", "Already set, chose something else.");
|
||||
}
|
||||
|
||||
} else {
|
||||
GGS.sendCommand(player_id, "warning", "Not your turn!");
|
||||
}
|
||||
}
|
||||
|
||||
function checkIfWon(player) {
|
||||
|
||||
var gameBoard = JSON.parse(GGS.localStorage.getItem("game_board"));
|
||||
|
||||
for (i = 0; i < ROWS; ++i) {
|
||||
for (j = 0; j < ROWS; ++j) {
|
||||
if (gameBoard[i][j] != 'X') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (j == ROWS) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (j = 0; j < ROWS; ++j) {
|
||||
if (gameBoard[j][i] != 'X') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (j == ROWS) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Now check diagnols
|
||||
for (i = 0; i < ROWS; ++i) {
|
||||
if (gameBoard[i][i] != 'X') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (i == ROWS) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (i = 0; i < ROWS; ++i) {
|
||||
if (gameBoard[i][ROWS - i - 1] != 'X') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i == ROWS) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function newGame() {
|
||||
// Initiate game with empty rows and columns
|
||||
var gameBoard = [];
|
||||
for (var i=0; i < ROWS; i++) {
|
||||
gameBoard[i] = [];
|
||||
for (var j=0; i < ROWS; i++) {
|
||||
gameBoard[i][j] = '';
|
||||
}
|
||||
}
|
||||
|
||||
GGS.localStorage.setItem("game_board", JSON.stringify(gameBoard));
|
||||
GGS.sendCommandToAll("new_game", "");
|
||||
GGS.sendCommandToAll("game_board", boardAsString(gameBoard));
|
||||
}
|
||||
|
||||
function boardAsString(gameBoard) {
|
||||
var out = "";
|
||||
for (var i=0; i < ROWS; i++) {
|
||||
for (var j=0; j < ROWS; j++) {
|
||||
var p = gameBoard[i][j];
|
||||
if (p == 1) {
|
||||
out += "X";
|
||||
} else if (p == 2) {
|
||||
out +="O";
|
||||
} else {
|
||||
out += " ";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
Reference in a new issue