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
10 changes: 10 additions & 0 deletions MathGames/MathGames.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>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
172 changes: 172 additions & 0 deletions MathGames/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
using System;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;

namespace HelloWorld;

class Program
{
static void Main(string[] args)
{
int points = 0;
bool playAgain = true;
Random random = new Random();

List<Questions> questions = new List<Questions>();
// Addition questions
questions.Add(new Questions("9 + 10 = ?", '+', 19));
questions.Add(new Questions("7 + 13 = ?", '+', 20));
questions.Add(new Questions("25 + 35 = ?", '+', 60));
questions.Add(new Questions("18 + 22 = ?", '+', 40));

// Subtraction questions
questions.Add(new Questions("15 - 4 = ?", '-', 11));
questions.Add(new Questions("50 - 23 = ?", '-', 27));
questions.Add(new Questions("100 - 45 = ?", '-', 55));
questions.Add(new Questions("73 - 28 = ?", '-', 45));

// Multiplication questions
questions.Add(new Questions("12 * 12 = ?", '*', 144));
questions.Add(new Questions("6 * 9 = ?", '*', 54));
questions.Add(new Questions("7 * 8 = ?", '*', 56));
questions.Add(new Questions("11 * 5 = ?", '*', 55));
questions.Add(new Questions("8 * 8 = ?", '*', 64));

// Division questions (integers only, dividends 0-100)
questions.Add(new Questions("56 / 7 = ?", '/', 8));
questions.Add(new Questions("81 / 9 = ?", '/', 9));
questions.Add(new Questions("100 / 10 = ?", '/', 10));
questions.Add(new Questions("48 / 6 = ?", '/', 8));
questions.Add(new Questions("72 / 8 = ?", '/', 9));
questions.Add(new Questions("63 / 7 = ?", '/', 9));

List<AnsweredQuestion> PreviousGames = new List<AnsweredQuestion>();

while (playAgain)
{
System.Console.WriteLine("Welcome to the Math Game!");
while (playAgain)
{
Console.WriteLine("=== Math Game Menu ===");
Console.WriteLine("1. Addition");
Console.WriteLine("2. Subtraction");
Console.WriteLine("3. Multiplication");
Console.WriteLine("4. Division");
Console.WriteLine("5. View Previous Games");
Console.WriteLine("6. Exit");
Console.Write("Enter your choice (1-6): ");
string choiceInput = Console.ReadLine();
int choice;

// Try parsing safely
if (!int.TryParse(choiceInput, out choice) || choice < 1 || choice > 5)
{
Console.WriteLine("\nExiting the game. Thanks for playing!");
playAgain = false;
break;
}
else if (choice == 5)
{
Console.WriteLine("\nHere are your previous games:");
foreach (var answered in PreviousGames)
{
string correctness = answered.IsCorrect ? "Correct" : "Wrong";
Console.WriteLine($"{answered.QuestionText} Your answer: {answered.UserAnswer}. Correct answer: {answered.Answer}. Result: {correctness}");
}
continue;
}

char operationChar = ' ';
switch (choice)
{
case 1: operationChar = '+'; break;
case 2: operationChar = '-'; break;
case 3: operationChar = '*'; break;
case 4: operationChar = '/'; break;
}

List<Questions> filteredQuestions = questions.Where(q => q.Operation == operationChar).ToList();
Questions question = filteredQuestions[random.Next(filteredQuestions.Count)];

Console.WriteLine(question.QuestionText);
if (!int.TryParse(Console.ReadLine(), out int userAnswer))
{
Console.WriteLine("Invalid input! Please enter a number.\n");
continue;
}

PreviousGames.Add(new AnsweredQuestion(question.QuestionText, question.Operation, question.Answer, userAnswer));

if (userAnswer == question.Answer)
{
points++;
Console.WriteLine("Correct!\n");
}
else
{
Console.WriteLine($"Wrong! The correct answer is {question.Answer}\n");
}
}
Console.WriteLine($"Your total points is: {points}!");
Console.WriteLine("Do you want to play again? (y/n)");
string response = Console.ReadLine();
if (response.ToLower() != "y")
{
playAgain = false;
}
else
{
playAgain = true;
points = 0;
}
}

}
}

