Ejemplo n.º 1
0
 private static bool castleSquaresInCheck(Move move, Piece piece, ControlledPositions controlledMoves)
 {
     if (move.isCastle)
     {
         UInt64 testMask = castleSquaresBitMask(move, piece);
         if ((controlledMoves.ControlledPositionsBitMask & testMask) != 0)
         {
             return(true);
         }
     }
     return(false);
 }
Ejemplo n.º 2
0
        private bool gameOver(ref PieceColor winner)
        {
            foreach (Square pos in gamestate.getPiecePositions())
            {
                PossibleMoves moves = new PossibleMoves(gamestate, pos);
                if (moves.MoveCount != 0)
                {
                    return(false);
                }
            }
            ControlledPositions control = new ControlledPositions(gamestate, (PieceColor)(1 - gamestate.turnColor));

            if (control.Check == null)
            {
                winner = PieceColor.PieceColor_NB;
            }
            else
            {
                winner = (PieceColor)(1 - gamestate.turnColor);
            }
            return(true);
        }
Ejemplo n.º 3
0
        public PossibleMoves(Gamestate board, Square StartPosition)
        {
            MoveCount   = 0;
            MoveBitMask = 0;
            Piece StartPiece = board.GetPiece(StartPosition);

            if (StartPiece == null)
            {
                return;
            }
            Moves = StartPiece.getPositionMoves(StartPosition);
            MoveDirections skipDirection = MoveDirections.Move_0;
            UInt32         mask          = 1;

            foreach (Move move in Moves)
            {
                mask <<= 1;
                if (move.Direction == skipDirection)
                {
                    continue;
                }
                else
                {
                    skipDirection = MoveDirections.Move_0;
                }

                if (continueIfCastle(board, StartPiece, move))
                {
                    continue;
                }

                Piece EndPiece     = board.GetPiece(move.MovePosition);
                Piece CapturePiece = board.GetPiece(move.CapturePosition);

                if (continueIfEnPassant(board, move, CapturePiece))
                {
                    continue;
                }

                if (endPiecesMatchMoveFlags(StartPiece, move, EndPiece, CapturePiece))
                {
                    // check if move is legal
                    Gamestate testBoard = new Gamestate(board);
                    if (testBoard.GetPiece(move.CapturePosition) != null)
                    {
                        testBoard.RemovePiece(move.CapturePosition);
                    }
                    Piece piece = testBoard.GetPiece(StartPosition);
                    testBoard.RemovePiece(StartPosition);
                    testBoard.PutPiece(piece, move.MovePosition);
                    testBoard.turnColor = (testBoard.turnColor == PieceColor.White) ? PieceColor.Black : PieceColor.White;
                    ControlledPositions controlledMoves = new ControlledPositions(testBoard);
                    if (controlledMoves.Check == null)
                    {
                        if (castleSquaresInCheck(move, piece, controlledMoves))
                        {
                            continue;
                        }

                        EndPositionBitMask |= ((UInt64)1 << (byte)move.MovePosition);
                        MoveBitMask        |= mask;
                        MoveCount++;
                    }
                }

                if (EndPiece != null || CapturePiece != null)
                {
                    skipDirection = move.Direction;
                }
            }
            MoveBitMask >>= 1;
        }
Ejemplo n.º 4
0
 private int calculateScore(uint depth, ref UInt64 candidateCount, Optimize optimize)
 {
     if (depth > 0)
     {
         gamestate.flipTurnColor();
         gamestate.setEnPassantPosition(gamestate.turnColor, null);
         IEnumerable <Move> allMoves = getAllMoves();
         int  bestScore = (optimize == Optimize.Highest) ? int.MinValue : int.MaxValue;
         Move bestMove  = null;
         foreach (Move move in allMoves)
         {
             Gamestate   testState   = new Gamestate(gamestate);
             GameControl testControl = new GameControl(testState);
             testControl.makeMove(move);
             Piece piece = gamestate.GetPiece(move.StartPosition);
             //for (int d = 0; d <= 5 - depth; d++) Console.Write("  ");
             //Console.WriteLine(" considering move " + gamestate.turnColor + " " + piece.Type +" from " + move.StartPosition + " to " + move.MovePosition);
             int score = testControl.calculateScore(depth - 1, ref candidateCount, (optimize == Optimize.Highest) ? Optimize.Lowest : Optimize.Highest);
             //for (int d = 0; d <= 5 - depth; d++) Console.Write("  ");
             //Console.WriteLine(" -> score " + score);
             if (optimize == Optimize.Highest && score > bestScore || optimize == Optimize.Lowest && score < bestScore)
             {
                 bestScore = score;
                 bestMove  = move;
             }
         }
         //Piece bestPiece = gamestate.GetPiece(bestMove.StartPosition);
         //for (int d = 0; d <= 5 - depth; d++) Console.Write("  ");
         //Console.WriteLine(" selected move " + gamestate.turnColor + " " + bestPiece.Type + " from " + bestMove.StartPosition + " to " + bestMove.MovePosition + " with score " + bestScore);
         return(bestScore);
     }
     else
     {
         ControlledPositions cp = new ControlledPositions(gamestate);
         int score = 0;
         for (Square pos = Square.A1; pos <= Square.H8; pos++)
         {
             Piece piece = gamestate.GetPiece(pos);
             if (piece != null && piece.Color == gamestate.turnColor)
             {
                 score += 3 * (int)piece.Value;
             }
             if ((cp.ControlledPositionsBitMask & ((UInt64)1 << (byte)pos)) != 0)
             {
                 score += 8;
                 if (piece != null && (piece.Type != PieceType.King || piece.Color != gamestate.turnColor))
                 {
                     score += (int)piece.Value;
                 }
             }
         }
         cp = new ControlledPositions(gamestate, (gamestate.turnColor == PieceColor.White) ? PieceColor.Black : PieceColor.White);
         for (Square pos = Square.A1; pos <= Square.H8; pos++)
         {
             Piece piece = gamestate.GetPiece(pos);
             if (piece != null && piece.Color != gamestate.turnColor)
             {
                 score -= 3 * (int)piece.Value;
             }
             if ((cp.ControlledPositionsBitMask & ((UInt64)1 << (byte)pos)) != 0)
             {
                 score -= 8;
                 if (piece != null && (piece.Type != PieceType.King || piece.Color != gamestate.turnColor))
                 {
                     score -= (int)piece.Value;
                 }
             }
         }
         candidateCount++;
         return(score);
     }
 }