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
63 changes: 63 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto

###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp

###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary

###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary

###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore

# User-specific files
*.rsuser
Expand Down Expand Up @@ -29,6 +28,7 @@ x86/
bld/
[Bb]in/
[Oo]bj/
[Oo]ut/
[Ll]og/
[Ll]ogs/

Expand Down Expand Up @@ -57,7 +57,6 @@ dlldata.c
# Benchmark Results
BenchmarkDotNet.Artifacts/

# .NET
project.lock.json
project.fragment.lock.json
artifacts/
Expand Down
10 changes: 10 additions & 0 deletions 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>
3 changes: 3 additions & 0 deletions MathGame.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Solution>
<Project Path="MathGame.csproj" />
</Solution>
125 changes: 125 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
namespace MathGame
{
internal class Program
{
static void Main(string[] args)
{
Qestion[] qestions =
[
new Qestion("2 + 2", 4),
new Qestion("3 * 3", 9),
new Qestion("10 / 2", 5),
new Qestion("5 - 3", 2),
];

List<string> history = [];

bool exit = false;
while (!exit)
{
Console.WriteLine("1 - Play");
Console.WriteLine("2 - View History");
Console.WriteLine("3 - Exit");
Console.Write("Choose an option: ");

string? choice = Console.ReadLine();

switch (choice)
{
case "1":
PlayGame(qestions, history);
break;

case "2":
ShowHistory(history);
break;

case "3":
exit = true;
break;

default:
Console.WriteLine("Invalid option.");
break;
}

Console.WriteLine();
}

Console.WriteLine("Thanks for playing!");
}

static void PlayGame(Qestion[] qestions, List<string> history)
{
DisplayQestions(qestions);
var inputIndex = ChooseQestion(qestions);

Console.WriteLine($"Answer this question {qestions[inputIndex].Text}: ");
string? answerInput = Console.ReadLine();

if (int.TryParse(answerInput, out int answer))
{
bool correct = qestions[inputIndex].IsCorrect(answer);
Console.WriteLine(correct ? "Correct" : "Incorrect");

history.Add($"{DateTime.Now:G} | {qestions[inputIndex].Text} | Your answer: {answer} | {(correct ? "Correct" : "Incorrect")}");
}
else
{
Console.WriteLine("Invalid integer.");
history.Add($"{DateTime.Now:G} | {qestions[inputIndex].Text} | Invalid answer");
}
}

static void ShowHistory(List<string> history)
{
Console.WriteLine("History");
Console.WriteLine("=======");

if (history.Count == 0)
{
Console.WriteLine("No history yet.");
return;
}

foreach (var item in history)
{
Console.WriteLine(item);
}
}

static void DisplayQestions(Qestion[] qestions)
{
Console.WriteLine("Choose Question To Answer");
Console.WriteLine("========================");
for (int i = 0; i < qestions.Length; i++)
{
Console.WriteLine($"{i + 1}- What is {qestions[i].Text} ?");
}
}

static int ChooseQestion(Qestion[] qestions)
{
string? input = Console.ReadLine();

if (int.TryParse(input, out int result))
{
if (result <= 0 || result > qestions.Length)
{
Console.WriteLine("Error: choose from range.");
return ChooseQestion(qestions);
}

return result - 1;
}

Console.WriteLine("Error: That is not a valid integer.");
return ChooseQestion(qestions);
}
}

internal record Qestion(string Text, int Answer)
{
public bool IsCorrect(int answer) => Answer == answer;
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# MathGame
Loading