public void Refresh() { this.Turn = $"{this.Game.Turn} ({this.Game.TurnColour})"; this.OnPropertyChanged(() => this.Turn); this.State = this.Game.Board.State.ToString(); this.OnPropertyChanged(() => this.State); this.Move = this.Game.Board.ExecutingMove; this.OnPropertyChanged(() => this.Move); int i = 0; for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { Square square = this.Squares[i++]; ChessBoardSquare sq = this.Game.Board[x, y]; square.Symbol = Convert.ToString(sq.OccupiedBy?.Symbol); square.BorderColour = sq.IsUnderAttack ? Brushes.Red : Brushes.Transparent; } } }
public void PushEPawn() { var move = new ChessMove { StartSquare = ChessBoardSquare.GetAN(5, 2), EndSquare = ChessBoardSquare.GetAN(5, 4), Piece = ChessPiece.WhitePawn }; Assert.AreEqual("Pe2-e4", move.ToString()); }
public bool Equals(ChessBoardSquare p) { // If parameter is null return false: if ((object)p == null) { return false; } // Return true if the fields match: return (position == p.position) && ( piece == p.piece ); }
public void PromotionCaptureCheckmate() { var move = new ChessMove { StartSquare = ChessBoardSquare.GetAN(1, 7), EndSquare = ChessBoardSquare.GetAN(2, 8), WasPieceCaptured = true, PawnPromotedTo = PromotionChessPiece.Queen, IsCheckmateMove = true, Piece = ChessPiece.WhitePawn }; Assert.AreEqual("Pa7xb8=q#", move.ToString()); }
public static bool GetBishopMoveList( ChessBoard board, ChessBoardSquare selSquare, List<sMove> listRetBoardPos ) { // left-up - diagonal GetStraightMoveList( board, selSquare, listRetBoardPos, MoveDirectionType.eDirection_Move_LeftUp_Diagonal ); // left-down - diagonal GetStraightMoveList( board, selSquare, listRetBoardPos, MoveDirectionType.eDirection_Move_LeftDown_Diagonal ); // right-up - diagonal GetStraightMoveList( board, selSquare, listRetBoardPos, MoveDirectionType.eDirection_Move_RightUp_Diagonal ); // right-down - diagonal GetStraightMoveList( board, selSquare, listRetBoardPos, MoveDirectionType.eDirection_Move_RightDown_Diagonal ); if( listRetBoardPos.Count > 0 ) return true; return false; }
public static bool GetKnightMoveList( ChessBoard board, ChessBoardSquare selSquare, List<sMove> listRetBoardPos ) { // left-up - steep diagonal GetLeapMoveList( board, selSquare, listRetBoardPos, MoveDirectionType.eDirection_Move_Steep_LeftUp_Leap ); // left-down - steep diagonal GetLeapMoveList( board, selSquare, listRetBoardPos, MoveDirectionType.eDirection_Move_Steep_LeftDown_Leap ); // right-up - steep diagonal GetLeapMoveList( board, selSquare, listRetBoardPos, MoveDirectionType.eDirection_Move_Steep_RightUp_Leap ); // right-down - steep diagonal GetLeapMoveList( board, selSquare, listRetBoardPos, MoveDirectionType.eDirection_Move_Steep_RightDown_Leap ); // left-up - non-steep diagonal GetLeapMoveList( board, selSquare, listRetBoardPos, MoveDirectionType.eDirection_Move_NonSteep_LeftUp_Leap ); // left-down - on-steep diagonal GetLeapMoveList( board, selSquare, listRetBoardPos, MoveDirectionType.eDirection_Move_NonSteep_LeftDown_Leap ); // right-up - on-steep diagonal GetLeapMoveList( board, selSquare, listRetBoardPos, MoveDirectionType.eDirection_Move_NonSteep_RightUp_Leap ); // right-down - on-steep diagonal GetLeapMoveList( board, selSquare, listRetBoardPos, MoveDirectionType.eDirection_Move_NonSteep_RightDown_Leap ); if( listRetBoardPos.Count > 0 ) return true; return false; }
// AI Move public bool AIMoveTo( ChessBoardSquare srcSquare, ChessBoardSquare trgSquare ) { if( CurrTurn != UserPlayerSide ) { //UnityEngine.Debug.LogError( "AIMoveTo() - current turn = " + CurrTurn ); if( srcSquare.IsBlank() == false ) { UnityEngine.Debug.LogError( "AIMoveTo() - no blank" ); List<ChessMoveManager.sMove> listAiMovable = new List<ChessMoveManager.sMove>(); bool bMoveList = ChessMoveManager.GetValidateMoveList( this, srcSquare, listAiMovable ); if( bMoveList ) { //UnityEngine.Debug.LogError( "AIMoveTo() - no blank" + " " + bMoveList ); ChessMoveManager.sMove aIMove = new ChessMoveManager.sMove(); if( IsValidAIMove( trgSquare, listAiMovable, aIMove ) ) { //UnityEngine.Debug.LogError( "AIMoveTo() - IsValidAIMove()" ); UpdateMove( aIMove ); return true; } } } } UnityEngine.Debug.LogError( "AIMoveTo() - !!!!" ); return false; }
public void UpdateCastlingState( ChessBoardSquare srcSquare ) { // disable castling state switch( srcSquare.piece.piecePlayerType ) { case PiecePlayerType.eWhite_King: { currCastlingState.CastlingWKSide = CastlingState.eCastling_Disable_State; currCastlingState.CastlingWQSide = CastlingState.eCastling_Disable_State; } break; case PiecePlayerType.eWhite_LookLeft: { currCastlingState.CastlingWQSide = CastlingState.eCastling_Disable_State; } break; case PiecePlayerType.eWhite_LookRight: { currCastlingState.CastlingWKSide = CastlingState.eCastling_Disable_State; } break; case PiecePlayerType.eBlack_King: { currCastlingState.CastlingBKSide = CastlingState.eCastling_Disable_State; currCastlingState.CastlingBQSide = CastlingState.eCastling_Disable_State; } break; case PiecePlayerType.eBlack_LookLeft: { currCastlingState.CastlingBQSide = CastlingState.eCastling_Disable_State; } break; case PiecePlayerType.eBlack_LookRight: { currCastlingState.CastlingBKSide = CastlingState.eCastling_Disable_State; } break; } }
// interface public void Init( BattleChessMain chessMain, Transform[] aPieceRef, ParticleSystem selectPSystemRef, ParticleSystem movablePSystemRef ) { // etc property CurrTurn = PlayerSide.e_White; UserPlayerSide = PlayerSide.e_White; ThinkingTime = 18000; nCurrHalfMove = 0; nCurrTotalMove = 0; Ready = false; // move currMove = new ChessMoveManager.sMove(); currCastlingState = new ChessCastling() { CastlingWKSide = CastlingState.eCastling_Enable_State, CastlingWQSide = CastlingState.eCastling_Enable_State, CastlingBKSide = CastlingState.eCastling_Enable_State, CastlingBQSide = CastlingState.eCastling_Enable_State }; listCurrMovable = new List<ChessMoveManager.sMove>(); // init board // piece list listPiece = new List<ChessPiece>(); aBoardSquare = new ChessBoardSquare[ChessData.nNumPile,ChessData.nNumRank]; ChessPiece currPiece = null; for( int i=0; i<ChessData.nNumPile; i++ ){ for( int j=0; j<ChessData.nNumRank; j++ ){ // movable square effect Particle System ParticleSystem movablePiecePSystem = MonoBehaviour.Instantiate( movablePSystemRef, Vector3.zero, Quaternion.identity ) as ParticleSystem; if( ChessData.aStartPiecePos[i,j] == PiecePlayerType.eNone_Piece ) { aBoardSquare[i,j] = new ChessBoardSquare( null, movablePiecePSystem, i, j ); } else { Vector3 currPos = new Vector3( j - 3.5f, 0.0f, i - 3.5f ); Transform currTransform = aPieceRef[(int)ChessData.aStartPiecePos[i,j]]; Transform currPieceObject = MonoBehaviour.Instantiate( currTransform, currPos, currTransform.rotation ) as Transform; if( i == 0 || i == 1 ) { currPiece = new ChessPiece( currPieceObject.gameObject, PlayerSide.e_White, ChessData.aStartPiecePos[i,j] ); listPiece.Add( currPiece ); aBoardSquare[i,j] = new ChessBoardSquare( currPiece, movablePiecePSystem, i, j ); } else if( i == 6 || i == 7 ) { currPiece = new ChessPiece( currPieceObject.gameObject, PlayerSide.e_Black, ChessData.aStartPiecePos[i,j] ); listPiece.Add( currPiece ); aBoardSquare[i,j] = new ChessBoardSquare( currPiece, movablePiecePSystem, i, j ); } } } } // piece coloar SetWhiteSidePieceColor( Color.white ); SetBlackSidePieceColor( Color.white ); // board material if( chessMain.renderer.materials.Length == 2 ) { matBoard1 = chessMain.renderer.materials[0]; matBoard2 = chessMain.renderer.materials[1]; Color rgbaWhiteBoard, rgbaBlackBoard; rgbaWhiteBoard = new Color( 1.0f, 1.0f, 1.0f, 1.0f ); rgbaBlackBoard = new Color( 0.039f, 0.34f, 0.22f, 1.0f ); SetWhiteSideBoardColor( rgbaWhiteBoard ); SetBlackSideBoardColor( rgbaBlackBoard ); } // particle effect selectSquare = null; selectPiecePSystem = MonoBehaviour.Instantiate( selectPSystemRef, Vector3.zero, Quaternion.identity ) as ParticleSystem; selectPiecePSystem.Stop(); }
public void IsValidSquare_StaticMethod(int file, int rank, string expectedAN) { var anResult = ChessBoardSquare.GetAN(file, rank); Assert.AreEqual(expectedAN, anResult); }
public Form1() { InitializeComponent(); isWhitesTurn = true; isKingInCheck = false; currentHighlight = null; possiblePositions = new List<Position>(); pieceMap = new Dictionary<Position, ChessPiece>(); statusLabel = new ToolStripLabel(); info = null; lastMove = null; // a reference for the last Position. //adding status label to the status strip, cant be added via the designer because this is a static object. statusStrip1.Items.Add(statusLabel); tableLayoutPanel1.RowCount = 8; tableLayoutPanel1.ColumnCount = 8; for (int i = 0; i < tableLayoutPanel1.RowCount; i++) { for (int j = 0; j < tableLayoutPanel1.ColumnCount; j++) { squares[i, j] = new ChessBoardSquare(i, j); tableLayoutPanel1.Controls.Add(squares[i,j],j,i); } } // defining the resource dictionary resourceMap = new Dictionary<ChessPiece, Icon>(); resourceMap.Add(ChessPiece.WhiteRook, WindowsFormsApplication2.Properties.Resources.White_Rook); resourceMap.Add(ChessPiece.WhiteHorse, WindowsFormsApplication2.Properties.Resources.White_Horse); resourceMap.Add(ChessPiece.WhiteBishop, WindowsFormsApplication2.Properties.Resources.White_Bishop); resourceMap.Add(ChessPiece.WhiteKing, WindowsFormsApplication2.Properties.Resources.White_King); resourceMap.Add(ChessPiece.WhiteQueen, WindowsFormsApplication2.Properties.Resources.White_Queen); resourceMap.Add(ChessPiece.WhitePawn, WindowsFormsApplication2.Properties.Resources.White_Pawn); resourceMap.Add(ChessPiece.BlackRook, WindowsFormsApplication2.Properties.Resources.Black_Rook); resourceMap.Add(ChessPiece.BlackHorse, WindowsFormsApplication2.Properties.Resources.Black_Horse); resourceMap.Add(ChessPiece.BlackBishop, WindowsFormsApplication2.Properties.Resources.Black_Bishop); resourceMap.Add(ChessPiece.BlackKing, WindowsFormsApplication2.Properties.Resources.Black_King); resourceMap.Add(ChessPiece.BlackQueen, WindowsFormsApplication2.Properties.Resources.Black_Queen); resourceMap.Add(ChessPiece.BlackPawn, WindowsFormsApplication2.Properties.Resources.Black_Pawn); // setting initial positions for pieces squares[0, 0].setPiece(ChessPiece.WhiteRook); squares[0, 1].setPiece(ChessPiece.WhiteHorse); squares[0, 2].setPiece(ChessPiece.WhiteBishop); squares[0, 3].setPiece(ChessPiece.WhiteKing); squares[0, 4].setPiece(ChessPiece.WhiteQueen); squares[0, 5].setPiece(ChessPiece.WhiteBishop); squares[0, 6].setPiece(ChessPiece.WhiteHorse); squares[0, 7].setPiece(ChessPiece.WhiteRook); squares[7, 0].setPiece(ChessPiece.BlackRook); squares[7, 1].setPiece(ChessPiece.BlackHorse); squares[7, 2].setPiece(ChessPiece.BlackBishop); squares[7, 3].setPiece(ChessPiece.BlackKing); squares[7, 4].setPiece(ChessPiece.BlackQueen); squares[7, 5].setPiece(ChessPiece.BlackBishop); squares[7, 6].setPiece(ChessPiece.BlackHorse); squares[7, 7].setPiece(ChessPiece.BlackRook); for (int i = 0; i < tableLayoutPanel1.ColumnCount; i++) { squares[1, i].setPiece(ChessPiece.WhitePawn); squares[6, i].setPiece(ChessPiece.BlackPawn); } setStatus("White's Move now"); }
public void Set( sMove move ) { this.moveType = move.moveType; this.trgSquare = move.trgSquare; this.srcSquare = move.srcSquare; this.capturedSquare = move.capturedSquare; }
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; }
// 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 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; }
public static bool GetLookMoveList( ChessBoard board, ChessBoardSquare selSquare, List<sMove> listRetBoardPos ) { // up GetStraightMoveList( board, selSquare, listRetBoardPos, MoveDirectionType.eDirection_Move_Up ); // down GetStraightMoveList( board, selSquare, listRetBoardPos, MoveDirectionType.eDirection_Move_Down ); // left GetStraightMoveList( board, selSquare, listRetBoardPos, MoveDirectionType.eDirection_Move_Left ); // right GetStraightMoveList( board, selSquare, listRetBoardPos, MoveDirectionType.eDirection_Move_Right ); if( listRetBoardPos.Count > 0 ) return true; return false; }
// 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; }
// validate move position public static bool GetValidateMoveList( ChessBoard board, ChessBoardSquare selSquare, List<sMove> listRetBoardPos ) { bool bRet = false; if( selSquare == null ) { UnityEngine.Debug.LogError( "GetValidateMoveList - selSquare - selSquare == null" ); return bRet; } if( selSquare.IsBlank() ) { UnityEngine.Debug.LogError( "GetValidateMoveList - selSquare - IsBlank()" ); return bRet; } if( selSquare.IsInvalidPos() ) { UnityEngine.Debug.LogError( "GetValidateMoveList - selSquare - IsInvalidPos()" ); return bRet; } switch( selSquare.piece.pieceType ) { case PieceType.e_King: { bRet = GetKingMoveList( board, selSquare, listRetBoardPos ); } break; case PieceType.e_Queen: { bRet = GetQueenMoveList( board, selSquare, listRetBoardPos ); } break; case PieceType.e_Rook: { bRet = GetRookMoveList( board, selSquare, listRetBoardPos ); } break; case PieceType.e_Bishop: { bRet = GetBishopMoveList( board, selSquare, listRetBoardPos ); } break; case PieceType.e_Knight: { bRet = GetKnightMoveList( board, selSquare, listRetBoardPos ); } break; case PieceType.e_Pawn: { bRet = GetPawnMoveList( board, selSquare, listRetBoardPos ); } break; default: { } break; } return bRet; }
public void CaptureSquare( ChessBoardSquare capturedSquare ) { //if( capturedSquare.IsBlank() ) // UnityEngine.Debug.LogError( "ChessBoard::CaptureSquare() - File : " + capturedSquare.position.nPile + " Rank : " + capturedSquare.position.nRank ); listLivePiece.Remove( capturedSquare.piece ); listCapturedPiece.Add( capturedSquare.piece ); capturedSquare.ClearPiece(true); }
public void Clear() { this.moveType = MoveType.eNone_Move; this.trgSquare = null; this.srcSquare = null; this.capturedSquare = null; }
// interface public void Init( BattleChessMain chessMain, Transform[] aPieceRef, ParticleSystem selectPSystemRef, ParticleSystem movablePSystemRef ) { battleChessMain = chessMain; // etc property CurrTurn = PlayerSide.e_White; UserPlayerSide = PlayerSide.e_White; ThinkingTime = 18000; nCurrHalfMove = 0; nCurrTotalMove = 0; Ready = false; IsWhiteCallCheck = false; IsBlackCallCheck = false; IsWhiteInCheckMate = false; IsBlackInCheckMate = false; // move currWhiteMove = new ChessMover.sMove(); currBlackMove = new ChessMover.sMove(); listWhiteMoveHistory = new List<ChessMover.sMove>(); listBlackMoveHistory = new List<ChessMover.sMove>(); listCurrMovable = new List<ChessMover.sMove>(); listCapturedPiece = new List<ChessPiece>(); // init board bitBoard.Init(); bitBoardVirtual.Init(); // piece list listAllPiece = new List<ChessPiece>(); listLivePiece = new List<ChessPiece>(); aBoardSquare = new ChessBoardSquare[ChessData.nNumPile,ChessData.nNumRank]; ChessPiece currPiece = null; for( int i=0; i<ChessData.nNumPile; i++ ){ for( int j=0; j<ChessData.nNumRank; j++ ){ // movable square effect Particle System //ParticleSystem movablePiecePSystem = MonoBehaviour.Instantiate( movablePSystemRef.gameObject, Vector3.zero, Quaternion.identity ) as ParticleSystem; ParticleSystem movablePiecePSystem = battleChessMain.gameObject.AddChildInstantiate<ParticleSystem>( movablePSystemRef, Vector3.zero, Quaternion.identity ); if( ChessData.aStartPiecePos[i,j] == PiecePlayerType.eNone_Piece ) { aBoardSquare[i,j] = new ChessBoardSquare( null, movablePiecePSystem, i, j ); } else { Vector3 currPos = new Vector3( j - 3.5f, 0.0f, i - 3.5f ); Transform currTransform = aPieceRef[(int)ChessData.aStartPiecePos[i,j]]; //Transform currPieceObject = MonoBehaviour.Instantiate( currTransform, currPos, currTransform.rotation ) as Transform; Transform currPieceObject = battleChessMain.gameObject.AddChildInstantiate<Transform>( currTransform, currPos, currTransform.rotation ); if( i == 0 || i == 1 ) { currPiece = new ChessPiece( currPieceObject.gameObject, PlayerSide.e_White, ChessData.aStartPiecePos[i,j] ); listAllPiece.Add( currPiece ); aBoardSquare[i,j] = new ChessBoardSquare( currPiece, movablePiecePSystem, i, j ); } else if( i == 6 || i == 7 ) { currPiece = new ChessPiece( currPieceObject.gameObject, PlayerSide.e_Black, ChessData.aStartPiecePos[i,j] ); listAllPiece.Add( currPiece ); aBoardSquare[i,j] = new ChessBoardSquare( currPiece, movablePiecePSystem, i, j ); } } } } // piece coloar SetWhiteSidePieceColor( Color.white ); SetBlackSidePieceColor( Color.white ); // board material if( chessMain.renderer.materials.Length == 2 ) { matBoard1 = chessMain.renderer.materials[0]; matBoard2 = chessMain.renderer.materials[1]; Color rgbaWhiteBoard, rgbaBlackBoard; rgbaWhiteBoard = new Color( 1.0f, 1.0f, 1.0f, 1.0f ); rgbaBlackBoard = new Color( 0.039f, 0.34f, 0.22f, 1.0f ); SetWhiteSideBoardColor( rgbaWhiteBoard ); SetBlackSideBoardColor( rgbaBlackBoard ); } // particle effect currSelectedSquare = null; //selectPiecePSystem = MonoBehaviour.Instantiate( selectPSystemRef, Vector3.zero, Quaternion.identity ) as ParticleSystem; selectPiecePSystem = battleChessMain.gameObject.AddChildInstantiate<ParticleSystem>( selectPSystemRef, Vector3.zero, Quaternion.identity ); selectPiecePSystem.Stop(); }
public static void BitBoardToMoveList( ulong ulBitboard, MoveType moveType, ChessBoard board, ChessBoardSquare srcSquare, List<sMove> listRetBoardPos ) { for( int i=0; i<ChessData.nNumBoardSquare; i++ ) { ulong ulCurrMask = (ulong)1 << i; ulong ulCurrPosition = ulBitboard & ulCurrMask; if( ulCurrPosition > 0 ) { int nCurrRank, nCurrFile; nCurrRank = i % ChessData.nNumRank; nCurrFile = i / ChessData.nNumPile; ChessBoardSquare trgSquare = board.aBoardSquare[nCurrFile, nCurrRank]; ChessBoardSquare capturedSquare = null; sMove move = new sMove(); // check capture move if( ChessMover.IsCaptureMove( moveType ) ) { if( ChessMover.IsEnpassantMove( moveType ) ) { int nCapturedPawnRank, nCapturedPawnFile; nCapturedPawnRank = nCurrRank; nCapturedPawnFile = srcSquare.piece.playerSide == PlayerSide.e_White ? nCurrFile - ChessData.nNumPile : nCurrFile + ChessData.nNumPile; capturedSquare = board.aBoardSquare[nCapturedPawnFile, nCapturedPawnRank]; } else capturedSquare = trgSquare; } // check pawn promote move if( ChessMover.IsPawnMove( moveType ) ) { // promote move check if( srcSquare.piece.playerSide == PlayerSide.e_White ) { if( (ulCurrMask & ChessBitBoard.firstRank) > 0 ) move.moveType |= MoveType.ePromote_Move; } else { if( (ulCurrMask & ChessBitBoard.lastRank) > 0 ) move.moveType |= MoveType.ePromote_Move; } } // normal move move.moveType = moveType; move.srcSquare = srcSquare; move.trgSquare = trgSquare; move.capturedSquare = capturedSquare; // check mate state // virtually move board.bitBoardVirtual.CopyFrom( board.bitBoard ); board.bitBoardVirtual.MoveUpdate( move ); bool bWillCheckMateState = false; if( srcSquare.piece.playerSide == PlayerSide.e_White ) { if( board.bitBoardVirtual.IsWhiteKingInCheck() ) { bWillCheckMateState = true; } } else if( srcSquare.piece.playerSide == PlayerSide.e_Black ) { if( board.bitBoardVirtual.IsBlackKingInCheck() ) { bWillCheckMateState = true; } } // unmove virtually // skip this move if( bWillCheckMateState ) continue; // for debug foreach( sMove aMove in listRetBoardPos ) { if( aMove.trgSquare == move.trgSquare ) { UnityEngine.Debug.Log( "!!!!!!!!!!!!!!!!!!!!!!!!ChessMover::BitBoardToMoveList() - move collision Aleady exist!!!! " + "file : " + move.trgSquare.position.nPile + " Rank : " + move.trgSquare.position.nRank ); string strOccupied = string.Format( "occupied : {0:X}", board.bitBoard.occupiedBB ); UnityEngine.Debug.LogError( "!!!!!!!!!!!!!!!!!!!!!!!!ChessMover::BitBoardToMoveList() - " + strOccupied ); string strEmpty = string.Format( "empty : {0:X}", board.bitBoard.emptyBB ); UnityEngine.Debug.LogError( "!!!!!!!!!!!!!!!!!!!!!!!!ChessMover::BitBoardToMoveList() - " + strEmpty ); } } listRetBoardPos.Add( move ); } } }
bool IsValidAIMove( ChessBoardSquare srcSquare, ChessBoardSquare trgSquare, List<ChessMover.sMove> listMove, ChessMover.sMove aiMove ) { foreach( ChessMover.sMove move in listMove ) { if( move.srcSquare == srcSquare && move.trgSquare == trgSquare ) { aiMove.Set( move ); return true; } } aiMove.Clear(); return false; }
public void IsValidSquare_ConstructWithChar(char file, int rank, string expectedAN) { var square = new ChessBoardSquare(file, rank); Assert.AreEqual(expectedAN, square.GetAN()); }
bool IsValidMove( ChessBoardSquare srcSquare, ChessBoardSquare trgSquare, ChessMover.sMove userMove ) { foreach( ChessMover.sMove move in listCurrMovable ) { if( move.srcSquare == srcSquare && move.trgSquare == trgSquare ) { userMove.Set( move ); return true; } } userMove.Clear(); return false; }
public void Set( ChessBoardSquare srcSquare, ChessBoardSquare trgSquare, MoveType moveType ) { this.moveType = moveType; this.trgSquare = trgSquare; this.srcSquare = srcSquare; this.enPassantTargetSquare.Rank = -1; this.enPassantTargetSquare.Pile = -1; this.enPassantTargetSquare.Available = false; }
public void Set( ChessBoardSquare srcSquare, ChessBoardSquare trgSquare, MoveType moveType, ChessEnPassant enPassantTrgSquare ) { this.moveType = moveType; this.trgSquare = trgSquare; this.srcSquare = srcSquare; this.enPassantTargetSquare = enPassantTrgSquare; }
public void SelectSquare( ChessBoardSquare selSquare ) { if( selSquare != null && selSquare.IsBlank() == false ) { if( selSquare.piece.playerSide == UserPlayerSide ) { PlaySelectEffect( selSquare.piece.gameObject.transform.position, selSquare.piece.gameObject.transform.rotation ); selectSquare = selSquare; return; } } // movable pos StopSelectEffect(); selectSquare = null; }
public void Clear() { this.moveType = MoveType.eNone_Move; this.trgSquare = null; this.srcSquare = null; this.enPassantTargetSquare.Rank = -1; this.enPassantTargetSquare.Pile = -1; this.enPassantTargetSquare.Available = false; }
bool IsValidMove( ChessBoardSquare trgSquare, ChessMoveManager.sMove targetMove ) { foreach( ChessMoveManager.sMove move in listCurrMovable ) { if( move.trgSquare.position == trgSquare.position ) { targetMove.Set( move ); return true; } } targetMove.Clear(); return false; }
public static bool GetRookMoveList( ChessBoard board, ChessBoardSquare selSquare, List<sMove> listRetBoardPos ) { PlayerSide srcPlayerSide = selSquare.piece.playerSide; int nSrcRookSq = (int)selSquare.position.pos; // attack/move!!!! // calc viable queen move ulong viableRookMove = board.bitBoard.RookMovesBB( nSrcRookSq ); if( viableRookMove > 0 ) { MoveType moveType = MoveType.eNormal_Move; // convert move list BitBoardToMoveList( viableRookMove, moveType, board, selSquare, listRetBoardPos ); } // calc viable queen attack ulong viableRookAttack = board.bitBoard.RookAttacksBB( (int)srcPlayerSide, nSrcRookSq ); if( viableRookAttack > 0 ) { MoveType moveType = MoveType.eCapture_Move; // convert move list BitBoardToMoveList( viableRookAttack, moveType, board, selSquare, listRetBoardPos ); } return listRetBoardPos.Count > 0; }
public static bool GetKingMoveList( ChessBoard board, ChessBoardSquare selSquare, List<sMove> listRetBoardPos ) { PlayerSide srcPlayerSide = selSquare.piece.playerSide; int nSrcKingSq = (int)selSquare.position.pos; // attack/move!!!! // calc viable king move ulong viableKingMove = board.bitBoard.KingMovesBB( (int)srcPlayerSide, nSrcKingSq ); if( viableKingMove > 0 ) { MoveType moveType = MoveType.eNormal_Move; // convert move list BitBoardToMoveList( viableKingMove, moveType, board, selSquare, listRetBoardPos ); } ulong viableKingAttack = board.bitBoard.KingAttacksBB( (int)srcPlayerSide, nSrcKingSq ); if( viableKingAttack > 0 ) { MoveType moveType = MoveType.eCapture_Move; // convert move list BitBoardToMoveList( viableKingAttack, moveType, board, selSquare, listRetBoardPos ); } // castling!!!! // king side castling ulong viableKingCastling = board.bitBoard.KingCastlingBB( (int)srcPlayerSide ); if( viableKingCastling > 0 ) { MoveType moveType = MoveType.eCastling_Move; if( srcPlayerSide == PlayerSide.e_White ) { if( board.bitBoard.currCastlingState.IsWhiteKingSideAvailable() ) moveType |= MoveType.eCastling_White_KingSide_Move; if( board.bitBoard.currCastlingState.IsWhiteQueenSideAvailable() ) moveType |= MoveType.eCastling_White_QueenSide_Move; } else{ if( board.bitBoard.currCastlingState.IsBlackKingSideAvailable() ) moveType |= MoveType.eCastling_Black_KingSide_Move; if( board.bitBoard.currCastlingState.IsWhiteQueenSideAvailable() ) moveType |= MoveType.eCastling_Black_QueenSide_Move; } // convert move list BitBoardToMoveList( viableKingCastling, moveType, board, selSquare, listRetBoardPos ); } return listRetBoardPos.Count > 0; }
public void Set( sMove move ) { this.moveType = move.moveType; this.trgSquare = move.trgSquare; this.srcSquare = move.srcSquare; this.enPassantTargetSquare = move.enPassantTargetSquare; }
public static bool GetPawnMoveList( ChessBoard board, ChessBoardSquare selSquare, List<sMove> listRetBoardPos ) { //UnityEngine.Debug.LogError( "GetPawnMoveList - start" + " " + piece.position + " " + piece.playerSide ); PlayerSide srcPlayerSide = selSquare.piece.playerSide; int nSrcPawnSq = (int)selSquare.position.pos; // attack/move!!!! // calc viable Pawn one move ulong viablePawnOneMove = board.bitBoard.PawnOneMovesBB( (int)srcPlayerSide, nSrcPawnSq ); if( viablePawnOneMove > 0 ) { MoveType moveType = MoveType.eNormal_Move | MoveType.ePawn_Move; // convert move list BitBoardToMoveList( viablePawnOneMove, moveType, board, selSquare, listRetBoardPos ); } // calc viable Pawn two move ulong viablePawnTwoMove = board.bitBoard.PawnTwoMovesBB( (int)srcPlayerSide, nSrcPawnSq ); if( viablePawnTwoMove > 0 ) { MoveType moveType = MoveType.eNormal_Move | MoveType.ePawn_Move | MoveType.ePawn_Two_Move; // convert move list BitBoardToMoveList( viablePawnTwoMove, moveType, board, selSquare, listRetBoardPos ); } // calc viable Pawn attack move ulong viablePawnAttackMove = board.bitBoard.PawnAttackMovesBB( (int)srcPlayerSide, nSrcPawnSq ); if( viablePawnAttackMove > 0 ) { MoveType moveType = MoveType.eCapture_Move | MoveType.ePawn_Move; // convert move list BitBoardToMoveList( viablePawnAttackMove, moveType, board, selSquare, listRetBoardPos ); } // calc viable Pawn attack move ulong viablePawnEnpassantMove = board.bitBoard.PawnEnpassantMovesBB( (int)srcPlayerSide, nSrcPawnSq ); if( viablePawnEnpassantMove > 0 ) { MoveType moveType = MoveType.eEnPassan_Move | MoveType.ePawn_Move; // convert move list BitBoardToMoveList( viablePawnEnpassantMove, moveType, board, selSquare, listRetBoardPos ); } return listRetBoardPos.Count > 0; }
public sMove( ChessBoardSquare srcSquare, ChessBoardSquare trgSquare, MoveType moveType ) { this.moveType = moveType; this.trgSquare = trgSquare; this.srcSquare = srcSquare; this.enPassantTargetSquare = new ChessEnPassant() { Rank = -1, Pile = -1, Available = false }; }