class Questions
{
public string QuestionText { get; }
public char Operation { get; }
public int Answer { get; }

public Questions(string questionText, char operation, int answer)
{
QuestionText = questionText;
Operation = operation;
Answer = answer;
}
}
class AnsweredQuestion : Questions
{
public int UserAnswer { get; set; }
public bool IsCorrect { get; set; }

// Call base constructor for QuestionText, Operation, Answer
public AnsweredQuestion(string questionText, char operation, int answer, int userAnswer)
: base(questionText, operation, answer)
{
UserAnswer = userAnswer;
IsCorrect = userAnswer == answer;
}
}


/*
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 deleted.
*/
Binary file added MathGames/bin/Debug/net8.0/MathGames
Binary file not shown.
23 changes: 23 additions & 0 deletions MathGames/bin/Debug/net8.0/MathGames.deps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v8.0",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v8.0": {
"MathGames/1.0.0": {
"runtime": {
"MathGames.dll": {}
}
}
}
},
"libraries": {
"MathGames/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
}
}
}
Binary file added MathGames/bin/Debug/net8.0/MathGames.dll
Binary file not shown.
Binary file added MathGames/bin/Debug/net8.0/MathGames.pdb
Binary file not shown.
12 changes: 12 additions & 0 deletions MathGames/bin/Debug/net8.0/MathGames.runtimeconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"runtimeOptions": {
"tfm": "net8.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "8.0.0"
},
"configProperties": {
"System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
}
}
}
22 changes: 22 additions & 0 deletions MathGames/obj/Debug/net8.0/MathGames.AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Reflection;

