增量游戏数学:尝试计算和自动购买尽可能多的升级

本文关键字:尽可能 游戏 计算 | 更新日期: 2023-09-27 18:21:11

// CostNextUpgrade[n] simply equals the cost of the next upgrade of upgrade 'n', 
// if n == 0, the buymax boolean = true
if (CostNextUpgrade[UpgradeNumber] <= TotalCash) { AbilityToBuyOne = true; }
if (n == 0) { BuyMax = true; }
else { BuyMax = false; }
// Boolean BuyMax simply states true if they have selected option to buy maximum buildings.
// BUYS MAX AMOUNT OF BUILDINGS
while (BuyMax == true)
{
    // AbilityToBuyOne is determined above if they have the cash to buy 1 upgrade, 
    // this should loop until AbilityToBuyOne becomes false.
    if (AbilityToBuyOne == true)
    {
        TotalCash -= CostNextUpgrade[UpgradeNumber]; //Subtracts cost from total cash
        NumberOwned[UpgradeNumber] += 1; //Increments # owned
    }
}

按下购买按钮时调用此代码片段。如果玩家想要购买最大数量的"n",则由另一个函数将购买最大值设置为 true。这工作正常。我的问题是当我尝试使用此循环时,Unity 如何给出负无穷大误差。如果我有 $ 购买少量升级(1-50 左右(,那么代码工作正常。但除此之外,抛出了一个关于使用无穷大的十进制数学的错误。

增量游戏数学:尝试计算和自动购买尽可能多的升级

我认为你的逻辑都是错误的。您希望在下一次升级的成本低于可用现金总额时循环(BuyMax 为真或 NumberOfItemsLeftToBuy>= 1(

看看这些测试方法,这应该能够帮助你。

using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {            
            int[] CostNextUpgrade = new int[5];
            int[] NumberOwned = new int[5];
            for (int i = 0; i < CostNextUpgrade.Length; i++)
            {
                CostNextUpgrade[i] = i;
            }
            // Buy the max number of upgrade number 2 then exit
            int n = 0;
            bool BuyMax = n == 0; // BuyMax is true if n == 0
            int UpgradeNumber = 2;
            int TotalCash = 10;

            while (CostNextUpgrade[UpgradeNumber] <= TotalCash && 
                (BuyMax == true || n >= 1))
            {
                TotalCash -= CostNextUpgrade[UpgradeNumber];
                NumberOwned[UpgradeNumber] += 1;
                n--;
            }
            // Assert we can't afford another upgrade
            Assert.IsTrue(TotalCash < CostNextUpgrade[UpgradeNumber]); 
        }
        [TestMethod]
        public void TestMethod2()
        {
            int[] CostNextUpgrade = new int[5];
            int[] NumberOwned = new int[5];
            for (int i = 0; i < CostNextUpgrade.Length; i++)
            {
                CostNextUpgrade[i] = i;
            }
            // Buy 3 of upgrade number 2
            int n = 3;
            bool BuyMax = n == 0; // BuyMax is true if n == 0
            int UpgradeNumber = 2;
            int TotalCash = 10;

            while (CostNextUpgrade[UpgradeNumber] <= TotalCash &&
                (BuyMax == true || n >= 1))
            {
                TotalCash -= CostNextUpgrade[UpgradeNumber];
                NumberOwned[UpgradeNumber] += 1;
                n--;
            }
            // Assert we bought exactly 3
            Assert.AreEqual(NumberOwned[UpgradeNumber], 3);
        }
    }
}