Skip to content

Commit

Permalink
Update the packages, refactor some code, update the output of some fu…
Browse files Browse the repository at this point in the history
…nctions, 1.1.0-alpha.2
  • Loading branch information
radi-cho committed Jul 2, 2018
1 parent c74a1d8 commit b360a7c
Show file tree
Hide file tree
Showing 6 changed files with 1,234 additions and 279 deletions.
64 changes: 22 additions & 42 deletions rsg-chess-api/cdn/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ Game.prototype.moveSelected = function (
) {
var x = to.x
var y = to.y
var result = {}

if (selected) {
var from = { x: selected.x, y: selected.y }
Expand Down Expand Up @@ -451,20 +452,27 @@ Game.prototype.moveSelected = function (
// check for pawn promotion
if (selected.type === 'pawn') {
if ((selected.color === 'W' && y === 0) || (selected.color === 'B' && y === 7)) {
if (promotionCallback) promotionCallback(selected, x, y, selected.color)
if (promotionCallback) {
result.promotion = true
promotionCallback(selected, x, y, selected.color)
}
}
};

var checkmateColor = selected.color === 'W' ? 'B' : 'W'
var checkmateValue = this.checkmate(checkmateColor)
if (checkmateValue) checkmateCallback(checkmateValue)
if (checkmateValue) {
result.checkmate = true
checkmateCallback(checkmateValue)
}

// // Play AI
// We decided to remove the AI movements from game.js, because this slows down your app
// and makes really hard to implement external chess sources like: web workers, backends, cloud functions, ect.
}

selected = null
return true
return result
}
}

Expand Down Expand Up @@ -982,11 +990,11 @@ var getPieceValue = function (piece) {

// get value for every piece on the board
var getAbsoluteValue = function (piece) {
var color = piece.color;
var type = piece.type;
var x = piece.x;
var y = piece.y;
var isWhite = color === 'W';
var color = piece.color
var type = piece.type
var x = piece.x
var y = piece.y
var isWhite = color === 'W'

if (type === 'pawn') {
return 10 + (isWhite ? pawnEvalWhite[y][x] : pawnEvalBlack[y][x])
Expand All @@ -1006,36 +1014,9 @@ var getPieceValue = function (piece) {
// calculate the absolute value and return it
var absoluteValue = getAbsoluteValue(piece)
return piece.color === 'W' ? absoluteValue : -absoluteValue
};

var getPieceValue = function (piece) {
if (piece === null) {
return 0
}

// get value for every piece on the board
var getAbsoluteValue = function (piece) {
if (piece.type === 'pawn') {
return 10
} else if (piece.type === 'rook') {
return 50
} else if (piece.type === 'knight') {
return 30
} else if (piece.type === 'bishop') {
return 30
} else if (piece.type === 'queen') {
return 90
} else if (piece.type === 'king') {
return 900
}
}

// calculate the absolute value and return it
var absoluteValue = getAbsoluteValue(piece, piece.color === 'W')
return piece.color === 'W' ? absoluteValue : -absoluteValue
}

var uncycleBoard = function(boardObject) {
var uncycleBoard = function (boardObject) {
var board = []

for (var i = 0; i < boardObject.length; i++) {
Expand Down Expand Up @@ -1063,7 +1044,7 @@ var uncycleBoard = function(boardObject) {
var uncycleTurns = function (turnObject) {
var turns = []

turnObject.map(function(ev) {
turnObject.map(function (ev) {
var move = {}
move.from = ev.from
move.to = ev.to
Expand All @@ -1083,7 +1064,7 @@ var uncycleTurns = function (turnObject) {
}

if (ev.movePiece) {
move.movePiece = uncycleMovePiece(ev.movePiece);
move.movePiece = uncycleMovePiece(ev.movePiece)
}

turns.push(move)
Expand All @@ -1106,7 +1087,7 @@ var uncycleMovePiece = function (movePiece) {
}
}

return newMovePiece;
return newMovePiece
}

var stringifyBoard = function (board) {
Expand All @@ -1115,13 +1096,12 @@ var stringifyBoard = function (board) {
return stringified
}

var stringifyBoard = function (turn) {
var stringifyTurns = function (turn) {
var uncycled = uncycleTurns(turn)
var stringified = JSON.stringify(uncycled)
return stringified
}


window.RSGChess = {
Game: Game,
AI: ChessAI,
Expand All @@ -1139,7 +1119,7 @@ window.RSGChess = {
uncycleTurns: uncycleTurns,
uncycleMovePiece: uncycleMovePiece,
stringifyBoard: stringifyBoard,
stringifyBoard: stringifyBoard
stringifyTurns: stringifyTurns
}
}

Expand Down
2 changes: 1 addition & 1 deletion rsg-chess-api/cdn/index.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit b360a7c

Please sign in to comment.