From ff4e9d9e0fa19894826c67f100dcddea69fe3e46 Mon Sep 17 00:00:00 2001 From: matejadb <92729211+matejadb@users.noreply.github.com> Date: Mon, 29 Jun 2026 16:11:24 +0200 Subject: [PATCH 1/4] finish MathGame project --- MathGame.matejadb/History.cs | 7 ++ MathGame.matejadb/Logic.cs | 136 +++++++++++++++++++++ MathGame.matejadb/MathGame.matejadb.csproj | 10 ++ MathGame.matejadb/MathGame.matejadb.slnx | 3 + MathGame.matejadb/Program.cs | 12 ++ MathGame.matejadb/Question.cs | 8 ++ 6 files changed, 176 insertions(+) create mode 100644 MathGame.matejadb/History.cs create mode 100644 MathGame.matejadb/Logic.cs create mode 100644 MathGame.matejadb/MathGame.matejadb.csproj create mode 100644 MathGame.matejadb/MathGame.matejadb.slnx create mode 100644 MathGame.matejadb/Program.cs create mode 100644 MathGame.matejadb/Question.cs diff --git a/MathGame.matejadb/History.cs b/MathGame.matejadb/History.cs new file mode 100644 index 00000000..4b31662f --- /dev/null +++ b/MathGame.matejadb/History.cs @@ -0,0 +1,7 @@ +namespace MathGame.matejadb; + +internal class History +{ + public int GameNumber { get; set; } + public int Points { get; set; } +} diff --git a/MathGame.matejadb/Logic.cs b/MathGame.matejadb/Logic.cs new file mode 100644 index 00000000..b160f71b --- /dev/null +++ b/MathGame.matejadb/Logic.cs @@ -0,0 +1,136 @@ +namespace MathGame.matejadb; + +public class Logic +{ + private int _gameNumber = 0; + List _histories = new List(); + private static readonly Random _random = new Random(); + public void Game() + { + GameMenu(); + } + + + void GameMenu() + { + Console.WriteLine("=================================="); + Console.WriteLine("Welcome to the Math Game!"); + Console.WriteLine("1. New Game"); + Console.WriteLine("2. Game History"); + Console.WriteLine("3. Exit"); + Console.WriteLine("=================================="); + + SelectOption(); + } + + void SelectOption() + { + Console.Write("Select an option: "); + string? option = Console.ReadLine(); + + switch (option) + { + case "1": + Console.Clear(); + NewGame(); + break; + case "2": + Console.Clear(); + ShowHistory(); + GameMenu(); + break; + case "3": + Console.WriteLine("See you soon!"); + Environment.Exit(0); + break; + default: + Console.WriteLine("Unknown action! Please try again!"); + GameMenu(); + break; + } + } + + void ShowHistory() + { + Console.WriteLine("==============HISTORY=============="); + foreach (History history in _histories) + { + Console.WriteLine($"Game {history.GameNumber}: {history.Points} points"); + } + Console.WriteLine("=================================="); + } + + void NewGame() + { + Console.WriteLine("=================================="); + Console.WriteLine("New Game Started. Good luck!"); + Console.WriteLine("=================================="); + _gameNumber++; + int totalPoints = 0; + int numberOfRounds = 5; + + + for (int i = 0; i < numberOfRounds; i++) + { + Console.WriteLine("=================================="); + Console.WriteLine($"Round {i + 1} of {numberOfRounds} \t\t Points: {totalPoints}"); + + Question question = GenerateQuestion(); + + Console.Write($"{i + 1}. {question.Number1} {question.Mark} {question.Number2} = "); + string? answer = Console.ReadLine(); + totalPoints = CheckAnswer(answer, question, totalPoints); + } + + _histories.Add(new History() { GameNumber = _gameNumber, Points = totalPoints }); + + Console.WriteLine($"Game ended! Your total points: {totalPoints}/{numberOfRounds}"); + GameMenu(); + } + + static int CheckAnswer(string answer, Question question, int totalPoints) + { + double result = 0; + + switch (question.Mark) + { + case '+': + result = question.Number1 + question.Number2; + break; + case '-': + result = question.Number1 - question.Number2; + break; + case '*': + result = question.Number1 * question.Number2; + break; + case '/': + result = question.Number1 / question.Number2; + break; + default: + Console.WriteLine("Something went wrong."); + break; + } + + if (int.TryParse(answer, out var userAnswer) && userAnswer == result) totalPoints++; + + return totalPoints; + } + + static Question GenerateQuestion() + { + char[] marks = ['+', '-', '*', '/']; + + double num1 = _random.Next(0, 101); + double num2 = _random.Next(1, 101); + char mark = marks[_random.Next(marks.Length)]; + + if (mark == '/' && num1 % num2 != 0) + { + return GenerateQuestion(); + } + + return new Question(num1, num2, mark); + + + } +} diff --git a/MathGame.matejadb/MathGame.matejadb.csproj b/MathGame.matejadb/MathGame.matejadb.csproj new file mode 100644 index 00000000..ed9781c2 --- /dev/null +++ b/MathGame.matejadb/MathGame.matejadb.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + + diff --git a/MathGame.matejadb/MathGame.matejadb.slnx b/MathGame.matejadb/MathGame.matejadb.slnx new file mode 100644 index 00000000..c452efa0 --- /dev/null +++ b/MathGame.matejadb/MathGame.matejadb.slnx @@ -0,0 +1,3 @@ + + + diff --git a/MathGame.matejadb/Program.cs b/MathGame.matejadb/Program.cs new file mode 100644 index 00000000..008b377e --- /dev/null +++ b/MathGame.matejadb/Program.cs @@ -0,0 +1,12 @@ +namespace MathGame.matejadb; + +class Program +{ + static void Main(string[] args) + { + Logic gameLogic = new Logic(); + + gameLogic.Game(); + } + +} diff --git a/MathGame.matejadb/Question.cs b/MathGame.matejadb/Question.cs new file mode 100644 index 00000000..df2c5279 --- /dev/null +++ b/MathGame.matejadb/Question.cs @@ -0,0 +1,8 @@ +namespace MathGame.matejadb; + +internal class Question(double num1, double num2, char mark) +{ + public double Number1 { get; set; } = num1; + public double Number2 { get; set; } = num2; + public char Mark { get; set; } = mark; +} From c9f685e7c09082d1ee7b45c1378cd28f6ccb67bf Mon Sep 17 00:00:00 2001 From: matejadb <92729211+matejadb@users.noreply.github.com> Date: Mon, 29 Jun 2026 16:35:07 +0200 Subject: [PATCH 2/4] improved game history --- MathGame.matejadb/History.cs | 1 + MathGame.matejadb/Logic.cs | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/MathGame.matejadb/History.cs b/MathGame.matejadb/History.cs index 4b31662f..1c94ba15 100644 --- a/MathGame.matejadb/History.cs +++ b/MathGame.matejadb/History.cs @@ -3,5 +3,6 @@ internal class History { public int GameNumber { get; set; } + public string? Answer { get; set; } public int Points { get; set; } } diff --git a/MathGame.matejadb/Logic.cs b/MathGame.matejadb/Logic.cs index b160f71b..d6318df9 100644 --- a/MathGame.matejadb/Logic.cs +++ b/MathGame.matejadb/Logic.cs @@ -55,9 +55,9 @@ void ShowHistory() Console.WriteLine("==============HISTORY=============="); foreach (History history in _histories) { - Console.WriteLine($"Game {history.GameNumber}: {history.Points} points"); + Console.WriteLine($"Game {history.GameNumber}: {history.Answer}. Points recieved: {history.Points}\n"); } - Console.WriteLine("=================================="); + Console.WriteLine("==================================\n"); } void NewGame() @@ -78,17 +78,22 @@ void NewGame() Question question = GenerateQuestion(); Console.Write($"{i + 1}. {question.Number1} {question.Mark} {question.Number2} = "); + + string? answer = Console.ReadLine(); - totalPoints = CheckAnswer(answer, question, totalPoints); + bool correctAnswer = CheckAnswer(answer, question); + + totalPoints += correctAnswer ? 1 : 0; + + _histories.Add(new History() { GameNumber = _gameNumber, Answer = $"{question.Number1} {question.Mark} {question.Number2} = {answer}" ,Points = correctAnswer ? 1 : 0}); } - _histories.Add(new History() { GameNumber = _gameNumber, Points = totalPoints }); Console.WriteLine($"Game ended! Your total points: {totalPoints}/{numberOfRounds}"); GameMenu(); } - static int CheckAnswer(string answer, Question question, int totalPoints) + static bool CheckAnswer(string answer, Question question) { double result = 0; @@ -111,9 +116,9 @@ static int CheckAnswer(string answer, Question question, int totalPoints) break; } - if (int.TryParse(answer, out var userAnswer) && userAnswer == result) totalPoints++; + if (int.TryParse(answer, out var userAnswer) && userAnswer == result) return true; - return totalPoints; + return false; } static Question GenerateQuestion() From c806c0824ca8564017d224740b926a950fbc9d5a Mon Sep 17 00:00:00 2001 From: matejadb <92729211+matejadb@users.noreply.github.com> Date: Mon, 29 Jun 2026 16:48:26 +0200 Subject: [PATCH 3/4] add question convertion to string to remove repetition --- MathGame.matejadb/Logic.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/MathGame.matejadb/Logic.cs b/MathGame.matejadb/Logic.cs index d6318df9..aebe3e7f 100644 --- a/MathGame.matejadb/Logic.cs +++ b/MathGame.matejadb/Logic.cs @@ -76,8 +76,9 @@ void NewGame() Console.WriteLine($"Round {i + 1} of {numberOfRounds} \t\t Points: {totalPoints}"); Question question = GenerateQuestion(); + string questionStr = ConvertQuestionToString(question); - Console.Write($"{i + 1}. {question.Number1} {question.Mark} {question.Number2} = "); + Console.Write($"{i + 1}. {questionStr}"); string? answer = Console.ReadLine(); @@ -85,7 +86,7 @@ void NewGame() totalPoints += correctAnswer ? 1 : 0; - _histories.Add(new History() { GameNumber = _gameNumber, Answer = $"{question.Number1} {question.Mark} {question.Number2} = {answer}" ,Points = correctAnswer ? 1 : 0}); + _histories.Add(new History() { GameNumber = _gameNumber, Answer = $"{questionStr}{answer}" ,Points = correctAnswer ? 1 : 0}); } @@ -121,6 +122,10 @@ static bool CheckAnswer(string answer, Question question) return false; } + static string ConvertQuestionToString(Question question) { + return $"{question.Number1} {question.Mark} {question.Number2} = "; + } + static Question GenerateQuestion() { char[] marks = ['+', '-', '*', '/']; From 20cdaebbb7efa691c1b406ba31ff7522f87c00df Mon Sep 17 00:00:00 2001 From: matejadb <92729211+matejadb@users.noreply.github.com> Date: Mon, 29 Jun 2026 21:57:31 +0200 Subject: [PATCH 4/4] add operation selection --- MathGame.matejadb/Logic.cs | 40 ++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/MathGame.matejadb/Logic.cs b/MathGame.matejadb/Logic.cs index aebe3e7f..76566ed2 100644 --- a/MathGame.matejadb/Logic.cs +++ b/MathGame.matejadb/Logic.cs @@ -15,9 +15,13 @@ void GameMenu() { Console.WriteLine("=================================="); Console.WriteLine("Welcome to the Math Game!"); - Console.WriteLine("1. New Game"); - Console.WriteLine("2. Game History"); - Console.WriteLine("3. Exit"); + Console.WriteLine("1. New Random Game"); + Console.WriteLine("2. Practice Addition"); + Console.WriteLine("3. Practice Subtraction"); + Console.WriteLine("4. Practice Multiplication"); + Console.WriteLine("5. Practice Division"); + Console.WriteLine("6. Game History"); + Console.WriteLine("7. Exit"); Console.WriteLine("=================================="); SelectOption(); @@ -35,11 +39,27 @@ void SelectOption() NewGame(); break; case "2": + Console.Clear(); + NewGame("+"); + break; + case "3": + Console.Clear(); + NewGame("-"); + break; + case "4": + Console.Clear(); + NewGame("*"); + break; + case "5": + Console.Clear(); + NewGame("/"); + break; + case "6": Console.Clear(); ShowHistory(); GameMenu(); break; - case "3": + case "7": Console.WriteLine("See you soon!"); Environment.Exit(0); break; @@ -60,7 +80,7 @@ void ShowHistory() Console.WriteLine("==================================\n"); } - void NewGame() + void NewGame(string userSelection = null) { Console.WriteLine("=================================="); Console.WriteLine("New Game Started. Good luck!"); @@ -75,7 +95,7 @@ void NewGame() Console.WriteLine("=================================="); Console.WriteLine($"Round {i + 1} of {numberOfRounds} \t\t Points: {totalPoints}"); - Question question = GenerateQuestion(); + Question question = GenerateQuestion(userSelection); string questionStr = ConvertQuestionToString(question); Console.Write($"{i + 1}. {questionStr}"); @@ -86,7 +106,7 @@ void NewGame() totalPoints += correctAnswer ? 1 : 0; - _histories.Add(new History() { GameNumber = _gameNumber, Answer = $"{questionStr}{answer}" ,Points = correctAnswer ? 1 : 0}); + _histories.Add(new History() { GameNumber = _gameNumber, Answer = $"{questionStr}{answer}", Points = correctAnswer ? 1 : 0}); } @@ -126,17 +146,17 @@ static string ConvertQuestionToString(Question question) { return $"{question.Number1} {question.Mark} {question.Number2} = "; } - static Question GenerateQuestion() + static Question GenerateQuestion(string selectedOperation) { char[] marks = ['+', '-', '*', '/']; double num1 = _random.Next(0, 101); double num2 = _random.Next(1, 101); - char mark = marks[_random.Next(marks.Length)]; + char mark = selectedOperation != null ? Convert.ToChar(selectedOperation) : marks[_random.Next(marks.Length)]; if (mark == '/' && num1 % num2 != 0) { - return GenerateQuestion(); + return GenerateQuestion(selectedOperation); } return new Question(num1, num2, mark);