Esempio n. 1
0
        // This method moves player in n steps
        private void MovePlayer(MonopolyPlayer player, int steps)
        {
            bool startPassed = false;
            var  currentCell = _playersPosition[player];
            int  next        = currentCell.ID + steps;

            if (next > _board.ListOfMonopolyCell.Count)
            {
                next = next - _board.ListOfMonopolyCell.Count;
            }

            if (steps > 0 && next < currentCell.ID)
            {
                startPassed = true;
            }

            _playersPosition[player] = _board.ListOfMonopolyCell[next];

            Console.WriteLine("Moving " + player.Username + " to " + _playersPosition[player].Name);

            if (startPassed)
            {
                MonopolyCell cell = _board.ListOfMonopolyCell[0];
                ((StartCell)cell).Step();
            }


            CallCellEvent(_playersPosition[player]);
        }
Esempio n. 2
0
        private void CommunityCard3Action(MonopolyPlayer p)
        {
            // Move 3 spaces
            Console.WriteLine("Move 3 spaces");

            MovePlayer(p, 3);
        }
Esempio n. 3
0
        private void ChanceCard4Action(MonopolyPlayer p)
        {
            // Go To Jail
            Console.WriteLine("Go To Jail");

            _playersPosition[p] = _board.ListOfMonopolyCell[10];
            CallCellEvent(_playersPosition[p]);
        }
Esempio n. 4
0
        // This method moves player directly to a cell
        private void MovePlayer(MonopolyPlayer player, MonopolyCell cell)
        {
            _playersPosition[player] = cell;

            Console.WriteLine("Moving " + player.Username + " to " + _playersPosition[player].Name);

            CallCellEvent(_playersPosition[player]);
        }
Esempio n. 5
0
        public void JailOnCellStepped(object sender, EventArgs evt)
        {
            MonopolyCell   cell   = (MonopolyCell)sender;
            MonopolyPlayer player = GetPlayerFromCell((MonopolyCell)sender);

            Console.WriteLine(player.Username + " stepped in " + cell.Name);
            Console.WriteLine("No event");
        }
Esempio n. 6
0
        private void CommunityCard4Action(MonopolyPlayer p)
        {
            // Go to jail
            Console.WriteLine("Go to jail");

            // change playersPosition to jail
            _playersPosition[p] = _board.ListOfMonopolyCell[10];
            CallCellEvent(_playersPosition[p]);
        }
Esempio n. 7
0
        // Cell event handlers
        public void GoToJailOnCellStepped(object sender, EventArgs evt)
        {
            MonopolyCell   cell   = (MonopolyCell)sender;
            MonopolyPlayer player = GetPlayerFromCell(cell);

            Console.WriteLine(player.Username + " stepped in " + cell.Name);
            Console.WriteLine("Your position will move to jail");

            // Move to jail cell
            MovePlayer(player, _board.ListOfMonopolyCell[10]);
        }
Esempio n. 8
0
        public void PayTaxOnCellStepped(object sender, EventArgs evt)
        {
            int            price  = ((PayTaxCell)sender).Price;
            MonopolyPlayer player = GetPlayerFromCell((MonopolyCell)sender);
            MonopolyCell   cell   = (MonopolyCell)sender;

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

            if (!TransferMoneyToBank(player, price))
            {
                GameLose(player);
            }
        }
Esempio n. 9
0
        private bool TransferMoneyToPlayer(MonopolyPlayer fromPlayer, int price, MonopolyPlayer toPlayer)
        {
            Dictionary <MoneyType, int> amountPaid = DeductPlayerMoney(fromPlayer, price);

            if (amountPaid != null)
            {
                toPlayer.Receive(amountPaid);

                return(true);
            }
            else
            {
                return(false);
            }
        }
Esempio n. 10
0
        public void ActionOnCellStepped(object sender, EventArgs evt)
        {
            MonopolyCell      cell   = (MonopolyCell)sender;
            MonopolyPlayer    player = GetPlayerFromCell(cell);
            ActionCellEvtArgs args   = (ActionCellEvtArgs)evt;

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

            // Get action
            IActionCard             card       = GetActionCard(args.TypeCard);
            Action <MonopolyPlayer> cardAction = card.Action;

            // Do action
            cardAction(GetPlayerFromCell((MonopolyCell)sender));
        }