[assembly: System.Reflection.AssemblyCompanyAttribute("MathGames")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+043425ecfa1f7cf5f208ca503ab4d36c9f26a980")]
[assembly: System.Reflection.AssemblyProductAttribute("MathGames")]
[assembly: System.Reflection.AssemblyTitleAttribute("MathGames")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

// Generated by the MSBuild WriteCodeFragment class.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bb2934a45acf3a162e9d119df0c693baba55113264d0183ec138818151ca7811
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
is_global = true
build_property.TargetFramework = net8.0
build_property.TargetFrameworkIdentifier = .NETCoreApp
build_property.TargetFrameworkVersion = v8.0
build_property.TargetPlatformMinVersion =
build_property.UsingMicrosoftNETSdkWeb =
build_property.ProjectTypeGuids =
build_property.InvariantGlobalization =
build_property.PlatformNeutralAssembly =
build_property.EnforceExtendedAnalyzerRules =
build_property._SupportedPlatformList = Linux,macOS,Windows
build_property.RootNamespace = MathGames
build_property.ProjectDir = /Users/hyungminoh/asp-learn/aspnet-learning/BeginnerConsole/MathGames/
build_property.EnableComHosting =
build_property.EnableGeneratedComInterfaceComImportInterop =
build_property.EffectiveAnalysisLevelStyle = 8.0
build_property.EnableCodeStyleSeverity =
8 changes: 8 additions & 0 deletions MathGames/obj/Debug/net8.0/MathGames.GlobalUsings.g.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// <auto-generated/>
global using System;
global using System.Collections.Generic;
global using System.IO;
global using System.Linq;
global using System.Net.Http;
global using System.Threading;
global using System.Threading.Tasks;
Binary file added MathGames/obj/Debug/net8.0/MathGames.assets.cache
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a9ac62a7453ed13a3f0254f4f97d7dfebed9b0a29bc3d42e680b831495d894d9
15 changes: 15 additions & 0 deletions MathGames/obj/Debug/net8.0/MathGames.csproj.FileListAbsolute.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/Users/hyungminoh/asp-learn/aspnet-learning/BeginnerConsole/MathGames/obj/Debug/net8.0/MathGames.GeneratedMSBuildEditorConfig.editorconfig
/Users/hyungminoh/asp-learn/aspnet-learning/BeginnerConsole/MathGames/obj/Debug/net8.0/MathGames.AssemblyInfoInputs.cache
/Users/hyungminoh/asp-learn/aspnet-learning/BeginnerConsole/MathGames/obj/Debug/net8.0/MathGames.AssemblyInfo.cs
/Users/hyungminoh/asp-learn/aspnet-learning/BeginnerConsole/MathGames/obj/Debug/net8.0/MathGames.csproj.CoreCompileInputs.cache
/Users/hyungminoh/asp-learn/aspnet-learning/BeginnerConsole/MathGames/obj/Debug/net8.0/MathGames.sourcelink.json
/Users/hyungminoh/asp-learn/aspnet-learning/BeginnerConsole/MathGames/bin/Debug/net8.0/MathGames
/Users/hyungminoh/asp-learn/aspnet-learning/BeginnerConsole/MathGames/bin/Debug/net8.0/MathGames.deps.json
/Users/hyungminoh/asp-learn/aspnet-learning/BeginnerConsole/MathGames/bin/Debug/net8.0/MathGames.runtimeconfig.json
/Users/hyungminoh/asp-learn/aspnet-learning/BeginnerConsole/MathGames/bin/Debug/net8.0/MathGames.dll
/Users/hyungminoh/asp-learn/aspnet-learning/BeginnerConsole/MathGames/bin/Debug/net8.0/MathGames.pdb
/Users/hyungminoh/asp-learn/aspnet-learning/BeginnerConsole/MathGames/obj/Debug/net8.0/MathGames.dll
/Users/hyungminoh/asp-learn/aspnet-learning/BeginnerConsole/MathGames/obj/Debug/net8.0/refint/MathGames.dll
/Users/hyungminoh/asp-learn/aspnet-learning/BeginnerConsole/MathGames/obj/Debug/net8.0/MathGames.pdb
/Users/hyungminoh/asp-learn/aspnet-learning/BeginnerConsole/MathGames/obj/Debug/net8.0/MathGames.genruntimeconfig.cache
/Users/hyungminoh/asp-learn/aspnet-learning/BeginnerConsole/MathGames/obj/Debug/net8.0/ref/MathGames.dll
Binary file added MathGames/obj/Debug/net8.0/MathGames.dll
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7e5a5cc1a296877529521fdcda4942f79fd896a8bc935621aefa4ac91c1b7012
Binary file added MathGames/obj/Debug/net8.0/MathGames.pdb
Binary file not shown.
1 change: 1 addition & 0 deletions MathGames/obj/Debug/net8.0/MathGames.sourcelink.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"documents":{"/Users/hyungminoh/asp-learn/aspnet-learning/*":"https://raw.githubusercontent.com/hyungminoh2/aspnet-learning/043425ecfa1f7cf5f208ca503ab4d36c9f26a980/*"}}
Binary file added MathGames/obj/Debug/net8.0/apphost
Binary file not shown.
Binary file added MathGames/obj/Debug/net8.0/ref/MathGames.dll
Binary file not shown.
Binary file not shown.
67 changes: 67 additions & 0 deletions MathGames/obj/MathGames.csproj.nuget.dgspec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"format": 1,
"restore": {
"/Users/hyungminoh/asp-learn/aspnet-learning/BeginnerConsole/MathGames/MathGames.csproj": {}
},
"projects": {
"/Users/hyungminoh/asp-learn/aspnet-learning/BeginnerConsole/MathGames/MathGames.csproj": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "/Users/hyungminoh/asp-learn/aspnet-learning/BeginnerConsole/MathGames/MathGames.csproj",
"projectName": "MathGames",
"projectPath": "/Users/hyungminoh/asp-learn/aspnet-learning/BeginnerConsole/MathGames/MathGames.csproj",
"packagesPath": "/Users/hyungminoh/.nuget/packages/",
"outputPath": "/Users/hyungminoh/asp-learn/aspnet-learning/BeginnerConsole/MathGames/obj/",
"projectStyle": "PackageReference",
"configFilePaths": [
"/Users/hyungminoh/.nuget/NuGet/NuGet.Config"
],
"originalTargetFrameworks": [
"net8.0"
],
"sources": {
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"projectReferences": {}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
},
"restoreAuditProperties": {
"enableAudit": "true",
"auditLevel": "low",
"auditMode": "direct"
},
"SdkAnalysisLevel": "10.0.100"
},
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"imports": [
"net461",
"net462",
"net47",
"net471",
"net472",
"net48",
"net481"
],
"assetTargetFallback": true,
"warn": true,
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "/usr/local/share/dotnet/sdk/10.0.102/PortableRuntimeIdentifierGraph.json"
}
}
}
}
}
15 changes: 15 additions & 0 deletions MathGames/obj/MathGames.csproj.nuget.g.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess>
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">/Users/hyungminoh/.nuget/packages/</NuGetPackageRoot>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">/Users/hyungminoh/.nuget/packages/</NuGetPackageFolders>
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">7.0.0</NuGetToolVersion>
</PropertyGroup>
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<SourceRoot Include="/Users/hyungminoh/.nuget/packages/" />
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions MathGames/obj/MathGames.csproj.nuget.g.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" />
Loading