In this program, whenever any of the statements under else occur, it doesn't go back to state 0 and perform the actions in that state, which is what I'm trying to do. Instead, the txtStatus, and txtScore boxes just continue to display "Roll again!" which is what is displayed when jumping to state 2. What am I doing wrong here?
int die1 = 0;
int die2 = 0;
int total = 0;
int state = 0;
int point = 0;
//int point2;
int score = 0;
//private int score;
//private int state;
public Form1()
{
InitializeComponent();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void pictureBox2_Click(object sender, EventArgs e)
{
}
private void btnRoll_Click(object sender, EventArgs e)
{
picDie1L.Visible = false;
picDie1R.Visible = false;
picDie2L.Visible = false;
picDie2R.Visible = false;
picDie3L.Visible = false;
picDie3R.Visible = false;
picDie4L.Visible = false;
picDie4R.Visible = false;
picDie5L.Visible = false;
picDie5R.Visible = false;
picDie6L.Visible = false;
picDie6R.Visible = false;
Random rand = new Random();
die1 = rand.Next(1, 7);
die2 = rand.Next(1, 7);
total = (die1 + die2);
txtDie1.Text = die1.ToString();
txtDie2.Text = die2.ToString();
txtTotal.Text = total.ToString();
{
if (die1 == 1)
{
picDie1L.Visible = true;
}
if (die1 == 2)
{
picDie2L.Visible = true;
}
if (die1 == 3)
{
picDie3L.Visible = true;
}
if (die1 == 4)
{
picDie4L.Visible = true;
}
if (die1 == 5)
{
picDie5L.Visible = true;
}
if (die1 == 6)
{
picDie6L.Visible = true;
}
if (die2 == 1)
{
picDie1R.Visible = true;
}
if (die2 == 2)
{
picDie2R.Visible = true;
}
if (die2 == 3)
{
picDie3R.Visible = true;
}
if (die2 == 4)
{
picDie4R.Visible = true;
}
if (die2 == 5)
{
picDie5R.Visible = true;
}
if (die2 == 6)
{
picDie6R.Visible = true;
}
if (state == 0)
{
picPuck4.Visible = false;
picPuck5.Visible = false;
picPuck6.Visible = false;
picPuck8.Visible = false;
picPuck9.Visible = false;
picPuck10.Visible = false;
txtStatus.Text = string.Empty;
state = 1;
txtPoint.Text = string.Empty;
}
}
if (state == 1)
{
if (total == 7 || total == 11)
{
txtStatus.Text = "You are a winner!";
score++;
txtScore.Text = Convert.ToString(score);
state = 0;
}
if (total == 2 || total == 3 || total == 12)
{
txtStatus.Text = "You lose. Play again!";
score--;
txtScore.Text = Convert.ToString(score);
state = 0;
}
if (total == 4 || total == 5 || total == 6 || total == 8 || total == 9 || total == 10 || total == 12)
{
txtStatus.Text = "Roll again!";
point = int.Parse(txtTotal.Text);
txtPoint.Text = point.ToString();
state = 2;
if (total == 4)
picPuck4.Visible = true;
if (total == 5)
picPuck5.Visible = true;
if (total == 6)
picPuck6.Visible = true;
if (total == 8)
picPuck8.Visible = true;
if (total == 9)
picPuck9.Visible = true;
if (total == 10)
picPuck10.Visible = true;
}
}
else
{
if (point == total)
{
txtStatus.Text = "You are a winner!";
score++;
txtScore.Text = Convert.ToString(score);
state = 0;
}
if (total == 7)
{
txtStatus.Text = "You lose. Play again!";
score--;
txtScore.Text = Convert.ToString(score);
state = 0;
}
if (total != 7 || point != total)
{
txtStatus.Text = "Roll again!";
state = 2;
}
}
}
}
}
I think you should change the last else
statement to this:
if (point == total)
{
txtStatus.Text = "You are a winner!";
score++;
txtScore.Text = Convert.ToString(score);
state = 0;
}
else if (total == 7)
{
txtStatus.Text = "You lose. Play again!";
score--;
txtScore.Text = Convert.ToString(score);
state = 0;
}
else
{
txtStatus.Text = "Roll again!";
state = 2;
}
Explanation: When total
has any value but 7 you switch state to 2. Because of that, when you roll again you immediately go the last else
statement and execute those 3 if
statements inside it. In this scenario, the last if
statement is always true (since total
is never 7 in this scenario) therefore the text remains "Roll again" and state is never changed (remains 2).
This will fix your issue, but I think you need to change the logic (for example, point
is always equal to total
, since point == txtTotal.text == total
which may not be desired behavior).
Hope this helps.