Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions MathGame.stevenwardlaw/MathGame.stevenwardlaw.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Solution>
<Project Path="MathGame.stevenwardlaw/MathGame.stevenwardlaw.csproj" />
</Solution>
131 changes: 131 additions & 0 deletions MathGame.stevenwardlaw/MathGame.stevenwardlaw/MathGame.cs
Original file line number Diff line number Diff line change
@@ -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 }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
44 changes: 44 additions & 0 deletions MathGame.stevenwardlaw/MathGame.stevenwardlaw/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace MathGame.stevenwardlaw
{
internal class Program
{
static void Main(string[] args)
{
// List of ints to keep score history
List<int> scoreHistory = new List<int>();
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;
}
}

}
}
}