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
8 changes: 8 additions & 0 deletions mathGame.rickardwg/DataSource/GameHistoryData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using MathGame.Models;

namespace MathGame.DataSource;

internal class GameHistoryData : IGameHistoryData
{
public List<GameEntry> GameHistory { get; set; } = [];
}
8 changes: 8 additions & 0 deletions mathGame.rickardwg/DataSource/IGameHistoryData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using MathGame.Models;

namespace MathGame.DataSource;

internal interface IGameHistoryData
{
List<GameEntry> GameHistory { get; set; }
}
8 changes: 8 additions & 0 deletions mathGame.rickardwg/Enums/Difficulty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace MathGame.Enums;

internal enum Difficulty
{
Easy,
Medium,
Hard
}
10 changes: 10 additions & 0 deletions mathGame.rickardwg/Enums/GameMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace MathGame.Enums;

internal enum GameMode
{
Addition,
Subtraction,
Multiplication,
Division,
Random
}
8 changes: 8 additions & 0 deletions mathGame.rickardwg/Enums/MainMenuOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace MathGame.Enums;

internal enum MainMenuOptions
{
Play,
History,
Exit
}
9 changes: 9 additions & 0 deletions mathGame.rickardwg/Enums/Operation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace MathGame.Enums;

internal enum Operation
{
Add,
Subtract,
Multiply,
Divide
}
27 changes: 27 additions & 0 deletions mathGame.rickardwg/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using MathGame.DataSource;
using MathGame.Menus;
using MathGame.Repositories;
using MathGame.Services;
using MathGame.Views;
using Microsoft.Extensions.DependencyInjection;

namespace MathGame.Extensions;

internal static class ServiceCollectionExtensions
{
public static IServiceCollection AddCustomServices(this IServiceCollection services)
{
services.AddTransient<IMainMenu, MainMenu>();
services.AddTransient<IGameMenu, GameMenu>();
services.AddTransient<IGameView, GameView>();
services.AddTransient<IGameHistoryView, GameHistoryView>();

services.AddTransient<IGameService, GameService>();
services.AddTransient<IQuestionService, QuestionService>();

services.AddTransient<IHistoryRepository, HistoryRepository>();
services.AddSingleton<IGameHistoryData, GameHistoryData>();

return services;
}
}
76 changes: 76 additions & 0 deletions mathGame.rickardwg/Helpers/ConsoleHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
namespace MathGame.Helpers;

internal static class ConsoleHelper
{
internal static T Prompt<T>(string title, params T[] options)
{
ArgumentNullException.ThrowIfNull(options);

if (options.Length == 0)
{
throw new ArgumentException(
"At least one option must be provided.",
nameof(options));
}

Console.CursorVisible = false;

try
{
var selectedIndex = 0;
while (true)
{
Console.Clear();
WriteLine(title);
Console.WriteLine();

for (int i = 0; i < options.Length; i++)
{
bool isSelected = selectedIndex == i;
string prefix = isSelected ? "> " : " ";
ConsoleColor color = isSelected ? ConsoleColor.Yellow : ConsoleColor.White;
WriteLine($"{prefix}{options[i]}", color);
}

var keyInfo = Console.ReadKey(true);

switch (keyInfo.Key)
{
case ConsoleKey.UpArrow:
case ConsoleKey.W:
if (selectedIndex > 0)
{
selectedIndex--;
}
break;

case ConsoleKey.DownArrow:
case ConsoleKey.S:
if (selectedIndex < options.Length - 1)
{
selectedIndex++;
}
break;

case ConsoleKey.Enter: return options[selectedIndex];
}
}
}
finally
{
Console.CursorVisible = true;
}
}

internal static void WriteLine(string text, ConsoleColor color = ConsoleColor.White)
{
Console.ForegroundColor = color;
Console.WriteLine(text);
}

internal static void Write(string text, ConsoleColor color = ConsoleColor.White)
{
Console.ForegroundColor = color;
Console.Write(text);
}
}
14 changes: 14 additions & 0 deletions mathGame.rickardwg/MathGame.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.10" />
</ItemGroup>

</Project>
21 changes: 21 additions & 0 deletions mathGame.rickardwg/Menus/GameMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using MathGame.Enums;
using MathGame.Helpers;

namespace MathGame.Menus;

