-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameManager.cs
243 lines (217 loc) · 8.69 KB
/
GameManager.cs
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using Guess_The_Number___Game.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Guess_The_Number___Game
{
public class GameManager
{
public int? PlayerQuantity { get; set; }
public int? Number { get; set; }
public List<Player>? Players { get; set; }
private DateTime? StartDateTime { get; set; }
private DateTime? EndDateTime { get; set; }
private TimeSpan? Duration { get; set; }
public string? WinnerName { get; set; }
private bool GameOver = false;
private int CurrentPlayerIndex;
public GameManager()
{
}
/// <summary>
/// Start the game.
/// </summary>
public void StartGame()
{
GameOver = false;
CurrentPlayerIndex = 0;
if(Players != null)
Players.Clear();
string introMessage = """
Welcome to Guess The Number game.
This game was developed by Bruno Lamonato, a student of production engineering, tech lover and programmer from Brazil!
My name is Bill and I'm the Game Manager.
To start playing, tell me how many players will play or enter "RECORDS" to check the biggest winners.
""";
Console.WriteLine(introMessage);
var read = Console.ReadLine();
if(read != null)
{
if(read.ToLower() == "records" || read.ToLower() == "record")
{
WriteRecords();
RestartGame();
}
else
{
RegisterPlayers(read);
var dif = new Difficulty(ChooseDifficulty());
Number = GetRandomNumber(dif.Id);
StartDateTime = DateTime.Now;
while (!GameOver)
{
Player currentPlayer = Players.ElementAt(CurrentPlayerIndex);
int guess = currentPlayer.TakeTurn(dif);
bool result = EvaluateGuess(guess);
if (result)
{
GameOver = true;
EndDateTime = DateTime.Now;
RegisterRecord(currentPlayer, currentPlayer.TriesInMatch, GetGameDuration(), dif.Name);
RestartGame();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Wrong!");
Console.ForegroundColor = ConsoleColor.Black;
CurrentPlayerIndex = (CurrentPlayerIndex + 1) % Players.Count;
}
}
}
}
}
private void RestartGame()
{
Console.WriteLine("Play again? Type y/n");
var c = Console.ReadLine();
if (c == "y" | c == "yes")
{
StartGame();
}
}
private void RegisterRecord(Player currentPlayer, int tries, TimeSpan timeSpan, string difficultyName)
{
Console.WriteLine($"Congratulations, {currentPlayer.Name}! You won the game!");
Record rec = new Record(currentPlayer.Name, tries, timeSpan, difficultyName);
using(GameDbContext dbContext = new GameDbContext())
{
dbContext.Records.Add(rec);
dbContext.SaveChanges();
}
}
private void RegisterPlayers(string _numberOfPlayers)
{
if(Players != null)
{
Players.Clear();
}
else
{
Players = new List<Player>();
}
bool canPass = false;
int numberOfPlayers = 0;
while (!canPass)
{
bool check = int.TryParse(_numberOfPlayers, out numberOfPlayers);
if (check)
{
canPass = true;
}
else
{
Console.WriteLine("Choose a valid option. Write the number of players or write \"records\" to see the records.");
_numberOfPlayers = String.Empty;
break;
}
for (int i = 0; i < numberOfPlayers; i++)
{
var id = i + 1;
Console.WriteLine($"Tell me the name of Player {id}");
var chosenName = Console.ReadLine();
while (string.IsNullOrEmpty(chosenName))
{
Console.WriteLine($"This name is invalid. Tell me the name of Player {i + 1}");
chosenName = Console.ReadLine();
}
Player player = new Player(id, chosenName);
Players.Add(player);
}
}
}
private bool EvaluateGuess(int guess)
{
return guess == Number;
}
/// <summary>
/// This method write in Console the Highscores.
/// </summary>
private void WriteRecords()
{
using(GameDbContext db = new GameDbContext())
{
var records = db.Records.ToList();
var recordsByDifficulty = records.GroupBy(r => r.DifficultyName);
foreach (var difficultyGroup in recordsByDifficulty)
{
Console.WriteLine($"Difficulty: {difficultyGroup.Key}\n");
// Sort records by number of tries and duration within each difficulty group
var sortedRecords = difficultyGroup.OrderBy(r => r.Tries).ThenBy(r => r.Duration);
int count = 1;
foreach (var record in sortedRecords.Take(10))
{
Console.WriteLine($"#{count++}: Player: {record.PlayerName}, Tries: {record.Tries}, Duration: {record.Duration}");
}
Console.WriteLine();
}
}
}
private TimeSpan GetGameDuration()
{
return (EndDateTime.GetValueOrDefault() - StartDateTime.GetValueOrDefault());
}
private int GetRandomNumber(int dif)
{
Difficulty difficulty = new(dif);
Random random = new Random();
int number = random.Next(0, difficulty.toRange + 1);
return number;
}
public int ChooseDifficulty()
{
Console.Write("╔════════════════════════════════════════╗\n");
Console.Write("║ ");
Console.ForegroundColor = ConsoleColor.DarkBlue;
Console.Write("Choose a Difficulty");
Console.ForegroundColor = ConsoleColor.Black;
Console.Write(" ║\n");
Console.Write("║ ║\n");
Console.Write("║ ");
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.Write("1. Oracle - Number range: 0-25");
Console.ForegroundColor = ConsoleColor.Black;
Console.Write(" ║\n");
Console.Write("║ ");
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.Write("2. Seer - Number range: 0-100");
Console.ForegroundColor = ConsoleColor.Black;
Console.Write(" ║\n");
Console.Write("║ ");
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.Write("3. Nostradamus - Number range: 0-1000");
Console.ForegroundColor = ConsoleColor.Black;
Console.Write("║\n");
Console.Write("╚════════════════════════════════════════╝\n\n\n");
int option = -1;
while (option != 1 && option != 2 && option != 3)
{
var input = Console.ReadLine();
if (int.TryParse(input, out option))
{
if (option != 1 && option != 2 && option != 3)
{
Console.WriteLine("Choose one difficulty: 1, 2, or 3.");
}
}
else
{
Console.WriteLine("Invalid input. Choose one difficulty: 1, 2, or 3.");
}
}
return option;
}
}
}