diff --git a/MathGame.stevenwardlaw/MathGame.stevenwardlaw.slnx b/MathGame.stevenwardlaw/MathGame.stevenwardlaw.slnx new file mode 100644 index 00000000..ee9ca754 --- /dev/null +++ b/MathGame.stevenwardlaw/MathGame.stevenwardlaw.slnx @@ -0,0 +1,3 @@ + + + diff --git a/MathGame.stevenwardlaw/MathGame.stevenwardlaw/MathGame.cs b/MathGame.stevenwardlaw/MathGame.stevenwardlaw/MathGame.cs new file mode 100644 index 00000000..9a6a433e --- /dev/null +++ b/MathGame.stevenwardlaw/MathGame.stevenwardlaw/MathGame.cs @@ -0,0 +1,131 @@ +namespace MathGame.stevenwardlaw +{ + internal class MathGame + { + // Fields to keep score, number of questions & player choice of game + public int Score { get; private set; } + private int numQuestions; + private int playerGameChoice; + + // Store game type from enum + private Operations gameType; + + // Set fields to starting values in constructor + public MathGame(int numQuestions) + { + Score = 0; + this.numQuestions = numQuestions; + } + + public MathGame() + { + Score = 0; + numQuestions = 5; + } + + // Method to run the game + public void RunGame() + { + Console.WriteLine("Welcome to the math game!"); + Console.WriteLine("Select from the following options:\n" + + "1 - Addition Quiz\n" + + "2 - Subtraction Quiz\n" + + "3 - Multiplication Quiz\n" + + "4 - Division Quiz\n" + + "5 - Mixed Quiz"); + + playerGameChoice = Convert.ToInt32(Console.ReadLine()); + + switch (playerGameChoice) + { + case 1: + gameType = Operations.Addition; + break; + case 2: + gameType = Operations.Subtraction; + break; + case 3: + gameType = Operations.Multiplication; + break; + case 4: + gameType = Operations.Division; + break; + case 5: + gameType = Operations.Mixed; + break; + default: + Console.WriteLine("Invalid option entered, defaulting to addition quiz."); + gameType = Operations.Addition; + break; + } + + for (int i = 0; i < numQuestions; i++) + { + AskQuestion(); + } + } + + // Method to get question + + public void AskQuestion() + { + int correctAnswer; + int firstNumber; + int secondNumber; + string symbol; + int randomNumber; + int playerGuess; + + firstNumber = Random.Shared.Next(1, 101); + secondNumber = Random.Shared.Next(1, 101); + randomNumber = Random.Shared.Next(1, 5); + + if (gameType == Operations.Addition || + (gameType == Operations.Mixed && randomNumber == 1)) + { + correctAnswer = firstNumber + secondNumber; + symbol = "+"; + } + else if (gameType == Operations.Subtraction || + (gameType == Operations.Mixed && randomNumber == 2)) + { + correctAnswer = firstNumber - secondNumber; + symbol = "-"; + } + else if (gameType == Operations.Multiplication || + (gameType == Operations.Mixed && randomNumber == 3)) + { + correctAnswer = firstNumber * secondNumber; + symbol = "*"; + } + else + { + do + { + firstNumber = Random.Shared.Next(1, 101); + secondNumber = Random.Shared.Next(1, 101); + } + while (firstNumber % secondNumber != 0); + + correctAnswer = firstNumber / secondNumber; + symbol = "/"; + } + + Console.WriteLine($"What is {firstNumber} {symbol} {secondNumber}?"); + playerGuess = Convert.ToInt32(Console.ReadLine()); + + if (playerGuess == correctAnswer) + { + Score++; + Console.WriteLine($"That's correct! Your score is now {Score}."); + } + else + { + Console.WriteLine("That's incorrect!"); + } + } + + } + + public enum Operations { Addition, Subtraction, Multiplication, Division, Mixed } +} diff --git a/MathGame.stevenwardlaw/MathGame.stevenwardlaw/MathGame.stevenwardlaw.csproj b/MathGame.stevenwardlaw/MathGame.stevenwardlaw/MathGame.stevenwardlaw.csproj new file mode 100644 index 00000000..ed9781c2 --- /dev/null +++ b/MathGame.stevenwardlaw/MathGame.stevenwardlaw/MathGame.stevenwardlaw.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + + diff --git a/MathGame.stevenwardlaw/MathGame.stevenwardlaw/Program.cs b/MathGame.stevenwardlaw/MathGame.stevenwardlaw/Program.cs new file mode 100644 index 00000000..d30aa5ef --- /dev/null +++ b/MathGame.stevenwardlaw/MathGame.stevenwardlaw/Program.cs @@ -0,0 +1,44 @@ +namespace MathGame.stevenwardlaw +{ + internal class Program + { + static void Main(string[] args) + { + // List of ints to keep score history + List scoreHistory = new List(); + MathGame currentGame; + bool gameState = true; + + while (gameState) + { + currentGame = new MathGame(); + + currentGame.RunGame(); + + Console.WriteLine($"Quiz complete. Your final score is {currentGame.Score}."); + scoreHistory.Add(currentGame.Score); + + Console.WriteLine("Would you like to play again or see your score history? " + + "1 - Play again. 2 - See score history. 3 - Exit game."); + int choice = Convert.ToInt32(Console.ReadLine()); + + switch (choice) + { + case 1: + Console.WriteLine("Let's play again!"); + break; + case 2: + for (int i = 0; i < scoreHistory.Count; i++) + { + Console.WriteLine($"Game {i + 1}: {scoreHistory[i]}"); + } + break; + case 3: + gameState = false; + break; + } + } + + } + } +}