From 38fff1d8db29a19e5402a75665fdf12344cec869 Mon Sep 17 00:00:00 2001 From: Rohit Bhuller Date: Wed, 4 Mar 2026 13:10:20 +0000 Subject: [PATCH 1/2] complete math game project --- MathGame.royeeet/MathGame.sln | 25 ++ MathGame.royeeet/MathGame/App.config | 6 + MathGame.royeeet/MathGame/GameEngine.cs | 49 ++++ MathGame.royeeet/MathGame/Helpers.cs | 257 ++++++++++++++++++ MathGame.royeeet/MathGame/MathGame.csproj | 60 ++++ MathGame.royeeet/MathGame/Menu.cs | 74 +++++ MathGame.royeeet/MathGame/Models/Game.cs | 29 ++ MathGame.royeeet/MathGame/Program.cs | 32 +++ .../MathGame/Properties/AssemblyInfo.cs | 33 +++ 9 files changed, 565 insertions(+) create mode 100644 MathGame.royeeet/MathGame.sln create mode 100644 MathGame.royeeet/MathGame/App.config create mode 100644 MathGame.royeeet/MathGame/GameEngine.cs create mode 100644 MathGame.royeeet/MathGame/Helpers.cs create mode 100644 MathGame.royeeet/MathGame/MathGame.csproj create mode 100644 MathGame.royeeet/MathGame/Menu.cs create mode 100644 MathGame.royeeet/MathGame/Models/Game.cs create mode 100644 MathGame.royeeet/MathGame/Program.cs create mode 100644 MathGame.royeeet/MathGame/Properties/AssemblyInfo.cs diff --git a/MathGame.royeeet/MathGame.sln b/MathGame.royeeet/MathGame.sln new file mode 100644 index 00000000..e4264b82 --- /dev/null +++ b/MathGame.royeeet/MathGame.sln @@ -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 diff --git a/MathGame.royeeet/MathGame/App.config b/MathGame.royeeet/MathGame/App.config new file mode 100644 index 00000000..193aecc6 --- /dev/null +++ b/MathGame.royeeet/MathGame/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/MathGame.royeeet/MathGame/GameEngine.cs b/MathGame.royeeet/MathGame/GameEngine.cs new file mode 100644 index 00000000..0a526aa5 --- /dev/null +++ b/MathGame.royeeet/MathGame/GameEngine.cs @@ -0,0 +1,49 @@ +using System; +using System.Diagnostics; +using MathGame.Models; + +namespace MathGame +{ + internal class GameEngine + { + public void PlayGame(GameType gameType, string symbol, Func operation, GameDifficulty difficulty) + { + int score = 0; + Helpers helpers = new Helpers(); + var loop = Helpers.InitialiseGame(); + + do + { + GameType currentGameType = gameType; + string currentSymbol = symbol; + Func 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); + } + } +} diff --git a/MathGame.royeeet/MathGame/Helpers.cs b/MathGame.royeeet/MathGame/Helpers.cs new file mode 100644 index 00000000..653e929e --- /dev/null +++ b/MathGame.royeeet/MathGame/Helpers.cs @@ -0,0 +1,257 @@ +using MathGame.Models; +using System; +using System.Collections.Generic; +using System.Timers; +using System.Diagnostics; +using System.Threading; +using System.Text; +using System.Data.SqlTypes; +using System.Xml.Schema; + +namespace MathGame +{ + internal class Helpers + { + public static string player = GetName(); + public static List games = new List(); + 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") + }; + } + } +} diff --git a/MathGame.royeeet/MathGame/MathGame.csproj b/MathGame.royeeet/MathGame/MathGame.csproj new file mode 100644 index 00000000..097f2df2 --- /dev/null +++ b/MathGame.royeeet/MathGame/MathGame.csproj @@ -0,0 +1,60 @@ + + + + + Debug + AnyCPU + {D51720E7-445A-466B-BE83-A0BAB765E58C} + Exe + MathGame + MathGame + v4.8 + 512 + true + true + + + 12.0 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/MathGame.royeeet/MathGame/Menu.cs b/MathGame.royeeet/MathGame/Menu.cs new file mode 100644 index 00000000..a4125ac0 --- /dev/null +++ b/MathGame.royeeet/MathGame/Menu.cs @@ -0,0 +1,74 @@ +using System; +using MathGame; +using MathGame.Models; + +namespace MathGame +{ + internal class Menu + { + GameEngine gameMenu = new(); + Game game = new(); + + public void GameMenu(string player) + { + Console.WriteLine("choose an operation to play in: "); + Console.WriteLine("a - addition"); + Console.WriteLine("s - subtraction"); + Console.WriteLine("m - multiplication"); + Console.WriteLine("d - division"); + Console.WriteLine("r - random"); + Console.WriteLine("v - view game history"); + Console.WriteLine("q - quit"); + Game.GameChoice = Console.ReadLine(); + var whichOperator = ""; + + switch (Game.GameChoice.Trim().ToLower()) + { + case "a": + whichOperator = "+"; + var gameDifficulty = game.Difficulty = Helpers.ChooseDifficulty(); + gameMenu.PlayGame(GameType.Addition, whichOperator, (a, b) => a + b, gameDifficulty); + break; + + case "s": + whichOperator = "-"; + gameDifficulty = game.Difficulty = Helpers.ChooseDifficulty(); + gameMenu.PlayGame(GameType.Subtraction, whichOperator, (a, b) => a - b, gameDifficulty); + break; + + case "m": + whichOperator = "x"; + gameDifficulty = game.Difficulty = Helpers.ChooseDifficulty(); + gameMenu.PlayGame(GameType.Multiplication, whichOperator, (a, b) => a * b, gameDifficulty); + break; + + case "d": + whichOperator = "/"; + gameDifficulty = game.Difficulty = Helpers.ChooseDifficulty(); + gameMenu.PlayGame(GameType.Division, whichOperator, (a, b) => a / b, gameDifficulty); + break; + + case "r": + gameDifficulty = game.Difficulty = Helpers.ChooseDifficulty(); + gameMenu.PlayGame(GameType.Random, "", null, gameDifficulty); + break; + + case "v": + Helpers.GameHistory(); + break; + + case "q": + Console.WriteLine("thanks for playing!"); + Console.Read(); + Environment.Exit(0); + break; + + default: + Console.Clear(); + Console.WriteLine("select one of the options"); + GameMenu(player); + break; + } + } + } +} diff --git a/MathGame.royeeet/MathGame/Models/Game.cs b/MathGame.royeeet/MathGame/Models/Game.cs new file mode 100644 index 00000000..46f7d78f --- /dev/null +++ b/MathGame.royeeet/MathGame/Models/Game.cs @@ -0,0 +1,29 @@ +using MathGame; +using System; + +namespace MathGame.Models; + +public class Game +{ + public int Score { get; set; } + public GameType Type { get; set; } + public TimeSpan Time { get; set; } + public static string GameChoice { get; set; } + public GameDifficulty Difficulty { get; set; } +} + +public enum GameType +{ + Addition, + Subtraction, + Multiplication, + Division, + Random +} + +public enum GameDifficulty +{ + Easy, + Medium, + Hard +} \ No newline at end of file diff --git a/MathGame.royeeet/MathGame/Program.cs b/MathGame.royeeet/MathGame/Program.cs new file mode 100644 index 00000000..c2e7c475 --- /dev/null +++ b/MathGame.royeeet/MathGame/Program.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using MathGame; + +/* math game requirements: + * 1. open with a menu prompting the user for their name, followed by choosing one of the 4 operators - done + * + * 2. for each operator, two random numbers should be generated, prompting the user to solve displayed equations - done + * + * 3. they should loop until the user inputs the wrong answer, with exceptions handled (putting a letter instead of a number) - done + * + * 4. the divisions must result in integers only, with dividends being 0-100 - done + * + * 5. any games played should be stored in some kind of list, which the user can view, no need for db tho - done + * + * 6. implement levels of difficulty - done + * + * 7. add a timer for games - done + * + * 8. use one method for all games - done + * + * 9. create a 'random game' option - done + */ + +Menu menu = new Menu(); +menu.GameMenu(Helpers.player); + + + + + + diff --git a/MathGame.royeeet/MathGame/Properties/AssemblyInfo.cs b/MathGame.royeeet/MathGame/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..d9202bde --- /dev/null +++ b/MathGame.royeeet/MathGame/Properties/AssemblyInfo.cs @@ -0,0 +1,33 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("MathGame")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("MathGame")] +[assembly: AssemblyCopyright("Copyright © 2024")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("d51720e7-445a-466b-be83-a0bab765e58c")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] From 44046b94ab2b8254d75427c4c93890e6b49b1285 Mon Sep 17 00:00:00 2001 From: Rohit Bhuller Date: Wed, 4 Mar 2026 13:18:35 +0000 Subject: [PATCH 2/2] removed unnecessary 'using' statements --- MathGame.royeeet/MathGame/GameEngine.cs | 1 - MathGame.royeeet/MathGame/Helpers.cs | 5 ----- MathGame.royeeet/MathGame/Menu.cs | 1 - MathGame.royeeet/MathGame/Program.cs | 4 +--- MathGame.royeeet/MathGame/Properties/AssemblyInfo.cs | 1 - 5 files changed, 1 insertion(+), 11 deletions(-) diff --git a/MathGame.royeeet/MathGame/GameEngine.cs b/MathGame.royeeet/MathGame/GameEngine.cs index 0a526aa5..cd82a683 100644 --- a/MathGame.royeeet/MathGame/GameEngine.cs +++ b/MathGame.royeeet/MathGame/GameEngine.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics; using MathGame.Models; namespace MathGame diff --git a/MathGame.royeeet/MathGame/Helpers.cs b/MathGame.royeeet/MathGame/Helpers.cs index 653e929e..d9469050 100644 --- a/MathGame.royeeet/MathGame/Helpers.cs +++ b/MathGame.royeeet/MathGame/Helpers.cs @@ -1,12 +1,7 @@ using MathGame.Models; using System; using System.Collections.Generic; -using System.Timers; using System.Diagnostics; -using System.Threading; -using System.Text; -using System.Data.SqlTypes; -using System.Xml.Schema; namespace MathGame { diff --git a/MathGame.royeeet/MathGame/Menu.cs b/MathGame.royeeet/MathGame/Menu.cs index a4125ac0..db6aa0c0 100644 --- a/MathGame.royeeet/MathGame/Menu.cs +++ b/MathGame.royeeet/MathGame/Menu.cs @@ -1,5 +1,4 @@ using System; -using MathGame; using MathGame.Models; namespace MathGame diff --git a/MathGame.royeeet/MathGame/Program.cs b/MathGame.royeeet/MathGame/Program.cs index c2e7c475..3be0d031 100644 --- a/MathGame.royeeet/MathGame/Program.cs +++ b/MathGame.royeeet/MathGame/Program.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using MathGame; +using MathGame; /* math game requirements: * 1. open with a menu prompting the user for their name, followed by choosing one of the 4 operators - done diff --git a/MathGame.royeeet/MathGame/Properties/AssemblyInfo.cs b/MathGame.royeeet/MathGame/Properties/AssemblyInfo.cs index d9202bde..5c9a634b 100644 --- a/MathGame.royeeet/MathGame/Properties/AssemblyInfo.cs +++ b/MathGame.royeeet/MathGame/Properties/AssemblyInfo.cs @@ -1,5 +1,4 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following