コード例 #1
0
        public void BankExchange(BankRefillEventArgs e, IMoneyHolder bank)
        {
            //TODO this is not even close to robust enough; needs to be able to convert to multiple types of coins
            while (e.Quarters > 0 && Wallet.Quarters >= 4 && bank.Dollars >= 1)
            {
                bank.Add(Wallet, 0, 0, 0, 4, 0);
                Wallet.Add(bank, 0, 0, 0, 0, 1);
                e.Quarters -= 4;
            }

            while (e.Dimes > 0 && Wallet.Dimes >= 2 && Wallet.Nickels >= 1 && bank.Quarters >= 1)
            {
                bank.Add(Wallet, 0, 1, 2, 0, 0);
                Wallet.Add(bank, 0, 0, 0, 1, 0);
                e.Dimes   -= 2;
                e.Nickels -= 1;
            }

            while (e.Nickels > 0 && Wallet.Nickels >= 2 && bank.Dimes >= 1)
            {
                bank.Add(Wallet, 0, 2, 0, 0, 0);
                Wallet.Add(bank, 0, 0, 1, 0, 0);
                e.Nickels -= 2;
            }

            while (e.Pennies > 0 && Wallet.Pennies >= 5 && bank.Nickels >= 1)
            {
                bank.Add(Wallet, 5, 0, 0, 0, 0);
                Wallet.Add(bank, 0, 1, 0, 0, 0);
                e.Pennies -= 5;
            }
            //TODO call bank exchange if it can't do an exchange?
        }
コード例 #2
0
        public void TakeMoney(int amount, IMoneyHolder bank, SpinnerSpaces restriction, IMoneyHolder pot = null)
        {
            IMoneyHolder destination  = restriction == SpinnerSpaces.MoneyMoney ? pot : Wallet;
            IMoneyHolder amountToTake = new Wallet();

            //int amountOfDollars, amountOfQuarters, amountOfDimes, amountOfNickels, amountOfPennies;
            while (true)
            {
                int cents = (amountToTake = amount.ToIMoneyHolder(restriction, bank)).ToCents();
                if (cents != amount)
                {
                    BankRefillEventArgs brea = new BankRefillEventArgs((amount - cents).ToIMoneyHolder(restriction));
                    CallForBankRefill(this, brea);
                    if (brea.CanExchange == false)
                    {
                        return;
                    }
                }
                else
                {
                    break;
                }
            }
            destination.Add(bank, amountToTake);
        }
コード例 #3
0
        public void BankExchange(BankExchangeSpace space, IMoneyHolder bank)
        {
            for (int i = 0; i < space.MaxExchanges; i++)
            {
                //Total up all the relevant money
                //int tradePennyNickles, tradeNickels, tradeDimes, tradeQuarters;
                Reset(out int tradePennyNickles, out int tradeNickels, out int tradeDimes, out int tradeQuarters);

                //Store the biggest coin these *could* add up to
                int tempValue = ((tradePennyNickles + tradeNickels) * 5) + (tradeDimes * 10) + (tradeQuarters * 25);
                int currentTarget;
                if (tempValue >= 100)
                {
                    currentTarget = 100;
                }
                else if (tempValue >= 25)
                {
                    currentTarget = 25;
                }
                else if (tempValue >= 10)
                {
                    currentTarget = 10;
                }
                else if (tempValue >= 5)
                {
                    currentTarget = 5;
                }
                else //I guess we aren't doing anything then...
                {
                    currentTarget = 0;
                }

                bool useQuarters = true;
                switch (currentTarget)
                {
                //Quick decision
                case 0:
                    tradePennyNickles = 0;
                    tradeNickels      = 0;
                    tradeDimes        = 0;
                    tradeQuarters     = 0;
                    break;

                case 5:
                    if (tradePennyNickles >= 1)
                    {
                        tradePennyNickles = 1;
                        tradeNickels      = 0;
                        tradeDimes        = 0;
                        tradeQuarters     = 0;
                    }
                    else
                    {
                        goto case 0;
                    }
                    break;

                case 10:
                    if (tradePennyNickles + tradeNickels >= 2)
                    {
                        tradePennyNickles = Math.Min(tradePennyNickles, 2);
                        tradeNickels      = Math.Min(tradeNickels, 2 - tradeNickels);
                        tradeDimes        = 0;
                        tradeQuarters     = 0;
                    }
                    else
                    {
                        goto case 5;
                    }
                    break;

                //Use the loop
                case 25:
                    useQuarters = false;
                    goto case 100;

                case 100:
                    //TODO use two for loops, one for the big decrement, then small decrement. might need to do somethin about cloning
                    bool useDimes, useNickels, usePennyNickels;
                    useDimes = useNickels = usePennyNickels = true;
                    bool wasLow     = false;
                    int  timesReset = 0;
                    while (true)
                    {
                        int currentValue = ((tradePennyNickles + tradeNickels) * 5) + (tradeDimes * 10) + (tradeQuarters * 25);
                        if (currentValue > currentTarget)
                        {
                            DecrementLowest(ref tradePennyNickles, ref tradeNickels, ref tradeDimes, ref tradeQuarters);
                            wasLow = false;
                        }
                        else if (currentValue < currentTarget)
                        {
                            //If we're low 2 times in a row, cancel and aim for 25
                            if (wasLow)
                            {
                                if (currentTarget == 100)
                                {
                                    currentTarget = 25;
                                    useQuarters   = false;
                                    timesReset    = 0;
                                }
                                if (currentTarget == 25)
                                {
                                    goto case 5;
                                }
                            }
                            Reset(out tradePennyNickles, out tradeNickels, out tradeDimes, out tradeQuarters, useQuarters, useDimes, useNickels, usePennyNickels);
                            timesReset++;
                            for (int j = 0; j < timesReset; j++)
                            {
                                DecrementHighest(ref tradePennyNickles, ref tradeNickels, ref tradeDimes, ref tradeQuarters);
                            }
                            wasLow = true;
                        }
                        else     //Break once we reach our target
                        {
                            break;
                        }
                    }
                    break;
                }

                //Actual trade
                if (currentTarget != 0)
                {
                    bank.Add(Wallet, tradePennyNickles * 5, tradeNickels, tradeDimes, tradeQuarters, 0);
                    Wallet.Add(bank, currentTarget.ToIMoneyHolder());
                    TakeMoney(space.Interest, bank, SpinnerSpaces.AnyCoins);
                }
            }
        }
コード例 #4
0
 public void CollectFromPot(IMoneyHolder pot)
 {
     Wallet.Add(pot);
 }