Exemple #1
0
        private void RankCandidate(List <Location> PossibleMoves, Dictionary <Location, Square> Map, Location l, int offset)
        {
            Location next = LocationFactory.Build(l, 0, offset);

            while (next != null)
            {
                KeyValuePair <Location, Square> m = new KeyValuePair <Location, Square>();
                foreach (KeyValuePair <Location, Square> SquareInstance in Map)
                {
                    if (next.Equals(SquareInstance.Key))
                    {
                        m = SquareInstance;
                    }
                }

                if (m.Key != null)
                {
                    if (Map[m.Key].IsOccupied)
                    {
                        Console.WriteLine("hit piece");
                        if (Map[m.Key].CurrentPiece.Color == this.Color)
                        {
                            break;
                        }
                        PossibleMoves.Add(next);
                        break;
                    }

                    PossibleMoves.Add(next);
                }

                next = LocationFactory.Build(next, 0, offset);
            }
        }
Exemple #2
0
        public override List <Location> GetValidMoves(ChessBoard board)
        {
            List <Location> PossibleMoves = new List <Location>();
            Location        l             = this.CurrentLocation;

            Console.WriteLine(l.File);
            int direction = 0;

            if (this.Color == PieceColor.WHITE)
            {
                direction = 1;
            }
            else
            {
                direction = -1;
            }

            // first move
            if (IsFirstMove)
            {
                PossibleMoves.Add(LocationFactory.Build(l, 0, 2 * direction));
            }

            // normal move
            PossibleMoves.Add(LocationFactory.Build(l, 0, 1 * direction));

            // capturing moves
            PossibleMoves.Add(LocationFactory.Build(l, 1 * direction, 1 * direction));
            PossibleMoves.Add(LocationFactory.Build(l, -1 * direction, 1 * direction));
            Console.WriteLine(PossibleMoves.Count);
            PossibleMoves.RemoveAll(n => n == null);

            // Moves based on board dimensions
            List <Location> ValidMoves = new List <Location>();

            for (int i = 0; i < PossibleMoves.Count(); i++)
            {
                foreach (KeyValuePair <Location, Square> m in board.Map)
                {
                    if (PossibleMoves[i].Equals(m.Key))
                    {
                        ValidMoves.Add(m.Key);
                    }
                }
            }

            // Moves based on board condition
            List <Location> OccupiedFileMoves = ValidMoves.Where(m => m.File == l.File && board.Map[m].IsOccupied).ToList();

            return(ValidMoves.Where(m => {
                if (m.File == l.File)
                {
                    foreach (Location f in OccupiedFileMoves)
                    {
                        if (m.File == f.File && m.Rank > f.Rank)
                        {
                            return false;
                        }
                    }
                    if (board.Map[m].IsOccupied)
                    {
                        return false;
                    }
                }
                else if ((m.File != l.File) && (board.Map[m].IsOccupied))
                {
                    return !(board.Map[m].CurrentPiece.Color == this.Color);
                }
                else if ((m.File != l.File) && !(board.Map[m].IsOccupied))
                {
                    return false;
                }
                return true;
            }).ToList());
        }