Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Math-Game/Math-Game.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Solution>
<Project Path="Math-Game/Math-Game.csproj" />
</Solution>
194 changes: 194 additions & 0 deletions Math-Game/Math-Game/Classes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
using System;


namespace Math_Game
{
internal class Addition
{
public void Start(List<string> games)
{
Console.WriteLine("How many questions would you like to answer? (It must be 5 or greater than 5)");
int numberOfQuestions = int.Parse(Console.ReadLine());
if(numberOfQuestions < 5)
{
Console.WriteLine("You must enter a number that is 5 or greater than 5.");
ConsoleHelper.Pause();
return;
}
Random random = new Random();
int score = 0;
for (int i = 0; i < numberOfQuestions; i++)
{
int num1 = random.Next(1, 100);
int num2 = random.Next(1, 100);
int answer = num1 + num2;
Console.WriteLine($"What is {num1} + {num2}?");
int userAnswer = int.Parse(Console.ReadLine());
if (userAnswer == answer)
{
Console.WriteLine("Correct!");
score++;
}
else
{
Console.WriteLine($"Incorrect. The correct answer is {answer}.");
}
}
Console.WriteLine($"Your score is {score} out of {numberOfQuestions}.");
games.Add($"Addition - score is {score}/{numberOfQuestions}");
ConsoleHelper.Pause();

}

}

internal class Subtraction
{
public void Start(List<string> games)
{
Console.WriteLine("How many questions would you like to answer? (It must be 5 or greater than 5)");
int numberOfQuestions = int.Parse(Console.ReadLine());
if (numberOfQuestions < 5)
{
Console.WriteLine("You must enter a number that is 5 or greater than 5.");
ConsoleHelper.Pause();
return;
}
Random random = new Random();
int score = 0;
for (int i = 0; i < numberOfQuestions; i++)
{
int num1 = random.Next(1, 100);
int num2 = random.Next(1, 100);
int answer = (num1 - num2);
Console.WriteLine($"What is {num1} - {num2}?");
int userAnswer = int.Parse(Console.ReadLine());
if (userAnswer == answer)
{
Console.WriteLine("Correct!");
score++;
}
else
{
Console.WriteLine($"Incorrect. The correct answer is {answer}.");
}
}
Console.WriteLine($"Your score is {score} out of {numberOfQuestions}.");
games.Add($"Subtraction - score is {score}/{numberOfQuestions}");
ConsoleHelper.Pause();
}
}

internal class Multiplication
{
public void Start(List<string> games)
{
Console.WriteLine("How many questions would you like to answer? (It must be 5 or greater than 5)");
int numberOfQuestions = int.Parse(Console.ReadLine());
if (numberOfQuestions < 5)
{
Console.WriteLine("You must enter a number that is 5 or greater than 5.");
ConsoleHelper.Pause();
return;
}
Random random = new Random();
int score = 0;
for (int i = 0; i < numberOfQuestions; i++)
{
int num1 = random.Next(1, 100);
int num2 = random.Next(1, 100);
int answer = num1 * num2;
Console.WriteLine($"What is {num1} * {num2}?");
int userAnswer = int.Parse(Console.ReadLine());
if (userAnswer == answer)
{
Console.WriteLine("Correct!");
score++;
}
else
{
Console.WriteLine($"Incorrect. The correct answer is {answer}.");
}
}
Console.WriteLine($"Your score is {score} out of {numberOfQuestions}.");
games.Add($"Multiplication - score is {score}/{numberOfQuestions}");
ConsoleHelper.Pause();
}
}

internal class Division
{
public void Start(List<string> games)
{
Console.WriteLine("How many questions would you like to answer? (It must be 5 or greater than 5)");
int numberOfQuestions = int.Parse(Console.ReadLine());
if (numberOfQuestions < 5)
{
Console.WriteLine("You must enter a number that is 5 or greater than 5.");
ConsoleHelper.Pause();
return;
}
Random random = new Random();
int score = 0;
for (int i = 0; i < numberOfQuestions; i++)
{
int num1, num2;
do
{
num1 = random.Next(1, 101);
num2 = random.Next(1, 101);
} while (num1 % num2 != 0);

int answer = num1 / num2;
Console.WriteLine($"What is {num1} / {num2}?");
int userAnswer = int.Parse(Console.ReadLine());
if (userAnswer == answer)
{
Console.WriteLine("Correct!");
score++;
}
else
{
Console.WriteLine($"Incorrect. The correct answer is {answer}.");
}
}
Console.WriteLine($"Your score is {score} out of {numberOfQuestions}.");
games.Add($" Division - score is {score}/{numberOfQuestions}");
ConsoleHelper.Pause();
}
}

internal class PreviousGameScore
{
public void Start(List<string> games)
{
Console.WriteLine("Here are your previous game scores:");
if (games.Count == 0)
{
Console.WriteLine("No games have been played yet.");
}
else
{
foreach (var game in games)
{
Console.WriteLine(game);
}
}
ConsoleHelper.Pause();

}

}

internal static class ConsoleHelper
{
public static void Pause()
{
Console.WriteLine();
Console.WriteLine("Press any key to return to the main menu...");
Console.ReadKey(true);
Console.Clear();
}
}

}
11 changes: 11 additions & 0 deletions Math-Game/Math-Game/Math-Game.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<RootNamespace>Math_Game</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
87 changes: 87 additions & 0 deletions Math-Game/Math-Game/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System;
using Math_Game;


