C
C#14mo ago
Mekasu0124

✅ C# Academy Math Game Issues

public void StartDivGame(string username, DateTime date, int totalQuestions, string difficulty)
{
int score = 0;
Stopwatch timer = new Stopwatch();

timer.Start();
for (int i = 0; i < totalQuestions; i++)
{
int[] numbers = GetNumber(difficulty);

int number1 = numbers[0];
int number2 = numbers[1];

while (number1 % number2 != 0)
{
numbers = GetNumber(difficulty);

number1 = numbers[0];
number2 = numbers[1];
}

int solution = number1 / number2;
string question = $"{number1} / {number2} = ";

Console.ForegroundColor = ConsoleColor.White;
Console.Write(question);
string guess = Console.ReadLine();
int input = Helpers.ValidateNumericInput(question, guess);

if (input == solution)
{
score++;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"\nYou Got It Right! Score: {score}");

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\nPress Any Key For Next Question");
Console.ReadLine();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\nOh No. You Got It Wrong. Next Question");

Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine($"Correct Solution: {solution}");

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\nPress Any Key For Next Question");
Console.ReadLine();
}

Console.Clear();
}

timer.Stop();

GameModel newGame = CreateGame(username, date, "Multiplication", score, totalQuestions, difficulty, timer.Elapsed.ToString(@"hh\:mm\:ss"));
PreviousGames.SaveGame(newGame);

Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("Game Over! Press Any Key To Go To Main Menu");
Console.ReadLine();

GameMenu.ShowMenu(username, date);
}
private static int[] GetNumber(string difficulty)
{
Random rng = new();
int firstNumber = 0;
int secondNumber = 0;
var result = new int[2];

if (difficulty == "Easy")
{
firstNumber = rng.Next(0, 99);
secondNumber = rng.Next(0, 99);
}
else if (difficulty == "Medium")
{
firstNumber = rng.Next(100, 199);
secondNumber = rng.Next(100,199);
}
else if (difficulty == "Hard")
{
firstNumber = rng.Next(200,299);
secondNumber = rng.Next(200,299);
}

result[0] = firstNumber;
result[1] = secondNumber;

return result;
}
public void StartDivGame(string username, DateTime date, int totalQuestions, string difficulty)
{
int score = 0;
Stopwatch timer = new Stopwatch();

timer.Start();
for (int i = 0; i < totalQuestions; i++)
{
int[] numbers = GetNumber(difficulty);

int number1 = numbers[0];
int number2 = numbers[1];

while (number1 % number2 != 0)
{
numbers = GetNumber(difficulty);

number1 = numbers[0];
number2 = numbers[1];
}

int solution = number1 / number2;
string question = $"{number1} / {number2} = ";

Console.ForegroundColor = ConsoleColor.White;
Console.Write(question);
string guess = Console.ReadLine();
int input = Helpers.ValidateNumericInput(question, guess);

if (input == solution)
{
score++;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"\nYou Got It Right! Score: {score}");

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\nPress Any Key For Next Question");
Console.ReadLine();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\nOh No. You Got It Wrong. Next Question");

Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine($"Correct Solution: {solution}");

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\nPress Any Key For Next Question");
Console.ReadLine();
}

Console.Clear();
}

timer.Stop();

GameModel newGame = CreateGame(username, date, "Multiplication", score, totalQuestions, difficulty, timer.Elapsed.ToString(@"hh\:mm\:ss"));
PreviousGames.SaveGame(newGame);

Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("Game Over! Press Any Key To Go To Main Menu");
Console.ReadLine();

