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
23 changes: 23 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (mathGame)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "dotnet: build",
"program": "${workspaceFolder}/bin/Debug/net10.0/mathGame.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"console": "internalConsole",
"internalConsoleOptions": "openOnSessionStart",
"launchBrowser": {
"enabled": false
}
}
]
}
14 changes: 14 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "dotnet: build",
"type": "shell",
"command": "dotnet build",
"problemMatcher": [
"$msCompile"
],
"group": "build"
}
]
}
75 changes: 75 additions & 0 deletions GameRound.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
class GameRound
{
public char Operation { get; }
public int TotalScore { get; set; }
private readonly int _questionsPerRound = 10;
public int QuestionsPerRound
{
get { return _questionsPerRound; }
}
private readonly (int, int)[] _questions;
public IReadOnlyList<(int, int)> Questions
{
get { return _questions; }
}
private readonly int[] _answers;
public IReadOnlyList<int> Answers
{
get { return _answers; }
}
private void GenerateQuestionsForOperation()
{
Random rnd = new();
var uniqueQuestions = new HashSet<(int, int)>();

int i = 0;
while (i < _questionsPerRound)
{
int a = rnd.Next(1, 101);
int b = rnd.Next(1, 101);
if (Operation == '-')
{
b = rnd.Next(1, a + 1);
}
else if (Operation == '/')
{
while (a % b != 0)
{
b = rnd.Next(2, a);
}
}

var question = (a, b);
if (uniqueQuestions.Add(question))
{
_questions[i] = question;
i += 1;
}
}
}
private void ComputeAnswersForOperation()
{
for (int i = 0; i < _questions.Length; i++)
{
(int, int) question = _questions[i];
int answer = Operation switch
{
'+' => question.Item1 + question.Item2,
'*' => question.Item1 * question.Item2,
'-' => question.Item1 - question.Item2,
'/' => question.Item1 / question.Item2,
_ => throw new InvalidOperationException("Unknown Operator.")
};

_answers[i] = answer;
}
}
public GameRound(char operation)
{
Operation = operation;
_questions = [.. new (int, int)[_questionsPerRound]];
GenerateQuestionsForOperation();
_answers = [.. new int[_questionsPerRound]];
ComputeAnswersForOperation();
}
}
136 changes: 136 additions & 0 deletions GameSession.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using System.Collections.Immutable;

class GameSession
{
public List<GameRound> History { get; set; }
private bool _isFirstRound;
public ImmutableDictionary<int, char> OptionsDictionary = new Dictionary<int, char>
{
{1, '+'},
{2, '-'},
{3, '*'},
{4, '/'},
}.ToImmutableDictionary();

public void ShowMenu()
{
int selectedOption = -1;

while (selectedOption != 3)
{
if (_isFirstRound)
{
_isFirstRound = false;
Console.WriteLine("WELCOME TO THE MATH QUIZ!");
StartGame();
}

Console.WriteLine("Choose an option:");
Console.WriteLine("1. Start another round");
Console.WriteLine("2. Check history of scores");
Console.WriteLine("3. Quit");
Console.WriteLine();

var menuOptionsSet = new HashSet<int>() { 1, 2, 3 };

_ = int.TryParse(Console.ReadLine()!, out selectedOption);
while (selectedOption == 0 || !menuOptionsSet.Contains(selectedOption))
{
Console.WriteLine("Invalid option selected. Please try again.");
Console.WriteLine("1. Start another round");
Console.WriteLine("2. Check history of scores");
Console.WriteLine("3. Quit");
Console.WriteLine();
_ = int.TryParse(Console.ReadLine()!, out selectedOption);
}

if (selectedOption == 1) StartGame();
else if (selectedOption == 2) ShowHistory();
}
}

public void StartGame()
{
Console.WriteLine();
Console.WriteLine("******************************* ROUND STARTED *******************************");
Console.WriteLine();
Console.WriteLine("Choose an operation to get started: ");
foreach (int o in OptionsDictionary.Keys)
{
Console.WriteLine($"{o} : {OptionsDictionary[o]}");
}

var optionsSet = new HashSet<int>(OptionsDictionary.Keys);
_ = int.TryParse(Console.ReadLine()!, out int option);

while (option == 0 || !optionsSet.Contains(option))
{
Console.WriteLine("Invalid option selected. Please try again.");
foreach (int o in OptionsDictionary.Keys)
{
Console.WriteLine($"{o} : {OptionsDictionary[o]}");
}
_ = int.TryParse(Console.ReadLine()!, out option);
}

char operation = OptionsDictionary[option];

GameRound gameRound = new(operation);

Console.WriteLine();
Console.WriteLine("You've selected: " + operation);
for (int i = 0; i < gameRound.Questions.Count; i++)
{
(int, int) q = gameRound.Questions[i];
Console.WriteLine();
Console.WriteLine($"What is {q.Item1} {operation} {q.Item2}?");

if (!int.TryParse(Console.ReadLine()!, out int response))
{
response = -1;
}

while (response == -1)
{
Console.WriteLine();
Console.WriteLine($"Invalid response. Please enter a valid number.");
if (!int.TryParse(Console.ReadLine()!, out response))
{
response = -1;
}
}

if (response == gameRound.Answers[i])
{
gameRound.TotalScore += 1;
}
}

Console.WriteLine();
Console.WriteLine($"YOU SCORED: {gameRound.TotalScore} POINT(S)!");
Console.WriteLine();
Console.WriteLine("******************************* ROUND ENDED *******************************");
Console.WriteLine();
History.Add(gameRound);
}

public void ShowHistory()
{
Console.WriteLine();
Console.WriteLine("History of scores for this session:");
for (int i = 0; i < History.Count; i++)
{
Console.WriteLine($"Game {i}: {History[i].TotalScore} POINT(S)");
}

Console.WriteLine();
Console.WriteLine("Press any key to exit the history section...");
Console.ReadLine();
}

public GameSession()
{
History = [];
_isFirstRound = true;
}
}
2 changes: 2 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GameSession gameSession = new();
gameSession.ShowMenu();
10 changes: 10 additions & 0 deletions mathGame.rumanstheddy.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>
24 changes: 24 additions & 0 deletions mathGame.rumanstheddy.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.2.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mathGame.rumanstheddy", "mathGame.rumanstheddy.csproj", "{8E44D313-4A8D-F5FA-1FB5-C00DA569A7BD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8E44D313-4A8D-F5FA-1FB5-C00DA569A7BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8E44D313-4A8D-F5FA-1FB5-C00DA569A7BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8E44D313-4A8D-F5FA-1FB5-C00DA569A7BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8E44D313-4A8D-F5FA-1FB5-C00DA569A7BD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8A0099C2-3CCB-42B6-B9C9-7389D2867AA2}
EndGlobalSection
EndGlobal