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 MathsGame/MathsGame.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Solution>
<Project Path="MathsGame/MathsGame.csproj" />
</Solution>
114 changes: 114 additions & 0 deletions MathsGame/MathsGame/GameFunctions.cs
Original file line number Diff line number Diff line change
@@ -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<string> 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;
}
}
}

180 changes: 180 additions & 0 deletions MathsGame/MathsGame/Games.cs
Original file line number Diff line number Diff line change
@@ -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<string> 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> operations = new List<Operations>()
{
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;
}
}
25 changes: 25 additions & 0 deletions MathsGame/MathsGame/Helpers.cs
Original file line number Diff line number Diff line change
@@ -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();
}

}
10 changes: 10 additions & 0 deletions MathsGame/MathsGame/MathsGame.csproj
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>
11 changes: 11 additions & 0 deletions MathsGame/MathsGame/Operations.cs
Original file line number Diff line number Diff line change
@@ -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) { }

}
}
Loading