GameMenu.ShowMenu(username, date);
}
private static int[] GetNumber(string difficulty)
{
Random rng = new();
int firstNumber = 0;
int secondNumber = 0;
var result = new int[2];

if (difficulty == "Easy")
{
firstNumber = rng.Next(0, 99);
secondNumber = rng.Next(0, 99);
}
else if (difficulty == "Medium")
{
firstNumber = rng.Next(100, 199);
secondNumber = rng.Next(100,199);
}
else if (difficulty == "Hard")
{
firstNumber = rng.Next(200,299);
secondNumber = rng.Next(200,299);
}

result[0] = firstNumber;
result[1] = secondNumber;

return result;
}
so I've decided to start going through the projects of The C# Academy and the first one was a math game. Everything works, and I passed all their tests, however, when playing the game, I often get errors of the game attempting to divide by zero, and when I select the Medium or Hard difficulty, it gives the same number like 173/173 for the equation. Their requirements were The division should result in integers only and dividends should go from 0 to 100. I can't figure out how to fix this. Thanks
37 Replies
Angius
Angius14mo ago
Well, on easy you generate numbers between 0 and 99... so a 0 will be generated every now and then And you can't divide by 0, so that's why The lower bound for a random.Next() is inclusive and the upper bound is exclusive So, for example, the possible numbers that random.Next(0, 5) can generate are 0, 1, 2, 3, 4
Mekasu0124
Mekasu0124OP14mo ago
I changed it to be 1,99 instead of 0,99 thank you 🙂 right. I didn't think about that part. so inclusive meaning it will include 1, but exclusive meaning it won't include 99? so change 99 to 100
Angius
Angius14mo ago
Exactly
Mekasu0124
Mekasu0124OP14mo ago
ok cool. so I've made that change and I currently have
private static int[] GetNumber(string difficulty)
{
Random rng = new();
int firstNumber = 0;
int secondNumber = 0;
var result = new int[2];

if (difficulty == "Easy")
{
firstNumber = rng.Next(1, 100);
secondNumber = rng.Next(1, 100);
}
else if (difficulty == "Medium")
{
firstNumber = rng.Next(100, 199);
secondNumber = rng.Next(100,199);
}
else if (difficulty == "Hard")
{
firstNumber = rng.Next(200,299);
secondNumber = rng.Next(200,299);
}

result[0] = firstNumber;
result[1] = secondNumber;

return result;
}
private static int[] GetNumber(string difficulty)
{
Random rng = new();
int firstNumber = 0;
int secondNumber = 0;
var result = new int[2];

if (difficulty == "Easy")
{
firstNumber = rng.Next(1, 100);
secondNumber = rng.Next(1, 100);
}
else if (difficulty == "Medium")
{
firstNumber = rng.Next(100, 199);
secondNumber = rng.Next(100,199);
}
else if (difficulty == "Hard")
{
firstNumber = rng.Next(200,299);
secondNumber = rng.Next(200,299);
}

result[0] = firstNumber;
result[1] = secondNumber;

return result;
}
so I need to change the 199 and 299 to the even hundred 200 and 300
Angius
Angius14mo ago
Yep
Mekasu0124
Mekasu0124OP14mo ago
so running the program again with those changes, I'm still getting the same number for num1 and num2 like 179/179 instead of something like 135/125 the result has to be integers only, and I know that there's more division options with numbers than just those that result in 1 for an option
Angius
Angius14mo ago
Right, how often does it happen that you get two of the same number?
Mekasu0124
Mekasu0124OP14mo ago
every single question
Mekasu0124
Mekasu0124OP14mo ago
Angius
Angius14mo ago
Well that's certainly weird
Mekasu0124
Mekasu0124OP14mo ago
public void StartDivGame(string username, DateTime date, int totalQuestions, string difficulty)
{
int score = 0;
Stopwatch timer = new Stopwatch();

timer.Start();
for (int i = 0; i < totalQuestions; i++)
{
int[] numbers = GetNumber(difficulty);

int number1 = numbers[0];
int number2 = numbers[1];

while (number1 % number2 != 0)
{
numbers = GetNumber(difficulty);

number1 = numbers[0];
number2 = numbers[1];
}

int solution = number1 / number2;
string question = $"{number1} / {number2} = ";

Console.ForegroundColor = ConsoleColor.White;
Console.Write(question);
string guess = Console.ReadLine();
int input = Helpers.ValidateNumericInput(question, guess);

if (input == solution)
{
score++;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"\nYou Got It Right! Score: {score}");

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\nPress Any Key For Next Question");
Console.ReadLine();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\nOh No. You Got It Wrong. Next Question");

Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine($"Correct Solution: {solution}");

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\nPress Any Key For Next Question");
Console.ReadLine();
}

Console.Clear();
}

