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.
maybe the problem is here
if (total != 7 || point != total)
{
txtStatus.Text = "Roll again!";
state = 2;
}
as you see the condition is most of the time true! the only time that this is false is when total == point == 7
Related
I'm trying to give the letters in my richtextbox different colors for my subnet calculator, but the richtextbox doesn't change the colors until the 26th letter.
How it looks:
int iValueSm = trackBarSmMask.Value;
rtbScroll.Text = "";
rtbScroll.SelectionStart = rtbScroll.TextLength;
rtbScroll.SelectionLength = 0;
for (int i = 1; i <= iValueSm; i++)
{
rtbScroll.SelectionColor = Color.Blue;
rtbScroll.AppendText("N");
if (i%8==0 && i != 32)
{
rtbScroll.Text = rtbScroll.Text + ".";
}
}
for (int i = iValueSm+1; i <= 32; i++)
{
rtbScroll.SelectionColor = Color.Red;
rtbScroll.AppendText("H");
if (i % 8 == 0 && i != 32)
{
rtbScroll.Text = rtbScroll.Text + ".";
}
}
labelAmountNetID.Text = "/" + iValueSm.ToString();
Well, can be a lot of approaches to deal with this problem but here is one suggestion:
// Track bar definitions...
private void SetTrackBarVals()
{
trackBar1.Minimum = 0;
trackBar1.Maximum = 31;
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
var counter = 0;
var dotsCounter = 0;
rtbScroll.Text = "";
int iValueSm = trackBar1.Value + 1; // +1 because we start counting from 0
for (int i = 1; i <= 32; i++)
{
if (counter > 0 && counter % 8 == 0)
{
// new octet
rtbScroll.AppendText(".");
dotsCounter++;
}
if (i > iValueSm)
{
// It is red
rtbScroll.AppendText("H");
rtbScroll.SelectionStart = (i - 1) + dotsCounter;
rtbScroll.SelectionLength = 1 ;
rtbScroll.SelectionColor = Color.Red;
}
else
{
rtbScroll.AppendText("N");
}
counter++;
}
}
Anytime you set the .Text() property, you RESET all formatting back to black and white.
Here is how I'd write it using SelectedText:
private void Form1_Load(object sender, EventArgs e)
{
updateRTB();
}
private void trackBarSmMask_ValueChanged(object sender, EventArgs e)
{
updateRTB();
}
private void trackBarSmMask_Scroll(object sender, EventArgs e)
{
updateRTB();
}
private void updateRTB()
{
rtbScroll.Text = "";
rtbScroll.SelectionStart = 0;
rtbScroll.SelectionLength = 0;
int iValueSm = trackBarSmMask.Value;
labelAmountNetID.Text = "/" + iValueSm.ToString();
for (int i = 1; i <= 32; i++)
{
rtbScroll.SelectionColor = (i <= iValueSm) ? Color.Blue : Color.Red;
rtbScroll.SelectedText = (i <= iValueSm) ? "N" : "H";
if (i % 8 == 0 && i != 32)
{
rtbScroll.SelectionColor = Color.Black;
rtbScroll.SelectedText = ".";
}
}
}
the code below is meant to detect when the player presses a key (a s or d) but it only responds to d and I can't figure out why. I'm pretty new to coding and this is for a school project in visual studio c#
p1 to 3 detects what character you're using and when you press a button it makes the idle animation stop( called auto) you cna only attack when your energy is full
private void FrmFinalProject_KeyDown(object sender, KeyEventArgs e)
{
if (p1 == 2)
{
if (e.KeyCode == Keys.D)
{
if (p1Energy >= 10)
{
tmrBatAtk2.Enabled = true;
picBatAuto.Visible = false;
picBatAtk2.Visible = true;
p1Energy = 0;
prgP1Bar.Value = 0;
pgrHpP2.Value = pgrHpP2.Value - 25;
}
if (e.KeyCode == Keys.S)
{
if (p1Energy >= 10)
{
tmrBatAtk2.Enabled = true;
picBatAuto.Visible = false;
picBatAtk1.Visible = true;
p1Energy = 0;
prgP1Bar.Value = 0;
pgrHpP2.Value = pgrHpP2.Value - 30;
}
}
if (e.KeyCode == Keys.A)
{
if (p1Energy >= 10)
{
tmrBatAtk2.Enabled = true;
picBatAuto.Visible = false;
picBatAtk3.Visible = true;
p1Energy = 0;
prgP1Bar.Value = 0;
pgrHpP2.Value = pgrHpP2.Value - 35;
}
}
}
}
if (p1 == 1)
{
if (e.KeyCode == Keys.D)
{
if (p1Energy >= 10)
{
tmrFriendAtk.Enabled = true;
PicFriendAuto.Visible = false;
picFriendAtk1.Visible = true;
p1Energy = 0;
prgP1Bar.Value = 0;
pgrHpP2.Value = pgrHpP2.Value - 25;
}
if (e.KeyCode == Keys.S)
{
if (p1Energy >= 10)
{
tmrFriendAtk.Enabled = true;
PicFriendAuto.Visible = false;
PicFriendAtk2.Visible = true;
p1Energy = 0;
prgP1Bar.Value = 0;
pgrHpP2.Value = pgrHpP2.Value - 30;
}
}
if (e.KeyCode == Keys.A)
{
if (p1Energy >= 10)
{
tmrFriendAtk.Enabled = true;
PicFriendAuto.Visible = false;
picFriendatk3.Visible = true;
p1Energy = 0;
prgP1Bar.Value = 0;
pgrHpP2.Value = pgrHpP2.Value - 35;
}
}
}
}
if (p1 == 3)
{
if (e.KeyCode == Keys.D)
{
if (p1Energy >= 10)
{
tmrKnightAtk.Enabled = true;
picKnightAuto.Visible = false;
knightatk1.Visible = true;
p1Energy = 0;
prgP1Bar.Value = 0;
pgrHpP2.Value = pgrHpP2.Value - 25;
}
if (e.KeyCode == Keys.S)
{
if (p1Energy >= 10)
{
tmrKnightAtk.Enabled = true;
picKnightAuto.Visible = false;
knightAtk2.Visible = true;
p1Energy = 0;
prgP1Bar.Value = 0;
pgrHpP2.Value = pgrHpP2.Value - 30;
}
}
if (e.KeyCode == Keys.A)
{
if (p1Energy >= 10)
{
tmrKnightAtk.Enabled = true;
picKnightAuto.Visible = false;
knightAtk3.Visible = true;
p1Energy = 0;
prgP1Bar.Value = 0;
pgrHpP2.Value = pgrHpP2.Value - 35;
}
}
}
}
}
As indicated by others in the comments, the problem is your bracketing for the IF statements. If you want to check the same thing, here a KeyPress, then it needs to be on the same level. Look at the image where I have added a green line and red line to show statements on the same level.
Your check for the keypress should all be on the green line, like the first one is, but the rest are not.
So to solve your problem you need to place an additional closing } bracket after your inner if statement (and do this for every one)
But the reason why you have this problem is because your code is too complex, so that when you have a bug like this it is difficult to find because it is difficult to read. You also repeat the same check in multiple places (p1Energy >= 10) when you only need this once.
So you need to refactor your code. Using switch statements and sub-methods means that you can better read your code, find bugs, and understand what is going on.
private void FrmFinalProject_KeyDown(object sender, KeyEventArgs e)
{
int p1 = 2, p1Energy = 1;
//if p1Energy >= 10 needs to be checked for every case, so we just check once at the beginning
if (p1Energy >= 10)
{
//Now we use a switch to check the value of p1, and call sub-methods to perform the code
switch (p1)
{
case 1: Process1(e);
break;
case 2: Process2(e);
break;
case 3: Process3(e);
break;
}
}
So now you call a sub-method Process1/2/3() for each case. Which would look something like this:
private void Process2(KeyEventArgs e)
{
switch(e.KeyCode)
{
case Keys.D: Process_2_D();
break;
case Keys.A: Process_2_A();
break;
case Keys.S: Process_2_S();
break;
}
}
(Repeat for each case)
This then calls a sub-method for each sub-case which could look something like this
private void Process_2_D()
{
tmrBatAtk2.Enabled = true;
picBatAuto.Visible = false;
picBatAtk2.Visible = true;
p1Energy = 0;
prgP1Bar.Value = 0;
pgrHpP2.Value = pgrHpP2.Value - 25;
}
(Repeat for each case)
Although I would not say that this is brilliant code, it is now easier to read, and more importantly, much easier to debug.
This code can probably be improved significantly to be more effective. But not knowing your program in detail then it is difficult to suggest more. Plus, as you are a beginner this is probably enough. And as this is a school project, then you first need to learn and if you turn up with amazing code then it will be clear that you have not coded it!!
At the end of the day, although you can spend a lot of time engineering a beautiful system, the end result may not be worth the effort in every case, especially for private projects where you are probably the only person working on this and will never or hardly ever change it.
When I step through the program using the debugger it works fine and the displays the correct results. However, when I run the program it displays some extremely high number for homeTeamRuns and awayTeamRuns. I can't seem to figure out the problem.
class BallGame
{
//Fields
private string output = "";
int outs = 0;
int runs = 0;
int homeTeamRuns = 0;
int awayTeamRuns = 0;
bool homeTeam = false;
bool awayTeam = false;
int[] innings = new int[12]; //Innings
//Properties
public string Output
{
get
{
return output;
}
set
{
output = value;
}
}
//Game Simulator
public void playBall()
{
int[] play = new int[4]; //Bases
for(int i = 1; i <= 2; i++)
{
homeTeam = false;
awayTeam = false;
if(i%2 == 0)
homeTeam = true;
else
awayTeam = true;
//Half Inning
while (outs < 3)
{
bool yourOut = false, single = false, double1 = false, triple = false, homerun = false;
Random rnd1 = new Random();
int randomNum = rnd1.Next(1, 101);
//
if (randomNum >= 1 && randomNum <= 50) //50% chance
yourOut = true;
else if (randomNum > 50 && randomNum <= 60) //10% chance
single = true;
else if (randomNum > 60 && randomNum <= 92) //42% chance
double1 = true;
else if (randomNum > 92 && randomNum <= 97) //5% chance
triple = true;
else if (randomNum > 97 && randomNum <= 100) //%3 chance
homerun = true;
if (yourOut == true)
{
outs++;
}
else if (single == true)
{
Shift(play);
runScored(play);
play[0] = 1;
}
else if (double1 == true)
{
Shift(play);
runScored(play);
Shift(play);
runScored(play);
play[1] = 1;
}
else if (triple == true)
{
Shift(play);
runScored(play);
Shift(play);
runScored(play);
Shift(play);
runScored(play);
play[2] = 1;
}
else if (homerun == true)
{
Shift(play);
runScored(play);
Shift(play);
runScored(play);
Shift(play);
runScored(play);
Shift(play);
runScored(play);
play[3] = 1;
}
play[3] = 0;
if (outs == 3)
{
play[0] = 0;
play[1] = 0;
play[2] = 0;
play[3] = 0;
}
}
outs = 0;
}
output = "Home Team Runs: " + homeTeamRuns + " Away Team Runs: " + awayTeamRuns;
}
//Shift Array
public int[] Shift(int[] array)
{
for (int i = array.Length -1; i >= 1; i--)
{
array[i] = array[i - 1];
}
array[0] = 0;
return array;
}
public void runScored(int[] array)
{
if(array[3] == 1)
{
if(awayTeam == true)
awayTeamRuns++;
else if(homeTeam == true)
homeTeamRuns++;
}
}
}
You are creating a new instance of the Random class on each iteration of your loop. The documentation for the constructor you are using says:
Initializes a new instance of the Random class, using a time-dependent default seed value.
As the loop executes very quickly when you aren't debugging you end up with the same seed being used on many iterations and thus the call to Next returns the same value each time. If that value happens to be a value that will increment the score then the score will be incremented on many iterations of the loop.
Moving the line
Random rnd1 = new Random();
above your for loop will construct one Random instance and then create a new random number for each call to Next.
You can't reproduce this when running in the debugger as the clock has moved on by the time you create a new Random instance and thus the seed has changed.
More information on the Random instance returning the same value can be found in this StackOverflow post. Also, Jon Skeet has a blog post covering pitfalls when using the Random class which is well worth a read.
This question already has answers here:
How can I test for primality?
(16 answers)
Closed 8 years ago.
I need to use a boolean variable to identify if a number inserted in a text box is a prime number and be written in C#
protected void isPrimeButton_Click(object sender, EventArgs e)
{
int TestNumber = int.Parse(primeNumberTextBox.Text);
bool isPrime = true;
for (int i = 0; i < TestNumber; i++)
{
while (TestNumber % i == 0)
{
bool isPrime = true;
yesNoPrimeTextBox.Text = "prime";
break;
}
while (TestNumber % i == 0)
{
bool isPrime = false;
yesNoPrimeTextBox.Text = "not prime";
break;
}
}
}
Extract IsPrime as a method and you'll have something like this:
public static Boolean IsPrime(int value) {
if (value <= 1)
return false;
else if (value <= 3) // 2 and 3 are primes
return true;
else if (value % 2 == 0) // even numbers (2 excluded) are not primes
return false;
// Test odd numbers 3, 5, 7, ... as potential dividers
// up to square root of the value
int n = (int) (Math.Sqrt(value) + 1);
for (int i = 3; i <= n; i += 2)
if (value % i == 0)
return false;
return true;
}
...
protected void isPrimeButton_Click(object sender, EventArgs e) {
int testNumber;
if (!int.TryParse(primeNumberTextBox.Text, out testNumber)) {
// primeNumberTextBox.Text is not a int (incorrect format)
//TODO: probably you have to put some text into yesNoPrimeTextBox.Text
return;
}
if (IsPrime(testNumber))
yesNoPrimeTextBox.Text = "prime";
else
yesNoPrimeTextBox.Text = "not prime";
}
Use this method to check :
Also consider changing the return type , maybe you want it to return boolean instead of string
public static string CheckPrime(int number)
{
bool isPrime = true;
for (int i = 2; i < number; i++)
{
if (number % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
return number.ToString() + " is a Prime number";
}
else
{
return number.ToString() + " is not a Prime number";
}
}
Not very efficient, but this should work:
protected void isPrimeButton_Click(object sender, EventArgs e)
{
int TestNumber = int.Parse(primeNumberTextBox.Text);
bool isPrime = true;
for (int i = 2; i < TestNumber; i++)
{
if (TestNumber % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
yesNoPrimeTextBox.Text = "prime";
else
yesNoPrimeTextBox.Text = "not prime";
}
This should do it.
protected void isPrimeButton_Click(object sender, EventArgs e)
{
int TestNumber = int.Parse(primeNumberTextBox.Text);
bool isPrime = false;
for (int i = 2; i < TestNumber-1; i++)
{
if (TestNumber % i == 0)
{
isPrime = true;
yesNoPrimeTextBox.Text = "prime";
break;
}
}
}
I try make timers without thread.sleep to stop lag so any ideas how to make this code without thread.sleep but it stop 20ms. Thanks for help! :) And sorry my bad english.
Timer BulletTimer = new Timer();
BulletTimer.Tick += (s, args) => BulletKillPlayer(Bullet);
BulletTimer.Interval = 20;
BulletTimer.Start();
private void BulletKillPlayer(Bullet)
{
bool pX, pY, pD1, pD2, nX, nY, nD1, nD2;
pX = false; pY = false; pD1 = false; pD2 = false; nX = false; nY = false;
for (int i = 1; i < 20; i++)
{
if (User.Range >= i)
{
Thread.Sleep(20);
foreach (MapItem MapItem in MapItem.Boxs.Values)
{
if (MapItem.X == Bullet.X && MapItem.Y == Bullet.Y + i && !pX) { pX = BreakBox(Bullet, MapItem); KillPlayer(Bullet, MapItem); }
if (MapItem.X == Bullet.X && MapItem.Y == Bullet.Y - i && !pY) { pY = BreakBox(Bullet, MapItem); KillPlayer(Bullet, MapItem); }
if (MapItem.X == Bullet.X + i && MapItem.Y == Bullet.Y && !nX) { nX = BreakBox(Bullet, MapItem); KillPlayer(Bullet, MapItem); }
if (MapItem.X == Bullet.X - i && MapItem.Y == Bullet.Y && !nY) { nY = BreakBox(Bullet, MapItem); KillPlayer(Bullet, MapItem); }
}
}
}
}