diff --git a/MathsGame/MathsGame.slnx b/MathsGame/MathsGame.slnx new file mode 100644 index 00000000..17475ec7 --- /dev/null +++ b/MathsGame/MathsGame.slnx @@ -0,0 +1,3 @@ + + + diff --git a/MathsGame/MathsGame/GameFunctions.cs b/MathsGame/MathsGame/GameFunctions.cs new file mode 100644 index 00000000..e0e11059 --- /dev/null +++ b/MathsGame/MathsGame/GameFunctions.cs @@ -0,0 +1,114 @@ +using System.Timers; + +namespace MathsGame; + +public class GameFunctions +{ + const int EASY = 1; + const int MEDIUM = 2; + const int HARD = 3; + + internal static System.Timers.Timer newTimer; + internal static int seconds; + + internal static int[] SetDifficulty(int[] difficulty) + { + // Set up initial variables + string? result; + bool validInput = false; + int userChoice = 0; + + do + { + Helpers.ConsoleClear(); + Console.WriteLine($"Choose your difficulty:\n"); + Console.WriteLine("1. Easy"); + Console.WriteLine("2. Medium"); + Console.WriteLine("3. Hard\n"); + Console.WriteLine("Enter a valid number from the list."); + + result = Console.ReadLine(); + + if (result == null) + { + Console.WriteLine("Input is null. Exiting the program"); + Environment.Exit(1); + return difficulty; + } + + try + { + int.Parse(result); + + } + catch (ArgumentNullException) + { + Console.WriteLine("No input detected. Please choose an option from the menu.\n"); + continue; + } + catch (FormatException) + { + Console.WriteLine($"{result} is not a valid number. Please choose an option from the menu.\n"); + continue; + } + + userChoice = int.Parse(result); + if (Helpers.isInRange(userChoice, 1, 3)) + { + validInput = true; + } + else + { + Console.WriteLine($"{userChoice} is not a menu option. Please choose again.\n"); + } + + } while (validInput == false); + + switch (userChoice) + { + case EASY: + difficulty[0] = 0; + difficulty[1] = 9; + return difficulty; + case MEDIUM: + difficulty[0] = 0; + difficulty[1] = 100; + return difficulty; + case HARD: + difficulty[0] = -100; + difficulty[1] = 100; + return difficulty; + default: + difficulty[0] = 0; + difficulty[1] = 9; + return difficulty; + } + } + + internal static void ViewLog(List log) + { + Console.Clear(); + Console.WriteLine("---------- Past Games Log ----------\n"); + + foreach (string line in log) + { + Console.WriteLine(line); + } + Console.WriteLine(); + Console.WriteLine("Press any key to return to the main menu."); + Console.ReadLine(); + } + + internal static bool DividesIntoInt(int firstNumber, int secondNumber) + { + if (firstNumber % secondNumber == 0) + { + return true; + } + else + { + return false; + } + } +} + diff --git a/MathsGame/MathsGame/Games.cs b/MathsGame/MathsGame/Games.cs new file mode 100644 index 00000000..6fbeb8c3 --- /dev/null +++ b/MathsGame/MathsGame/Games.cs @@ -0,0 +1,180 @@ +using System.Timers; + +namespace MathsGame; + +internal class Games +{ + + internal static System.Timers.Timer aTimer; + internal static int seconds; + + /* Function to run the game rounds. Picks a game based on the game choice + * which is found in the main Program.cs.*/ + internal static void Game(string gameChoice, int[] difficulty, int MAX_ROUNDS, List gameLog) + { + // Initial variables + Random random = new Random(); + int firstNumber; + int secondNumber; + int score = 0; + int userAnswer; + seconds = 0; + + Console.Clear(); + Console.WriteLine($"{gameChoice} game started...\n"); + + startTimer(); + + // Run rounds + for (int round = 0; round < MAX_ROUNDS; round++) + { + // Temporarily set the answer back to zero within the loop + userAnswer = 0; + + // + firstNumber = random.Next(difficulty[0], difficulty[1]); + secondNumber = random.Next(difficulty[0], difficulty[1]); + + // Handle division game option + if (gameChoice == "Division") + { + if (firstNumber == 0) + firstNumber += 1; + if (secondNumber == 0) + secondNumber += 1; + while (!GameFunctions.DividesIntoInt(firstNumber, secondNumber)) + { + firstNumber = random.Next(difficulty[0], difficulty[1]); + secondNumber = random.Next(difficulty[0], difficulty[1]); + if (firstNumber == 0) + firstNumber += 1; + if (secondNumber == 0) + secondNumber += 1; + } + } + + // List of Operation objects with different game operations + List operations = new List() + { + new Operations(firstNumber, secondNumber) + { + Result = firstNumber + secondNumber, + Question = $"What is {firstNumber} + {secondNumber}?\n" + }, + new Operations(firstNumber, secondNumber) + { + Result = firstNumber - secondNumber, + Question = $"What is {firstNumber} - {secondNumber}?\n" + } + }; + + + // Set a random game (or operation) from operations + Operations randomChoice = operations[random.Next(2)]; + + // Ask the question based on the game choice and get the answer + int correctAnswer = GetAnswer(gameChoice, firstNumber, secondNumber, randomChoice); + + // Get user input + string? result = Console.ReadLine(); + if (result == null) + { + Console.WriteLine("Result was null."); + Environment.Exit(1); + } + + // Check the user input + try + { + userAnswer = int.Parse(result); + } + catch (FormatException) + { + Console.WriteLine("Invalid number."); + } + + // Check the users answer and display messages + score += CheckAnswers(round, MAX_ROUNDS, userAnswer, correctAnswer); + + Helpers.ConsoleClear(); + } + + // Remove the timer + aTimer.Stop(); + aTimer.Dispose(); + + Console.WriteLine("Game Over. Press any key to return to the main menu.\n"); + Console.ReadLine(); + + string log = $"{gameChoice}: Difficulty: {difficulty[0]} to {difficulty[1]}, Total Score: {score}, Total Seconds: {seconds} --- {DateTime.Now}"; + gameLog.Add(log); + } + + // Function for asking the appropriate game question and getting the result. + private static int GetAnswer(string gameChoice, int firstNumber, int secondNumber, Operations randomChoice) + { + switch (gameChoice) + { + case "Addition": + Console.WriteLine($"What is {firstNumber} + {secondNumber}?\n"); + return firstNumber + secondNumber; + case "Subtraction": + Console.WriteLine($"What is {firstNumber} - {secondNumber}?\n"); + return firstNumber - secondNumber; + case "Multiplication": + Console.WriteLine($"What is {firstNumber} * {secondNumber}?\n"); + return firstNumber * secondNumber; + case "Division": + Console.WriteLine($"What is {firstNumber} / {secondNumber}?\n"); + return firstNumber / secondNumber; + case "Random": + Console.WriteLine(randomChoice.Question); + return randomChoice.Result; + default: + Console.WriteLine("Error, no gamemode selected."); + return -10000; // This number will never be reached, so it equals an error. + } + } + + // Function for checking the user answer against the correct answer. + private static int CheckAnswers(int round, int MAX_ROUNDS, int userAnswer, int correctAnswer) + { + int score = 0; + if (userAnswer == correctAnswer && round == MAX_ROUNDS - 1) + { + Console.WriteLine("Correct answer."); + score++; + return score; + } + else if (userAnswer == correctAnswer && round <= MAX_ROUNDS - 1) + { + Console.WriteLine("Correct answer.\n"); + score++; + return score; + } + else if (userAnswer != correctAnswer && round == MAX_ROUNDS - 1) + { + Console.WriteLine($"Incorrect. The answer was {correctAnswer}."); + return score; + } + else + { + Console.WriteLine($"Incorrect. The answer was {correctAnswer}.\n"); + return score; + } + } + + private static void startTimer() + { + aTimer = new System.Timers.Timer(1000); + aTimer.Start(); + aTimer.Elapsed += timerEvent; + aTimer.AutoReset = true; + aTimer.Enabled = true; + } + + private static void timerEvent(Object source, ElapsedEventArgs e) + { + seconds += 1; + } +} diff --git a/MathsGame/MathsGame/Helpers.cs b/MathsGame/MathsGame/Helpers.cs new file mode 100644 index 00000000..f0e0ae23 --- /dev/null +++ b/MathsGame/MathsGame/Helpers.cs @@ -0,0 +1,25 @@ +using System.Diagnostics.Contracts; + +namespace MathsGame; + +internal class Helpers +{ + internal static bool isInRange(int number, int lowerBound, int higherBound) + { + if (number < lowerBound || number > higherBound) + { + return false; + } + else + { + return true; + } + } + + internal static void ConsoleClear() + { + Thread.Sleep(1000); + Console.Clear(); + } + +} diff --git a/MathsGame/MathsGame/MathsGame.csproj b/MathsGame/MathsGame/MathsGame.csproj new file mode 100644 index 00000000..ed9781c2 --- /dev/null +++ b/MathsGame/MathsGame/MathsGame.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + + diff --git a/MathsGame/MathsGame/Operations.cs b/MathsGame/MathsGame/Operations.cs new file mode 100644 index 00000000..f5e1545e --- /dev/null +++ b/MathsGame/MathsGame/Operations.cs @@ -0,0 +1,11 @@ +namespace MathsGame +{ + internal class Operations + { + internal int Result { get; set; } + internal string? Question { get; set; } + + internal Operations(int first, int second) { } + + } +} diff --git a/MathsGame/MathsGame/Program.cs b/MathsGame/MathsGame/Program.cs new file mode 100644 index 00000000..67680c5f --- /dev/null +++ b/MathsGame/MathsGame/Program.cs @@ -0,0 +1,101 @@ +using MathsGame; + +// Game parameters +const int MAX_ROUNDS = 5; +int[] difficulty = new int[2]; +bool gameRunning = false; + +// Game Log +List gameLog = new List(); + +// Run initial difficulty setting +GameFunctions.SetDifficulty(difficulty); +Console.Clear(); + +TheMenu(); + +void TheMenu() +{ + do + { + Console.Clear(); + gameRunning = true; + + Console.WriteLine("---------- Maths Game ----------"); + Console.WriteLine("Select an option below by typing in the command line:\n"); + + Console.WriteLine("! - Change Difficulty\n"); + + Console.WriteLine("V - View Past Games\n"); + + Console.WriteLine("A - Addition"); + Console.WriteLine("S - Subtraction"); + Console.WriteLine("M - Multiplication"); + Console.WriteLine("D - Division"); + Console.WriteLine("R - Random\n"); + + Console.WriteLine("Q - Quit\n"); + + string? result = Console.ReadLine(); + if (result == null) + { + Console.WriteLine("Input was null, exiting game."); + Environment.Exit(1); + } + + try + { + char userInput = Convert.ToChar(result.Trim().ToLower()); + if (Char.IsPunctuation(userInput)) + { + switch (userInput) + { + case '!': + GameFunctions.SetDifficulty(difficulty); + break; + + } + } + else if (Char.IsAsciiLetter(userInput)) + { + switch (userInput) + { + // Place an arguement in the main Game() function that can be used to determine what + // operations to use. + case 'a': + Games.Game("Addition", difficulty, MAX_ROUNDS, gameLog); + break; + case 's': + Games.Game("Subtraction", difficulty, MAX_ROUNDS, gameLog); + break; + case 'm': + Games.Game("Multiplication", difficulty, MAX_ROUNDS, gameLog); + break; + case 'd': + Games.Game("Division", difficulty, MAX_ROUNDS, gameLog); + break; + case 'v': + GameFunctions.ViewLog(gameLog); + break; + case 'r': + Games.Game("Random", difficulty, MAX_ROUNDS, gameLog); + break; + case 'q': + gameRunning = false; + Environment.Exit(1); + break; + } + } + else + { + + } + } + catch (FormatException) + { + Console.WriteLine($"{result} has too many characters. Please only input one character."); + Helpers.ConsoleClear(); + } + + } while (gameRunning); +}