//////////////////// //Tres en Raya //////////////////// PRESENCE.GAME.TER = (function(P,$,undefined){ //Game core var core = P.GAME //Game variables var players = null; var player = null; var currentPlayer = null; var options = null; function action(player,square) { this.player = player; this.square = square; } var playing=true; //Html Divs var divID = null; var containerDivId = 'tresEnRayaContainerDivID'; var messageDivId = 'tresEnRayaMessageDivID'; //Boardgame var board = []; var boardDimension = null; var squareDimension = null; var middleSquareDimension = null; var init = function(myPlayers,myPlayer,myOptions,myDivID){ if (!validateParams(myPlayers,myPlayer,myOptions,myDivID)){ showMessage("Invalid parameters"); return; } players = myPlayers; player = myPlayer; options = settingOptions(myOptions); divID = myDivID; drawBoard(); settingBoard(); setBoardEvents(); currentPlayer = players[0]; if(player==currentPlayer){ showMessage("You start!") } else { showMessage(currentPlayer + " starts!") } }; var validateParams = function(myPlayers,myPlayer,myOptions,myDivID){ if ((typeof myPlayers=="undefined")||(typeof myPlayer=="undefined")||(typeof myOptions=="undefined")||(typeof myDivID=="undefined")){ return false; } //Check if myPlayers is an Array if (myPlayers.constructor.toString().indexOf("Array") == -1){ return false; } //2 players at least if(myPlayers.lenght < 2){ return false; } //Check if myOptions is an Array if (myOptions.constructor.toString().indexOf("Array") == -1){ return false; } //Check that myPlayer is contained on myPlayers if(myPlayers.indexOf(myPlayer)==-1){ return false; } return true; } var settingOptions = function(myOptions){ if (!('theme' in myOptions)){ myOptions['theme'] = "Modern" } if (!('rounds' in myOptions)){ myOptions['rounds'] = "1" } //Apply default options for non specified opts. return myOptions; }; var drawBoard = function(){ var containerDiv = document.createElement('div'); containerDiv.setAttribute('id', containerDivId); containerDiv.setAttribute('class', 'tresEnRayaContainerDivClass'); $("#" + divID).append(containerDiv); var messageDiv = document.createElement('div'); messageDiv.setAttribute('id', messageDivId); $(messageDiv).addClass('tresEnRayaMessageDivClass') $(messageDiv).addClass('tresEnRayaMessageTheme' + options['theme']) $("#" + divID).append(messageDiv); var p = document.createElement('p'); p.setAttribute('id', messageDivId + "p"); $(p).addClass('tresEnRayaMessagePTheme' + options['theme']) $("#" + messageDivId).append(p); //Adjust dimensions var parentWidth = $("#" + divID).width(); var parentHeight = $("#" + divID).height(); var maxDimension = 300; var minDimension = 100; var dimension = Math.min(parentWidth,parentHeight,maxDimension) boardDimension = Math.max(dimension,minDimension) $("#" + containerDivId).height(boardDimension) $("#" + containerDivId).width(boardDimension); //Apply Theme $("#" + containerDivId).addClass('tresEnRayaContainerTheme' + options['theme']) }; var settingBoard = function(){ squareDimension = boardDimension/3; middleSquareDimension = squareDimension/2; //board[squareID] = [x,y,ElementInSquare {0=none,1=circle,2=aspa}] board[0] = [middleSquareDimension,middleSquareDimension,0] board[1] = [middleSquareDimension+squareDimension,middleSquareDimension,0] board[2] = [boardDimension-middleSquareDimension,middleSquareDimension,0] board[3] = [middleSquareDimension,middleSquareDimension+squareDimension,0] board[4] = [middleSquareDimension+squareDimension,middleSquareDimension+squareDimension,0] board[5] = [boardDimension-middleSquareDimension,middleSquareDimension+squareDimension,0] board[6] = [middleSquareDimension,boardDimension-middleSquareDimension,0] board[7] = [middleSquareDimension+squareDimension,boardDimension-middleSquareDimension,0] board[8] = [boardDimension-middleSquareDimension,boardDimension-middleSquareDimension,0] }; var setBoardEvents = function(){ $("#" + containerDivId).click(function (event) { var offsetX = event.pageX - $("#" + containerDivId).offset().left var offsetY = event.pageY - $("#" + containerDivId).offset().top if($(event.target).is('img')){ return; } if(currentPlayer!=player){ //return; } square = getSquare(offsetX,offsetY) var actionNew = new action(currentPlayer,square); processAction(actionNew) }); }; var getSquare = function(x,y){ for(var i=0; i