骰子游戏没有正确显示奖金

时间:2012-10-02 16:59:32

标签: c#

我正在编写一个简单的骰子游戏。表格由3个文本框和多个标签组成(我稍后会将1个文本框更改为标签) 在左上角,将出现3个骰子值 从赌博文本框中留下总金额。

    在Gamble文本框中输入您的猜测
  • 在inzet文本框中输入您想要下注的金额
  • 在奖金文本框中
  • 应显示您赢得的金额

标准

  • 当你投掷3个六分时,你会获得20英镑的奖励
  • 当你扔掉一套低于6的东西时,你会获得10英镑的奖励
  • 当你扔掉任何东西时,你会得到5分的奖励
  • 当你的赌博是正确的时候你的inzet加倍(inzet = 1你赢了2)
  • 奖金必须显示奖金+ inzet

问题是当你的赌博等于总金额并且似乎仅适用+5奖金时,奖金才会更新。

以下是代码:

{
public partial class Form1 : Form
{
    private Random throwDice = new Random();
    private int total;
    private int inzet;
    private int gamble;
    private int prize;
    private int bonus;
    public Form1()
    {
        InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)
    {
        int die1, die2, die3;

        die1 = throwDice.Next(1, 7);
        die2 = throwDice.Next(1, 7);
        die3 = throwDice.Next(1, 7);

        label1.Text = die1.ToString();
        label2.Text = die2.ToString();
        label3.Text = die3.ToString();
        label4.Text = Convert.ToString(die1 + die2 + die3);

        total = Convert.ToInt32(label4.Text);
        inzet = Convert.ToInt32(textBox1.Text);
        gamble = Convert.ToInt32(textBox2.Text);

        if (gamble == total)
        {
            if (die1 == 6 && die1 == die2 && die1 == die3)
            {
                bonus = 20;
            }
            else if (die1 == die2 && die1 == die3)
            {
                bonus = 10;
            }
            else if (die1 == die2)
            {
                bonus = 5;
            }
            else if (die2 == die3)
            {
                bonus = 5;
            }
            else if (die1 == die3)
            {
                bonus = 5;
            }
            prize = inzet * 2 + bonus;
            textBox3.Text = prize.ToString();

        }
        else if (gamble < total && gamble > total)
        {
            if (die1 == 6 && die1 == die2 && die1 == die3)
            {
                bonus = 20;
            }
            else if (die1 == die2 && die1 == die3 )
            {
                bonus = 10;
            }
            else if (die1 == die2)
            {
                bonus = 5;
            }
            else if (die2 == die3)
            {
                bonus = 5;
            }
            else if (die1 == die3)
            {
                bonus = 5;
            }
            prize = (inzet - inzet) + bonus;
            textBox3.Text = prize.ToString();
        }
    }

}

我需要获得6 6 6的奖金(以及其他奖金)。 另外,当你的赌博不等于骰子总数时,我需要申请奖金。

3 个答案:

答案 0 :(得分:6)

这个条件:

    else if (gamble < total && gamble > total)

永远不会实现!

答案 1 :(得分:4)

这是你的问题:

else if (gamble < total && gamble > total)

这总是错误的。赌博不能小于和大于总和。

我认为您需要的只是else

答案 2 :(得分:3)

其他答案将找到您的根本原因(在同一比较中同时使用&lt;和&gt;)。

else if (gamble < total && gamble > total)

需要只是:

else

此错误是unit tests的绝佳广告。只需将结果提取到一个类(DiceResults)中,就可以轻松地测试正在运行的应用程序上下文之外的所有可能结果。现在你有重复的逻辑来计算奖金,如果你在更改你的奖金逻辑时忘记更新两者,这本身就很容易导致错误。

我并不是说我的解决方案是最好的代码(因为添加了更多的奖励规则,if / else if列表会变得笨拙 - design patterns可以帮助解决这个问题),但作为对代码的快速重构,它使得跟踪并最终进行测试变得更加简单。

我添加了一些测试作为例子;然后可以很容易地添加其他的以测试所有结果。当您添加到代码并进行更改时,您可以重新运行测试以检查您是否没有破坏任何内容。如果您使用的是Visual Studio,则可以run the unit tests easily

这是你的新事件处理程序:

private void button1_Click(object sender, EventArgs e)
{
    int die1 = throwDice.Next(1, 7);
    int die2 = throwDice.Next(1, 7);
    int die3 = throwDice.Next(1, 7);

    var results = new DiceResults(die1, die2, die3);

    label1.Text = results.Dice1;
    label2.Text = results.Dice2;
    label3.Text = results.Dice3;
    label4.Text = results.GetDiceTotal();

    inzet = Convert.ToInt32(textBox1.Text);
    gamble = Convert.ToInt32(textBox2.Text);

    int prize = results.GetPrize(gamble, inzet);
    textBox3.Text = prize;
}

支持它的新代码:

public class DiceResults
{
    public DiceResults(int dice1, int dice2, int dice3)
    {
        Dice1 = dice1;
        Dice2 = dice2;
        Dice3 = dice3;
    }

    public int Dice1 { get; private set; }
    public int Dice2 { get; private set; }
    public int Dice3 { get; private set; }

    public int GetBonus()
    {
        int bonus = 0;

        if (AllSixes())
        {
            bonus = 20;
        }
        else if (SetLessThanSix())
        {
            bonus = 10;
        }
        else if (AnyDouble())
        {
            bonus = 5;
        }
        return bonus;
    }

    public bool AllSixes()
    {
        return Dice1 == 6 &&
               Dice1 == Dice2 &&
               Dice2 == Dice3;
    }

    public bool SetLessThanSix()
    {
        return Dice1 < 6 &&
               Dice1 == Dice2 &&
               Dice2 == Dice3;
    }

    public bool AnyDouble()
    {
        return Dice1 == Dice2 ||
               Dice2 == Dice3 ||
               Dice1 == Dice3;
    }

    public int GetDiceTotal()
    {
        return Dice1 + Dice2 + Dice3;
    }

    public int GetPrize(int gamble, int inzet)
    {
        int prize = GetBonus();
        if (gamble == GetDiceTotal())
        {
            prize += inzet * 2;
        }
        return prize;
    }
}

单元测试:

[TestClass]
public class DiceResultsTest
{
    [TestMethod]
    public void TestDiceTotal()
    {
        //Arrange
        var results = new DiceResults(1, 2, 3);

        //Act
        int diceTotal = results.GetDiceTotal();

        //Assert
        Assert.AreEqual(6, diceTotal);
    }

    [TestMethod]
    public void TestForAllSixes()
    {
        //Arrange
        var results = new DiceResults(6, 6, 6);

        //Assert
        Assert.IsTrue(results.AllSixes());
    }

    [TestMethod]
    public void TestForAllFives()
    {
        //Arrange
        var results = new DiceResults(5, 5, 5);

        //Assert
        Assert.IsTrue(results.SetLessThanSix());
    }

    [TestMethod]
    public void TestForDoubleTwo()
    {
        //Arrange
        var results = new DiceResults(2, 5, 2);

        //Assert
        Assert.IsTrue(results.AnyDouble());
    }

    [TestMethod]
    public void TestBonusForAllSixes()
    {
        //Arrange
        var results = new DiceResults(6, 6, 6);

        //Act
        int bonus = results.GetBonus();

        //Assert
        Assert.AreEqual(20, bonus);
    }

    [TestMethod]
    public void TestBonusForAllFives()
    {
        //Arrange
        var results = new DiceResults(5, 5, 5);

        //Act
        int bonus = results.GetBonus();

        //Assert
        Assert.AreEqual(10, bonus);
    }

    [TestMethod]
    public void TestBonusForDoubleTwo()
    {
        //Arrange
        var results = new DiceResults(2, 5, 2);

        //Act
        int bonus = results.GetBonus();

        //Assert
        Assert.AreEqual(5, bonus);
    }

    [TestMethod]
    public void TestBonusWhenNoBonus()
    {
        //Arrange
        var results = new DiceResults(1, 2, 3);

        //Act
        int bonus = results.GetBonus();

        //Assert
        Assert.AreEqual(0, bonus);
    }

    [TestMethod]
    public void TestPrizeForAllSixesAndCorrectGamble()
    {
        //Arrange
        var results = new DiceResults(6, 6, 6);

        //Act
        var prize = results.GetPrize(18, 50);

        //Assert
        Assert.AreEqual(120, prize);
    }

    [TestMethod]
    public void TestPrizeForAllSixesAndIncorrectGamble()
    {
        //Arrange
        var results = new DiceResults(6, 6, 6);

        //Act
        var prize = results.GetPrize(15, 50);

        //Assert
        Assert.AreEqual(20, prize);
    }

    //add more tests to cover all cases.....
}