示例#1
0
        /// <summary>
        /// Get any legal move from the current position on the provided board.
        /// </summary>
        /// <param name="board">The state of the game.</param>
        /// <param name="pos">The piece/position to check for valid moves.</param>
        /// <param name="verify_check">Whether or not to recurse and check if the current move puts you in check.</param>
        /// <returns>A list of positions the piece can move to.</returns>
        public static List <Position> getLegalMove(BoardHelper board, Position pos, bool verify_check = true)
        {
            piece_t p = board.Grid[pos.number][pos.letter];

            if (p.piece == PieceType.None)
            {
                return(new List <Position>());
            }

            switch (p.piece)
            {
            case PieceType.Pawn:
                return(LegalMoveSet.Pawn(board, pos, verify_check));

            case PieceType.Rook:
                return(LegalMoveSet.Rook(board, pos, verify_check));

            case PieceType.Knight:
                return(LegalMoveSet.Knight(board, pos, verify_check));

            case PieceType.Bishop:
                return(LegalMoveSet.Bishop(board, pos, verify_check));

            case PieceType.Queen:
                return(LegalMoveSet.Queen(board, pos, verify_check));

            case PieceType.King:
                return(LegalMoveSet.King(board, pos, verify_check));

            default:
                return(new List <Position>());
            }
        }
示例#2
0
        private static List <Position> Rook(BoardHelper board, Position pos, bool verify_check = true)
        {
            List <Position> moves = new List <Position>();

            piece_t p = board.Grid[pos.number][pos.letter];

            if (p.piece == PieceType.None)
            {
                return(moves);
            }

            // slide along vert/hor for possible moves
            moves.AddRange(Slide(board, p.player, pos, new Position(1, 0)));
            moves.AddRange(Slide(board, p.player, pos, new Position(-1, 0)));
            moves.AddRange(Slide(board, p.player, pos, new Position(0, 1)));
            moves.AddRange(Slide(board, p.player, pos, new Position(0, -1)));

            if (verify_check)// make sure each move doesn't put us in check
            {
                for (int i = moves.Count - 1; i >= 0; i--)
                {
                    BoardHelper b2 = LegalMoveSet.move(board, new Move(pos, moves[i]));
                    if (isCheck(b2, p.player))
                    {
                        moves.RemoveAt(i);
                    }
                }
            }
            return(moves);
        }
示例#3
0
        private static List <Position> Knight(BoardHelper board, Position pos, bool verify_check = true)
        {
            List <Position> moves = new List <Position>();

            piece_t p = board.Grid[pos.number][pos.letter];

            if (p.piece == PieceType.None)
            {
                return(moves);
            }

            // collect all relative moves possible
            List <Position> relative = new List <Position>();

            relative.Add(new Position(2, 1));
            relative.Add(new Position(2, -1));

            relative.Add(new Position(-2, 1));
            relative.Add(new Position(-2, -1));

            relative.Add(new Position(1, 2));
            relative.Add(new Position(-1, 2));

            relative.Add(new Position(1, -2));
            relative.Add(new Position(-1, -2));

            // iterate moves
            foreach (Position move in relative)
            {
                Position moved = new Position(move.letter + pos.letter, move.number + pos.number);

                // bounds check
                if (moved.letter < 0 || moved.letter > 7 || moved.number < 0 || moved.number > 7)
                {
                    continue;
                }

                // if empty space or attacking
                if (board.Grid[moved.number][moved.letter].piece == PieceType.None ||
                    board.Grid[moved.number][moved.letter].player != p.player)
                {
                    moves.Add(moved);
                }
            }

            if (verify_check)// make sure each move doesn't put us in check
            {
                for (int i = moves.Count - 1; i >= 0; i--)
                {
                    BoardHelper b2 = LegalMoveSet.move(board, new Move(pos, moves[i]));
                    if (isCheck(b2, p.player))
                    {
                        moves.RemoveAt(i);
                    }
                }
            }
            return(moves);
        }
