C#WPF投注游戏奖金问题

时间:2017-09-14 13:08:41

标签: c# wpf dice

所以我一直在玩骰子投注游戏(如果两个骰子结合7,你就赢了,否则你输了)。我得到的问题是我不知道如何让我的投注筹码减少或上升。我已经将筹码设置为100,如果你赢了,你会获得10个筹码,如果你输了,你将获得10筹码。但是在输赢的情况下,我的筹码会达到110或90.我怎么能做到这一点,以便我的程序可以在最近的筹码数量上加10个筹码,而不是原来的100筹码。这是我目前的代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        #region dice throw

        Random num = new Random();
        int Number = new int();
        int Number1 = new int();
        int Chips = 100;

        Number1 = num.Next(1, 7);
        dice1.Text = Number1.ToString();
        Number = num.Next(1, 7);
        dice2.Text = Number.ToString();

        #endregion

        #region Win/lose

        if (Number + Number1 >= 7)
        {
            if (bet10.IsChecked == true)
            {
                totalChips.Text = Chips.ToString();
                Chips = Chips + 10;
            }
            if (bet20.IsChecked == true)
            {
                Chips = Chips + 20;
                totalChips.Text = Chips.ToString();
            }
            if (bet50.IsChecked == true)
            {
                Chips =  Chips + 50;
                totalChips.Text = Chips.ToString();
            }
            winloseblock.Text = "You Win!";

        }
        else
        {
            if (bet10.IsChecked == true)
            {
                Chips = Chips - 10;
                totalChips.Text = Chips.ToString();
            }
            if (bet20.IsChecked == true)
            {
                Chips = Chips - 20;
                totalChips.Text = Chips.ToString();
            }
            if (bet50.IsChecked == true)
            {
                Chips = Chips - 50;
                totalChips.Text = Chips.ToString();
            }
            winloseblock.Text = "You lose!";

        }

        #endregion
    }
}

1 个答案:

答案 0 :(得分:0)

每次单击按钮时,您都在初始化Chips变量:

private void button_Click(object sender, RoutedEventArgs e)
{
    #region dice throw
    Random num = new Random();
    int Number = new int();
    int Number1 = new int();
    int Chips = 100;

    ...
}

您应该通过在此方法之外移动声明来使Chips成为该类的字段:

int Chips = 100;    

private void button_Click(object sender, RoutedEventArgs e)
{
    #region dice throw
    Random num = new Random();
    int Number = new int();
    int Number1 = new int();

    ...
}