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
18 changes: 18 additions & 0 deletions MathGame.whoami3222/.vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "windows-gcc-x64",
"includePath": [
"${workspaceFolder}/**"
],
"compilerPath": "gcc",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "windows-gcc-x64",
"compilerArgs": [
""
]
}
],
"version": 4
}
24 changes: 24 additions & 0 deletions MathGame.whoami3222/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"externalConsole": true,
"cwd": "c:/cSharpProjects/MathGame",
"program": "c:/cSharpProjects/MathGame/build/Debug/outDebug",
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
59 changes: 59 additions & 0 deletions MathGame.whoami3222/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"C_Cpp_Runner.cCompilerPath": "gcc",
"C_Cpp_Runner.cppCompilerPath": "g++",
"C_Cpp_Runner.debuggerPath": "gdb",
"C_Cpp_Runner.cStandard": "",
"C_Cpp_Runner.cppStandard": "",
"C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat",
"C_Cpp_Runner.useMsvc": false,
"C_Cpp_Runner.warnings": [
"-Wall",
"-Wextra",
"-Wpedantic",
"-Wshadow",
"-Wformat=2",
"-Wcast-align",
"-Wconversion",
"-Wsign-conversion",
"-Wnull-dereference"
],
"C_Cpp_Runner.msvcWarnings": [
"/W4",
"/permissive-",
"/w14242",
"/w14287",
"/w14296",
"/w14311",
"/w14826",
"/w44062",
"/w44242",
"/w14905",
"/w14906",
"/w14263",
"/w44265",
"/w14928"
],
"C_Cpp_Runner.enableWarnings": true,
"C_Cpp_Runner.warningsAsError": false,
"C_Cpp_Runner.compilerArgs": [],
"C_Cpp_Runner.linkerArgs": [],
"C_Cpp_Runner.includePaths": [],
"C_Cpp_Runner.includeSearch": [
"*",
"**/*"
],
"C_Cpp_Runner.excludeSearch": [
"**/build",
"**/build/**",
"**/.*",
"**/.*/**",
"**/.vscode",
"**/.vscode/**"
],
"C_Cpp_Runner.useAddressSanitizer": false,
"C_Cpp_Runner.useUndefinedSanitizer": false,
"C_Cpp_Runner.useLeakSanitizer": false,
"C_Cpp_Runner.showCompilationTime": false,
"C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false
}
1 change: 1 addition & 0 deletions MathGame.whoami3222/CodeReviews.Console.MathGame
Submodule CodeReviews.Console.MathGame added at 2625a6
10 changes: 10 additions & 0 deletions MathGame.whoami3222/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>
165 changes: 165 additions & 0 deletions MathGame.whoami3222/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
using System.Diagnostics;
int points = 0;
List<string> history = new List<string>();
MathGame();


void MathGame()
{
DisplayMenu();
string? input = Console.ReadLine();
HandleMenuChoice(input);

}

void DisplayMenu()
{
Console.WriteLine("Choose from the menu below:");
Console.WriteLine("1. Summation");
Console.WriteLine("2. Subtraction");
Console.WriteLine("3. Multiplication");
Console.WriteLine("4. Division");
Console.WriteLine("5. Random Operation");
Console.WriteLine("6. Display History");
Console.WriteLine("7. Exit");
}

void HandleMenuChoice(string? choice)
{
switch (choice)
{
case "1":
StartGame(MathOperation.Addition);
break;
case "2":
StartGame(MathOperation.Subtraction);
break;
case "3":
StartGame(MathOperation.Multiplication);
break;
case "4":
StartGame(MathOperation.Division);
break;
case "5":
StartGame(MathOperation.Random);
break;
case "6":
DisplayList();
break;
case "7":
Console.WriteLine("Exiting the game. Goodbye!");
Environment.Exit(0);
break;
default:
Console.WriteLine("Invalid choice. Please try again.");
break;
}
}

void StartGame(MathOperation operation)
{
Stopwatch stopwatch = Stopwatch.StartNew();

for (int i = 0; i < 5; i++)
{
MathOperation questionOperation = operation == MathOperation.Random
? (MathOperation)Random.Shared.Next(0, 4)
: operation;
int number1;
int number2;

if (questionOperation == MathOperation.Division)
{
number2 = Random.Shared.Next(1, 11);
int maxQuotient = 100 / number2;
int quotient = Random.Shared.Next(0, maxQuotient + 1);
number1 = number2 * quotient;
}
else
{
number1 = Random.Shared.Next(1, 9);
number2 = Random.Shared.Next(1, 9);
}

string operationSymbol = questionOperation switch
{
MathOperation.Addition => "+",
MathOperation.Subtraction => "-",
MathOperation.Multiplication => "*",
MathOperation.Division => "/",
_ => throw new ArgumentOutOfRangeException()
};

Console.WriteLine($"Question {i + 1}: What is {number1} {operationSymbol} {number2}?");
string? input = Console.ReadLine();
if (CheckAnswer(number1, number2, questionOperation, input))
{
points++;
}
}
stopwatch.Stop();
Console.WriteLine($"You scored {points} out of 5.");
Console.WriteLine($"Time taken: {stopwatch.Elapsed.TotalSeconds:F2} seconds.");
history.Add($"Game played on {DateTime.Now}: Scored {points}/5, Time taken: {stopwatch.Elapsed.TotalSeconds:F2} seconds.");
PlayAgainPrompt();
}

void PlayAgainPrompt()
{
Console.WriteLine("Do you want to return to the menu? (yes/no)");
string? playAgain = Console.ReadLine();
if (playAgain?.ToLower() == "yes")
{
points = 0;
MathGame();
}
else if(playAgain?.ToLower() == "no")
{
Console.WriteLine("Thank you for playing!");
Environment.Exit(0);
}
else
{
Console.WriteLine("Invalid input, try again.");
PlayAgainPrompt();
}
}

void DisplayList()
{
Console.WriteLine("History of previous games:");
foreach (string item in history)
{
Console.WriteLine($"- {item}");
}

PlayAgainPrompt();
}

bool CheckAnswer(int number1, int number2, MathOperation operation, string? result)
{
int correctAnswer = operation switch
{
MathOperation.Addition => number1 + number2,
MathOperation.Subtraction => number1 - number2,
MathOperation.Multiplication => number1 * number2,
MathOperation.Division => number1 / number2,
_ => throw new ArgumentException("Invalid operation")
};

if (int.TryParse(result, out int parsedValue) && parsedValue == correctAnswer)
{
return true;
}

return false;
}

public enum MathOperation
{
Addition,
Subtraction,
Multiplication,
Division,
Random
}