From 514c44b738b594d3c3d8bb5865699b7ed507e986 Mon Sep 17 00:00:00 2001 From: Lessmeister Date: Fri, 20 Feb 2026 22:02:58 -0600 Subject: [PATCH 1/7] Setup game structure --- .gitignore | 4 + MathGame/MathGame.slnx | 3 + MathGame/MathGame/MathGame.csproj | 10 ++ MathGame/MathGame/Program.cs | 178 ++++++++++++++++++++++++++++++ 4 files changed, 195 insertions(+) create mode 100644 MathGame/MathGame.slnx create mode 100644 MathGame/MathGame/MathGame.csproj create mode 100644 MathGame/MathGame/Program.cs diff --git a/.gitignore b/.gitignore index 154e1272..9c828168 100644 --- a/.gitignore +++ b/.gitignore @@ -475,3 +475,7 @@ $RECYCLE.BIN/ # Windows shortcuts *.lnk + + +# User Defined Section +Planning.txt \ No newline at end of file diff --git a/MathGame/MathGame.slnx b/MathGame/MathGame.slnx new file mode 100644 index 00000000..c91187ad --- /dev/null +++ b/MathGame/MathGame.slnx @@ -0,0 +1,3 @@ + + + diff --git a/MathGame/MathGame/MathGame.csproj b/MathGame/MathGame/MathGame.csproj new file mode 100644 index 00000000..2150e379 --- /dev/null +++ b/MathGame/MathGame/MathGame.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/MathGame/MathGame/Program.cs b/MathGame/MathGame/Program.cs new file mode 100644 index 00000000..3a38cb29 --- /dev/null +++ b/MathGame/MathGame/Program.cs @@ -0,0 +1,178 @@ +using System.Text; + +namespace MathGame +{ + internal class Program + { + Random _randGenerator = new(); + List _history = new(); + int _numOfQuestions = 5; + GameType _currentGameType = GameType.Addition; + + static void Main(string[] args) + { + Program program = new Program(); + program.MainMenuGameStart(); + } + + private void MainMenuGameStart() + { + Console.Clear(); + Console.WriteLine("This is The Math Game."); + Console.WriteLine($"Your current score is: {CalculateScore()}."); + Console.WriteLine("\nSelect an option:"); + Console.WriteLine($"1: Play Game (Current selection: {_currentGameType}, {_numOfQuestions} quesitons)"); + Console.WriteLine($"2: Options"); + Console.WriteLine($"3: Game History"); + Console.WriteLine($"4: Quit"); + + string selection = GetMenuOption("1", "2", "3", "4"); + + switch (selection) + { + case "1": + //PlayGame(); + MainMenuGameStart(); + break; + case "2": + OptionsMenu(); + break; + case "3": + ShowGameHistory(); + break; + case "4": + return; + } + } + private void OptionsMenu() + { + Console.Clear(); + Console.WriteLine("Options:"); + Console.WriteLine($"1: Select game type (Current: {_currentGameType})"); + Console.WriteLine($"2: Set number of questions (Current:{_numOfQuestions})"); + Console.WriteLine("3: Back"); + + var selection = GetMenuOption("1", "2", "3"); + + Console.Clear(); + switch (selection) + { + case "1": + Console.WriteLine("Choose Game Type"); + Console.WriteLine("1: Addition"); + Console.WriteLine("2: Subtraction"); + Console.WriteLine("3: Multiplication"); + Console.WriteLine("4: Division"); + _currentGameType = (GameType)int.Parse(GetMenuOption("1", "2", "3", "4")); + break; + case "2": + Console.WriteLine("Enter the number of questions per round."); + string inputNum = string.Empty; + int num; + while( !int.TryParse(selection, out num)) + { + inputNum = Console.ReadLine(); + } + _numOfQuestions = num; + break; + case "3": + MainMenuGameStart(); + break; + } + OptionsMenu(); + } + private void ShowGameHistory() + { + Console.Clear(); + if (_history.Count == 0) + { + Console.WriteLine("Play a game to see your game history"); + } + else + { + foreach (Game run in _history) + run.ToString(); + } + Console.WriteLine("Press enter to continue"); + Console.ReadLine(); + MainMenuGameStart(); + + } + + private string GetMenuOption(params string[] validInputs) + { + bool firstRun = true; + string input = Console.ReadLine(); + while (!validInputs.Contains(input)) + { + ClearLinesAbove(1); + if (firstRun) + { + Console.WriteLine("Enter valid option:"); + firstRun = false; + } + input = Console.ReadLine(); + } + return input; + } + public static void ClearLinesAbove(int linesToClear) + { + for (int i = 0; i < linesToClear; i++) + { + Console.SetCursorPosition(0, Console.CursorTop - 1); + ClearCurrentConsoleLine(); + } + } + public static void ClearCurrentConsoleLine() + { + int currentLineCursor = Console.CursorTop; + Console.SetCursorPosition(0, Console.CursorTop); + Console.Write(new string(' ', Console.WindowWidth)); + Console.SetCursorPosition(0, currentLineCursor); + } + private string CalculateScore() + { + int score = 0; + foreach(Game run in _history) + score += run.Score; + return score.ToString(); + } + + record Game() + { + public int Score; + public string GameType; + public List Questions; + + public override string ToString() + { + StringBuilder sb = new(); + + sb.AppendLine($"{GameType} game with {Questions.Count} questions."); + for ( int i = 0; i < Questions.Count; i++) + { + sb.AppendLine($"\nQuesiton {i + 1}"); + sb.AppendLine( Questions[i].ToString() ); + } + return sb.ToString(); + } + } + + record Question(string questionText) + { + public string QuestionText = questionText; + public string Answer = "\"question not answered\""; + public bool AnswerCorrect = false; + + public override string ToString() => QuestionText + $"\nUser's Answer: {Answer} was {(AnswerCorrect ? "correct" : "incorrect")}."; + } + + enum GameType + { + Addition = 1, + Subtraction, + Multiplication, + Division + } + } +} From a7ff26ffe9deea43baa0b1423a1643eafdb742c1 Mon Sep 17 00:00:00 2001 From: Lessmeister Date: Mon, 23 Feb 2026 08:54:35 -0600 Subject: [PATCH 2/7] Code complete. Testing needed --- MathGame/MathGame/Program.cs | 119 ++++++++++++++++++++++++++++++----- 1 file changed, 103 insertions(+), 16 deletions(-) diff --git a/MathGame/MathGame/Program.cs b/MathGame/MathGame/Program.cs index 3a38cb29..d7b8f840 100644 --- a/MathGame/MathGame/Program.cs +++ b/MathGame/MathGame/Program.cs @@ -1,10 +1,10 @@ using System.Text; +using static System.Runtime.InteropServices.JavaScript.JSType; namespace MathGame { internal class Program { - Random _randGenerator = new(); List _history = new(); int _numOfQuestions = 5; GameType _currentGameType = GameType.Addition; @@ -31,8 +31,7 @@ private void MainMenuGameStart() switch (selection) { case "1": - //PlayGame(); - MainMenuGameStart(); + PlayGame(); break; case "2": OptionsMenu(); @@ -41,9 +40,18 @@ private void MainMenuGameStart() ShowGameHistory(); break; case "4": + Environment.Exit(0); return; } } + private void PlayGame() + { + Console.Clear(); + Game currentGame = new(_currentGameType, _numOfQuestions); + currentGame.RunGame(); + _history.Add(currentGame); + MainMenuGameStart(); + } private void OptionsMenu() { Console.Clear(); @@ -69,7 +77,7 @@ private void OptionsMenu() Console.WriteLine("Enter the number of questions per round."); string inputNum = string.Empty; int num; - while( !int.TryParse(selection, out num)) + while( !int.TryParse(inputNum, out num)) { inputNum = Console.ReadLine(); } @@ -90,8 +98,17 @@ private void ShowGameHistory() } else { - foreach (Game run in _history) - run.ToString(); + bool firstGame = true; + for (int i = 0; i < _history.Count; i++) + { + if (!firstGame) + { + Console.WriteLine("\n\n"); + firstGame = false; + } + Console.Write($"Game {i + 1}:"); + Console.WriteLine(_history[i].ToString()); + } } Console.WriteLine("Press enter to continue"); Console.ReadLine(); @@ -138,17 +155,80 @@ private string CalculateScore() return score.ToString(); } - record Game() + record Game(GameType gameType, int numOfQuestions) { - public int Score; - public string GameType; - public List Questions; + public int Score { get; private set; } + GameType _gameType = gameType; + int _numOfQuestions = numOfQuestions; + public List Questions = new(numOfQuestions); + private void BuildGame() + { + Random rand = new(); + while (_numOfQuestions > 0) + { + int one = rand.Next(0, 100); + int two = rand.Next(0,100); + int answer = 0; + string operation = string.Empty; + switch (_gameType) + { + case GameType.Addition: + operation = "+"; + answer = one + two; + break; + case GameType.Subtraction: + operation = "-"; + answer = one - two; + break; + case GameType.Multiplication: + operation = "*"; + answer = two - one; + break; + case GameType.Division: + List factors = new(); + if (one%two != 0) + { + for (two = 1; two < one; two++) + { + if(one%two == 0) factors.Add(two); + } + } + var index = rand.Next(0, factors.Count); + two = factors[index]; + operation = "/"; + answer = one / two; + break; + } + Questions.Add(new Question($"{one} {operation} {two}", answer)); + _numOfQuestions--; + } + } + public void RunGame() + { + BuildGame(); + foreach (Question question in Questions) + { + Console.Clear(); + Console.WriteLine(question.QuestionText); + string answer = Console.ReadLine(); + bool answerValid = int.TryParse(answer, out int intAnswer); + if (!answerValid) + { + question.Answer = "Invalid Input"; + question.AnswerCorrect = false; + } + question.Answer = answer; + question.AnswerCorrect = intAnswer == question.correctAnswer; + if (question.AnswerCorrect) Score++; + } + } public override string ToString() { StringBuilder sb = new(); - sb.AppendLine($"{GameType} game with {Questions.Count} questions."); + sb.AppendLine($"{_gameType} game with {Questions.Count} questions."); + sb.AppendLine($"User scored {Score} out of {Questions.Count}."); for ( int i = 0; i < Questions.Count; i++) { sb.AppendLine($"\nQuesiton {i + 1}"); @@ -158,13 +238,20 @@ public override string ToString() } } - record Question(string questionText) + record Question(string questionText, int correctAnswer) { - public string QuestionText = questionText; - public string Answer = "\"question not answered\""; - public bool AnswerCorrect = false; + public string QuestionText { get; init; } = questionText; + public string Answer { get; set; } = "\"question not answered\""; + public int CorrectAnswer { get; init; } = correctAnswer; + public bool AnswerCorrect { get; set; } = false; + + public override string ToString() + { + string ans = QuestionText + $"\nUser's Answer: {Answer} was {(AnswerCorrect ? "correct" : "incorrect")}."; + if (!AnswerCorrect) ans += $"\nCorrect answer: {CorrectAnswer.ToString()}"; + return ans; + } - public override string ToString() => QuestionText + $"\nUser's Answer: {Answer} was {(AnswerCorrect ? "correct" : "incorrect")}."; } enum GameType From 015e5cd858c24d37cb07c39bf0bd20c906d94ac1 Mon Sep 17 00:00:00 2001 From: Lessmeister Date: Mon, 23 Feb 2026 15:36:32 -0600 Subject: [PATCH 3/7] Updated Random bounds to include 100 --- MathGame/MathGame/Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MathGame/MathGame/Program.cs b/MathGame/MathGame/Program.cs index d7b8f840..b376b70a 100644 --- a/MathGame/MathGame/Program.cs +++ b/MathGame/MathGame/Program.cs @@ -167,8 +167,8 @@ private void BuildGame() Random rand = new(); while (_numOfQuestions > 0) { - int one = rand.Next(0, 100); - int two = rand.Next(0,100); + int one = rand.Next(0, 101); + int two = rand.Next(0,101); int answer = 0; string operation = string.Empty; switch (_gameType) From fd306d945654bed4197355b86ffadf31a6d575a1 Mon Sep 17 00:00:00 2001 From: Lessmeister Date: Tue, 24 Feb 2026 09:32:49 -0600 Subject: [PATCH 4/7] Fixed multiplicaiton --- MathGame/MathGame/Program.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/MathGame/MathGame/Program.cs b/MathGame/MathGame/Program.cs index b376b70a..4e4fe866 100644 --- a/MathGame/MathGame/Program.cs +++ b/MathGame/MathGame/Program.cs @@ -162,6 +162,7 @@ record Game(GameType gameType, int numOfQuestions) int _numOfQuestions = numOfQuestions; public List Questions = new(numOfQuestions); + // Debated making this private vs public. Rationale for private: no params or return value, so there's no outside user input allowed, so why expose it private void BuildGame() { Random rand = new(); @@ -183,7 +184,7 @@ private void BuildGame() break; case GameType.Multiplication: operation = "*"; - answer = two - one; + answer = one * two; break; case GameType.Division: List factors = new(); From ec95566574149743bf482e9195b020f171d6ee12 Mon Sep 17 00:00:00 2001 From: Lessmeister Date: Tue, 24 Feb 2026 09:33:45 -0600 Subject: [PATCH 5/7] Removed extraneous using statement --- MathGame/MathGame/Program.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/MathGame/MathGame/Program.cs b/MathGame/MathGame/Program.cs index 4e4fe866..ef00229d 100644 --- a/MathGame/MathGame/Program.cs +++ b/MathGame/MathGame/Program.cs @@ -1,5 +1,4 @@ using System.Text; -using static System.Runtime.InteropServices.JavaScript.JSType; namespace MathGame { From c320b1beb14f556b965becf6b0d217f3644a0688 Mon Sep 17 00:00:00 2001 From: Lessmeister Date: Tue, 24 Feb 2026 09:35:55 -0600 Subject: [PATCH 6/7] Removed "Console." with static using statement --- MathGame/MathGame/Program.cs | 81 ++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 40 deletions(-) diff --git a/MathGame/MathGame/Program.cs b/MathGame/MathGame/Program.cs index ef00229d..a4f2ac7e 100644 --- a/MathGame/MathGame/Program.cs +++ b/MathGame/MathGame/Program.cs @@ -1,4 +1,5 @@ using System.Text; +using static System.Console; namespace MathGame { @@ -16,14 +17,14 @@ static void Main(string[] args) private void MainMenuGameStart() { - Console.Clear(); - Console.WriteLine("This is The Math Game."); - Console.WriteLine($"Your current score is: {CalculateScore()}."); - Console.WriteLine("\nSelect an option:"); - Console.WriteLine($"1: Play Game (Current selection: {_currentGameType}, {_numOfQuestions} quesitons)"); - Console.WriteLine($"2: Options"); - Console.WriteLine($"3: Game History"); - Console.WriteLine($"4: Quit"); + Clear(); + WriteLine("This is The Math Game."); + WriteLine($"Your current score is: {CalculateScore()}."); + WriteLine("\nSelect an option:"); + WriteLine($"1: Play Game (Current selection: {_currentGameType}, {_numOfQuestions} quesitons)"); + WriteLine($"2: Options"); + WriteLine($"3: Game History"); + WriteLine($"4: Quit"); string selection = GetMenuOption("1", "2", "3", "4"); @@ -45,7 +46,7 @@ private void MainMenuGameStart() } private void PlayGame() { - Console.Clear(); + Clear(); Game currentGame = new(_currentGameType, _numOfQuestions); currentGame.RunGame(); _history.Add(currentGame); @@ -53,32 +54,32 @@ private void PlayGame() } private void OptionsMenu() { - Console.Clear(); - Console.WriteLine("Options:"); - Console.WriteLine($"1: Select game type (Current: {_currentGameType})"); - Console.WriteLine($"2: Set number of questions (Current:{_numOfQuestions})"); - Console.WriteLine("3: Back"); + Clear(); + WriteLine("Options:"); + WriteLine($"1: Select game type (Current: {_currentGameType})"); + WriteLine($"2: Set number of questions (Current:{_numOfQuestions})"); + WriteLine("3: Back"); var selection = GetMenuOption("1", "2", "3"); - Console.Clear(); + Clear(); switch (selection) { case "1": - Console.WriteLine("Choose Game Type"); - Console.WriteLine("1: Addition"); - Console.WriteLine("2: Subtraction"); - Console.WriteLine("3: Multiplication"); - Console.WriteLine("4: Division"); + WriteLine("Choose Game Type"); + WriteLine("1: Addition"); + WriteLine("2: Subtraction"); + WriteLine("3: Multiplication"); + WriteLine("4: Division"); _currentGameType = (GameType)int.Parse(GetMenuOption("1", "2", "3", "4")); break; case "2": - Console.WriteLine("Enter the number of questions per round."); + WriteLine("Enter the number of questions per round."); string inputNum = string.Empty; int num; while( !int.TryParse(inputNum, out num)) { - inputNum = Console.ReadLine(); + inputNum = ReadLine(); } _numOfQuestions = num; break; @@ -90,10 +91,10 @@ private void OptionsMenu() } private void ShowGameHistory() { - Console.Clear(); + Clear(); if (_history.Count == 0) { - Console.WriteLine("Play a game to see your game history"); + WriteLine("Play a game to see your game history"); } else { @@ -102,15 +103,15 @@ private void ShowGameHistory() { if (!firstGame) { - Console.WriteLine("\n\n"); + WriteLine("\n\n"); firstGame = false; } - Console.Write($"Game {i + 1}:"); - Console.WriteLine(_history[i].ToString()); + Write($"Game {i + 1}:"); + WriteLine(_history[i].ToString()); } } - Console.WriteLine("Press enter to continue"); - Console.ReadLine(); + WriteLine("Press enter to continue"); + ReadLine(); MainMenuGameStart(); } @@ -118,16 +119,16 @@ private void ShowGameHistory() private string GetMenuOption(params string[] validInputs) { bool firstRun = true; - string input = Console.ReadLine(); + string input = ReadLine(); while (!validInputs.Contains(input)) { ClearLinesAbove(1); if (firstRun) { - Console.WriteLine("Enter valid option:"); + WriteLine("Enter valid option:"); firstRun = false; } - input = Console.ReadLine(); + input = ReadLine(); } return input; } @@ -135,16 +136,16 @@ public static void ClearLinesAbove(int linesToClear) { for (int i = 0; i < linesToClear; i++) { - Console.SetCursorPosition(0, Console.CursorTop - 1); + SetCursorPosition(0, CursorTop - 1); ClearCurrentConsoleLine(); } } public static void ClearCurrentConsoleLine() { - int currentLineCursor = Console.CursorTop; - Console.SetCursorPosition(0, Console.CursorTop); - Console.Write(new string(' ', Console.WindowWidth)); - Console.SetCursorPosition(0, currentLineCursor); + int currentLineCursor = CursorTop; + SetCursorPosition(0, CursorTop); + Write(new string(' ', WindowWidth)); + SetCursorPosition(0, currentLineCursor); } private string CalculateScore() { @@ -209,9 +210,9 @@ public void RunGame() BuildGame(); foreach (Question question in Questions) { - Console.Clear(); - Console.WriteLine(question.QuestionText); - string answer = Console.ReadLine(); + Clear(); + WriteLine(question.QuestionText); + string answer = ReadLine(); bool answerValid = int.TryParse(answer, out int intAnswer); if (!answerValid) { From edd0922f235473fad35d8ba866abe692b5e0c868 Mon Sep 17 00:00:00 2001 From: Lessmeister Date: Tue, 24 Feb 2026 15:50:13 -0600 Subject: [PATCH 7/7] Removed Question.AnswerCorrect default value per Codacy Static Code Analysis --- MathGame/MathGame/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MathGame/MathGame/Program.cs b/MathGame/MathGame/Program.cs index a4f2ac7e..2a4cef4e 100644 --- a/MathGame/MathGame/Program.cs +++ b/MathGame/MathGame/Program.cs @@ -244,7 +244,7 @@ record Question(string questionText, int correctAnswer) public string QuestionText { get; init; } = questionText; public string Answer { get; set; } = "\"question not answered\""; public int CorrectAnswer { get; init; } = correctAnswer; - public bool AnswerCorrect { get; set; } = false; + public bool AnswerCorrect { get; set; } public override string ToString() {