timer.Stop();

GameModel newGame = CreateGame(username, date, "Multiplication", score, totalQuestions, difficulty, timer.Elapsed.ToString(@"hh\:mm\:ss"));
PreviousGames.SaveGame(newGame);

Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("Game Over! Press Any Key To Go To Main Menu");
Console.ReadLine();

GameMenu.ShowMenu(username, date);
}
public void StartDivGame(string username, DateTime date, int totalQuestions, string difficulty)
{
int score = 0;
Stopwatch timer = new Stopwatch();

timer.Start();
for (int i = 0; i < totalQuestions; i++)
{
int[] numbers = GetNumber(difficulty);

int number1 = numbers[0];
int number2 = numbers[1];

while (number1 % number2 != 0)
{
numbers = GetNumber(difficulty);

number1 = numbers[0];
number2 = numbers[1];
}

int solution = number1 / number2;
string question = $"{number1} / {number2} = ";

Console.ForegroundColor = ConsoleColor.White;
Console.Write(question);
string guess = Console.ReadLine();
int input = Helpers.ValidateNumericInput(question, guess);

if (input == solution)
{
score++;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"\nYou Got It Right! Score: {score}");

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\nPress Any Key For Next Question");
Console.ReadLine();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\nOh No. You Got It Wrong. Next Question");

Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine($"Correct Solution: {solution}");

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\nPress Any Key For Next Question");
Console.ReadLine();
}

Console.Clear();
}

timer.Stop();

GameModel newGame = CreateGame(username, date, "Multiplication", score, totalQuestions, difficulty, timer.Elapsed.ToString(@"hh\:mm\:ss"));
PreviousGames.SaveGame(newGame);

Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("Game Over! Press Any Key To Go To Main Menu");
Console.ReadLine();

GameMenu.ShowMenu(username, date);
}
this is the division game function
Angius
Angius14mo ago
It really shouldn't be generating same results
No description
Angius
Angius14mo ago
Wait, does it happen with medium difficulty as well?
Mekasu0124
Mekasu0124OP14mo ago
yes
Angius
Angius14mo ago
I know why it happens
Mekasu0124
Mekasu0124OP14mo ago
watch it be something simple and I feel dumb 😂
Angius
Angius14mo ago
The only possible way a division can happen without a rest, or without decimal spaces, is when both numbers are the same It's true for ranges of numbers above 100, within that 100 So for 100-200 range, 200-300, etc
Mekasu0124
Mekasu0124OP14mo ago
so do I need to make the ranges like 100-400 and 400-800?
Angius
Angius14mo ago
Let's take the smallest number in 100-199 range. It's 100 The only number that can be divided by 100 in that range is... 100
Mekasu0124
Mekasu0124OP14mo ago
I'm with you so far
Angius
Angius14mo ago
So yes, you would either need to increase the ranges, or allow decimal spaces in the results
Mekasu0124
Mekasu0124OP14mo ago
I'll have to increase the ranges because they want integers only in the results I also have another question. I was trying to get it to where the question would show the bigger number / smaller number, but trying to do that kept resulting in a 0 divisor
Angius
Angius14mo ago
Alternatively, make the answer be a decimal number, but allow the answer to be within a certain range So the result is 78.7, and it should accept 79 as the answer For example
Mekasu0124
Mekasu0124OP14mo ago
won't converting the double to an int cause the rounding up/down to happen automatically?
Angius
Angius14mo ago
Depends whether you ceil it, floor it, or round it But yeah
Mekasu0124
Mekasu0124OP14mo ago
int[] numbers = GetNumber(difficulty);
int number1 = numbers[0];
int number2 = numbers[1];

