public void UnMakeMove() { var index = History.Count - 1; if (index < 0) return; var m = History[index]; HashKey ^= TranspositionTable.SideToMoveKey; SideToMove ^= 1; //restore captured piece if (m.CapturedPiece > 0) UnmakeCapture(m); if ((m.Bits & (byte)MoveBits.King) > 0 && Math.Abs(m.From.File() - m.To.File()) == 2) UnmakeCastleMove(m); if (m.Promotion > 0) UpdatePromotion(m); if (EnPassant > 0) HashKey ^= TranspositionTable.EnPassantFileKey[m.To.File()]; EnPassant = m.EnPassant; if (EnPassant > 0) { int file = EnPassant.BitScanForward().File(); HashKey ^= TranspositionTable.EnPassantFileKey[file]; } if (CastleStatus != m.CastleStatus){ HashKey ^= TranspositionTable.CastleStatusKey[CastleStatus]; CastleStatus = m.CastleStatus; HashKey ^= TranspositionTable.CastleStatusKey[CastleStatus]; } UpdateBitBoards(m); History.RemoveLast(); }
static void Main(string[] args) { Console.Title = "Chess"; var chessGame = new Game(); var enPassantChecker = new EnPassant(); chessGame.CreateGame(enPassantChecker); PlayGame.Play(chessGame); }
public static void ParseMove_CorrectNotation_ReturnExpectedEnPassant(string moveString, string expectedFromPositionString, string expectedToPositionString) { var expectedMove = new EnPassant((Pawn)_board[expectedFromPositionString], new Position(expectedToPositionString), _board); var move = MoveParser.ParseMove(moveString, _board); Assert.That(move.Equals(expectedMove)); }
public bool MakeMove(Move m, bool checkLegal=true) { var hm = new HistoryMove(HashKey, m); hm.EnPassant = EnPassant; hm.CastleStatus = CastleStatus; //castle if ((m.Bits & (byte)MoveBits.King) > 0 && Math.Abs(m.To.File() - m.From.File()) == 2) MakeCastleMove(m); UpdateCastleStatus(m); if(CastleStatus != hm.CastleStatus){ HashKey ^= TranspositionTable.CastleStatusKey[hm.CastleStatus]; HashKey ^= TranspositionTable.CastleStatusKey[CastleStatus]; } UpdateCapture(m, hm); UpdateBitBoards(m); if (m.Promotion > 0) UpdatePromotion(m); if(EnPassant >0){ int file = EnPassant.BitScanForward().File(); HashKey ^= TranspositionTable.EnPassantFileKey[file]; // remove } //set enpassant sq if (((m.Bits & (byte)MoveBits.Pawn) > 0) && Math.Abs(m.To - m.From) == 16) { EnPassant = BitMask.Mask[m.From + (SideToMove == 1 ? 8 : -8)]; HashKey ^= TranspositionTable.EnPassantFileKey[m.To.File()]; } else EnPassant = 0; SideToMove ^= 1; HashKey ^= TranspositionTable.SideToMoveKey; // push the move onto the list of moves History.Add(hm); //make sure we are legal if (checkLegal && InCheck(SideToMove ^ 1)) { UnMakeMove(); return false; } return true; }
public ActionResult <string> StartGame() { try { chessGame = new Game(); var enPassantChecker = new EnPassant(); chessGame.CreateGame(enPassantChecker); return(Ok(JsonConvert.SerializeObject(chessGame.Board.Squares))); } catch (System.Exception) { return(NotFound()); } }
public override HashSet <IMove> PossibleMoves(Board board) { var position = board.FindPiece(this); var result = new HashSet <IMove>(); try { var before = position.Before(Color); if (AddMoveIfNotBlocked(before, board, result) && !board.HasPieceBeenMoved(this)) { try { AddMoveIfNotBlocked(before.Before(Color), board, result); } catch (ArgumentException) { } } void AddCapture(int columnDistance) { try { var to = new Position(before.Row, (byte)(position.Column + columnDistance)); if (board.IsTherePieceOfColor(to, !Color)) { AddMoveOrPromotions(to, board, result); } else if (EnPassant.IsEnPassant(this, to, board)) { result.Add(new EnPassant(this, to, board)); } } catch (ArgumentException) { } } AddCapture(+1); AddCapture(-1); } catch (ArgumentException) { } return(result); }
public override string ToString() { switch (Type) { case MoveType.Castle: return(Castle.ToString()); case MoveType.EnPassant: return(EnPassant.ToString()); case MoveType.NormalPiece: return(NormalPieceMove.ToString()); case MoveType.Promotion: return(Promotion.ToString()); default: throw new AlienChessException(); } }
public override bool CalculateMoves() { var possibleMoves = new List <IMove>(); var pos = this.CurrentPosition; pos.row += _direction; if (pos.row < _board.RowColLen && _board[pos].OccupyingPiece == null) { if (pos.row == 0 || pos.row == _board.RowColLen - 1) { possibleMoves.Add(new Promotion(_board[pos], this, new Queen(this.PieceOwner, this._board, pos))); } else { possibleMoves.Add(new Move(_board[pos], this)); } if (TimesMoved == 0) { pos.row += _direction; if (pos.row < _board.RowColLen && _board[pos].OccupyingPiece == null) { if (pos.row == 0 || pos.row == _board.RowColLen - 1) { possibleMoves.Add(new Promotion(_board[pos], this, new Queen(this.PieceOwner, this._board, pos))); } else { possibleMoves.Add(new Move(_board[pos], this)); } } } } //reset pos = this.CurrentPosition; pos.row += _direction; if (pos.row < _board.RowColLen) { if (pos.col + 1 < _board.RowColLen) { var tempPos = new PiecePosition(pos.row, pos.col + 1); var tile = _board[tempPos]; tile.ThreateningPieces.Add(this); if (tile.OccupyingPiece != null && tile.OccupyingPiece.PieceOwner.Id != this.PieceOwner.Id) { if (tempPos.row == 0 || tempPos.row == _board.RowColLen - 1) { possibleMoves.Add(new Promotion(tile, this, new Queen(this.PieceOwner, this._board, tempPos))); } else { possibleMoves.Add(new Move(tile, this)); } } } if (pos.col - 1 >= 0) { var tempPos = new PiecePosition(pos.row, pos.col - 1); var tile = _board[tempPos]; tile.ThreateningPieces.Add(this); if (tile.OccupyingPiece != null && tile.OccupyingPiece.PieceOwner.Id != this.PieceOwner.Id) { if (tempPos.row == 0 || tempPos.row == _board.RowColLen - 1) { possibleMoves.Add(new Promotion(tile, this, new Queen(this.PieceOwner, this._board, tempPos))); } else { possibleMoves.Add(new Move(tile, this)); } } } } //reset pos = this.CurrentPosition; pos.col++; if (pos.col < _board.RowColLen && _board[pos].OccupyingPiece != null && _board[pos].OccupyingPiece.PieceOwner.Id != this.PieceOwner.Id && _board[pos].OccupyingPiece.PieceName == "Pawn" && ((Pawn)_board[pos].OccupyingPiece).EnPassant) { var tile = _board[new PiecePosition(pos.row + _direction, pos.col)]; var move = new EnPassant(_board[tile.Position], this); possibleMoves.Add(move); } pos.col -= 2; if (pos.col >= 0 && _board[pos].OccupyingPiece != null && _board[pos].OccupyingPiece.PieceOwner.Id != this.PieceOwner.Id && _board[pos].OccupyingPiece.PieceName == "Pawn" && ((Pawn)_board[pos].OccupyingPiece).EnPassant) { var tile = _board[new PiecePosition(pos.row + _direction, pos.col)]; var move = new EnPassant(tile, this); possibleMoves.Add(move); } PossibleMoves = possibleMoves; return(true); }
public override string ToString() { StringBuilder piecesBuilder = new(); for (int i = 7; i >= 0; i--) { int emptyCount = 0; for (int j = 0; j < 8; j++) { if (pieces[i, j] is null) { emptyCount++; } else { if (emptyCount > 0) { piecesBuilder.Append(emptyCount); emptyCount = 0; } piecesBuilder.Append(pieces[i, j].ToFenChar()); } } if (emptyCount > 0) { piecesBuilder.Append(emptyCount); } if (i - 1 >= 0) { piecesBuilder.Append('/'); } } StringBuilder castlesBuilder = new(); if (CastleWK) { castlesBuilder.Append('K'); } if (CastleWQ) { castlesBuilder.Append('Q'); } if (CastleBK) { castlesBuilder.Append('k'); } if (CastleBQ) { castlesBuilder.Append('q'); } if (castlesBuilder.Length == 0) { castlesBuilder.Append('-'); } string enPasBuilder; if (EnPassant.HasValue) { enPasBuilder = EnPassant.ToString(); } else { enPasBuilder = "-"; } return(string.Join(' ', piecesBuilder, Turn.AsChar, castlesBuilder, enPasBuilder, HalfMoves, FullMoves)); }
public void Process(char c, ref IProcess step) { switch (c) { case 'K': if (this.result.WhiteCanCastleKingside || this.foundHyphen) { this.result.Error = true; step = null; } this.result.WhiteCanCastleKingside = true; break; case 'Q': if (this.result.WhiteCanCastleQueenside || this.foundHyphen) { this.result.Error = true; step = null; } this.result.WhiteCanCastleQueenside = true; break; case 'k': if (this.result.BlackCanCastleKingside || this.foundHyphen) { this.result.Error = true; step = null; } this.result.BlackCanCastleKingside = true; break; case 'q': if (this.result.BlackCanCastleQueenside || this.foundHyphen) { this.result.Error = true; step = null; } this.result.BlackCanCastleQueenside = true; break; case '-': if (this.processedChar) { this.result.Error = true; step = null; } this.foundHyphen = true; break; case ' ': step = new EnPassant(this.result); break; default: // Unexpected char. this.result.Error = true; step = null; break; } this.processedChar = true; }