[英]Can't get simple c# craps game to work correctly
In craps, two 6 sided dice are rolled. 在掷骰子中,掷出两个6面骰子。 If the result is a 7 or 11. The player or wins automatically. 如果结果是7或11,则或会自动获胜。 If a 2, 3, or 12 is rolled, the player loses automatically. 如果掷出2、3或12,则玩家会自动输。 However, if another number is rolled, that number becomes the "point". 但是,如果滚动另一个数字,则该数字将成为“点”。 The player rolls again until either they roll the point again or a 7. If the point is rerolled the player wins. 玩家再次滚动直到他们再次掷出该点或7。如果该点被重新掷出,则玩家获胜。 If a 7 is rolled, this time it is a loss. 如果掷出7,则这是损失。
I'm having two issues with my code 我的代码有两个问题
State is used to say whether this is the first roll or one of subsequent rolls. 状态用于说这是第一卷还是后续卷之一。 I have omitted some of the code which is only used to display images of dice. 我省略了一些仅用于显示骰子图像的代码。
int die1;
int die2;
int total;
int state = 1;
int point =0;
int point2;
int score = 0;
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 (state == 1)
{
if (total == 7 || total == 11)
{
txtStatus.Text = "You are a winner!";
score++;
txtScore.Text = Convert.ToString(score);
state = 1;
}
if (total == 2 || total == 3 || total == 12)
{
txtStatus.Text = "You lose. Play again!";
score --;
txtScore.Text = Convert.ToString(score);
state = 1;
}
if (total == 4 || total == 5 || total == 6 || total == 8 || total == 9 || total == 10)
{
txtStatus.Text = "Roll again!";
point = int.Parse(txtTotal.Text);
txtPoint.Text = point.ToString();
state = 2;
}
}
if (state == 2)
{
if (total == point)
{
txtStatus.Text = "You are a winner!";
score ++;
txtScore.Text = Convert.ToString(score);
state = 1;
}
if (total == 7)
{
txtStatus.Text = "You lose. Play again!";
score --;
txtScore.Text = Convert.ToString(score);
state = 1;
}
if (total != 7 || total != point)
{
txtStatus.Text = "Roll again!";
state = 2;
}
}
First, change your code to read 首先,将代码更改为
else if (state == 2)
As written, your code first goes through the if (state==1)
section and then immediately proceeds into the if (state==2)
section. 如所写,您的代码首先经过if (state==1)
部分,然后立即进入if (state==2)
部分。
Also, you have left out the the looping and flow control portion of your code. 另外,您还省略了代码的循环和流控制部分。 You might be redefining state=1 on every pass -- make sure that's outside the loop when state==2
. 您可能会在每次通过时都重新定义state = 1-确保当state==2
时不在循环之内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.