Skip to content

Commit

Permalink
Adding modules/chess antichess improvements based on the chess antich…
Browse files Browse the repository at this point in the history
…ess_improvements branch. We no longer have an AntiKing which was a bit of a hack.
  • Loading branch information
Happy0 committed Jan 15, 2015
1 parent 01e3341 commit cb9478f
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 17 deletions.
2 changes: 1 addition & 1 deletion modules/chess
Submodule chess updated from e7d8b3 to 9df655
11 changes: 6 additions & 5 deletions modules/game/src/main/BinaryFormat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lila.game

import org.joda.time.DateTime
import scala.util.{ Try, Success, Failure }
import chess.variant.Variant

import chess._

Expand Down Expand Up @@ -163,13 +164,13 @@ object BinaryFormat {
} toArray)
}

def read(ba: ByteArray): PieceMap = {
def read(ba: ByteArray, variant: Variant): PieceMap = {
def splitInts(b: Byte) = {
val int = b.toInt
Array(int >> 4, int & 0x0F)
}
def intPiece(int: Int): Option[Piece] =
intToRole(int & 7) map { role => Piece(Color((int & 8) == 0), role) }
intToRole(int & 7, variant) map { role => Piece(Color((int & 8) == 0), role) }
val pieceInts = ba.value flatMap splitInts
(Pos.all zip pieceInts flatMap {
case (pos, int) => intPiece(int) map (pos -> _)
Expand All @@ -179,14 +180,15 @@ object BinaryFormat {
// cache standard start position
val standard = write(Board.init(chess.variant.Standard).pieces)

private def intToRole(int: Int): Option[Role] = int match {
private def intToRole(int: Int, variant: Variant): Option[Role] = int match {
case 6 => Some(Pawn)
case 1 => Some(King)
case 2 => Some(Queen)
case 3 => Some(Rook)
case 4 => Some(Knight)
case 5 => Some(Bishop)
case 7 => Some(Antiking)
// Legacy from when we used to have an 'Antiking' piece
case 7 if variant.antichess => Some(King)
case _ => None
}
private def roleToInt(role: Role): Int = role match {
Expand All @@ -196,7 +198,6 @@ object BinaryFormat {
case Rook => 3
case Knight => 4
case Bishop => 5
case Antiking => 7
}
}

Expand Down
2 changes: 1 addition & 1 deletion modules/game/src/main/Game.scala
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ case class Game(

lazy val toChess: ChessGame = {

val pieces = BinaryFormat.piece read binaryPieces
val pieces = BinaryFormat.piece.read(binaryPieces, variant)

ChessGame(
board = Board(pieces, toChessHistory, variant),
Expand Down
3 changes: 2 additions & 1 deletion modules/game/src/test/BinaryPieceTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import org.specs2.mutable._
import org.specs2.specification._

import lila.db.ByteArray
import chess.variant.Standard

class BinaryPieceTest extends Specification {

val noop = "00000000"
def write(all: PieceMap): List[String] =
(BinaryFormat.piece write all).showBytes.split(',').toList
def read(bytes: List[String]): PieceMap =
BinaryFormat.piece read ByteArray.parseBytes(bytes)
BinaryFormat.piece.read(ByteArray.parseBytes(bytes), Standard)

"binary pieces" should {
"write" should {
Expand Down
5 changes: 0 additions & 5 deletions ui/round/src/ground.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ function reload(ground, data, fen, flip) {

function promote(ground, key, role) {

// If the player has promoted to a king in the antichess mode, we treat
// the piece like a king
if (role === "antiking")
role = "king";

var pieces = {};
var piece = ground.data.pieces[key];
if (piece && piece.role == 'pawn') {
Expand Down
6 changes: 2 additions & 4 deletions ui/round/src/promotion.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,13 @@ module.exports = {

view: function(ctrl) {
var pieces = ctrl.data.game.variant.key != "antichess" ?
['queen', 'knight', 'rook', 'bishop'] : ['queen', 'knight', 'rook', 'bishop', 'antiking']
['queen', 'knight', 'rook', 'bishop'] : ['queen', 'knight', 'rook', 'bishop', 'king']

return promoting ? m('div#promotion_choice', {
onclick: partial(cancel, ctrl)
}, pieces.map(function(serverRole) {
// We display the piece for the antiking the same way we would for a normal king
var internalPiece = serverRole == "antiking" ? "king" : serverRole;

return m('div.cg-piece.' + internalPiece + '.' + ctrl.data.player.color, {
return m('div.cg-piece.' + serverRole + '.' + ctrl.data.player.color, {
onclick: function(e) {
e.stopPropagation();
finish(ctrl, serverRole);
Expand Down

0 comments on commit cb9478f

Please sign in to comment.