$(function() {
$('td.soccer-field').click(function(){
if ($('#dice1').text() != '' && $('#dice2').text() != '') {
if ($('td[data-path="begin-path"]').length == 0) {
$(this).attr('data-path','begin-path');
$(this).off('mouseenter mouseleave');
$(this).addClass('selected-path');
} else if ($('td[data-path="begin-path"]').length && $(this).attr('data-path') != 'begin-path' && $('td[data-path="end-path"]').length == 0){
$(this).attr('data-path','end-path');
$(this).addClass('actual-path');
$('.selected-path').addClass('actual-path');
$('.actual-path').removeClass('selected-path');
} else if ($(this).attr('data-path') == 'end-path'){
$(this).removeAttr('data-path');
$('.actual-path').addClass('selected-path');
$('.selected-path').removeClass('actual-path');
};
}
});
$('div.soccer-field').hover(
function () {
if ($('#dice1').text() != '' && $('#dice2').text() != '') {
if ($('td[data-path="begin-path"]').length == 0) {
$(this).attr('data-path','select-begin-path');
} else if($(this).attr('data-path') != 'begin-path'){
var presentCell = $(this).attr('id') + $(this).parent().attr('id');
var cell;
if ($(this).attr('data-path') == 'ending') {
$('.selected-path').each(function(){
cell = $(this).attr('id') + $(this).parent().attr('id');
distance = new Board().calculateDistanceSquares(presentCell,cell);
if (distance == 1 ){
$('div[data-path="ending"]').removeClass('selected-path');
$('div[data-path="ending"]').removeAttr('data-path');
if ($(this).attr('data-path') != 'begin-path')
{
$(this).attr('data-path','ending');
}
return;
}
});
}else if ($('td[data-path="end-path"]').length == 0){
var actualCell =$(this);
var dicesResult = parseInt($('#dice1').text()) + parseInt($('#dice2').text());
$('.selected-path').each(function(){
cell = $(this).attr('id') + $(this).parent().attr('id');
distance = new Board().calculateDistanceSquares(presentCell,cell);
if (distance == 1 && $('.selected-path').length <= (dicesResult -1)){
actualCell.attr('data-path','ending');
actualCell.addClass('selected-path');
if ($(this).attr('data-path') != 'begin-path') {
$(this).removeAttr('data-path');
};
return ;
}
});
}
}
}
},function () {
if ( $(this).attr('data-path') == "select-begin-path") {
$(this).removeAttr('data-path');
}
});
$('#diceRoller').click(function() {
$('#dice1').text(Math.floor(Math.random()*6)+1);
$('#dice2').text(Math.floor(Math.random()*6)+1);
$(this).attr('disabled',true);
});
$('.drag-container img.draggable').draggable({
cursor : 'move',
revert: 'invalid',
refreshPositions : true,
opacity: .45,
delay : 50
});
$('#board .soccer-field').droppable({
accept: '.drag-container img.draggable',
hoverClass: 'hover-pawn',
drop: function(event, ui){
$(this).html(ui.draggable);
ui.draggable.draggable("option","disabled",true);
}
});
});
//function Board(playerTurn, piecesPosition, gamePhase, gameBegginingType, container)
function Board(){
this.buildBoard = function (container,gamePhase,playerTurn) {
var playerTurn = playerTurn;
var gamePhase = gamePhase;
var board = $('<table></table>').attr('id','board');
if (gamePhase == 0) {
var sparePiecesBlack = $('<div></div>').attr('id','spare-pieces-black'),img;
sparePiecesBlack.addClass('drag-container');
for (var i = 0; i < 5 ; i++) {
img = $('<img>').attr('id',i+1);
img.addClass('black draggable');
img.attr('src','src/img/pieces/blackPawn.jpg');
sparePiecesBlack.append(img);
$("#diceRoller").attr('disabled',true);
};
$('#'+container).append(sparePiecesBlack);
};
var row, cell,containerHeight,containerWidth;
for (var i=0; i<10; i++){
row = $('<tr></tr>').attr('id',i+1);
for (var j=0; j<20; j++){
cell = $('<td></td>');
if ((j == 0 || j == 19) && (i >= 3) && (i <= 6)) {
cell.addClass('behind-goal');
}
else if ((j > 0) && (j < 19)){
cell.attr('id',String.fromCharCode(j+96));
cell.addClass("soccer-field");
};
row.append(cell);
}
board.append(row);
}
$('#'+container).append(board);
if (gamePhase == 0) {
var sparePiecesWhite = $('<div></div>').attr('id','spare-pieces-white'),img;
sparePiecesWhite.addClass('drag-container');
for (var i = 0; i < 5 ; i++) {
img = $('<img>').attr('id',i+1);
img.addClass('white draggable');
img.attr('src','src/img/pieces/whitePawn.jpg');
sparePiecesWhite.append(img);
};
$('#'+container).append(sparePiecesWhite);
};
};
this.calculateHorizontalDistance = function (sq1,sq2) {
var column1 = sq1.substring(0,1).charCodeAt(0);
var column2 = sq2.substring(0,1).charCodeAt(0);
return ( Math.abs(column1-column2) );
};
this.calculateVerticalDistance = function (sq1, sq2) {
var row1 = parseInt(sq1.substring(1));
var row2 = parseInt(sq1.substring(1));
return ( Math.abs(row1-row2) );
};
this.calculateDistanceSquares = function(sq1, sq2){
return(this.calculateVerticalDistance(sq1,sq2)+this.calculateHorizontalDistance(sq1,sq2));
}
}