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
10 changes: 10 additions & 0 deletions MathGame.paddymcn/MathGame.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

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

</Project>
88 changes: 88 additions & 0 deletions MathGame.paddymcn/MathGameSession.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
namespace MathGame;

public class MathGameSession
{
public int Score { get; set; } = 0;
public int Answer { get; set; } = 0;
public int GameNumber { get; set; } = 0;

public List<string> GameHistory = new List<string>();

// Constructor
public MathGameSession()
{

}

// Method
public string AskQuestion(string type)
{
int rand1;
int rand2;
switch (type)
{
case "addition":
rand1 = randomNumber(0, 100);
rand2 = randomNumber(0, 100);
Answer = rand1 + rand2;
return $"What is {rand1} + {rand2}";
break;

case "substraction":
rand1 = randomNumber(0, 100);
rand2 = randomNumber(0, 100);
Answer = rand1 - rand2;
return $"What is {rand1} - {rand2}";
break;

case "multiplication":
rand1 = randomNumber(0, 100);
rand2 = randomNumber(0, 100);
Answer = rand1 * rand2;
return $"What is {rand1} * {rand2}";
break;

case "division":
int divisor = randomNumber(1, 10);
int quotient = randomNumber(1, 10);
int dividend = divisor * quotient;
Answer = quotient;
return $"What is {dividend} / {divisor}";
break;
default:
Console.WriteLine("No type returned.");
break;
}
return type;
}

public void CheckAnswer()
{
Console.WriteLine("Enter your answer:");
string input = Console.ReadLine();
int userAnswer;

while (!int.TryParse(input, out userAnswer))
{
Console.WriteLine("That's not a valid number. Please enter your answer again:");
input = Console.ReadLine();
}

if (userAnswer == Answer)
{
Console.WriteLine("Answer is correct.");
Score++;
}
else
{
Console.WriteLine("Wrong answer.");
}
}
int randomNumber(int min, int max)
{
var random = new Random();
var rNum = random.Next(min, max);
return rNum;
}
}

90 changes: 90 additions & 0 deletions MathGame.paddymcn/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using MathGame;
MathGameSession gameSession = new();
DisplayMenu();
void DisplayMenu()
{
int userAnswer=0;
string menuChoice = "";
while (menuChoice != "6")
{
Console.WriteLine("Menu\n====");
Console.WriteLine("1. Addition");
Console.WriteLine("2. Subtraction");
Console.WriteLine("3. Multiplication");
Console.WriteLine("4. Division");
Console.WriteLine("5. View History");
Console.WriteLine("6. Exit");
menuChoice = Console.ReadLine();
MenuChoice(menuChoice);
}
void MenuChoice(string menuChoice)
{

switch (menuChoice)
{
case "1":
// Addition game
for (int i = 0; i < 5; i++)
{
Console.WriteLine(gameSession.AskQuestion("addition"));
gameSession.CheckAnswer();
}
gameSession.GameNumber++;
gameSession.GameHistory.Add($"Game: {gameSession.GameNumber};Score: {gameSession.Score}");
gameSession.Score = 0;
break;

case "2":
// Subtraction game
for (int i = 0; i < 5; i++)
{
Console.WriteLine(gameSession.AskQuestion("substraction"));
gameSession.CheckAnswer();
}
gameSession.GameNumber++;
gameSession.GameHistory.Add($"Game: {gameSession.GameNumber};Score: {gameSession.Score}");
gameSession.Score = 0;
break;

case "3":
// Multiplication game
for (int i = 0; i < 5; i++)
{
Console.WriteLine(gameSession.AskQuestion("multiplication"));
gameSession.CheckAnswer();
}
gameSession.GameNumber++;
gameSession.GameHistory.Add($"Game: {gameSession.GameNumber};Score: {gameSession.Score}");
gameSession.Score = 0;
break;

case "4":
// Division
for (int i = 0; i < 5; i++)
{
Console.WriteLine(gameSession.AskQuestion("division"));
gameSession.CheckAnswer();
}
gameSession.GameNumber++;
gameSession.GameHistory.Add($"Game: {gameSession.GameNumber};Score: {gameSession.Score}");
gameSession.Score = 0;
break;

case "5":
foreach (var list in gameSession.GameHistory)
{
Console.WriteLine(list);
}
break;

case "6":
Console.WriteLine("Goodbye!");
break;

default:
Console.WriteLine("Invalid choice, try again.");
break;
}
}
}

Loading