예제 #1
0
파일: Game.cs 프로젝트: benp44/ga-monopoly
        public bool PlayPlayerTurn(Player player)
        {
            if (Winner != null)
            {
                return(true);
            }

            PlayerTurnsPlayed++;

            var bContinueMoving = true;
            var doubleCount     = 0;

            while (bContinueMoving == true && player.Status != PlayerStatus.Bankrupt)
            {
                var diceResult = Dice.RollDice();

                player.LogEvent("Rolled " + diceResult.ToString());

                if (player.Status == PlayerStatus.InJail)
                {
                    // Players in jail don't get to move this turn,
                    // but there a several ways to get out

                    bContinueMoving = false;

                    if (diceResult.DoubleRolled)
                    {
                        // A double - free but no second roll this time.

                        player.FreeFromJail();
                    }
                    else if (player.GetOutOfJailFreeCards.Count > 0)
                    {
                        // GOOJF
                        player.FreeFromJail();

                        CardManager.ReturnGetOutOfJailFreeCard(player.GetOutOfJailFreeCards.Dequeue());
                    }
                    else if (player.Money > 50)
                    {
                        // Pay the bail

                        player.Pay(50, Bank, Bank);
                        player.FreeFromJail();
                    }
                    else
                    {
                        // No luck,  but only in jail for three turns,
                        // and will move on the next

                        player.TurnsInJail++;

                        if (player.TurnsInJail > 2)
                        {
                            player.FreeFromJail();
                        }
                    }
                }
                else // Not in jail
                {
                    // Handle three doubles

                    if (diceResult.DoubleRolled && (++doubleCount == 3))
                    {
                        SendToJail(player);
                        bContinueMoving = false;
                    }
                    else
                    {
                        bContinueMoving = diceResult.DoubleRolled;

                        // Move the player

                        player.Location = Board.AllSquares[(player.Location.Order + diceResult.Total) % Board.AllSquares.Count];

                        // If gone past go, collect 200

                        if (player.Location.Order - diceResult.Total < 0)
                        {
                            Bank.Pay(200, player, Bank);
                        }

                        if (player.Location is AssetSquare)
                        {
                            var assetSquare = (AssetSquare)player.Location;

                            if (assetSquare.Asset.Owner is Bank)
                            {
                                player.ConsiderPurchase(assetSquare.Asset);
                            }
                            else if (player.Assets.Contains(assetSquare.Asset) == false)
                            {
                                foreach (var otherPlayer in Players.Excluding(player))
                                {
                                    if (otherPlayer.Assets.Contains(assetSquare.Asset))
                                    {
                                        if (otherPlayer.Status != PlayerStatus.InJail)
                                        {
                                            var rent = assetSquare.Asset.CalculateRent(diceResult.Total);
                                            player.Pay(rent, otherPlayer, Bank);
                                        }

                                        break;
                                    }
                                }
                            }
                        }
                        else if (player.Location is SpecialSquare)
                        {
                            LandedOnSpecialSquare(player);
                        }
                        else
                        {
                            throw new ApplicationException("Unknown square type");
                        }
                    }
                }
            }

            if (player.Status == PlayerStatus.Normal)
            {
                player.ConsiderUnmortgaging();
                player.Trade(Players);
                player.ConsiderBuilding();
            }

            NextPlayer = Players[(Players.FindIndex(x => x == player) + 1) % Players.Count];

            // Win conditions

            if (Players.Where(x => x.Status != PlayerStatus.Bankrupt).Count() == 1)
            {
                Winner = Players.Where(x => x.Status != PlayerStatus.Bankrupt).First();
                return(true);
            }

            foreach (var activePlayer in ActivePlayers)
            {
                if (activePlayer.Money >= 20000)
                {
                    Winner = activePlayer;
                    return(true);
                }
            }

            return(false);
        }