Esempio n. 1
0
 public Tuple <ReturnState, Dictionary <MoneyType, int> > BuyLand(LandCard landCard, LandCell landCell)
 {
     if (!_landCards.Contains(landCard))
     {
         int price = landCell.Price;
         if (TotalMoney < price)
         {
             return(new Tuple <ReturnState, Dictionary <MoneyType, int> >(ReturnState.InsufficientMoney, null));
         }
         Tuple <ReturnState, Dictionary <MoneyType, int> > resp = DeductMoneyFromPlayer(price);
         if (resp.Item1 == ReturnState.Success)
         {
             _landCards.Add(landCard);
         }
         return(new Tuple <ReturnState, Dictionary <MoneyType, int> >(resp.Item1, resp.Item2));
     }
     return(new Tuple <ReturnState, Dictionary <MoneyType, int> >(ReturnState.LandAlreadyBoughtError, null));
 }
Esempio n. 2
0
        public void LandOnCellStepped(object sender, EventArgs evt)
        {
            LandCell       cell   = (LandCell)sender;
            MonopolyPlayer player = GetPlayerFromCell(cell);
            LandCard       card   = _cellToCardLink[cell];

            Console.WriteLine(player.Username + " stepped in " + cell.Name);

            Tuple <ReturnState, Dictionary <MoneyType, int> > buyState;
            string resp;

            // Check for ownership
            if (_cellsOwner[cell] == null) // If not owned by anyone
            {
                Console.Write("Do you want to buy this land? (y/n)");
                resp = Console.ReadLine();

                if (!resp.Equals("y") && !resp.Equals("Y"))
                {
                    return;
                }

                buyState = player.BuyLand(card, cell);
                if (buyState.Item1 != ReturnState.Success)
                {
                    Console.WriteLine("Error while buy this land");
                }
                else
                {
                    _bank.DepositMoney(buyState.Item2);
                }
            }
            else if (_cellsOwner[cell] != player) // If owned by other player
            {
                Console.WriteLine("This cell is owned by " + _cellsOwner[cell].Username);

                // Current player need to pay rent to the cell owner
                if (!TransferMoneyToPlayer(player, card.RentPrice, _cellsOwner[cell]))
                {
                    GameLose(player);
                }

                return;
            }
            else if (_cellsOwner[cell] == null) // If owned by himself
            {
                if (card.Properties.Count < 4)
                {
                    // Ask player want to buy house or not?
                    Console.Write("Do you want to buy house(s) on this land? (y/n)");
                    resp = Console.ReadLine();
                    Console.WriteLine();

                    if (!resp.Equals("y") && !resp.Equals("Y"))
                    {
                        return;
                    }

                    // Buy house 1-4
                    int parsed = -1;
                    while (parsed < 1 || parsed > 4)
                    {
                        Console.Write("How many? (1-4)");
                        parsed = int.Parse(Console.ReadLine());
                    }
                    buyState = player.BuyProperty(card, PropertyType.House, parsed);
                    if (buyState.Item1 != ReturnState.Success)
                    {
                        Console.WriteLine("Error while buy house on this land");
                    }
                    else
                    {
                        _bank.DepositMoney(buyState.Item2);
                    }
                }
                else if (card.Properties.Count == 4)
                {
                    // Ask player want to buy hotel or not?
                    Console.Write("Do you want to buy hotel on this land? (y/n)");
                    resp = Console.ReadLine();
                    Console.WriteLine();

                    if (!resp.Equals("y") && !resp.Equals("Y"))
                    {
                        return;
                    }

                    // Buy hotel
                    buyState = player.BuyProperty(card, PropertyType.Hotel, 1);
                    if (buyState.Item1 != ReturnState.Success)
                    {
                        Console.WriteLine("Error while buy hotel on this land");
                    }
                    else
                    {
                        _bank.DepositMoney(buyState.Item2);
                    }
                }
            }
            else
            {
                Console.WriteLine("Undefined behavior");
                return;
            }
        }