Source: site.view [edit]
Function name: playTicTacToe
Arguments:
Description: Plays tic tac toe. If no arguments, returns a new game. If one argument, new game of that level. If 2 arguments, send turnStr,gameStr representing player move and current game.
Page type: webl
Render function:  
Module: global

Page source:

var board = [. textRender = "? ? ?\n? ? ?\n? ? ?", message = "", boardPiecePositions = [] .];
var msg = "";
var playerSide = "?";
var status = "Playing";
var makeMove = true;
var turns = [];
var turnPosition = nil;
var level = "5";

if Size(OPTIONALARGS) == 1 then
   level = ToString(ToInt(OPTIONALARGS[0])) ? "5";
end;

if Size(OPTIONALARGS) == 2 then

   var turn = WubCall("jsonToWebL", [OPTIONALARGS[0]]);
   var game = WubCall("jsonToWebL", [OPTIONALARGS[1]]);

   if (turn != nil) and (game != nil) then
      turnPosition = (turn.toPiecePosition.position ? nil);
      board = game.board;
      playerSide = game.playerSide;
      status = game.status;
      level = game.gameLevel ? "5";                
      turns = game.turns ? [];
   else  // Should never go here, only if can't read turn or game for some reason.  But if so, start new game.
      msg = "You move first (You're X)";
      playerSide = "X";
      makeMove = false
   end
else
   if Wub_RandomInt(2) == 0 then
      msg = "You move first (You're X)";
      playerSide = "X";
      turnPosition = nil;
      makeMove = true
   else
      playerSide = "O";
      turnPosition = "?";
      makeMove = true;
   end

end;                                            

if (status == "Playing") and makeMove then
    var updatedGame = WubCall("makeTicTacToeMove", [ board, turns, playerSide, turnPosition, level]);
    msg = updatedGame.board.message;
    board = updatedGame.board;
    turns = updatedGame.turns;
    status = updatedGame.status;
end;
                      
                      
board.message := msg;
var res = [. board=board, playerSide=playerSide, status=status, turns=turns, gameLevel=level .];

NewPage(WubCall("toJSON", [res]), "text/plain");