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: 0 additions & 3 deletions MathGame.hakonRein/MathGame.hakonRein.slnx

This file was deleted.

128 changes: 0 additions & 128 deletions MathGame.hakonRein/MathGame.hakonRein/Program.cs

This file was deleted.

8 changes: 8 additions & 0 deletions MathGame/DifficultyLevel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace MathGame;

public enum DifficultyLevel
{
Easy = 1,
Intermediate = 2,
Advanced = 3
}
113 changes: 113 additions & 0 deletions MathGame/Game.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System.Diagnostics;

namespace MathGame;

public static class Game
{
private static List<(int, TimeSpan)> games = [];
private readonly static string[] mathOperators = ["+", "-", "*", "/"];
private static readonly Random random = new();
private static DifficultyLevel difficultyLevel = DifficultyLevel.Easy;

public static void PlayRound()
{
int points = 0;

difficultyLevel = Menu.ChooseOperationDifficulty();

Stopwatch sw = Stopwatch.StartNew();

for (int i = 0; i < 5; i++)
{
Operation operation = Menu.ChooseOperation();
SolveEquation(ref points, operation);
}

sw.Stop();

Console.WriteLine($"\nPoints for this round: {points}");
Console.WriteLine($"Time spent: {sw.Elapsed:hh\\:mm\\:ss\\.fff}");

games.Add((points, sw.Elapsed));
}

public static void ShowHistory()
{
if (games.Count == 0)
Console.WriteLine("No games yet");

for (int i = 0; i < games.Count; i++)
{
Console.WriteLine($"Points gained in {i + 1} game : {games[i].Item1}");
Console.WriteLine($"Time spent on {i + 1} game : {games[i].Item2:hh\\:mm\\:ss\\.fff}");
}
Console.WriteLine();
}

private static (int, int) GenerateNumbers(Operation operation)
{
int num1 = 0, num2 = 0;
switch (difficultyLevel)
{
case DifficultyLevel.Easy:
num1 = random.Next(0, 10);
num2 = random.Next(0, 10);
break;
case DifficultyLevel.Intermediate:
num1 = random.Next(0, 100);
num2 = random.Next(0, 100);
break;
case DifficultyLevel.Advanced:
num1 = random.Next(0, 1000);
num2 = random.Next(0, 1000);
break;
}


if (operation == Operation.Division && (num2 == 0 || num1 % num2 != 0 || num1 > num2))
return GenerateNumbers(operation);

return (num1, num2);
}

private static void SolveEquation(ref int points, Operation operation)
{
(int num1, int num2) = GenerateNumbers(operation);

Console.Write($"{num1} {mathOperators[(int)operation - 1]} {num2} = ");

int result = GetAnswer();

switch (operation)
{
case Operation.Addition:
if (result == num1 + num2)
points++;
break;
case Operation.Substraction:
if (result == num1 - num2)
points++;
break;
case Operation.Multiplication:
if (result == num1 * num2)
points++;
break;
case Operation.Division:
if (result == num1 / num2)
points++;
break;
}
}

private static int GetAnswer()
{
bool userInput = int.TryParse(Console.ReadLine(), out int result);

if (!userInput)
{
Console.Write("Incorrect format. Try again: ");
return GetAnswer();
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">

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

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

public static class Menu
{
public static string StartMenu()
{
string? input;

Console.WriteLine("Play");
Console.WriteLine("History");
Console.WriteLine("Exit");

Console.Write("Enter your choise => ");

input = Console.ReadLine();
if (input is null)
{
Console.WriteLine("Your input is incorrect");
return StartMenu();
}

return input;
}
public static Operation ChooseOperation()
{
string? input;

Console.WriteLine();
Console.WriteLine("Choose an operation: ");
Console.WriteLine("1. Addition");
Console.WriteLine("2. Substraction");
Console.WriteLine("3. Multiplication");
Console.WriteLine("4. Division");
Console.Write("Enter your choise => ");

input = Console.ReadLine();
if (input == string.Empty || !Enum.TryParse(input, out Operation operation) || !Enum.IsDefined(operation))
{
Console.WriteLine("Your input is incorrect");
return ChooseOperation();
}

return operation;
}
public static DifficultyLevel ChooseOperationDifficulty()
{
string? input;

Console.Clear();
Console.WriteLine("Choose an operation difficulty level: ");
Console.WriteLine("1. Easy");
Console.WriteLine("2. Intermediate");
Console.WriteLine("3. Advanced");
Console.Write("Enter your choise => ");

input = Console.ReadLine();
if (input == string.Empty || !Enum.TryParse(input, out DifficultyLevel difficultyLevel) || !Enum.IsDefined(difficultyLevel))
{
Console.WriteLine("Your input is incorrect");
return ChooseOperationDifficulty();
}

return difficultyLevel;
}
}
9 changes: 9 additions & 0 deletions MathGame/Operation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace MathGame;

public enum Operation
{
Addition = 1,
Substraction = 2,
Multiplication = 3,
Division = 4
}
28 changes: 28 additions & 0 deletions MathGame/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using MathGame;

Console.WriteLine("Welcome to math game!");

string input;
do
{
input = Menu.StartMenu().Trim().ToLower();

switch (input)
{
case "play":
Console.Clear();
Game.PlayRound();
break;
case "history":
Console.Clear();
Game.ShowHistory();
break;
case "exit":
Console.WriteLine("Thanks for the game!");
break;
default:
Console.WriteLine("Your input is incorrect");
break;
}

} while (input != "exit");
Loading