Exemple #1
0
        public override List <PieceMove> GetMovesMask(Board board, Point position)
        {
            List <PieceMove> ret    = new List <PieceMove>();
            BitBoard         locked = new BitBoard();

            board[position] = null;

            board.ForEach(
                (Piece piece) => piece.GetColour() != GetColour(),
                (Point point) => locked |= board[point].GetSeen(board, point));

            board[position] = this;

            IEnumerable <Point> kingMoves = GetSeen(board, position).GetAllSet().Where(kingMove => !locked[kingMove] && board[kingMove]?.GetColour() != GetColour());

            foreach (Point kingMove in kingMoves)
            {
                ret.Add(new PieceMove(new Vector(position, kingMove), board[kingMove] == null? MoveType.Move : MoveType.Capture));
            }

            if (!board.GetHistory().Any(historicalMove => historicalMove.p2 == position))
            {
                foreach (CastelingMask mask in castelingMasks)
                {
                    if (!board.GetHistory().Any(historicalMove => historicalMove.p1 == new Point(mask.rookInitial, position.y)) && Extentions.Extentions.Enumerate(position.x, mask.kingFinal).All(kingMoveX => board.FindAttacks(GetColour(), new Point(kingMoveX, position.y)).Count() == 0) && Extentions.Extentions.Enumerate(mask.rookInitial, position.x).SubArray(1, 1).All(movedThrough => board[movedThrough, position.y] == null))
                    {
                        ret.Add(new PieceMove(new Vector(position, new Point(mask.kingFinal, position.y)), new Vector[] { new Vector(new Point(mask.rookInitial, position.y), new Point(mask.rookFinal, position.y)) }, MoveType.Move));
                    }
                }
            }

            return(ret);
        }
Exemple #2
0
        public static BitBoard operator |(BitBoard l, BitBoard r)
        {
            BitBoard ret = new BitBoard();

            Board.ForEach(point => { if (l[point] || r[point])
                                     {
                                         ret[point] = true;
                                     }
                          });
            return(ret);
        }
Exemple #3
0
        public override BitBoard GetSeen(Board board, Point position)
        {
            BitBoard seen = new BitBoard();

            foreach (Point offset in mask)
            {
                Point absolutePosition = new Point(offset.x + position.x, offset.y + position.y);
                if (absolutePosition.x >= 0 && absolutePosition.x < 8 && absolutePosition.y >= 0 && absolutePosition.y < 8)
                {
                    seen[absolutePosition] = true;
                }
            }
            return(seen);
        }
Exemple #4
0
        public override BitBoard GetSeen(Board board, Point position)
        {
            BitBoard seen = new BitBoard();

            if ((dir == 1 ? position.y < 7 : position.y > 0) && position.x > 0)
            {
                seen[position.x - 1, position.y + dir] = true;
            }
            if ((dir == 1 ? position.y < 7 : position.y > 0) && position.x < 7)
            {
                seen[position.x + 1, position.y + dir] = true;
            }
            return(seen);
        }
Exemple #5
0
        public override BitBoard GetSeen(Board board, Point position)
        {
            BitBoard ret = new BitBoard();

            foreach (Point offset in mask)
            {
                Point absolutePosition = new Point(position.x + offset.x, position.y + offset.y);
                if (absolutePosition.x >= 0 && absolutePosition.x < 8 && absolutePosition.y >= 0 && absolutePosition.y < 8)
                {
                    ret[absolutePosition] = true;
                }
            }
            return(ret);
        }
Exemple #6
0
        public BitBoard GetSeen(Board board, Point position, Point[] dirs)
        {
            BitBoard seen = new BitBoard();

            foreach (Point dir in dirs)
            {
                bool  hitPiece = false;
                Point pointer  = new Point(position.x + dir.x, position.y + dir.y);
                while (pointer.x <= 7 && pointer.x >= 0 && pointer.y <= 7 && pointer.y >= 0 && !hitPiece)
                {
                    seen[pointer] = true;
                    if (board[pointer.x, pointer.y] is Piece)
                    {
                        hitPiece = true;
                    }
                    pointer.x += dir.x;
                    pointer.y += dir.y;
                }
            }
            return(seen);
        }
Exemple #7
0
        public override PieceMove Move(Board board)
        {
            BitBoard highlighted = new BitBoard();

            PieceMove[] moves       = board.GetMoves(GetColour());
            Vector      buildVector = new Vector();

            new StateMachine(
                new State[]
            {
                new State(
                    new TransitionExpressioin[]
                {
                    new TransitionExpressioin
                    {
                        function = () => { },
                        ptr      = 0
                    },

                    new TransitionExpressioin
                    {
                        function = () =>
                        {
                            buildVector.p1 = mouse.GetLastClicked();
                            renderer.SetHighlight(renderHandle, Highlight.CellSelected, buildVector.p1);
                            foreach (PieceMove selectedMove in moves.Where(move => move.vector.p1 == mouse.GetLastClicked()))
                            {
                                highlighted[selectedMove.vector.p2] = true;
                                renderer.SetHighlight(renderHandle, selectedMove.type == MoveType.Move ? Highlight.MovePossible : Highlight.AttackMovePossible, selectedMove.vector.p2);
                            }
                        },
                        ptr = 1
                    }
                },

                    () =>
                {
                    mouse.WaitForInput();
                    return(board[mouse.GetLastClicked()]?.GetColour() == GetColour() ? 1 : 0);
                }),

                new State(
                    new TransitionExpressioin[]
                {
                    new TransitionExpressioin
                    {
                        function = () =>
                        {
                            highlighted = new BitBoard();
                            renderer.ResetHighlights(renderHandle);
                        },
                        ptr = 0
                    },

                    new TransitionExpressioin
                    {
                        function = () =>
                        {
                            highlighted = new BitBoard();
                            renderer.ResetHighlights(renderHandle);
                            buildVector.p1 = mouse.GetLastClicked();
                            renderer.SetHighlight(renderHandle, Highlight.CellSelected, buildVector.p1);
                            foreach (PieceMove selectedMove in moves.Where(move => move.vector.p1 == mouse.GetLastClicked()))
                            {
                                highlighted[selectedMove.vector.p2] = true;
                                renderer.SetHighlight(renderHandle, selectedMove.type == MoveType.Move ? Highlight.MovePossible : Highlight.AttackMovePossible, selectedMove.vector.p2);
                            }
                        },
                        ptr = 1
                    },

                    new TransitionExpressioin
                    {
                        function = () => buildVector.p2 = mouse.GetLastClicked(),
                        ptr      = -1
                    }
                },

                    () =>
                {
                    mouse.WaitForInput();
                    return(highlighted[mouse.GetLastClicked()] ? 2 : (board[mouse.GetLastClicked()]?.GetColour() == GetColour() ? 1 : 0));
                })
            }).Run();

            renderer.ResetHighlights(renderHandle);

            PieceMove ret = moves.First(x => x.vector == buildVector);

            if (board[buildVector.p1]?.GetType() == Type.Pawn && (buildVector.p2.y == 0 || buildVector.p2.y == 7))
            {
                ret.additionalMoves = new Vector[] { new Vector(new Point((int)RequestPawnPromo().Value, GetColour() ? -1 : -2), buildVector.p2) }
            }
            ;

            return(ret);
        }
Exemple #8
0
 public PieceMovesMask(BitBoard attacks, BitBoard moves)
 {
     this.attacks = attacks;
     this.moves   = moves;
 }