首页 > 解决方案 > 需要在 Java 中决定一个球队的进球数(足球模拟器)

问题描述

我正在尝试制作基于文本的足球模拟。我发现很难编写决定球队进球数的方法。每个团队都有攻击和防御等级(目前是20)。我的程序功能性的,但我正在寻找使它更现实的方法,包括惊喜的机会(最近它们在 IRL 联盟中发生了很多)。这是我写的方法:

public static int determineGoals(Team t1, Team t2)
    // Determines how many goals a team scores by comparing attack and defense levels
    {
            double percent = randDouble(1, 100);
            int goals = 0;

            if (t1.attack - t2.defence >= 15)
            {
                    if (percent <= 5)
                    {
                            goals = 0;
                    }
                    else if (percent <= 25)
                    {
                            goals = rand(1, 2);
                    }
                    else if (percent <= 90)
                    {
                            goals = rand(3, 4);
                    }
                    else
                    {
                            goals = rand(5, 6);
                    }
            }

            else if (t1.attack - t2.defence >= 7)
            {
                    if (percent <= 10)
                    {
                            goals = 0;
                    }
                    else if (percent <= 30)
                    {
                            goals = rand(1, 2);
                    }
                    else if (percent <= 92)
                    {
                            goals = rand(3, 4);
                    }
                    else
                    {
                            goals = rand(5, 6);
                    }
            }

            else if (t1.attack - t2.defence >= 3)
            {
                    if (percent <= 20)
                    {
                            goals = 0;
                    }
                    else if (percent <= 55)
                    {
                            goals = rand(1, 2);
                    }
                    else if (percent <= 94)
                    {
                            goals = rand(3, 4);
                    }
                    else
                    {
                            goals = rand(5, 6);
                    }
            }

            else if (t1.attack - t2.defence >= 0)
            {
                    if (percent <= 25)
                    {
                            goals = 0;
                    }
                    else if (percent <= 70)
                    {
                            goals = rand(1, 2);
                    }
                    else if (percent <= 96)
                    {
                            goals = rand(3, 4);
                    }
                    else
                    {
                            goals = rand(5, 6);
                    }
            }

            else if (t1.attack - t2.defence >= -3)
            {
                    if (percent <= 30)
                    {
                            goals = 0;
                    }
                    else if (percent <= 75)
                    {
                            goals = rand(1, 2);
                    }
                    else if (percent <= 98)
                    {
                            goals = rand(3, 4);
                    }
                    else
                    {
                            goals = rand(5, 6);
                    }
            }

            else if (t1.attack - t2.defence >= -7)
            {
                    if (percent <= 35)
                    {
                            goals = 0;
                    }
                    else if (percent <= 85)
                    {
                            goals = rand(1, 2);
                    }
                    else
                    {
                            goals = rand(3, 4);
                    }
            }

            else if (t1.attack - t2.defence >= -15)
            {
                    if (percent <= 65)
                    {
                            goals = 0;
                    }
                    else if (percent <= 95)
                    {
                            goals = rand(1, 2);
                    }
                    else
                    {
                            goals = rand(3, 4);
                    }
            }

            else
            {
                    if (percent <= 75)
                    {
                            goals = 0;
                    }
                    else
                    {
                            goals = rand(1, 2);
                    }
            }

            return goals;
    }

如果您有任何想法,我想听听他们,包括全新的算法想法。

编辑:另外,我意识到我使用随机双精度但将其视为 int (1-100),我会改变它。

标签: java

解决方案


我会使用随机双生成器函数来随机化攻击和防御分数。通过这样做,您不会得到“直接结果”,但它们可能会以令人惊讶的方式扭曲结果。您可以为此研究高斯分布以“引导”它一点点。


推荐阅读