|
@ -145,16 +145,6 @@ Chess.UI.prototype.updatePieces = function() { |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
Chess.UI.prototype.makeMove = function(move) { |
|
|
|
|
|
this.chessPosition.makeMove(move); |
|
|
|
|
|
var state = "#" + this.chessPosition.getString().trim(); |
|
|
|
|
|
if (this.chessPosition.getTurnColor() === Chess.PieceColor.BLACK) { |
|
|
|
|
|
window.history.pushState(null, move.getString(), state); |
|
|
|
|
|
} else { |
|
|
|
|
|
window.history.replaceState(null, move.getString(), state); |
|
|
|
|
|
} |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Adds chessboard cell hover, and chess piece dragging and dropping capabilities to the chessboard |
|
|
* Adds chessboard cell hover, and chess piece dragging and dropping capabilities to the chessboard |
|
|
*/ |
|
|
*/ |
|
@ -223,7 +213,7 @@ Chess.UI.prototype.updateMoves = function() { |
|
|
|
|
|
|
|
|
if (makeMoves.length > 0) { |
|
|
if (makeMoves.length > 0) { |
|
|
// TODO: it's possible that there is more than one move (promotions). Either ask the user here or have a droplist somewhere ("promote to")
|
|
|
// TODO: it's possible that there is more than one move (promotions). Either ask the user here or have a droplist somewhere ("promote to")
|
|
|
ui.makeMove(makeMoves[0]); |
|
|
|
|
|
|
|
|
ui.chessPosition.makeMove(makeMoves[0]); |
|
|
ui.updateChessPosition(); |
|
|
ui.updateChessPosition(); |
|
|
} else { |
|
|
} else { |
|
|
// Dropped to an invalid square
|
|
|
// Dropped to an invalid square
|
|
@ -256,7 +246,7 @@ Chess.UI.prototype.updateMoves = function() { |
|
|
} else if (id === "auto") { |
|
|
} else if (id === "auto") { |
|
|
ui.doComputerMove(); |
|
|
ui.doComputerMove(); |
|
|
} else { |
|
|
} else { |
|
|
ui.makeMove(moves[parseInt(id, 10)]); |
|
|
|
|
|
|
|
|
ui.chessPosition.makeMove(moves[parseInt(id, 10)]); |
|
|
ui.updateChessPosition(); |
|
|
ui.updateChessPosition(); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
@ -276,7 +266,7 @@ Chess.UI.prototype.doComputerMove = function() { |
|
|
throw new Error("Move not found"); |
|
|
throw new Error("Move not found"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
ui.makeMove(move); |
|
|
|
|
|
|
|
|
ui.chessPosition.makeMove(move); |
|
|
var from = $("#" + Chess.getAlgebraicFromIndex(move.getFrom())); |
|
|
var from = $("#" + Chess.getAlgebraicFromIndex(move.getFrom())); |
|
|
var to = $("#" + Chess.getAlgebraicFromIndex(move.getTo())); |
|
|
var to = $("#" + Chess.getAlgebraicFromIndex(move.getTo())); |
|
|
var dx = (to.offset().left - from.offset().left); |
|
|
var dx = (to.offset().left - from.offset().left); |
|
@ -313,23 +303,11 @@ Chess.UI.prototype.updateChessPosition = function() { |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
Chess.UI.prototype.loadState = function() { |
|
|
|
|
|
var moves = window.location.href.split('#'); |
|
|
|
|
|
if (moves.length > 1) { |
|
|
|
|
|
this.chessPosition = Chess.Parser.parseMoves(moves[1]); |
|
|
|
|
|
} else { |
|
|
|
|
|
this.chessPosition = new Chess.Position; |
|
|
|
|
|
} |
|
|
|
|
|
this.updateChessPosition(); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Creates a new chessboard and sets up the game at the standard chess initial position. |
|
|
* Creates a new chessboard and sets up the game at the standard chess initial position. |
|
|
*/ |
|
|
*/ |
|
|
function makeChessGame() { |
|
|
function makeChessGame() { |
|
|
Chess.UI.makeBoard(); |
|
|
Chess.UI.makeBoard(); |
|
|
var ui = new Chess.UI; |
|
|
var ui = new Chess.UI; |
|
|
ui.loadState(); |
|
|
|
|
|
ui.updateChessPosition(); |
|
|
ui.updateChessPosition(); |
|
|
window.onpopstate = ui.loadState.bind(ui); |
|
|
|
|
|
} |
|
|
} |
|
|