Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
133 lines (117 loc) · 3.24 KB
/
Copy pathProgram.cs
File metadata and controls
133 lines (117 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System.Diagnostics;
int points = 0;
List<string> history = new List<string>();
MathGame();
void MathGame()
{
DisplayMenu();
string? input = Console.ReadLine();
HandleMenuChoice(input);
}
void DisplayMenu()
{
Console.WriteLine("Choose from the menu below:");
Console.WriteLine("1. Start Game");
Console.WriteLine("2. Display History");
Console.WriteLine("3. Exit");
}
void HandleMenuChoice(string? choice)
{
switch (choice)
{
case "1":
StartGame();
break;
case "2":
DisplayList();
break;
case "3":
Console.WriteLine("Exiting the game. Goodbye!");
Environment.Exit(0);
break;
default:
Console.WriteLine("Invalid choice. Please try again.");
break;
}
}
void StartGame()
{
Stopwatch stopwatch = Stopwatch.StartNew();
for (int i = 0; i < 5; i++)
{
int number1 = new Random().Next(1, 9);
int number2 = new Random().Next(1, 9);
MathOperation operation = (MathOperation)new Random().Next(0, 4);
string operationSymbol = operation switch
{
MathOperation.Addition => "+",
MathOperation.Subtraction => "-",
MathOperation.Multiplication => "*",
MathOperation.Division => "/",
_ => throw new ArgumentOutOfRangeException()
};
Console.WriteLine($"Question {i + 1}: What is {number1} {operationSymbol} {number2}?");
string? input = Console.ReadLine();
if (CheckAnswer(number1, number2, operation, input))
{
points++;
}
}
stopwatch.Stop();
Console.WriteLine($"You scored {points} out of 5.");
Console.WriteLine($"Time taken: {stopwatch.Elapsed.TotalSeconds:F2} seconds.");
history.Add($"Game played on {DateTime.Now}: Scored {points}/5, Time taken: {stopwatch.Elapsed.TotalSeconds:F2} seconds.");
PlayAgainPrompt();
}
void PlayAgainPrompt()
{
Console.WriteLine("Do you want to return to the menu? (yes/no)");
string? playAgain = Console.ReadLine();
if (playAgain?.ToLower() == "yes")
{
points = 0;
MathGame();
}
else if(playAgain?.ToLower() == "no")
{
Console.WriteLine("Thank you for playing!");
Environment.Exit(0);
}
else
{
Console.WriteLine("Invalid input, try again.");
PlayAgainPrompt();
}
}
void DisplayList()
{
Console.WriteLine("History of previous games:");
foreach (string item in history)
{
Console.WriteLine($"- {item}");
}
PlayAgainPrompt();
}
bool CheckAnswer(int number1, int number2, MathOperation operation, string? result)
{
int correctAnswer = operation switch
{
MathOperation.Addition => number1 + number2,
MathOperation.Subtraction => number1 - number2,
MathOperation.Multiplication => number1 * number2,
MathOperation.Division => number1 / number2,
_ => throw new ArgumentException("Invalid operation")
};
if (int.TryParse(result, out int parsedValue) && parsedValue == correctAnswer)
{
return true;
}
return false;
}
public enum MathOperation
{
Addition,
Subtraction,
Multiplication,
Division
}