/// <summary> /// Adds a capture to the capture set. /// </summary> /// <param name="capturePosition"></param> /// <param name="enemyBoardState"></param> protected void AddCaptureToCaptureSet(ChessPosition capturePosition, IBoardState enemyBoardState) { if (enemyBoardState.Contains(capturePosition)) { CaptureSet.Add(capturePosition); } }
public ActionResult <IEnumerable <string> > GetPossiblePositions(string position, int moves = 1) { var inputPattern = @"^\w\d$"; Regex r = new Regex(inputPattern, RegexOptions.IgnoreCase); var validMoves = moves > 0 && moves <= 2; if (!r.Match(position).Success || !validMoves) { return(BadRequest("Invalid input")); } var chessPosition = new ChessPosition(position); if (!chessPosition.Valid) { return(BadRequest("Invalid chess position")); } var boardKnightPredictor = new BoardKnightPredictor(); IEnumerable <ChessPosition> positions = moves == 1 ? boardKnightPredictor.GetPossiblePositions(chessPosition) : boardKnightPredictor.GetPossiblePositionsWith2Moves(chessPosition); var result = positions.Select(x => x.AlgebraicNotation); return(Ok(result)); }
/// <summary> /// Make an additional special move from the latest snapshot (Without generating the next snapshot) /// </summary> /// <param name="specialRule">The special rule used</param> /// <param name="position">The specified piece's data</param> /// <param name="resultPositions">The returning result of positions</param> /// <returns> /// TRUE if move is valid. /// </returns> private bool AdditionalMove(ChessPieceSpecialRule specialRule, ChessPosition position, out ChessPosition[] resultPositions) { resultPositions = null; if (specialRule == ChessPieceSpecialRule.CastlingLeft || specialRule == ChessPieceSpecialRule.CastlingRight) { int xFromCoord = position.coord.x; int xToCoord = position.coord.x; if (specialRule == ChessPieceSpecialRule.CastlingLeft) { xFromCoord = 0; xToCoord -= 1; } else if (specialRule == ChessPieceSpecialRule.CastlingRight) { xFromCoord = ChessSettings.boardSize - 1; xToCoord += 1; } ChessCoordinate rookFromCoord = new ChessCoordinate(xFromCoord, position.coord.y); ChessCoordinate rookToCoord = new ChessCoordinate(xToCoord, position.coord.y); return(Move(rookFromCoord, rookToCoord, out resultPositions)); } return(true); }
private void testCatchDraws() { // simulate for white and black side for each ally and target chess pieces for (int colorValues = 0; colorValues < 4; colorValues++) { var allyColor = (ChessColor)(colorValues / 2); var targetColor = (ChessColor)(colorValues % 2); // check for all rows on the chess board for (int rowDiff = 0; rowDiff < 6; rowDiff++) { int allyRow = (allyColor == ChessColor.White) ? (1 + rowDiff) : (6 - rowDiff); int nextRow = (allyColor == ChessColor.White) ? (allyRow + 1) : (allyRow - 1); // get the column where the peasant is moving foreward for (int allyCol = 0; allyCol < 8; allyCol++) { var oldPos = new ChessPosition(allyRow, allyCol); for (int targetColMiddle = 1; targetColMiddle < 7; targetColMiddle++) { var targetPosLeft = new ChessPosition(nextRow, targetColMiddle - 1); var targetPosRight = new ChessPosition(nextRow, targetColMiddle + 1); var allyPeasant = new ChessPiece(ChessPieceType.Peasant, allyColor, true); var enemyPeasant = new ChessPiece(ChessPieceType.Peasant, targetColor, true); int kingsRow = (allyColor == ChessColor.White) ? 0 : 7; var pieces = new List <ChessPieceAtPos>() { new ChessPieceAtPos(oldPos, allyPeasant), new ChessPieceAtPos(targetPosLeft, enemyPeasant), new ChessPieceAtPos(targetPosRight, enemyPeasant), new ChessPieceAtPos(new ChessPosition(kingsRow, 0), new ChessPiece(ChessPieceType.King, ChessColor.White, false)), new ChessPieceAtPos(new ChessPosition(kingsRow, 7), new ChessPiece(ChessPieceType.King, ChessColor.Black, false)), }; //output.WriteLine($"current constellation: allyColor={allyColor}, targetColor={targetColor}, allyRow={allyRow}, allyCol={allyCol}, targetColMiddle={targetColMiddle}"); IChessBoard board = new ChessBoard(pieces); var catchDraws = ChessDrawGenerator.Instance.GetDraws(board, oldPos, null, true).Where(x => x.OldPosition.Column != x.NewPosition.Column); int expectedCatchDrawsCount = (targetColor == allyColor) ? 0 : (targetColMiddle == allyCol) ? 2 : ((Math.Abs(targetColMiddle - allyCol) == 2) ? 1 : 0); expectedCatchDrawsCount = ((rowDiff == 5) ? 4 : 1) * expectedCatchDrawsCount; Assert.True(catchDraws.Count() == expectedCatchDrawsCount); // check if the draws are correctly applied to the chess board foreach (var draw in catchDraws) { board = new ChessBoard(pieces); board = board.ApplyDraw(draw); var pieceCmp = new ChessPiece(ChessPieceType.Peasant, allyColor, true); Assert.True(!board.IsCapturedAt(draw.OldPosition) && (board.GetPieceAt(draw.NewPosition) == pieceCmp || (rowDiff == 5))); } } } } } }
public ChessPosition( ChessPosition chessPos ) { this.posType = chessPos.posType; this.nRank = chessPos.nRank; this.nPile = chessPos.nPile; this.pos = chessPos.pos; }
public override void ClickAt(ChessPosition position) { if (SelectedPiecePosition == position) { Select(null); } var piece = game.Board[position]; if (piece?.Color == game.WhoseTurn) { Select(position); } if (SelectedPiecePosition != null && piece != null) { if (game.TryMakeMove(SelectedPiecePosition, position)) { // maybe increment move number or something else SelectedPiecePosition = null; StateChanged?.Invoke(); } else { Select(null); } } }
/// <summary> /// Will update whether the Pawn is able to be promoted or captured via En Passant. /// </summary> /// <param name="position"></param> public override void MoveTo(ChessPosition position) { if (!HasMoved) { IChessPieceMover cpm = ModelLocator.ChessPieceMover; bool moveIsTwoForward = Color == ChessColor.White ? position == cpm.North(cpm.North(Location)) : position == cpm.South(cpm.South(Location)); IsCapturableByEnPassant = moveIsTwoForward; } else { IsCapturableByEnPassant = false; } base.MoveTo(position); ChessPosition promotionRank = Color == ChessColor.White ? ChessPosition.Rank8 : ChessPosition.Rank1; if ((position & promotionRank) == position) { IsPromotable = true; } }
public Pawn(ChessPosition initialPosition, ChessColor color) : base(initialPosition, color) { Value = 1; IsCapturableByEnPassant = false; IsPromotable = false; }
public static List <ChessPosition> knightmove(ChessPosition pos_start, ChessPiece[,] Map_Chess) { List <ChessPosition> list = new List <ChessPosition>(); ChessColor color = Map_Chess[pos_start.X, pos_start.Y].Color; if (color == ChessColor.Die) { return(list); } ChessEnum type = Map_Chess[pos_start.X, pos_start.Y].ChessType; for (int val = 0; val < 8; val++) { int _x = pos_start.X + maps_knight[val, 0]; int _y = pos_start.Y + maps_knight[val, 1]; if (_x < 0 | _x > 7 | _y < 0 | _y > 7) { continue; //outside } if (Map_Chess[_x, _y].Color == color) { continue; // if ally -> can't move } else { list.Add(new ChessPosition() { X = _x, Y = _y }); } } return(list); }
public override List <ChessPosition> ListCanMove(ChessPosition pos_start, ChessPiece[,] Map_Chess) { List <ChessPosition> list = new List <ChessPosition>(); list.AddRange(knightmove(pos_start, Map_Chess)); return(list); }
public void BishopDrawsTest() { // test draws of white / black bishops for (int colorValue = 0; colorValue < 2; colorValue++) { var allyColor = (ChessColor)colorValue; var enemyColor = allyColor.Opponent(); var bishopPos = new ChessPosition(4, 4); var pieces = new List <ChessPieceAtPos>() { new ChessPieceAtPos(bishopPos, new ChessPiece(ChessPieceType.Bishop, allyColor, true)), new ChessPieceAtPos(new ChessPosition(0, 0), new ChessPiece(ChessPieceType.Queen, allyColor, true)), new ChessPieceAtPos(new ChessPosition(7, 7), new ChessPiece(ChessPieceType.Knight, allyColor, true)), new ChessPieceAtPos(new ChessPosition(0, 6), new ChessPiece(ChessPieceType.King, allyColor, true)), new ChessPieceAtPos(new ChessPosition(6, 2), new ChessPiece(ChessPieceType.Bishop, enemyColor, true)), new ChessPieceAtPos(new ChessPosition(1, 7), new ChessPiece(ChessPieceType.Knight, enemyColor, true)), new ChessPieceAtPos(new ChessPosition(7, 4), new ChessPiece(ChessPieceType.King, enemyColor, true)), }; IChessBoard board = new ChessBitboard(new ChessBoard(pieces)); var draws = ((ChessBitboard)board).GetAllDraws(allyColor, null, true).Where(x => x.OldPosition == bishopPos).ToArray(); Assert.True(draws.Count() == 10); // check if the draws are correctly applied to the chess board foreach (var draw in draws) { var simBoard = board.ApplyDraw(draw); var pieceCmp = new ChessPiece(ChessPieceType.Bishop, allyColor, true); Assert.True(!simBoard.IsCapturedAt(draw.OldPosition) && simBoard.GetPieceAt(draw.NewPosition) == pieceCmp); } } }
/// <summary> /// Generates all legal <see cref="IPawn" /> moves /// </summary> /// <param name="boardState"></param> public override void GenerateMoves(IBoardState boardState) { IChessPieceMover cpm = ModelLocator.ChessPieceMover; MoveSet.Clear(); ChessPosition oneSpaceFromLocation = Color == ChessColor.White ? cpm.North(Location) : cpm.South(Location); bool oneSpaceMoveIsLegal = !boardState.Contains(oneSpaceFromLocation); if (oneSpaceMoveIsLegal) { MoveSet.Add(oneSpaceFromLocation); } else { return; } ChessPosition twoSpaceFromLocation = Color == ChessColor.White ? cpm.North(oneSpaceFromLocation) : cpm.South(oneSpaceFromLocation); bool twoSpaceMoveIsLegal = !boardState.Contains(twoSpaceFromLocation); if (!HasMoved && twoSpaceMoveIsLegal) { MoveSet.Add(Color == ChessColor.White ? cpm.North(cpm.North(Location)) : cpm.South(cpm.South(Location))); } }
public void clickItemOrChess() { GameObject obj = this.GetComponent <Button>().gameObject; ChessPosition pos = View._instance.GetClickItemPos(obj); ViewManager._instance.ChessGoStepView(pos); }
public Vector3 GetWorldPosition(ChessPosition chessPosition) { return(new Vector3( bottomLeftSquareCenter.x + (chessPosition.column * squareSize), bottomLeftSquareCenter.y + (chessPosition.row * squareSize), 0 )); }
public void Should_GenerateOneMoveNorthEast() { // execute ChessPosition position = _mover.NorthEast(ChessPosition.B1); // verify Assert.AreEqual(ChessPosition.C2, position); }
public void Should_GenerateOneMoveSouthWest() { // execute ChessPosition position = _mover.SouthWest(ChessPosition.B2); // verify Assert.AreEqual(ChessPosition.A1, position); }
public void TestMethod1() { ChessPosition pos = new ChessPosition(); Assert.AreEqual(pos.Board[0, 0], ChessPiece.WHITE_ROOK); string text = File.ReadAllText("Output.pgn"); ChessGame result = PGNInterface.LoadFromPGN(text); }
/// <summary> /// Moves a piece without considering legality of move. /// </summary> /// <param name="position"></param> public void CaptureAt(ChessPosition position) { Location = position; if (!HasMoved) { HasMoved = true; } }
protected Piece(ChessPosition initialPosition, ChessColor color) { Location = initialPosition; Color = color; MoveSet = ModelLocator.BoardState; CaptureSet = ModelLocator.BoardState; ThreatenSet = ModelLocator.BoardState; }
private ChessPosition GenerateCaptureWest(IBoardState enemyPieces, IChessPieceMover cpm) { ChessPosition potentialCapture = Color == ChessColor.White ? cpm.NorthWest(Location) : cpm.SouthWest(Location); bool isPieceAtCaptureLocation = enemyPieces.Contains(potentialCapture); return(isPieceAtCaptureLocation ? potentialCapture : ChessPosition.None); }
/// <summary> /// Moves a piece without considering legality of move. /// </summary> /// <param name="position"></param> public virtual void MoveTo(ChessPosition position) { Location = position; if (!HasMoved) { HasMoved = true; } }
public void AiLastMovePicSet(ChessPosition pos) { GameObject _aiMove = PosGetChess(pos.x, pos.y); _aiMoveTipPic.transform.SetParent(_aiMove.transform); _aiMoveTipPic.transform.localPosition = Vector3.zero; _aiMoveTipPic.transform.SetParent(_nextTips.transform); }
/// <summary> /// Check whether the special rule is matching for the specific piece /// </summary> /// <param name="specialRule">The special rule used</param> /// <param name="position">The specified piece's data</param> /// <param name="from">The specified piece's coordinate</param> /// <param name="to">The destination's coordinate</param> /// <returns> /// TRUE if move is valid regarding special rule. /// </returns> public bool IsValidSpecialRule(ChessPieceSpecialRule specialRule, ChessPosition position, ChessCoordinate from, ChessCoordinate to) { if (specialRule == ChessPieceSpecialRule.None) { return(true); } // Pawn - Move 2 square when starting from initial position if (specialRule == ChessPieceSpecialRule.Pawn2Squares) { if (position.hasMoved) { return(false); } return(true); } if (specialRule == ChessPieceSpecialRule.CastlingLeft || specialRule == ChessPieceSpecialRule.CastlingRight) { if (position.hasMoved) { return(false); } int xCoord = position.coord.x; if (specialRule == ChessPieceSpecialRule.CastlingLeft) { xCoord = 0; } else if (specialRule == ChessPieceSpecialRule.CastlingRight) { xCoord = ChessSettings.boardSize - 1; } int rookCoord = new ChessCoordinate(xCoord, position.coord.y).ToArrayCoord(); if (!piecesDict.ContainsKey(rookCoord)) { return(false); } if (!piecesDict[rookCoord].Type.IsRook()) { return(false); } if (piecesDict[rookCoord].HasMoved) { return(false); } return(true); } return(false); }
public bool Contains(ChessPosition position) { if (position == ChessPosition.None) { return(true); } return(_occupiedSquares.Contains(position) && _bitboard.IsLocationOccupied(position)); }
private static bool IsPowerOfTwo(ChessPosition x) { // first operand is obvious... are we dealing with zero? // second operand is basically... if it's a power of two, then there is a single 1 and rest 0. // subtract 1 and we have a single 0 and rest 1 // thus, (x & (x - 1)) always equals 0 if x is a power of two. return(x != 0 && (x & (x - 1)) == 0); }
public override List <ChessPosition> ListCanMove(ChessPosition pos_start, ChessPiece[,] Map_Chess) { List <ChessPosition> list = new List <ChessPosition>(); list.AddRange(Diagonal(pos_start, Map_Chess)); list.AddRange(Horizontal(pos_start, Map_Chess)); list.AddRange(Vertical(pos_start, Map_Chess)); return(list); }
public void moveChess(ChessPosition posFrom, ChessPosition posTo) { GameObject FromObj = PosGetChess(posFrom.x, posFrom.y); GameObject ToObj = PosGetChess(posTo.x, posTo.y); FromObj = FromObj.transform.GetChild(0).gameObject; FromObj.transform.SetParent(ToObj.transform); FromObj.transform.localPosition = Vector3.zero; }
public void PlayerSelectPicSet(ChessPosition pos) { if (_redSelectPic != null) { _redSelectPic.SetActive(false); } _redSelectPic = PosGetChess(pos.x, pos.y).transform.GetChild(0).GetChild(0).gameObject; _redSelectPic.SetActive(true); }
public ChessBoardSquare( ChessPiece piece, ParticleSystem moveablePSystem, int nPile, int nRank ) { this.position = new ChessPosition( nRank, nPile ); this.piece = piece; if( this.piece != null ) this.piece.SetPosition( this.position.Get3DPosition() ); SetMovableEffect( moveablePSystem ); }
public void Should_Return_RookEndingPosition_For_WhiteQueenSideCastle() { var king = ModelLocator.ChessPieceFactory.CreateKing(ChessPosition.E1, ChessColor.White) as IKing; var rook = ModelLocator.ChessPieceFactory.CreateRook(ChessPosition.A1, ChessColor.White) as IRook; ChessPosition actualEndingPosition = ModelLocator.CastlingHelper.GetEndingPositionForCastlingRook(king, rook); Assert.AreEqual(ChessPosition.D1, actualEndingPosition); }
public void Should_Return_GivenRookLocation_For_InvalidKingLocation() { var king = ModelLocator.ChessPieceFactory.CreateKing(ChessPosition.E2, ChessColor.White) as IKing; var rook = ModelLocator.ChessPieceFactory.CreateRook(ChessPosition.A1, ChessColor.White) as IRook; ChessPosition actualEndingPosition = ModelLocator.CastlingHelper.GetEndingPositionForCastlingRook(king, rook); Assert.AreEqual(rook.Location, actualEndingPosition); }
public void Should_Return_RookEndingPosition_For_BlackKingSideCastle() { var king = ModelLocator.ChessPieceFactory.CreateKing(ChessPosition.E8, ChessColor.Black) as IKing; var rook = ModelLocator.ChessPieceFactory.CreateRook(ChessPosition.H8, ChessColor.Black) as IRook; ChessPosition actualEndingPosition = ModelLocator.CastlingHelper.GetEndingPositionForCastlingRook(king, rook); Assert.AreEqual(ChessPosition.F8, actualEndingPosition); }
public void SetMovableEffect( ParticleSystem movablePSystem ) { if( position.IsInvalidPos() ) return; this.movablePSystem = movablePSystem; this.movablePSystem.Stop(); Vector3 pos = Vector3.zero; pos.x = position.nRank - 3.5f; pos.y = 0.01f; pos.z = position.nPile - 3.5f; Quaternion rot = Quaternion.identity; this.movablePSystem.gameObject.transform.position = pos; this.movablePSystem.gameObject.transform.rotation = rot; this.movablePSystem.gameObject.renderer.material.SetColor( "_Color", Color.blue ); }
// leap move public static bool GetLeapMoveList( ChessBoard board, ChessBoardSquare selSquare, List<sMove> listRetBoardPos, MoveDirectionType moveDirection ) { ChessPosition srcPos = selSquare.position; PlayerSide srcPlayerSide = selSquare.piece.playerSide; ChessPosition movePos = new ChessPosition(srcPos.pos); // all(radial) direction one move int nTempRank = 0, nTempPile = 0; GetNextDirectionRankPile( ref nTempRank, ref nTempPile, moveDirection, 0 ); movePos.SetPosition( srcPos ); bool bValidMove = movePos.MovePosition( nTempRank, nTempPile ); if( bValidMove ) { ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank]; // normal move if( trgSquare.IsBlank() ) { sMove move = new sMove(); // normal move move.moveType = MoveType.eNormal_Move; move.srcSquare = selSquare; move.trgSquare = trgSquare; listRetBoardPos.Add( move ); return true; } // capture move else if( trgSquare.IsEnemy( srcPlayerSide ) ) { sMove move = new sMove(); // normal move move.moveType = MoveType.eCapture_Move; move.srcSquare = selSquare; move.trgSquare = trgSquare; listRetBoardPos.Add( move ); return true; } // our piece else { return false; } } return false; }
// stright line move public static bool GetStraightMoveList( ChessBoard board, ChessBoardSquare selSquare, List<sMove> listRetBoardPos, MoveDirectionType moveDirection ) { ChessPosition srcPos = selSquare.position; PlayerSide srcPlayerSide = selSquare.piece.playerSide; ChessPosition movePos = new ChessPosition(srcPos.pos); // all(radial) direction one move int nTempRank, nTempPile; int nIterCount; nIterCount = GetNumDirectionIterCount( movePos.nRank, movePos.nPile, moveDirection ); //UnityEngine.Debug.LogError( "GetStraightMoveList() - nIterCount = " + nIterCount + " movePos.nRank, movePos.nPile " + movePos.nRank + " " + movePos.nPile ); for( int nCurrIter=1; nCurrIter<=nIterCount; nCurrIter++ ) { nTempRank = 0; nTempPile = 0; GetNextDirectionRankPile( ref nTempRank, ref nTempPile, moveDirection, nCurrIter ); //UnityEngine.Debug.LogError( "GetStraightMoveList() - nTempRank, nTempPile " + nTempRank + " " + nTempPile ); movePos.SetPosition( srcPos ); bool bValidMove = movePos.MovePosition( nTempRank, nTempPile ); if( bValidMove ) { UnityEngine.Debug.LogError( "GetStraightMoveList() - bValidMove - nTempRank, nTempPile " + nTempRank + " " + nTempPile ); ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank]; // normal move if( trgSquare.IsBlank() ) { sMove move = new sMove(); // normal move move.moveType = MoveType.eNormal_Move; move.srcSquare = selSquare; move.trgSquare = trgSquare; listRetBoardPos.Add( move ); } // capture move else if( trgSquare.IsEnemy( srcPlayerSide ) ) { sMove move = new sMove(); // normal move move.moveType = MoveType.eCapture_Move; move.srcSquare = selSquare; move.trgSquare = trgSquare; listRetBoardPos.Add( move ); return true; } // our piece else { if( nCurrIter > 1 ) return true; return false; } } } return false; }
public ChessBoardSquare() { piece = null; position = new ChessPosition(-1, -1); movablePSystem = null; }
public static bool GetKingMoveList( ChessBoard board, ChessBoardSquare selSquare, List<sMove> listRetBoardPos ) { ChessPosition srcPos = selSquare.position; PlayerSide srcPlayerSide = selSquare.piece.playerSide; ChessPosition movePos = new ChessPosition(srcPos.pos); // all(radial) direction one move int nTempRank, nTempPile; for( int nMovePile=-1; nMovePile<=1; nMovePile++ ) { for( int nMoveRank=-1; nMoveRank<=1; nMoveRank++ ) { nTempRank = nMoveRank; nTempPile = nMovePile; movePos.SetPosition( srcPos ); bool bValidMove = movePos.MovePosition( nTempRank, nTempPile ); if( bValidMove ) { ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank]; // normal move if( trgSquare.IsBlank() ) { sMove move = new sMove(); // normal move move.moveType = MoveType.eNormal_Move; move.srcSquare = selSquare; move.trgSquare = trgSquare; listRetBoardPos.Add( move ); } // capture move else if( trgSquare.IsEnemy( srcPlayerSide ) ) { sMove move = new sMove(); // normal move move.moveType = MoveType.eCapture_Move; move.srcSquare = selSquare; move.trgSquare = trgSquare; listRetBoardPos.Add( move ); } } } } // castling move // king side castling movePos.SetPosition( srcPos ); nTempRank = 2; nTempPile = 0; bool bValidCastlingMove = movePos.MovePosition( nTempRank, nTempPile ); if( bValidCastlingMove ) { ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank]; if( trgSquare.IsBlank() ) { // position check castling bool bCalstling = false; if( srcPlayerSide == PlayerSide.e_White ) { if( board.currCastlingState.CastlingWKSide == CastlingState.eCastling_Enable_State ) { bCalstling = true; } } else { if( board.currCastlingState.CastlingBKSide == CastlingState.eCastling_Enable_State ) { bCalstling = true; } } if( bCalstling ) { // check look square blank nTempRank = -1; nTempPile = 0; ChessPosition moveLookPos = new ChessPosition(movePos.pos); bValidCastlingMove = moveLookPos.MovePosition( nTempRank, nTempPile ); if( bValidCastlingMove ) { ChessBoardSquare lookTrgSquare = board.aBoardSquare[moveLookPos.nPile, moveLookPos.nRank]; if( lookTrgSquare.IsBlank() ) { sMove move = new sMove(); MoveType castlingSideType = srcPlayerSide == PlayerSide.e_White ? MoveType.eCastling_White_KingSide_Move : MoveType.eCastling_Black_KingSide_Move; move.moveType = MoveType.eCastling_Move | castlingSideType; move.srcSquare = selSquare; move.trgSquare = trgSquare; listRetBoardPos.Add( move ); } } } } } // queen side castling movePos.SetPosition( srcPos ); nTempRank = -2; nTempPile = 0; bValidCastlingMove = movePos.MovePosition( nTempRank, nTempPile ); if( bValidCastlingMove ) { ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank]; if( trgSquare.IsBlank() ) { // position check castling bool bCalstling = false; if( srcPlayerSide == PlayerSide.e_White ) { if( board.currCastlingState.CastlingWQSide == CastlingState.eCastling_Enable_State ) { bCalstling = true; } } else { if( board.currCastlingState.CastlingBQSide == CastlingState.eCastling_Enable_State ) { bCalstling = true; } } if( bCalstling ) { // check look square blank nTempRank = 1; nTempPile = 0; ChessPosition moveLookPos = new ChessPosition(movePos.pos); bValidCastlingMove = moveLookPos.MovePosition( nTempRank, nTempPile ); if( bValidCastlingMove ) { ChessBoardSquare lookTrgSquare = board.aBoardSquare[moveLookPos.nPile, moveLookPos.nRank]; if( lookTrgSquare.IsBlank() ) { sMove move = new sMove(); MoveType castlingSideType = srcPlayerSide == PlayerSide.e_White ? MoveType.eCastling_White_QueenSide_Move : MoveType.eCastling_Black_QueenSide_Move; move.moveType = MoveType.eCastling_Move | castlingSideType; move.srcSquare = selSquare; move.trgSquare = trgSquare; listRetBoardPos.Add( move ); } } } } } return true; }
public bool SetPosition( ChessPosition chessPos ) { this.posType = chessPos.posType; this.nRank = chessPos.nRank; this.nPile = chessPos.nPile; this.pos = chessPos.pos; if( chessPos.posType == BoardPositionType.eNone ) { return false; } return true; }
public bool Equals(ChessPosition rho) { // Return true if the fields match: return (pos == rho.pos);// && (posType == rho.posType) && (nRank == rho.nRank) && (nPile == rho.nPile)); }
public static bool GetPawnMoveList( ChessBoard board, ChessBoardSquare selSquare, List<sMove> listRetBoardPos ) { //UnityEngine.Debug.LogError( "GetPawnMoveList - start" + " " + piece.position + " " + piece.playerSide ); ChessPosition srcPos = selSquare.position; PlayerSide srcPlayerSide = selSquare.piece.playerSide; ChessPosition movePos = new ChessPosition(srcPos.pos); // pure move // pure move - one pile move int nTempRank, nTempPile; nTempRank = 0; nTempPile = srcPlayerSide == PlayerSide.e_White ? 1 : -1; bool bValidMove = movePos.MovePosition( nTempRank, nTempPile ); if( bValidMove ) { ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank]; // check already existing piece if( trgSquare.IsBlank() ) { // normal move sMove move = new sMove(); move.moveType = MoveType.eNormal_Move | MoveType.ePawn_Move; // promote move if( srcPlayerSide == PlayerSide.e_White ) { if( movePos.IsTopBoundary() ) move.moveType |= MoveType.ePromote_Move; } else { if( movePos.IsBottomBoundary() ) move.moveType |= MoveType.ePromote_Move; } move.srcSquare = selSquare; move.trgSquare = trgSquare; listRetBoardPos.Add( move ); } } // pure move - two pile move if( ( srcPos.nPile == 1 && srcPlayerSide == PlayerSide.e_White ) || ( srcPos.nPile == 6 && srcPlayerSide == PlayerSide.e_Black ) ) { movePos.SetPosition( srcPos ); nTempRank = 0; nTempPile = srcPlayerSide == PlayerSide.e_White ? 2 : -2; bValidMove = movePos.MovePosition( nTempRank, nTempPile ); if( bValidMove ) { ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank]; if( trgSquare.IsBlank() ) { sMove move = new sMove(); move.moveType = MoveType.eNormal_Move | MoveType.ePawn_Move | MoveType.ePawn_Two_Move ; move.srcSquare = selSquare; move.trgSquare = trgSquare; // en passant target move check move.enPassantTargetSquare.Rank = movePos.nRank; move.enPassantTargetSquare.Pile = movePos.nPile; move.enPassantTargetSquare.Available = true; listRetBoardPos.Add( move ); } } } // capture move // left diagonal capture // check left boundary movePos.SetPosition( srcPos ); nTempRank = -1; nTempPile = srcPlayerSide == PlayerSide.e_White ? 1 : -1; bValidMove = movePos.MovePosition( nTempRank, nTempPile ); if( bValidMove ) { ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank]; if( trgSquare.IsEnemy( srcPlayerSide ) ) { sMove move = new sMove(); // capture move move.moveType = MoveType.eCapture_Move | MoveType.ePawn_Move; // promote move if( srcPlayerSide == PlayerSide.e_White ) { if( movePos.IsTopBoundary() ) move.moveType |= MoveType.ePromote_Move; } else { if( movePos.IsBottomBoundary() ) move.moveType |= MoveType.ePromote_Move; } move.srcSquare = selSquare; move.trgSquare = trgSquare; listRetBoardPos.Add( move ); } } // right diagonal capture // check right boundary movePos.SetPosition( srcPos ); nTempRank = 1; nTempPile = srcPlayerSide == PlayerSide.e_White ? 1 : -1; bValidMove = movePos.MovePosition( nTempRank, nTempPile ); if( bValidMove ) { ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank]; if( trgSquare.IsEnemy( srcPlayerSide ) ) { sMove move = new sMove(); // capture move move.moveType = MoveType.eCapture_Move | MoveType.ePawn_Move; // promote move if( srcPlayerSide == PlayerSide.e_White ) { if( movePos.IsTopBoundary() ) move.moveType |= MoveType.ePromote_Move; } else { if( movePos.IsBottomBoundary() ) move.moveType |= MoveType.ePromote_Move; } move.srcSquare = selSquare; move.trgSquare = trgSquare; listRetBoardPos.Add( move ); } } // en-passant move // left en passant move check movePos.SetPosition( srcPos ); nTempRank = -1; nTempPile = 0; bValidMove = movePos.MovePosition( nTempRank, nTempPile ); if( bValidMove ) { ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank]; if( trgSquare.IsEnemy( srcPlayerSide ) && trgSquare.piece.bEnPassantCapture ) { if( srcPlayerSide == PlayerSide.e_White ) bValidMove = movePos.MovePosition( 0, 1 ); else bValidMove = movePos.MovePosition( 0, -1 ); if( bValidMove ) { ChessBoardSquare finalTrgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank]; if( finalTrgSquare.IsBlank() ) { sMove move = new sMove(); // capture move move.moveType = MoveType.eEnPassan_Move | MoveType.ePawn_Move; move.srcSquare = selSquare; move.trgSquare = finalTrgSquare; listRetBoardPos.Add( move ); } } } } // right en passant move check movePos.SetPosition( srcPos ); nTempRank = 1; nTempPile = 0; bValidMove = movePos.MovePosition( nTempRank, nTempPile ); if( bValidMove ) { ChessBoardSquare trgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank]; if( trgSquare.IsEnemy( srcPlayerSide ) && trgSquare.piece.bEnPassantCapture ) { if( srcPlayerSide == PlayerSide.e_White ) bValidMove = movePos.MovePosition( 0, 1 ); else bValidMove = movePos.MovePosition( 0, -1 ); if( bValidMove ) { ChessBoardSquare finalTrgSquare = board.aBoardSquare[movePos.nPile, movePos.nRank]; if( finalTrgSquare.IsBlank() ) { sMove move = new sMove(); // capture move move.moveType = MoveType.eEnPassan_Move | MoveType.ePawn_Move; move.srcSquare = selSquare; move.trgSquare = finalTrgSquare; listRetBoardPos.Add( move ); } } } } return true; }