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
24 changes: 24 additions & 0 deletions CodeReviews.Console.MathGame.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.2.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tenebris-06.MathGame", "Tenebris-06.MathGame\Tenebris-06.MathGame.csproj", "{3E3BF72D-9C95-C189-A25A-77F3C1042C60}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3E3BF72D-9C95-C189-A25A-77F3C1042C60}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3E3BF72D-9C95-C189-A25A-77F3C1042C60}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3E3BF72D-9C95-C189-A25A-77F3C1042C60}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3E3BF72D-9C95-C189-A25A-77F3C1042C60}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1F3971B0-9543-463C-8B64-17441F96B5FB}
EndGlobalSection
EndGlobal
10 changes: 10 additions & 0 deletions Tenebris-06.MathGame/Game.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class Game
{
public int score;
public List<Problem> problemSet;
public Game(int score, List<Problem> problemSet)
{
this.score = score;
this.problemSet = problemSet;
}
}
9 changes: 9 additions & 0 deletions Tenebris-06.MathGame/MenuOptionEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public enum MenuOption
{
Exit = 0,
Add = 1,
Subtract,
Multiply,
Divide,
History
}
61 changes: 61 additions & 0 deletions Tenebris-06.MathGame/Problem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
public class Problem
{
public string Text;
public int Answer;
public int UserAnswer;
public bool CorrectlyAnswered;


public Problem(string Text, int Answer)
{
this.Text = Text;
this.Answer = Answer;
}

public void SetText(string Text)
{
this.Text = Text;
}

public void SetAnswer(int Answer)
{
this.Answer = Answer;
}

public string GetText()
{
return Text;
}

public int GetAnswer()
{
return Answer;
}

public void SetUserAnswer(int userAnswer)
{
this.UserAnswer = userAnswer;
}

public int GetUserAnswer()
{
return UserAnswer;
}

public override string ToString()
{
string AnswerState;

switch (CorrectlyAnswered)
{
case true:
AnswerState = "✔";
break;

case false:
AnswerState = "✘";
break;
}
return $"{Text.PadRight(12)}{UserAnswer.ToString().PadRight(12)}{Answer.ToString().PadRight(12)}{AnswerState.PadLeft(10)}";
}
}
66 changes: 66 additions & 0 deletions Tenebris-06.MathGame/ProblemGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
public static class ProblemGenerator
{
static Random randomizer = new Random();
static int rand1;
static int rand2;
public static Problem GenerateProblem(MenuOption operatorValue)
{
do
{
rand1 = randomizer.Next(10);
rand2 = randomizer.Next(10);

if (!(operatorValue == MenuOption.Divide)) break;

} while (rand2 == 0 || rand1 % rand2 != 0);

string problemText = $"{rand1} {GetOperator(operatorValue)} {rand2} = ?";
int problemAnswer = -1;

switch (operatorValue)
{
case MenuOption.Add:
problemAnswer = rand1 + rand2;
break;

case MenuOption.Subtract:
problemAnswer = rand1 - rand2;
break;

case MenuOption.Multiply:
problemAnswer = rand1 * rand2;
break;

case MenuOption.Divide:
problemAnswer = rand1 / rand2;
break;

default:
break;
}

return new Problem(problemText, problemAnswer);

}

public static string GetOperator(MenuOption operatorValue)
{
switch (operatorValue)
{
case MenuOption.Add:
return "+";

case MenuOption.Subtract:
return "-";

case MenuOption.Multiply:
return "*";

case MenuOption.Divide:
return "/";

default:
return "";
}
}
}
78 changes: 78 additions & 0 deletions Tenebris-06.MathGame/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
class Program
{
static void Main(string[] args)
{

List<Game> gameHistory = new List<Game>();

while (true) {

Console.WriteLine("\t====== Math Game ======\nPick an Option:");
Console.WriteLine("[1] Addition \n[2] Subtraction \n[3] Multiplication \n[4] Division \n[5] History \n[0] Exit");
Console.Write("=> ");

int userChoice;

if (!(int.TryParse(Console.ReadLine(), out userChoice)))
{
Console.WriteLine("Invalid Input!");
}

if ((MenuOption)userChoice == MenuOption.History)
{
Console.WriteLine("\t=== Game History ===");

for (int i = 0; i <= gameHistory.Count - 1; i++)
{
Console.WriteLine($"\n[Game {i + 1}] (Score: {gameHistory[i].score})");
Console.WriteLine($"{"Problem".PadRight(12)}{"Your Answer".PadRight(12)}{"Correct Answer".PadRight(12)}{"Status".PadLeft(10)}");
foreach (Problem p in gameHistory[i].problemSet)
{
Console.WriteLine(p.ToString());
}
}

Console.WriteLine("Press any key to go back to main menu => ");
Console.ReadKey();
continue;

} else if ((MenuOption)userChoice == MenuOption.Exit)
{
Console.WriteLine("Goodbye!");
Environment.Exit(0);
}

int score = 0;
List<Problem> ProblemSet = new List<Problem>();

for (int i = 0; i <= 4; i++)
{
// cast the userChoice to it's corresponding enum option

Problem generatedProblem = ProblemGenerator.GenerateProblem((MenuOption)userChoice);
ProblemSet.Add(generatedProblem);
Console.WriteLine($"\t == Problem {i}: ==");
Console.WriteLine($"{generatedProblem.Text}");
Console.Write("=> ");
int.TryParse(Console.ReadLine(), out int userAnswer);

generatedProblem.SetUserAnswer(userAnswer);

if (userAnswer == generatedProblem.Answer)
{
Console.WriteLine("Correct!");
generatedProblem.CorrectlyAnswered = true;
score++;
} else
{
Console.WriteLine("Wrong!");
generatedProblem.CorrectlyAnswered = false;
}
}

Game currentGame = new Game(score, ProblemSet);
gameHistory.Add(currentGame);

}
}
}
11 changes: 11 additions & 0 deletions Tenebris-06.MathGame/Tenebris-06.MathGame.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<RootNamespace>Tenebris_06.MathGame</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>