internal class GameMenu : IGameMenu
{
private const string GameModeTitle = "Select Game Mode:";
private readonly GameMode[] _gameModes = [GameMode.Addition, GameMode.Subtraction, GameMode.Multiplication, GameMode.Division, GameMode.Random];
private const string DifficultyTitle = "Select Difficulty:";
private readonly Difficulty[] _difficulties = [Difficulty.Easy, Difficulty.Medium, Difficulty.Hard];
public GameMode SelectGameMode()
{
return ConsoleHelper.Prompt<GameMode>(GameModeTitle, _gameModes);
}

public Difficulty SelectDifficulty()
{
return ConsoleHelper.Prompt(DifficultyTitle, _difficulties);
}
}
9 changes: 9 additions & 0 deletions mathGame.rickardwg/Menus/IGameMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using MathGame.Enums;

namespace MathGame.Menus;

internal interface IGameMenu
{
GameMode SelectGameMode();
Difficulty SelectDifficulty();
}
6 changes: 6 additions & 0 deletions mathGame.rickardwg/Menus/IMainMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace MathGame.Menus;

internal interface IMainMenu
{
void Display();
}
40 changes: 40 additions & 0 deletions mathGame.rickardwg/Menus/MainMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using MathGame.Enums;
using MathGame.Helpers;
using MathGame.Views;

namespace MathGame.Menus;

internal class MainMenu(IGameMenu gameMenu, IGameView gameView, IGameHistoryView historyView) : IMainMenu
{
private readonly IGameMenu _gameMenu = gameMenu;
private readonly IGameView _gameView = gameView;
private readonly IGameHistoryView _historyView = historyView;

private const string Title = "Welcome to The Math Game!";
private readonly MainMenuOptions[] MenuOptions = [MainMenuOptions.Play, MainMenuOptions.History, MainMenuOptions.Exit];
public void Display()
{
var exit = false;
while (!exit)
{
Console.Clear();
var selection = ConsoleHelper.Prompt<MainMenuOptions>(Title, MenuOptions);

switch (selection)
{
case MainMenuOptions.Play:
var gameMode = _gameMenu.SelectGameMode();
var difficulty = _gameMenu.SelectDifficulty();
_gameView.DisplayGame(gameMode, difficulty);
break;

case MainMenuOptions.History:
_historyView.DisplayHistory();
break;
case MainMenuOptions.Exit:
exit = true;
break;
}
}
}
}
13 changes: 13 additions & 0 deletions mathGame.rickardwg/Models/GameEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using MathGame.Enums;

namespace MathGame.Models;

internal class GameEntry
{
internal int Id { get; set; }
internal GameMode GameMode { get; set; }
internal Difficulty Difficulty { get; set; }
internal int CorrectAnswers { get; set; }
internal int TotalQuestions { get; set; }
internal TimeSpan TimeTaken { get; set; }
}
11 changes: 11 additions & 0 deletions mathGame.rickardwg/Models/MathQuestion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using MathGame.Enums;

namespace MathGame.Models;

internal class MathQuestion
{
internal int LeftOperand { get; set; }
internal int RightOperand { get; set; }
internal Operation Operation { get; set; }
internal int CorrectAnswer { get; set; }
}
11 changes: 11 additions & 0 deletions mathGame.rickardwg/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using MathGame.Extensions;
using MathGame.Menus;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var builder = Host.CreateApplicationBuilder();
builder.Services.AddCustomServices();
using var host = builder.Build();

var mainMenu = host.Services.GetRequiredService<IMainMenu>();
mainMenu.Display();
21 changes: 21 additions & 0 deletions mathGame.rickardwg/Repositories/HistoryRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using MathGame.DataSource;
using MathGame.Models;

namespace MathGame.Repositories;

internal class HistoryRepository(IGameHistoryData dataSource) : IHistoryRepository
{
private readonly IGameHistoryData _dataSource = dataSource;

public List<GameEntry> GetGameHistory()
{
return _dataSource.GameHistory;
}

public bool AddGameEntry(GameEntry entry)
{
entry.Id = _dataSource.GameHistory.Count + 1;
_dataSource.GameHistory.Add(entry);
return true;
}
}
9 changes: 9 additions & 0 deletions mathGame.rickardwg/Repositories/IHistoryRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using MathGame.Models;

namespace MathGame.Repositories;

internal interface IHistoryRepository
{
List<GameEntry> GetGameHistory();
bool AddGameEntry(GameEntry entry);
}
Loading