Пример #1
0
        public void ClaimRoute(BoardRoute route, PlayerColor color)
        {
            var matchingRoute = Routes.Where(x => x.Origin == route.Origin &&
                                             x.Destination == route.Destination &&
                                             x.Color == route.Color &&
                                             x.Length == route.Length &&
                                             x.IsOccupied == false);

            if (matchingRoute.Any())
            {
                var firstRoute = matchingRoute.First();
                Routes.Remove(firstRoute);
                firstRoute.IsOccupied           = true;
                firstRoute.OccupyingPlayerColor = color;
                Routes.Add(firstRoute);
            }
        }
        public bool ClaimRoute(BoardRoute route, TrainColor color)
        {
            //If we don't have enough train cars remaining to claim this route, we cannot do so.
            if (route.Length > RemainingTrainCars)
            {
                return(false);
            }

            //First, see if we have enough cards in the hand to claim this route.
            var colorCards = Hand.Where(x => x.Color == color).ToList();

            //If we don't have enough color cards for this route...
            if (colorCards.Count < route.Length)
            {
                //...see if we have enough Locomotive cards to fill the gap
                var gap             = route.Length - colorCards.Count;
                var locomotiveCards = Hand.Where(x => x.Color == TrainColor.Locomotive).ToList();
                if (locomotiveCards.Count < gap)
                {
                    return(false); //Cannot claim this route.
                }

                var matchingWilds = Hand.GetMatching(TrainColor.Locomotive, gap);
                Board.DiscardPile.AddRange(matchingWilds);

                if (matchingWilds.Count != route.Length)
                {
                    var matchingColors = Hand.GetMatching(colorCards.First().Color, colorCards.Count);
                    Board.DiscardPile.AddRange(matchingColors);
                }


                Board.Routes.ClaimRoute(route, this.Color);

                //Add the cities to the list of connected cities
                ConnectedCities.Add(route.Origin);
                ConnectedCities.Add(route.Destination);

                ConnectedCities = ConnectedCities.Distinct().ToList();

                RemainingTrainCars = RemainingTrainCars - route.Length;

                Console.WriteLine(Name + " claims the route " + route.Origin + " to " + route.Destination + " (" + color + ")!");

                return(true);
            }

            //If we only need color cards to claim this route, discard the appropriate number of them
            var neededColorCards = Hand.Where(x => x.Color == color).Take(route.Length).ToList();

            foreach (var colorCard in neededColorCards)
            {
                Hand.Remove(colorCard);
                Board.DiscardPile.Add(colorCard);
            }

            //Mark the route as claimed on the board
            Board.Routes.ClaimRoute(route, this.Color);

            RemainingTrainCars = RemainingTrainCars - route.Length;

            Console.WriteLine(Name + " claims the route " + route.Origin + " to " + route.Destination + " (" + color + ")!");

            return(true);
        }