示例#4
0
        private static bool allowCastle(BoardHelper board, PieceSide player, Position pos, bool isRight)
        {
            bool isValid = true;
            int  rookPos;
            int  kingDirection;

            if (isRight)
            {
                rookPos       = 7;
                kingDirection = 1;
            }
            else
            {
                rookPos       = 0;
                kingDirection = -1;
            }

            //Check for valid right castling
            // Is the peice at H,7 a rook owned by the player and has it moved
            if (board.Grid[pos.number][rookPos].piece == PieceType.Rook &&
                board.Grid[pos.number][rookPos].player == player && board.Grid[pos.number][rookPos].lastPosition.Equals(new Position(-1, -1)))
            {
                // Check that the adjacent two squares are empty
                for (int i = 0; i < 2; i++)
                {
                    if (board.Grid[pos.number][pos.letter + (i + 1) * kingDirection].piece != PieceType.None)
                    {
                        isValid = false;
                        break;
                    }
                }

                // Don't bother running secondary checks if the way isn't even clear
                if (isValid)
                {
                    for (int i = 0; i < 2; i++)
                    {
                        // Move kings postion over i squares to check if king is passing over an attackable
                        // square
                        BoardHelper b2 = LegalMoveSet.move(board, new Move(pos, new Position(pos.letter + (i + 1) * kingDirection, pos.number)));

                        // Attackable square is in between king and rook so
                        // its not possible to castle to the right rook
                        if (isCheck(b2, player))
                        {
                            isValid = false;
                            break;
                        }
                    }
                }
            }
            else
            {
                isValid = false;
            }
            return(isValid);
        }
示例#5
0
        public int fitness(PieceSide max)
        {
            int fitness = 0;

            int[] blackPieces = { 0, 0, 0, 0, 0, 0 };
            int[] whitePieces = { 0, 0, 0, 0, 0, 0 };
            int   blackMoves  = 0;
            int   whiteMoves  = 0;

            // sum up the number of moves and pieces
            foreach (Position pos in Pieces[PieceSide.Black])
            {
                blackMoves += LegalMoveSet.getLegalMove(this, pos).Count;
                blackPieces[(int)Grid[pos.number][pos.letter].piece]++;
            }

            // sum up the number of moves and pieces
            foreach (Position pos in Pieces[PieceSide.White])
            {
                whiteMoves += LegalMoveSet.getLegalMove(this, pos).Count;
                whitePieces[(int)Grid[pos.number][pos.letter].piece]++;
            }

            // if viewing from black side
            if (max == PieceSide.Black)
            {
                // apply weighting to piece counts
                for (int i = 0; i < 6; i++)
                {
                    fitness += pieceWeights[i] * (blackPieces[i] - whitePieces[i]);
                }

                // apply move value
                fitness += (int)(0.5 * (blackMoves - whiteMoves));
            }
            else
            {
                // apply weighting to piece counts
                for (int i = 0; i < 6; i++)
                {
                    fitness += pieceWeights[i] * (whitePieces[i] - blackPieces[i]);
                }

                // apply move value
                fitness += (int)(0.5 * (whiteMoves - blackMoves));
            }

            return(fitness);
        }
示例#6
0
        /// <summary>
        /// Get all legal moves for the player on the current board.
        /// </summary>
        /// <param name="b">The state of the game.</param>
        /// <param name="player">The player whose moves you want.</param>
        /// <returns>A 1-to-many dictionary of moves from one position to many</returns>
        public static Dictionary <Position, List <Position> > getPlayerMoves(BoardHelper b, PieceSide player)
        {
            Dictionary <Position, List <Position> > moves = new Dictionary <Position, List <Position> >();

            foreach (Position pos in b.Pieces[player])
            {
                if (b.Grid[pos.number][pos.letter].piece != PieceType.None)
                {
                    if (!moves.ContainsKey(pos))
                    {
                        moves[pos] = new List <Position>();
                    }
                    moves[pos].AddRange(LegalMoveSet.getLegalMove(b, pos));
                }
            }
            return(moves);
        }