- while (number1 % number2 != 0)
- {
- numbers = GetNumber(difficulty);
- number1 = numbers[0];
- number2 = numbers[1];
- }

- int solution = number1 / number2;
+ int solution = Math.Round(number1 / number2);
int[] numbers = GetNumber(difficulty);
int number1 = numbers[0];
int number2 = numbers[1];

- while (number1 % number2 != 0)
- {
- numbers = GetNumber(difficulty);
- number1 = numbers[0];
- number2 = numbers[1];
- }

- int solution = number1 / number2;
+ int solution = Math.Round(number1 / number2);
so this should be how it's written then, yes? and that will allow me to keep my current ranges?
Angius
Angius14mo ago
Yep Then again, worth testing what the actual results will be Entirely possible they will all round to either 0 or 1
Mekasu0124
Mekasu0124OP14mo ago
it's telling me that this is an ambiguous invocation but won't tell me what the actual problem is
No description
Angius
Angius14mo ago
Seems the only possible results will be 0, 1, or 2
No description
Mekasu0124
Mekasu0124OP14mo ago
I've tried
double number1 = numbers[0];
double number2 = numbers[1];
int solution = Math.Round(number1 / number2);
double number1 = numbers[0];
double number2 = numbers[1];
int solution = Math.Round(number1 / number2);
and I have tried
int number1;
int number2;
int solution = (int)Math.Round((double)number1 / (double)number2);
int number1;
int number2;
int solution = (int)Math.Round((double)number1 / (double)number2);
and it's not liking anything I'm doing
Angius
Angius14mo ago
I think if you want to increase difficulty you should just be increasing the upper bound, but not the lower bound So 0-100 for easy, 0-200 for medium, 0-300 for hard That way you're going to get the least amount of issues Or, well, lower bound being 1
Mekasu0124
Mekasu0124OP14mo ago
ok I changed the ranges. That's not a bad idea but I'm still having problems figuring out the cast types
private static int[] GetNumber(string difficulty)
{
Random rng = new();
int firstNumber = 0;
int secondNumber = 0;
var result = new int[2];

if (difficulty == "Easy")
{
firstNumber = rng.Next(1, 100);
secondNumber = rng.Next(1, 100);
}
else if (difficulty == "Medium")
{
firstNumber = rng.Next(1, 200);
secondNumber = rng.Next(1,200);
}
else if (difficulty == "Hard")
{
firstNumber = rng.Next(1,300);
secondNumber = rng.Next(1,300);
}

result[0] = firstNumber;
result[1] = secondNumber;

return result;
}
private static int[] GetNumber(string difficulty)
{
Random rng = new();
int firstNumber = 0;
int secondNumber = 0;
var result = new int[2];

if (difficulty == "Easy")
{
firstNumber = rng.Next(1, 100);
secondNumber = rng.Next(1, 100);
}
else if (difficulty == "Medium")
{
firstNumber = rng.Next(1, 200);
secondNumber = rng.Next(1,200);
}
else if (difficulty == "Hard")
{
firstNumber = rng.Next(1,300);
secondNumber = rng.Next(1,300);
}

result[0] = firstNumber;
result[1] = secondNumber;

return result;
}
here's the updated ranges
Angius
Angius14mo ago
Now you can keep your integer division, the check and all No need to fiddle with decimals now Yeah, this works
Mekasu0124
Mekasu0124OP14mo ago
ok I put the while-loop back in. running a test
Mekasu0124
Mekasu0124OP14mo ago
no double numbers now 🙂 thank you!!!
Angius
Angius14mo ago
Nice
Mekasu0124
Mekasu0124OP14mo ago
thank you again for your help!
Want results from more Discord servers?
Add your server