Skip to content
Open
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 Sangeerths.MathGame/Sangeerths.MathGame.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Solution>
<Project Path="Sangeerths.MathGame/Sangeerths.MathGame.csproj" />
</Solution>
165 changes: 165 additions & 0 deletions Sangeerths.MathGame/Sangeerths.MathGame/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
using System.Diagnostics;
using Spectre.Console;

namespace MathGame
{
class Program
{
static void Main(string[] args)
{
List<GameHistory> history = new List<GameHistory>();
while (true)
{
var choice = AnsiConsole.Prompt(new SelectionPrompt<string>().Title("* * MENU * * ").AddChoices("Quiz me ..", "Previous Scores", "Exit"));
int points = 0;
switch (choice)
{

case "Quiz me ..":
var timer = new Stopwatch();
timer.Start();
var op = AnsiConsole.Prompt(new SelectionPrompt<string>().Title("Choose an operation:").AddChoices("+", "-", "*", "/"));
points = MathOperation(op);
timer.Stop();

history.Add(new GameHistory
{
Date = DateTime.Now,
GameType = op,
Score = points,
TimeTaken = timer.Elapsed
});

break;
case "Previous Scores":
if (history.Count == 0)
{
Console.WriteLine("No games played yet.");
}
else
{
Console.WriteLine("\nGame History\n");

foreach (var game in history)
{
Console.WriteLine(
$"Date : {game.Date:dd-MM-yyyy HH:mm:ss}\n" +
$"Game : {game.GameType}\n" +
$"Score: {game.Score}\n" +
$"Time : {game.TimeTaken.TotalSeconds:F2} seconds\n");
}
}
break;
case "Exit":
Environment.Exit(0);
break;
default:
Console.WriteLine("Invalid choice");
break;

}


}
}

static int MathOperation(string op)
{
Random random = new Random();
int points = 0;
int count = 0;
while (count < 5)
{
int a = random.Next(1, 100);
int b = random.Next(1, 100);
int answer = 0;
switch (op)
{

case "+":

Console.WriteLine($"What is {a} + {b} ");
if (!int.TryParse(Console.ReadLine(), out answer))
{
Console.WriteLine("Invalid input.");
}
else if (answer == a + b)
{
Console.WriteLine("Correct!");
points += 10;
}
else
{
Console.WriteLine($"Wrong! The correct answer is {a + b}");
}
break;
case "-":
Console.WriteLine($"What is {a} - {b}");
if (!int.TryParse(Console.ReadLine(), out answer))
{
Console.WriteLine("Invalid input.");
}
else if (answer == a - b)
{
Console.WriteLine("Correct!");
points += 10;
}
else
{
Console.WriteLine($"Wrong! The correct answer is {a + b}");
}
break;
case "*":
Console.WriteLine($"What is {a} * {b}");
if (!int.TryParse(Console.ReadLine(), out answer))
{
Console.WriteLine("Invalid input.");
}
else if (answer == a * b)
{
Console.WriteLine("Correct!");
points += 10;
}
else
{
Console.WriteLine($"Wrong! The correct answer is {a + b}");
}
break;
case "/":
Console.WriteLine($"What is {a} / {b}");
if (!int.TryParse(Console.ReadLine(), out answer))
{
Console.WriteLine("Invalid input.");
}
else if (answer == a / b)
{
Console.WriteLine("Correct!");
points += 10;
}
else
{
Console.WriteLine($"Wrong! The correct answer is {a + b}");
}
break;

default:
Console.WriteLine("Invalid operation");
break;
}
count++;
}

return points;

}

class GameHistory
{
public DateTime Date { get; set; }
public string GameType { get; set; }
public int Score { get; set; }
public TimeSpan TimeTaken { get; set; }
}

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

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

<ItemGroup>
<PackageReference Include="Spectre.Console" Version="0.57.1" />
</ItemGroup>

</Project>