Console.WriteLine(@" __ __ _ _ _ _ __ __ _ _ ____ _
\ \ / /__| | ___ ___ _ __ ___ ___ | |_ ___ | |_| |__ ___ | \/ | __ _| |_| |__ / ___| __ _ _ __ ___ ___ | |
\ \ /\ / / _ \ |/ __/ _ \| '_ ` _ \ / _ \ | __/ _ \ | __| '_ \ / _ \ | |\/| |/ _` | __| '_ \| | _ / _` | '_ ` _ \ / _ \ | |
\ V V / __/ | (_| (_) | | | | | | __/ | || (_) | | |_| | | | __/ | | | | (_| | |_| | | | |_| | (_| | | | | | | __/ |_|
\_/\_/ \___|_|\___\___/|_| |_| |_|\___| \__\___/ \__|_| |_|\___| |_| |_|\__,_|\__|_| |_|\____|\__,_|_| |_| |_|\___| (_)
");






Console.WriteLine("Please Enter your Name");
string name = Console.ReadLine();
bool isPlaying = true;

var games = new List<string>();

while (isPlaying)
{

Console.WriteLine($"Hello, {name}! Let's start the game.");
Console.WriteLine("What Game would you like to play?");
Console.WriteLine("1. Addition");
Console.WriteLine("2. Subtraction");
Console.WriteLine("3. Multiplication");
Console.WriteLine("4. Division");
Console.WriteLine("5. Previous Game Score");
Console.WriteLine("6. Exit");

int choice;
while (true)
{
string rawInput = Console.ReadLine()?.Trim();
if (string.IsNullOrWhiteSpace(rawInput))
{
Console.WriteLine("Input was empty. Please enter a number between 1 and 6.");
continue;
}
if (!int.TryParse(rawInput, out choice) || choice < 1 || choice > 6)
{
Console.WriteLine("Invalid selection. Please enter a number between 1 and 6.");
continue;
}
break;
}
switch (choice)
{
case 1:
Addition addition = new Addition();
addition.Start(games);
break;
case 2:
Subtraction subtraction = new Subtraction();
subtraction.Start(games);
break;
case 3:
Multiplication multiplication = new Multiplication();
multiplication.Start(games);
break;
case 4:
Division division = new Division();
division.Start(games);
break;

case 5:
PreviousGameScore previousGameScore = new PreviousGameScore();
previousGameScore.Start(games);
break;

case 6:
Console.WriteLine("Thank you for playing!");
isPlaying = false;
break;



default:
Console.WriteLine("Invalid choice. Please try again.");
ConsoleHelper.Pause();
break;
}
}
Loading