0

I am doing MVC and I am new at it. I am making a game where 5 dice roll by clicking on the 'roll dice' button and show random numbers. Under each die label there is a check box. If the player clicks on the check box the dice don't roll again and the other dice do roll.

I can show dice numbers in my view but I don't understand how to make it so the dice don't roll if you check the checkbox. I translated my program because it's in dutch: Waarde=value; dobbesteen=dice; werp=throw.

public class Dobbelsteen
{
    Random rnd = new Random();

    public int Waarde { get; set; }
    public bool Checked { get; set; }
    public int Worpen { get; set; }

    public void Rollen()
    {
        if (Checked == false)
            Waarde = rnd.Next(1, 7);

    }


}


public class BusinessController
{
    List<int> dice = new List<int>();

    Dobbelsteen objdobbelsteen = new Dobbelsteen();
    public BusinessController()
    {
    }
    public int Roll()
    {
        for (int i = 0; i < 5; i++)
        {
            objdobbelsteen.Rollen();
            dice.Add(i);
        }
        return objdobbelsteen.Waarde;
    }
    public int Werp()
    {
        objdobbelsteen.Worpen++;
        return objdobbelsteen.Worpen;
    }
    public int nietroll()
    {
        int i = 1;
            return i;

     }
    /*public bool Winnaar()
    {
        if (dice[1] == dice[2])
        {
            return true;
        }
        else return false;
    }*/

    public void SetLock(int p)
    {
     if(objdobbelsteen.Checked==false)
        {
            nietroll();
        }

    }
}


public partial class Form1 : Form
{
    BusinessController busniessController = new BusinessController();

    public Form1()
    {

        InitializeComponent();

    }
    public void Gooien_Click(object sender, EventArgs e)
    {

        lblWorpen.Text = Convert.ToString(busniessController.Werp());
        dblsteen1.Text = Convert.ToString(busniessController.Roll());
        dblsteen2.Text = Convert.ToString(busniessController.Roll());
        dblsteen3.Text = Convert.ToString(busniessController.Roll());
        dblsteen4.Text = Convert.ToString(busniessController.Roll());
        dblsteen5.Text = Convert.ToString(busniessController.Roll());


        if (dblsteen1.Text==dblsteen2.Text)
            MessageBox.Show("u win");
    }

    public void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
            busniessController.SetLock(1);
    }

    private void checkBox2_CheckedChanged(object sender, EventArgs e)
    {
        busniessController.SetLock(2);
    }

    private void checkBox3_CheckedChanged(object sender, EventArgs e)
    {
        busniessController.SetLock(3);
    }

    private void checkBox4_CheckedChanged(object sender, EventArgs e)
    {
        busniessController.SetLock(4);
    }

    private void checkBox5_CheckedChanged(object sender, EventArgs e)
    {
        busniessController.SetLock(5);
    }
}
AlG
  • 14,697
  • 4
  • 41
  • 54

2 Answers2

1

You need 5 different dices, not just one. In this way each dice can have its own Checked property set to a different value

List<Dobbelsteen> dices = new List<Dobbelsteen>()
{
    {new Dobbelsteen()},
    {new Dobbelsteen()},
    {new Dobbelsteen()},
    {new Dobbelsteen()},
    {new Dobbelsteen()}
};
public void RollAll()
{
    for (int i = 0; i < 5; i++)
        dices[i].Rollen();
}
public int GetDiceValue(int i)
{
   if(i >= 0 && i <= dices.Count)
      return dices[i].Waarde;
   else
      throw new IndexOutOfRangeException($"Invalid index {i}");
}
public void SetLock(int p)
{
   if(p >= 0 && p <= dices.Count)
      return dices[p].Checked = true;
   else
      throw new IndexOutOfRangeException($"Invalid index {p}");
}

Also you should make your Random variable of the class DobbelSteen static otherwise the strict loop inside the Rollen method results in the same value returned for every dice.

After this you can call

busniessController.RollAll();
dblsteen1.Text = busniessController.GetDiceValue(0).ToString();
dblsteen2.Text = busniessController.GetDiceValue(1).ToString();
dblsteen3.Text = busniessController.GetDiceValue(2).ToString();
dblsteen4.Text = busniessController.GetDiceValue(3).ToString();
dblsteen5.Text = busniessController.GetDiceValue(4).ToString();

Finally remember that in NET the index for a collection like a list starts at zero and ends to the collection count - 1. So your call to SetLock should be changed accordingly to avoid the exception

Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
0

You never actually set the "Checked" value, SetLock refers to the nietRol function which just sets an int. Here you need to set the "Checked" value to true.

And please programmeer niet in het Nederlands maar doe alles in het Engels.

D.J. Klomp
  • 2,429
  • 1
  • 15
  • 30