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
14 changes: 14 additions & 0 deletions MathGame.AidanLDev/MathGame.AidanLDev.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.2" />
</ItemGroup>

</Project>
234 changes: 234 additions & 0 deletions MathGame.AidanLDev/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
using Spectre.Console;

Random rng = new();

bool stillPlaying = true;
int curGame = 1;
int curScore = 0;
int totalScore = 0;
List<string> gameHistory = [];


Dictionary<int, char> operators = new()
{
{ 1, '+' },
{ 2, '-' },
{ 3, '*' },
{ 4, '/' }
};

void addToGameHistory(int num1, int num2, int res, int usersRes, int curScore, char mathSymbol)
{
gameHistory.Add($"{num1} {mathSymbol} {num2} = {res} - your answer {usersRes}. Current score {curScore}");
}

void displayWrongAnswer(int res)
{
AnsiConsole.MarkupLine($"[red]Unlucky... the result was {res}[/]");
}

void displayCorrectAnswer(int res)
{
AnsiConsole.MarkupLine($"[green]Right you are! The answer was {res}[/]");
curScore++;
}


void additionQuestion()
{
int num1 = rng.Next(1, 100);
int num2 = rng.Next(1, 100);
Console.WriteLine($"What is {num1:N0} + {num2:N0}");
int res = num1 + num2;
string? usersResStr = Console.ReadLine();
if (usersResStr != null)
{
int.TryParse(usersResStr, out int usersRes);
if (usersRes != res)
{
displayWrongAnswer(res);
}
else
{
displayCorrectAnswer(res);
}
addToGameHistory(num1, num2, res, usersRes, curScore, '+');
}
}

void subtractionQuestion()
{
int num1 = rng.Next(1, 100);
int num2 = rng.Next(1, 100);
Console.WriteLine($"What is {num1:N0} - {num2:N0}");
int res = num1 - num2;
string? usersResStr = Console.ReadLine();
if (usersResStr != null)
{
int.TryParse(usersResStr, out int usersRes);
if (usersRes != res)
{
displayWrongAnswer(res);
}
else
{
displayCorrectAnswer(res);
}
addToGameHistory(num1, num2, res, usersRes, curScore, '-');
}
}

void multiplicationQuestion()
{
int num1 = rng.Next(1, 10);
int num2 = rng.Next(1, 10);
Console.WriteLine($"What is {num1:N0} * {num2:N0}");
int res = num1 * num2;
string? usersResStr = Console.ReadLine();
if (usersResStr != null)
{
int.TryParse(usersResStr, out int usersRes);
if (usersRes != res)
{
displayWrongAnswer(res);
}
else
{
displayCorrectAnswer(res);
}
addToGameHistory(num1, num2, res, usersRes, curScore, '*');
}
}

void divisionQuestion()
{
int divisor = rng.Next(1, 11);
int quotient = rng.Next(1, 11);
int divided = divisor * quotient;
Console.WriteLine($"What is {divided:N0}/{quotient:N0}");
int res = divided / quotient;
string? usersResStr = Console.ReadLine();
if (usersResStr != null)
{
int.TryParse(usersResStr, out int usersRes);
if (usersRes != res)
{
displayWrongAnswer(res);
}
else
{
displayCorrectAnswer(res);
}
addToGameHistory(divided, quotient, res, usersRes, curScore, '/');
}
}

char chooseOperation()
{
Dictionary<string, char> operationChoices = new()
{
{ "Addition (+)", '+' },
{ "Subtraction (-)", '-' },
{ "Multiplication (*)", '*' },
{ "Division (/)", '/' },
{ "Mixed (random)", '?' }
};

string selection = AnsiConsole.Prompt(
new SelectionPrompt<string>()
.Title("Which operation would you like to practice?")
.AddChoices(operationChoices.Keys)
);

return operationChoices[selection];
}

void askQuestion(char mathOperator)
{
char resolvedOperator = mathOperator == '?'
? operators[rng.Next(1, 5)]
: mathOperator;

switch (resolvedOperator)
{
case '+':
additionQuestion();
break;
case '-':
subtractionQuestion();
break;
case '*':
multiplicationQuestion();
break;
case '/':
divisionQuestion();
break;
}
}

void playGame()
{
char mathOperator = chooseOperation();

for (int i = 1; i < 6; i++)
{
askQuestion(mathOperator);
}
Console.WriteLine($"You got {curScore}/5");
curGame++;
totalScore += curScore;
curScore = 0;
}

void displayHistory()
{
for (int i = 0; i < gameHistory.Count; i++)
{
if (i % 5 == 0)
{
int gameNumber = (i / 5) + 1;
Console.WriteLine($"== Game {gameNumber} ==");
}
Console.WriteLine(gameHistory[i]);
}
AnsiConsole.MarkupLine($"Total score [green]{totalScore}[/]");
Console.WriteLine("\n");
}

List<string> BuildMenuChoices()
{
List<string> menuChoices = ["Start a new game"];

if (curGame > 1)
{
menuChoices.Add("View previous game history");
}
menuChoices.Add("Exit game");
return menuChoices;
}

while (stillPlaying)
{
string userChoice = AnsiConsole.Prompt(
new SelectionPrompt<string>().
Title("Please select from the following...")
.AddChoices(BuildMenuChoices())
).ToLower().Trim();

switch (userChoice)
{
case "exit game":
Console.WriteLine("Thank you for playing :)");
stillPlaying = false;
break;
case "start a new game":
playGame();
break;
case "view previous game history":
displayHistory();
break;
default:
playGame();
break;
}
}
10 changes: 10 additions & 0 deletions MathGame.AidanLDev/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# The Maths Game rules

You need to create a game that consists of asking the player what's the result of a math question (i.e. 9 x 9 = ?), collecting the input and adding a point in case of a correct answer.


- A game needs to have at least 5 questions.
- The divisions should result on INTEGERS ONLY and dividends should go from 0 to 100. Example: Your app shouldn't present the division 7/2 to the user, since it doesn't result in an integer.
- Users should be presented with a menu to choose an operation
- You should record previous games in a List and there should be an option in the menu for the user to visualize a history of previous games.
- You don't need to record results on a database. Once the program is closed the results will be