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
15 changes: 15 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/indexLayout.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions MathGame.Dknx8888/.idea/.idea.MathGame.Dknx8888/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions MathGame.Dknx8888/.idea/.idea.MathGame.Dknx8888/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

public enum Difficulty
{
Easy,
Medium,
Hard
}
115 changes: 115 additions & 0 deletions MathGame.Dknx8888/GameHistory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System.Text.Json;
using static MathGame.Dknx8888.GameResultTemplate;

namespace MathGame.Dknx8888;

public class GameHistory
{
private readonly JsonSerializerOptions _options = new() { WriteIndented = true };
public void ShowHistory()
{
while (true)
{
Console.Clear();

var filePath = GameHistoryJson.FilePath;
if (!File.Exists(filePath))
{
EmptyHistoryError();
return;
}

var json = File.ReadAllText(filePath);
if (string.IsNullOrWhiteSpace(json))
{
EmptyHistoryError();
return;
}

var gameResults = JsonSerializer.Deserialize<List<GameResult>>(json, _options) ?? [];
if (gameResults.Count == 0)
{
EmptyHistoryError();
return;
}

Console.WriteLine("Game History\n");
Console.WriteLine("Please select a game via ID (1,2, etc.) to show more details");
Console.WriteLine("Type q to go back\n");
Console.WriteLine($"{"ID",-4} | {"Date and Time Started",-22} | {"Score",-7} | {"Mode",-15} | {"Duration (sec)",-10}");
Console.WriteLine(new string('-', 76));

// Show from latest (bad idea?)
// gameResults.Reverse();
for (var i = 0; i < gameResults.Count; i++)
{
var (gameMode, _, score, duration, _, startTime) = gameResults[i];

Console.WriteLine($"{i + 1,-4} | {startTime,-22} | {score,-7} | {gameMode,-15} | {duration,-10:F2}");
}

int selectedId;
bool isValidId;

do
{
var input = Console.ReadLine()?.Trim();

// while loop in GameSession.cs does not work here because of this q
if (string.Equals(input, "q", StringComparison.OrdinalIgnoreCase))
{
return;
}

isValidId = int.TryParse(input, out selectedId)
&& selectedId >= 1
&& selectedId <= gameResults.Count;

if (!isValidId)
{
Console.WriteLine("Please enter a valid ID number, or q to go back");
}
} while (!isValidId);

var selectedGameResult = gameResults[selectedId - 1];
ShowDetails(selectedGameResult);
}
}

private static void EmptyHistoryError()
{
Console.WriteLine("No game history found.");
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}

private static void ShowDetails(GameResult selectedGameResult)
{
var (gameMode, difficulty, score, duration, questionResults, startTime) = selectedGameResult;

Console.Clear();
Console.WriteLine($"Start time: {startTime}");
Console.WriteLine($"Game mode: {gameMode}");
Console.WriteLine($"Difficulty: {difficulty}");
Console.WriteLine($"Duration (sec): {duration:F2}");
Console.WriteLine($"Score: {score}/5\n");

// Way better idea to include id in the records
var questionId = 1;
// Mind the one spaces
Console.WriteLine($"{"Q. Number", -10} {"Result", -8} {"Question", -12} {"Your Answer", 12} {"Correct Answer", 14}");
Console.WriteLine(new string('-', 62));

foreach (var questionResult in questionResults)
{
var (num1, num2, sign, playerAnswer, correctAnswer, isCorrect) = questionResult;
var correctDisplay = isCorrect ? "Right" : "Wrong";
var questionDisplay = $"{num1} {sign} {num2}";

Console.WriteLine($"{questionId, -10} {correctDisplay, -8} {questionDisplay, -12} {playerAnswer, 12} {correctAnswer, 14}");
++questionId;
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
}
18 changes: 18 additions & 0 deletions MathGame.Dknx8888/GameHistoryJson.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Runtime.CompilerServices;

namespace MathGame.Dknx8888;

// Repository pattern in the future?
public static class GameHistoryJson
{
public static string FilePath => GetGameResultsFilePath();

// CallerFilePath gets the path of the source file that called this func
private static string GetGameResultsFilePath([CallerFilePath] string sourceFilePath = "")
{
var sourceDirectory = Path.GetDirectoryName(sourceFilePath)
?? throw new InvalidOperationException("Unable to determine the source file directory.");

return Path.Combine(sourceDirectory, "game-results.json");
}
}
10 changes: 10 additions & 0 deletions MathGame.Dknx8888/GameMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace MathGame.Dknx8888;

public enum GameMode
{
Addition,
Subtraction,
Multiplication,
Division,
Random
}
22 changes: 22 additions & 0 deletions MathGame.Dknx8888/GameResultTemplate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace MathGame.Dknx8888;

public static class GameResultTemplate
{
public record QuestionResult (
int Num1,
int Num2,
char Sign,
double PlayerAnswer,
int CorrectAnswer,
bool IsCorrect
);

public record GameResult (
GameMode GameMode,
Difficulty Difficulty,
int Score,
double Duration,
List<QuestionResult> Questions,
DateTime StartTime
);
}
Loading