For some reason this code makes the numbers jump around in place value.
For an ex.
If I add the numbers up to up to 10 when I subtract one time it will change to 90 if I subtract again it goes to 80.
If I start adding again 90 turns into 01...
So somewhere in my code I must be telling it to do this...
I have tried a few different things but the same thing keeps happening.
int x;
int y;
Console.WriteLine("X:" + (x=0) + "Y:" + (y=0));
//Change Coordinates
while(true){
ConsoleKey key = Console.ReadKey().Key;
Console.SetCursorPosition(Console.CursorLeft, Console.CursorTop -1);
Console.WriteLine("X:"+x +"Y:"+y);
if (key == ConsoleKey.UpArrow){
y=y+1;
}
else if (key == ConsoleKey.DownArrow){
y=y-1;
}
}
You need
Console.WriteLine($"X:{x,2} Y:{y,2}");
You were not printing y for a start. But your main problem was that you were leaving old characters in the line once you had got to 2 digits. THis forces the output to be always 2 digits wide
Related
I've been working on a slotmachine in C# for practise purposes, and the machine itself works as intentional. The points system, however, does not. The game starts at 100 points, and if, for example, the player lose three 5-point bets and wins 40 points on the fourth bet, the expected points would be 100-20+40=120 points. For some reason however, the code treats ALL the previous bets as being 40 point wins as well, bringing the total to 100-20+160=240 points. If the player then lose the fifth 5-point bet, the score jumps to 75.
I start by setting the 'points' value to 100, which should then update everytime the 'game()' function is called upon.
public static void Main()
{
int points = 100;
int num = 0;
Console.WriteLine("Welcome to 'Slotmachine'!\nThe aim of this game is to get a score of 1000 or higher.\nYou lose if you reach 0 or lower.\nPress enter to play");
Console.ReadLine();
Console.Clear();
points = points + game(100);
while(points<1000 && points>0)
{
num = num + 1;
Console.WriteLine("You've played for "+num+" number of round(s)");
points = points + game(points);
}
}
The 'game()' funtion returns the players winnings, which is used to update the 'points' value (Suspect nr 1?).
Inside the game function I have a 'usrbet' which takes an input from the user (1-10), which is then fed into the 'slots()' function to determine the winnings (the 'points' that are fed from 'Main()' are checked to see what the user can bet)
Console.WriteLine("Here are your current points: "+points+"\nHow much would you like to bet?\nmin bet: 1\nmax bet: 10");
try
{
usrbet = Convert.ToInt32(Console.ReadLine());
}
catch
{
usrbet = 1;
}
winnings = slots(usrbet);
int RetWin = winnings - usrbet;
return RetWin;
Here's what the 'slots()' function does, with some examples of the winnings calculations
public static int slots(int usrbet)
{
int Win;
int x;
int y;
int z;
Random slot = new Random();
x = slot.Next(2,10);
y = slot.Next(2,10);
z = slot.Next(2,10);
Console.WriteLine(x+""+y+""+z);
Example 1
if(x == y && y == z && x== 7)
{
Win = usrbet*250;
Console.WriteLine("WOW! That's incredible, you just won "+Win+"!");
}
Example 2
else if(x == z)
{
Win = usrbet*5;
Console.WriteLine("Congratulations, you win "+Win+".");
}
Example 3
else
{
Win = 0;
Console.WriteLine("Ah, bummer. You didn't win anything this time.");
}
After that, the 'Win' value is sent back to 'game()', updating 'winnings'.
I apologize for poor formatting of the question. I'll include a link to the code, in case my problem lies elsewhere in the code: https://dotnetfiddle.net/D5TwL0
I've tried making arrays of the 'bet' and 'usrbet' variables, in an attempt to have a "new" value to update the 'points' with at every run of 'game()', but that changed absolutely nothing other than limiting how many times 'game()' can run before getting an overflow error.
It turns out that the problem wasn't with the code, but with the compiler. Dotnetfiddle is where I made the code and had the issue, but trying it in another compiler, it managed to count just fine.
I am using foreach loop to check answers entered by user to given 5 multiplication questions. (using instance to display 5 questions on start)
Now i an trying to add score counter and increment it by 1 if answer is correct and depending on score i will display stars .for example, if score is less than 3 then 1 star, if 4 then 2 stars and if 5 then 3 stars. I am getting only one star even if all answers are correct. Can anyone possibly tell me how to achieve this please?
I tried adding score++ inside if statement. but it does not increment score value.
public void CheckButton()
{
int answer;
foreach (TestModeQuestionUI _TestModeQuestionUIRefrence in testModeQuestionExampleList)
{
answer = _TestModeQuestionUIRefrence.GetAnswerInputField();
if ((_TestModeQuestionUIRefrence.a * _TestModeQuestionUIRefrence.b) == answer)
{
_TestModeQuestionUIRefrence.SetResultOfAnswerInputField(1);
score++;
}
else
{
_TestModeQuestionUIRefrence.SetResultOfAnswerInputField(2);
}
}
roundCompletePanel.SetActive(true);
CheckHowManyStars();
ShowRoundCompletePanel(score);
}
Just assuming here that
(_TestModeQuestionUIRefrence.a * _TestModeQuestionUIRefrence.b) == answer
are all float values. Never compare float directly using ==. They might never be equal even though logically they should. They might differ by a very small "epsilon" due to Floating point precision.
Use Mathf.Approximately instead which uses exactly that small "epsilon" for its equality definition.
if (Mathf.Approximately(_TestModeQuestionUIRefrence.a * _TestModeQuestionUIRefrence.b, answer))
It is similar to
if (_TestModeQuestionUIRefrence.a * _TestModeQuestionUIRefrence.b - answer <= diferenceSmallEnoughThreshold)
where differenceSmallEnoughThreshold would be a defined threshold to use for euality
else if (userAnswer.Equals("2"))
{
blankspace();
Console.WriteLine("How many squares would you like to see? ");
double num = Convert.ToInt32(Console.ReadLine());
while (0 >= num)
{
Console.WriteLine(num * num);
}
}
My problem is that I don't know where to go from here. I had previously been able to output just one square, but I need to know how to output a list instead of just a single number.
Here's what your code currently says:
If the user input is 2:
Add a blank space -- Console.WriteLine() ??
Ask how many squares they want
Get their input
Get the square of their input while their input is less than 0
As you can see, things go reasonably well until you hit the loop. What I think you're really trying to do is this:
Get their input
Generate that number of perfect squares
To accomplish this, your loop should look something more like this:
for (int i = 1; i <= num; i++)
{
Console.WriteLine(i * i);
}
This says the following:
Start at 1
square the current number (i)
add 1 to the number (i)
repeat until you reach the user's number (num)
So if the input is 3, you'll get 1, 2, and 3 squared. If you want to square specific numbers the logic needs to change a bit, but this should at least accomplish your basic goal.
EDIT: If you want to keep the while loop, all you really need to do is add num-- (subtract 1 from num) and switch the comparison to 0 <= num (assuming you want positive numbers) to stop the infinite loop. This will generate the squares in descending order (3, 2, 1) but accomplish the same result.
You're hitting an infinite loop because num never changes so if it doesn't start at 0, it can never be 0.
I created an IF Statement for my XNA game which makes my characters leave their classroom(screen). There are 40 characters, in lines of 10. 'Dest' means destination of the character. This code is in my update method and I have a list of characters in my initialize method:
if (activeCharacter.DestX < 680)
{
activeCharacter.DestX = activeCharacter.DestX + 6;
}
else if (activeCharacter.DestY < 600) //600
{
activeCharacter.DestY = activeCharacter.DestY + 6;
}
else if (activeCharacter.DestY == 600)
{
activeCharacter = classroom[(Random.Next(classroom.Count))];
Console.WriteLine(activeCharacter.DestY);
The aim is to have random characters leave the classroom. When a character from the 1st row leaves, another random character will leave straight after and another depending on if it's from the first row. However if a character from any other row except the 1st leaves, only one character will go.
Can someone please tell me stop this from happening?
Thanks!
DestY is incremented by 6, until it is bigger then or equal to 600. However the final else if only checks for equal to 600.
Changing the final check to:
else if (activeCharacter.DestY >= 600)
Or simply:
else
Will most likely solve your problem.
Hey there, So I'm knocking together a random pattern generation thing.
My code so far:
int permutes = 100;
int y = 31;
int x = 63;
while (permutes > 0) {
int rndTurn = random(1, 4);
if (rndTurn == 1) { y = y - 1; } //go up
if (rndTurn == 2) { y = y + 1; } //go down
if (rndTurn == 3) { x = x - 1; } //go right
if (rndTurn == 4) { x = x + 1; } //go left
setP(x, y, 1);
delay(250);
}
My question is, how would I go about getting the code to not go back on itself?
e.g. The code says "Go Left" but the next loop through it says "Go Right", how can I stop this?
NOTE: setP turns a specific pixel on.
Cheers peoples!
It depends on what you mean.
If you mean "avoid going back to a step I was most previously on" then you have to remember the direction of the last movement. That is if you move up your next movement can't be down.
If you mean "avoid going back on a spot you've ever been on" then you're going to have to remember every spot you've been on. This can be implemented efficiently with a hash table using a key with a class representing a coordinate with appropriate Equals/HashCode functions.
Since each square corresponds to a pixel, your coordinate space must be finite, so you could keep track of coordinates you've already visited.
If there's a corresponding getP function to determine if a pixel has already been turned on, you could just use that.
You remember the last direction and, using random(1,3), pick either of the remaining three, then store that as the last one.
Not sure if this approach will work.
Create a new variable called lastRndTurn as int, and assign this after your if statements.
Then add a new while loop after your int rndTurn = random(1, 4).
while (lastRndTurn == rndTurn)
{
rndTurn = random(1, 4);
}