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
25 changes: 25 additions & 0 deletions MathGame.royeeet/MathGame.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.11.35327.3
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MathGame", "MathGame\MathGame.csproj", "{D51720E7-445A-466B-BE83-A0BAB765E58C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D51720E7-445A-466B-BE83-A0BAB765E58C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D51720E7-445A-466B-BE83-A0BAB765E58C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D51720E7-445A-466B-BE83-A0BAB765E58C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D51720E7-445A-466B-BE83-A0BAB765E58C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2A1701F9-30F5-423A-9653-334A66AA338A}
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions MathGame.royeeet/MathGame/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
</configuration>
48 changes: 48 additions & 0 deletions MathGame.royeeet/MathGame/GameEngine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using MathGame.Models;

namespace MathGame
{
internal class GameEngine
{
public void PlayGame(GameType gameType, string symbol, Func<int, int, int> operation, GameDifficulty difficulty)
{
int score = 0;
Helpers helpers = new Helpers();
var loop = Helpers.InitialiseGame();

do
{
GameType currentGameType = gameType;
string currentSymbol = symbol;
Func<int, int, int> currentOperation = operation;

if (gameType == GameType.Random)
{
currentGameType = Helpers.Randomise();
currentSymbol = currentGameType switch
{
GameType.Addition => "+",
GameType.Subtraction => "-",
GameType.Multiplication => "x",
GameType.Division => "/",
};
currentOperation = currentSymbol switch
{
"+" => (a, b) => a + b,
"-" => (a, b) => a - b,
"x" => (a, b) => a * b,
"/" => (a, b) => a / b,
_ => (a, b) => a + b
};
}
var (firstValue, secondValue, userAnswer) = currentGameType == GameType.Division ? Helpers.CheckThatNumbersAreDivisable(difficulty) : Helpers.GetNumbersAndQuestion(currentSymbol, difficulty);

int expected = currentOperation(firstValue, secondValue);

helpers.GameLogic(userAnswer, expected, ref score, gameType, difficulty);

} while (!loop);
}
}
}
252 changes: 252 additions & 0 deletions MathGame.royeeet/MathGame/Helpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
using MathGame.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace MathGame
{
internal class Helpers
{
public static string player = GetName();
public static List<Game> games = new List<Game>();
public static Stopwatch timer = new Stopwatch();

public static void GameHistory()
{
Console.Clear();
Console.WriteLine("game history");

if (games == null || games.Count == 0)
{
Console.WriteLine("nothing to show");
}
else
{
foreach (var game in games)
{
Console.WriteLine($"Player Name: {player} || Game mode: {game.Type} || Game difficulty: {game.Difficulty} || Score: {game.Score} || Time: {game.Time:mm\\:ss}");
}
}

Console.WriteLine("press any key to exit");
Console.ReadLine();
Console.Clear();

Menu menu = new Menu();
menu.GameMenu(player);
}

internal static string GetName()
{
Console.WriteLine("enter your name: ");
var player = Console.ReadLine();
while (string.IsNullOrEmpty(player))
{
Console.WriteLine("i said state your name");
player = Console.ReadLine();
}
return player;
}

internal static void AddToHistory(int score, GameType gameChoice, TimeSpan timeTaken, GameDifficulty gameDifficulty)
{
if (Game.GameChoice == "r")
{
gameChoice = GameType.Random;
}

var game = new Game
{
Type = gameChoice,
Score = score,
Time = timeTaken,
Difficulty = gameDifficulty
};
games.Add(game);
}

internal static int Validation(string input)
{
int result;
while (string.IsNullOrEmpty(input) || !Int32.TryParse(input, out result))
{
Console.WriteLine("answer needs to be an integer, try again");
input = Console.ReadLine();
}
return result;
}

internal static string ValidationYesOrNo(string prompt)
{
Console.WriteLine(prompt);
string yesOrNo = Console.ReadLine();
while (yesOrNo != "y" && yesOrNo != "n")
{
Console.WriteLine("decide yes or no");
yesOrNo = Console.ReadLine();
}
return yesOrNo;
}

public static (int, int, int) GetNumbersAndQuestion(string operation, GameDifficulty difficulty)
{
Random rand = new Random();
int firstValue = 0;
int secondValue = 0;

switch (difficulty)
{
case GameDifficulty.Easy:
firstValue = rand.Next(0, 10);
secondValue = rand.Next(0, 10);
break;
case GameDifficulty.Medium:
firstValue = rand.Next(10, 50);
secondValue = rand.Next(10, 50);
break;
case GameDifficulty.Hard:
firstValue = rand.Next(10, 100);
secondValue = rand.Next(10, 100);
break;
}

Console.Clear();
Console.WriteLine("solve the following: ");
Console.WriteLine($"{firstValue} {operation} {secondValue}");

Console.Write("your answer: ");
timer.Start();
string input = Console.ReadLine();

int userAnswer = Validation(input);
return (firstValue, secondValue, userAnswer);
}

internal static bool InitialiseGame()
{
Console.Clear();
timer.Reset();
bool loop = false;
return loop;
}

internal static (int, int, int) CheckThatNumbersAreDivisable(GameDifficulty difficulty)
{
Random rand = new Random();
int dividend = 0;
int divisor = 0;

do
{
switch (difficulty)
{
case GameDifficulty.Easy:
dividend = rand.Next(1, 100);
divisor = rand.Next(1, 100);
break;
case GameDifficulty.Medium:
dividend = rand.Next(1, 100);
divisor = rand.Next(1, 200);
break;
case GameDifficulty.Hard:
dividend = rand.Next(1, 100);
divisor = rand.Next(1, 300);
break;
}
}
while (dividend % divisor != 0);

Console.WriteLine("solve the following: ");
Console.WriteLine($"{dividend} / {divisor}");
timer.Start();
string input = Console.ReadLine();
int userAnswer = Validation(input);

return (dividend, divisor, userAnswer);
}

public static GameType Randomise()
{
Random rand = new Random();
var randomGameMode = (GameType)rand.Next(4);
return randomGameMode;
}

internal void GameLogic(int userAnswer, int expected, ref int score, GameType gameChoice, GameDifficulty gameDifficulty)
{
GameEngine engine = new GameEngine();
Menu menu = new Menu();

if (userAnswer == expected)
{
Console.Clear();
Console.WriteLine("correct");
score++;
}

else
{
timer.Stop();
TimeSpan timeTaken = timer.Elapsed;
string elapsedTime = "time taken (mm:ss): " + timeTaken.ToString(@"mm\:ss");
Console.WriteLine(elapsedTime);

AddToHistory(score, gameChoice, timeTaken, gameDifficulty);
Console.WriteLine($"incorrect. the correct answer was {expected}. your score is {score}.");
string restartGame = ValidationYesOrNo("try again? y/n");

if (restartGame == "y")
{
Console.Clear();
timer.Reset();
switch (gameChoice)
{
case GameType.Addition:
gameDifficulty = ChooseDifficulty();
engine.PlayGame(GameType.Addition, "+", (a, b) => a + b, gameDifficulty);
break;
case GameType.Subtraction:
gameDifficulty = ChooseDifficulty();
engine.PlayGame(GameType.Subtraction, "-", (a, b) => a - b, gameDifficulty);
break;
case GameType.Multiplication:
gameDifficulty = ChooseDifficulty();
engine.PlayGame(GameType.Multiplication, "x", (a, b) => a * b, gameDifficulty);
break;
case GameType.Division:
gameDifficulty = ChooseDifficulty();
engine.PlayGame(GameType.Division, "/", (a, b) => a / b, gameDifficulty);
break;
case GameType.Random:
gameDifficulty = ChooseDifficulty();
engine.PlayGame(GameType.Random, "", null, gameDifficulty);
break;
}
}
else
{
Console.Clear();
menu.GameMenu(player);
}
}
}

public static GameDifficulty ChooseDifficulty()
{
Console.Clear();
Console.WriteLine("choose a difficulty: ");
Console.WriteLine("1 - easy");
Console.WriteLine("2 - medium");
Console.WriteLine("3 - hard");
string input = Console.ReadLine();
int choice = Validation(input);
return choice switch
{
1 => GameDifficulty.Easy,
2 => GameDifficulty.Medium,
3 => GameDifficulty.Hard,
_ => throw new ArgumentOutOfRangeException("invalid choice")
};
}
}
}
60 changes: 60 additions & 0 deletions MathGame.royeeet/MathGame/MathGame.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{D51720E7-445A-466B-BE83-A0BAB765E58C}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>MathGame</RootNamespace>
<AssemblyName>MathGame</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup>
<LangVersion>12.0</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="GameEngine.cs" />
<Compile Include="Helpers.cs" />
<Compile Include="Menu.cs" />
<Compile Include="Models\Game.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Loading