Esempio n. 11
0
        private bool TransferMoneyToBank(MonopolyPlayer fromPlayer, int price)
        {
            Dictionary <MoneyType, int> amountPaid = DeductPlayerMoney(fromPlayer, price);

            if (amountPaid != null)
            {
                //_bank.DepositMoney(amountPaid);
                _bank.DepositAndWithdrawWithChange(amountPaid);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Esempio n. 12
0
        private void ChanceCard5Action(MonopolyPlayer p)
        {
            // Advance to Cell 19 : Collect 200 if pass Start
            Console.WriteLine("Advance to Cell 19 : Collect {0} if pass Start", _moneyAtStartCell);

            MonopolyCell curretPosition = _playersPosition[p];
            int          step           = 0;

            if (curretPosition.ID >= 19)
            {
                step = 19 + (_board.ListOfMonopolyCell.Count - curretPosition.ID);
            }
            else
            {
                step = 19 - curretPosition.ID;
            }
            // change position
            MovePlayer(p, step);
        }
Esempio n. 13
0
        // Extra func
        private Dictionary <MoneyType, int> DeductPlayerMoney(MonopolyPlayer player, int price)
        {
            Tuple <ReturnState, Dictionary <MoneyType, int> > resp = new Tuple <ReturnState, Dictionary <MoneyType, int> >(0, null);

            while (resp.Item1 != ReturnState.Success)
            {
                resp = player.ValidateAndPay(price);

                switch (resp.Item1)
                {
                case ReturnState.InsufficientMoney:
                    Console.WriteLine("Insufficient money");
                    break;

                case ReturnState.PayPriceMismatchError:
                    Console.WriteLine("Error while paying, please retry");
                    break;

                case ReturnState.Success:
                    Console.WriteLine("Pay success");
                    break;

                default:
                    Console.WriteLine("Unrecognized return state");
                    break;
                }

                // Need to add function to check all assets
                // If still have assets, call sell prop or mrtgage
                // Else break;
            }

            if (resp.Item1 == ReturnState.Success)
            {
                return(resp.Item2);
            }
            else
            {
                return(null);
            }
        }
Esempio n. 14
0
        // Chance cards action
        private void ChanceCard1Action(MonopolyPlayer p)
        {
            // Advance to Start : Collect 200
            Console.WriteLine("Advance to Start : Collect {0}", _moneyAtStartCell);

            // change player position
            _playersPosition[p] = _board.ListOfMonopolyCell[0];

            int price = _moneyAtStartCell;
            Dictionary <MoneyType, int> inputPay = _bank.getInput(price);
            int counter = 1;
            int change  = _bank.ValidateGetInputPrice(price, inputPay, "pay");

            while (change < 0 && counter <= 5)
            {
                inputPay = _bank.getInput(price);
                counter++;
            }
            if (counter > 5)
            {
                return;
            }

            // check if change exist
            if (change > 0)
            {
                // change always true
                Dictionary <MoneyType, int> inputChange = _bank.getInput(change);
                if (_bank.ValidateGetInputPrice(change, inputChange, "receive") != 0)
                {
                    return;
                }
                _bank.DepositMoney(inputChange);
                p.Pay(inputChange);
            }

            // receive money from bank
            _bank.WithdrawMoney(inputPay);
            p.Receive(inputPay);
        }
Esempio n. 15
0
        // Community chest cards action
        private void CommunityCard1Action(MonopolyPlayer p)
        {
            // Bank Error : collect 200
            Console.WriteLine("Bank Error : collect 200");

            int price = 200;
            Dictionary <MoneyType, int> inputPay = _bank.getInput(price);
            int counter = 1;

            int change = _bank.ValidateGetInputPrice(price, inputPay, "pay");

            while (change < 0 && counter <= 5)
            {
                inputPay = _bank.getInput(price);
                counter++;
            }
            if (counter > 5)
            {
                return;
            }

            // check if change exist
            if (change > 0)
            {
                // change always true
                Dictionary <MoneyType, int> inputChange = _bank.getInput(change);
                if (_bank.ValidateGetInputPrice(change, inputChange, "receive") != 0)
                {
                    return;
                }
                _bank.DepositMoney(inputChange);
                p.Pay(inputChange);
            }

            // receive money from bank
            _bank.WithdrawMoney(inputPay);
            p.Receive(inputPay);
        }
Esempio n. 16
0
        public void StartOnPassed(object sender, EventArgs evt)
        {
            int            price  = _moneyAtStartCell;
            MonopolyPlayer player = (MonopolyPlayer)_currentPlayer;

            Console.WriteLine(player.Username + " Passed Start Cell. Get " + _moneyAtStartCell);

            Dictionary <MoneyType, int> inputPay = _bank.getInput(price);
            int counter = 1;
            int change  = _bank.ValidateGetInputPrice(price, inputPay, "pay");

            while (change < 0 && counter <= 5)
            {
                inputPay = _bank.getInput(price);
                counter++;
            }
            if (counter > 5)
            {
                return;
            }

            // check if change exist
            if (change > 0)
            {
                // change always true
                Dictionary <MoneyType, int> inputChange = _bank.getInput(change);
                if (_bank.ValidateGetInputPrice(change, inputChange, "receive") != 0)
                {
                    return;
                }
                _bank.DepositMoney(inputChange);
                player.Pay(inputChange);
            }

            // receive money from bank
            _bank.WithdrawMoney(inputPay);
            player.Receive(inputPay);
        }
Esempio n. 17
0
        private void ChanceCard2Action(MonopolyPlayer p)
        {
            // Pay poor tax  : Pay 15
            Console.WriteLine("Pay poor tax  : Pay 15");

            int price = 15;
            Dictionary <MoneyType, int> inputPay = p.getInput(price);
            int counter = 1;

            int change = p.ValidateGetInputPrice(price, inputPay, "pay");

            while (change < 0 && counter <= 5)
            {
                inputPay = p.getInput(price);
                counter++;
            }
            if (counter > 5)
            {
                return;
            }

            // check if change exist
            if (change > 0)
            {
                // change always true
                Dictionary <MoneyType, int> inputChange = p.getInput(change);
                if (p.ValidateGetInputPrice(change, inputChange, "receive") != 0)
                {
                    return;
                }
                p.Receive(inputChange);
                _bank.WithdrawMoney(inputChange);
            }

            //  pay to bank
            _bank.DepositMoney(inputPay);
            p.Pay(inputPay);
        }
Esempio n. 18
0
 private void GameLose(MonopolyPlayer player)
 {
     Console.WriteLine("Player " + player.Username + " is bankrupt!");
     _losers.Add(player);
 }
Esempio n. 19
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;
            }
        }