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
10 changes: 10 additions & 0 deletions ConsoleMathGame/ConsoleMathGame.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>
24 changes: 24 additions & 0 deletions ConsoleMathGame/ConsoleMathGame.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}") = "ConsoleMathGame", "ConsoleMathGame.csproj", "{98C51DDF-AF71-1511-B745-A8B42D2494AA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{98C51DDF-AF71-1511-B745-A8B42D2494AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{98C51DDF-AF71-1511-B745-A8B42D2494AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{98C51DDF-AF71-1511-B745-A8B42D2494AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{98C51DDF-AF71-1511-B745-A8B42D2494AA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {794E7496-4DFE-4778-AA51-94AD03C5EFD4}
EndGlobalSection
EndGlobal
166 changes: 166 additions & 0 deletions ConsoleMathGame/GameLogic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
namespace MathGame {
public class GameLogic {
public List<string> GameHistory { get; set; } = new List<string>();

public enum MathOperation
{
Addition,
Subtraction,
Multiplication,
Division
}

public enum Difficulty
{
Easy,
Medium,
Hard
}

private int[] GenerateNumbers(Difficulty difficulty, MathOperation operation)
{
var rng = new Random();
int maxValue;
int[] operands = new int[2];

switch (difficulty)
{
case Difficulty.Easy:
maxValue = 20;
break;
case Difficulty.Medium:
maxValue = 50;
break;
case Difficulty.Hard:
maxValue = 100;
break;
default:
return operands;
}

if (operation == MathOperation.Division)
{
operands[1] = (int)rng.NextInt64(100) + 1;
operands[0] = (int)((rng.NextInt64(maxValue)+1) * operands[1]);
}
else
{
operands[0] = (int)rng.NextInt64(maxValue+1);
operands[1] = (int)rng.NextInt64(maxValue+1);
}

return operands;
}

public void NewGame()
{
Console.Clear();
//Get difficulty input
Console.WriteLine("Please select a difficulty:");
Console.WriteLine("[1] Easy");
Console.WriteLine("[2] Medium");
Console.WriteLine("[3] Hard");
Console.WriteLine("[anykey] Exit Current Game");
Difficulty difficulty;
var input = Console.ReadKey();
int score = 0;
Console.WriteLine();

switch (input.KeyChar)
{
case '1':
difficulty = Difficulty.Easy;
break;
case '2':
difficulty = Difficulty.Medium;
break;
case '3':
difficulty = Difficulty.Hard;
break;
default:
return;
}

Console.Clear();
//Get operation input
Console.WriteLine("Please select an operation:");
Console.WriteLine("[1] Addition");
Console.WriteLine("[2] Subtraction");
Console.WriteLine("[3] Multiplication");
Console.WriteLine("[4] Division");
Console.WriteLine("[anykey] Exit Current Game");
MathOperation operation;
input = Console.ReadKey();
Console.WriteLine();

switch (input.KeyChar)
{
case '1':
operation = MathOperation.Addition;
break;
case '2':
operation = MathOperation.Subtraction;
break;
case '3':
operation = MathOperation.Multiplication;
break;
case '4':
operation = MathOperation.Division;
break;
default:
return;
}

var opSign = operation switch
{
MathOperation.Addition => "+",
MathOperation.Subtraction => "-",
MathOperation.Multiplication => "*",
MathOperation.Division => "/",
_ => throw new ArgumentException("Invalid Operation")
};

for(int i = 0; i < 5; i++)
{
int[] operands = GenerateNumbers(difficulty, operation);

string question = $"{operands[0]} {opSign} {operands[1]}";
Console.WriteLine(question);
var response = Console.ReadLine();
int userAnswer;

int answer = operation switch
{
MathOperation.Addition => operands[0] + operands[1],
MathOperation.Subtraction => operands[0] - operands[1],
MathOperation.Multiplication => operands[0] * operands[1],
MathOperation.Division => operands[0] / operands[1],
_ => throw new ArgumentException("Invalid operation")
};

bool validInput = int.TryParse(response, out userAnswer);
if (validInput && userAnswer == answer)
{
score++;
Console.WriteLine($"Correct! Your score is now {score}/{i+1}");
GameHistory.Add(question + $" = {answer}");
} else
{
Console.WriteLine($"Whoops! The correct answer was {answer}. Your score is now {score}/{i+1}");
GameHistory.Add(question + $" =/= {response}\tINCORRECT");
}

if(i != 4) {
for(int j = 3; j > 0; j--)
{
Console.Write($"\rNext question in {j}...");
Thread.Sleep(700);
}
}
Console.Clear();
}

Console.WriteLine($"You got {score}/5 questions correct.");
}
}
}
55 changes: 55 additions & 0 deletions ConsoleMathGame/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
You need to create a game that consists of asking the player what's the result of a math question (i.e. 9 x 9 = ?), collecting the input and adding a point in case of a correct answer.

A game needs to have at least 5 questions.

The divisions should result in INTEGERS ONLY and dividends should go from 0 to 100. Example: Your app shouldn't present the division 7/2 to the user, since it doesn't result in an integer.

Users should be presented with a menu to choose an operation

You should record previous games in a List and there should be an option in the menu for the user to visualize a history of previous games.

You don't need to record results on a database. Once the program is closed the results will be deleted.
*/

using MathGame;
var game = new GameLogic();

bool playing = true;

while(playing){
Console.Clear();
Console.WriteLine("Start a new game? y/n");
var input = Console.ReadKey();

if(input.KeyChar != 'y')
{
playing = false;
}
else
{
game.NewGame();
}

if(playing)
{
Console.WriteLine("View Game History? y/n");
input = Console.ReadKey();
if(input.KeyChar != 'y')
{
continue;
}
else
{
Console.Clear();
foreach (string question in game.GameHistory)
{
Console.WriteLine(question);
}

Console.WriteLine("Questions answered: " + game.GameHistory.Count);
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
}
}