示例#7
0
        private static List <Position> Pawn(BoardHelper board, Position pos, bool verify_check = true)
        {
            List <Position> moves = new List <Position>();

            piece_t p = board.Grid[pos.number][pos.letter];

            if (p.piece == PieceType.None)
            {
                return(moves);
            }

            // gather relative moves
            List <Position> relative = new List <Position>();

            relative.Add(new Position(-1, 1 * ((p.player == PieceSide.Black) ? -1 : 1)));
            relative.Add(new Position(0, 1 * ((p.player == PieceSide.Black) ? -1 : 1)));
            relative.Add(new Position(0, 2 * ((p.player == PieceSide.Black) ? -1 : 1)));
            relative.Add(new Position(1, 1 * ((p.player == PieceSide.Black) ? -1 : 1)));

            // iterate moves
            foreach (Position move in relative)
            {
                Position moved = new Position(move.letter + pos.letter, move.number + pos.number);

                // bounds check
                if (moved.letter < 0 || moved.letter > 7 || moved.number < 0 || moved.number > 7)
                {
                    continue;
                }

                // double forward move
                if (moved.letter == pos.letter && board.Grid[moved.number][moved.letter].piece == PieceType.None && Math.Abs(moved.number - pos.number) == 2)
                {
                    // check the first step
                    int  step        = -((moved.number - pos.number) / (Math.Abs(moved.number - pos.number)));
                    bool hasnt_moved = pos.number == ((p.player == PieceSide.Black) ? 6 : 1);
                    if (board.Grid[moved.number + step][moved.letter].piece == PieceType.None && hasnt_moved)
                    {
                        moves.Add(moved);
                    }
                }
                // if it's not blocked we can move forward
                else if (moved.letter == pos.letter && board.Grid[moved.number][moved.letter].piece == PieceType.None)
                {
                    moves.Add(moved);
                }
                // angled attack
                else if (moved.letter != pos.letter && board.Grid[moved.number][moved.letter].piece != PieceType.None && board.Grid[moved.number][moved.letter].player != p.player)
                {
                    moves.Add(moved);
                }
                // en passant
                else if (isEnPassant(board, new Move(pos, moved)))
                {
                    moves.Add(moved);
                }
            }

            if (verify_check)// make sure each move doesn't put us in check
            {
                for (int i = moves.Count - 1; i >= 0; i--)
                {
                    BoardHelper b2 = LegalMoveSet.move(board, new Move(pos, moves[i]));
                    if (isCheck(b2, p.player))
                    {
                        moves.RemoveAt(i);
                    }
                }
            }
            return(moves);
        }
示例#8
0
        private static List <Position> King(BoardHelper board, Position pos, bool verify_check = true)
        {
            List <Position> moves = new List <Position>();

            piece_t p = board.Grid[pos.number][pos.letter];

            if (p.piece == PieceType.None)
            {
                return(moves);
            }

            // collect all relative moves possible
            List <Position> relative = new List <Position>();

            relative.Add(new Position(-1, 1));
            relative.Add(new Position(0, 1));
            relative.Add(new Position(1, 1));

            relative.Add(new Position(-1, 0));
            relative.Add(new Position(1, 0));

            relative.Add(new Position(-1, -1));
            relative.Add(new Position(0, -1));
            relative.Add(new Position(1, -1));

            // Iterate moves
            foreach (Position move in relative)
            {
                Position moved = new Position(move.letter + pos.letter, move.number + pos.number);

                // bound check
                if (moved.letter < 0 || moved.letter > 7 || moved.number < 0 || moved.number > 7)
                {
                    continue;
                }

                // if it's not blocked we can move
                if (board.Grid[moved.number][moved.letter].piece == PieceType.None || board.Grid[moved.number][moved.letter].player != p.player)
                {
                    if (verify_check) // make sure we don't put ourselves in check
                    {
                        BoardHelper b2 = LegalMoveSet.move(board, new Move(pos, moved));
                        if (!isCheck(b2, p.player))
                        {
                            moves.Add(moved);
                        }
                    }
                    else
                    {
                        moves.Add(moved);
                    }
                }
            }

            // Castling

            /* A king can only castle if:
             * king has not moved
             * rook has not moved
             * king is not in check
             * king does not end up in check
             * king does not pass through any other peieces
             * king does not pass through any squares under attack
             * king knows secret handshake
             */
            if (verify_check)
            {
                if (!isCheck(board, p.player) &&
                    p.lastPosition.Equals(new Position(-1, -1)))
                {
                    bool castleRight = allowCastle(board, p.player, pos, true);
                    bool castleLeft  = allowCastle(board, p.player, pos, false);

                    if (castleRight)
                    {
                        moves.Add(new Position(6, pos.number));
                    }
                    if (castleLeft)
                    {
                        moves.Add(new Position(2, pos.number));
                    }
                }
            }

            return(moves);
        }
示例#9
0
        private Move MiniMaxAB(BoardHelper board, PieceSide turn)
        {
            RUNNING = true;  // we've started running
            STOP    = false; // no interupt command sent
            MAX     = turn;  // who is maximizing

            // gather all possible moves
            Dictionary <Position, List <Position> > moves = LegalMoveSet.getPlayerMoves(board, turn);

            // because we're threading safely store best result from each thread
            int[]  bestresults = new int[moves.Count];
            Move[] bestmoves   = new Move[moves.Count];

            // thread the generation of each move
            Parallel.ForEach(moves, (movelist, state, index) =>
            {
                if (STOP) // interupt
                {
                    state.Stop();
                    return;
                }

                // initialize thread best
                bestresults[index] = int.MinValue;
                bestmoves[index]   = new Move(new Position(-1, -1), new Position(-1, -1));

                // for each move for the current piece(thread)
                foreach (Position move in movelist.Value)
                {
                    if (STOP) // interupt
                    {
                        state.Stop();
                        return;
                    }

                    // make initial move and start recursion
                    BoardHelper b2 = LegalMoveSet.move(board, new Move(movelist.Key, move));
                    int result     = mimaab(b2, (turn == PieceSide.White) ? PieceSide.Black : PieceSide.White, 1, Int32.MinValue, Int32.MaxValue);

                    // if result is better or best hasn't been set yet
                    if (bestresults[index] < result || (bestmoves[index].to.Equals(new Position(-1, -1)) && bestresults[index] == int.MinValue))
                    {
                        bestresults[index]    = result;
                        bestmoves[index].from = movelist.Key;
                        bestmoves[index].to   = move;
                    }
                }
            });

            // interupted
            if (STOP)
            {
                return(new Move(new Position(-1, -1), new Position(-1, -1)));
            }

            // find the best of the thread results
            int  best = int.MinValue;
            Move m    = new Move(new Position(-1, -1), new Position(-1, -1));

            for (int i = 0; i < bestmoves.Length; i++)
            {
                if (best < bestresults[i] || (m.to.Equals(new Position(-1, -1)) && !bestmoves[i].to.Equals(new Position(-1, -1))))
                {
                    best = bestresults[i];
                    m    = bestmoves[i];
                }
            }
            return(m);
        }
示例#10
0
        private int mimaab(BoardHelper board, PieceSide turn, int depth, int alpha, int beta)
        {
            // base case, at maximum depth return board fitness
            if (depth >= DEPTH)
            {
                return(board.fitness(MAX));
            }
            else
            {
                List <BoardHelper> boards = new List <BoardHelper>();

                // get available moves / board states from moves for the current player
                foreach (Position pos in board.Pieces[turn])
                {
                    if (STOP)
                    {
                        return(-1);      // interupts
                    }
                    List <Position> moves = LegalMoveSet.getLegalMove(board, pos);
                    foreach (Position move in moves)
                    {
                        if (STOP)
                        {
                            return(-1);      // interupts
                        }
                        BoardHelper b2 = LegalMoveSet.move(board, new Move(pos, move));
                        boards.Add(b2);
                    }
                }

                int a = alpha, b = beta;
                if (turn != MAX) // minimize
                {
                    foreach (BoardHelper b2 in boards)
                    {
                        if (STOP)
                        {
                            return(-1);      // interupt
                        }
                        b = Math.Min(b, mimaab(b2, (turn == PieceSide.White) ? PieceSide.Black : PieceSide.White, depth + 1, a, b));
                        if (a >= b)
                        {
                            return(a);
                        }
                    }
                    return(b);
                }
                else // maximize
                {
                    foreach (BoardHelper b2 in boards)
                    {
                        if (STOP)
                        {
                            return(-1);      // interupt
                        }
                        a = Math.Max(a, mimaab(b2, (turn == PieceSide.White) ? PieceSide.Black : PieceSide.White, depth + 1, a, b));
                        if (a >= b)
                        {
                            return(b);
                        }
                    }
                    return(a);
                }
            }
        }