-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameManager.cs
343 lines (322 loc) · 10.7 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
using System.Diagnostics;
using DotQueens.Core;
using Spectre.Console;
namespace DotQueens.Game;
public class GameManager
{
private Board? currentBoard;
private int cursorX = 0;
private int cursorY = 0;
private Stopwatch stopwatch = new Stopwatch();
public GameManager()
{
RenderMainMenu();
}
private void RenderMainMenu()
{
// Using Spectre.Console for rendering the main menu
AnsiConsole.Clear();
// The menu is a panel with a header and a list of options
var titleText = "♛ Welcome to DotQueens! ♛";
var titleTextMarkup = new Markup(titleText, new Style(foreground: Color.Red));
var totalTitleLenght = titleText.Length + 4;
// Center title text in console
var titleTextCentered = new Padder(titleTextMarkup).PadLeft((Console.WindowWidth + totalTitleLenght) / 4);
AnsiConsole.Write(titleTextCentered);
var selection = AnsiConsole.Prompt(
new SelectionPrompt<string>()
.Title("Select your option:")
.AddChoices(new[] {
"Start Game",
"About",
"Exit"
}));
if (selection == "Exit")
{
ExitGame();
}
else if (selection == "About")
{
RenderAbout();
}
else if (selection == "Start Game")
{
RenderLevelSelect();
}
}
private void RenderLevelSelect()
{
// Create a prompt to ask the user for the level difficulty
// The user can choose between easy, medium, and hard
var difficulty = AnsiConsole.Prompt(
new SelectionPrompt<string>()
.Title("Select your level:")
.AddChoices(new[] { "😊 Easy", "🤨 Medium", "😱 Hard" }));
StartNewGame(difficulty);
}
private void StartNewGame(string difficulty)
{
int size = 4;
if (difficulty == "🤨 Medium")
{
size = 6;
}
else if (difficulty == "😱 Hard")
{
size = 9;
}
var board = new Board(size);
while (!board.IsLevelSolvable())
{
board = new Board(size);
}
board.ResetQueens();
currentBoard = board;
cursorX = 0;
cursorY = 0;
stopwatch.Reset();
stopwatch.Start();
AnsiConsole.Clear();
RenderGame();
}
private void RenderAbout()
{
AnsiConsole.Clear();
var aboutText = @"
[bold]The rules:[/]
DotQueens is a console-based game where you have to place N queens ♛ on an NxN colored grid.
Each color must only have one queen, and no queen can be in the same row or column as another queen.
Queens can also not be next to each other diagonally.
The game is won when all queens are placed on the board without any conflicts.
[bold]About the game:[/]
Created by :person_raising_hand: Christian Mühle :e_mail: [email protected] / :globe_showing_europe_africa: devowl.de
The game is inspired by Queens from LinkedIn.";
AnsiConsole.MarkupLine(aboutText);
var back = AnsiConsole.Prompt(
new SelectionPrompt<string>()
.Title("Press Enter to go back")
.AddChoices(new[] { "Back" }));
if (back == "Back")
{
RenderMainMenu();
}
}
private void RenderGame(bool toggleHelp = false)
{
Console.SetCursorPosition(0, 0);
Console.CursorVisible = false;
if (currentBoard!.ValidateDetail() == Board.ValidationType.None)
{
RenderWinScreen();
}
var board = currentBoard;
var boardSize = board!.Size;
var canvas = new Canvas(boardSize, boardSize);
for (int r = 0; r < boardSize; r++)
{
for (int c = 0; c < boardSize; c++)
{
canvas.SetPixel(r, c, GetColorAt(r, c));
}
}
canvas.Scale = true;
canvas.PixelWidth = 3;
AnsiConsole.Write(canvas);
// Render already placed queens
var blockedRows = new HashSet<int>();
var blockedColumns = new HashSet<int>();
var blockedColors = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
for (int r = 0; r < boardSize; r++)
{
for (int c = 0; c < boardSize; c++)
{
if (board.Grid[r, c].HasQueen)
{
RenderQueen(r * 3, c);
blockedRows.Add(r);
blockedColumns.Add(c);
blockedColors.Add(board.Grid[r, c].Color);
}
}
}
if (toggleHelp)
{
// Render an X in each cell that would be invalid
for (int r = 0; r < boardSize; r++)
{
for (int c = 0; c < boardSize; c++)
{
if ((blockedRows.Contains(r) || blockedColumns.Contains(c) || blockedColors.Contains(board.Grid[r, c].Color)) && !board.Grid[r, c].HasQueen)
{
RenderBlock(r * 3, c);
}
// If the cell has a queen also render a block in the direct diagonals but not in the same cell
if (board.Grid[r, c].HasQueen)
{
if (r > 0 && c > 0)
{
RenderBlock((r - 1) * 3, c - 1);
}
if (r > 0 && c < boardSize - 1)
{
RenderBlock((r - 1) * 3, c + 1);
}
if (r < boardSize - 1 && c > 0)
{
RenderBlock((r + 1) * 3, c - 1);
}
if (r < boardSize - 1 && c < boardSize - 1)
{
RenderBlock((r + 1) * 3, c + 1);
}
}
}
}
}
// Render the cursor
MoveCursor(cursorX, cursorY);
Console.WriteLine("Arrow keys to move, Press Enter to toggle a queen, r to reset, ESC to go back to main menu");
if (toggleHelp)
{
AnsiConsole.MarkupLine("h to toggle help view: [green]Enabled[/] ");
}
else
{
AnsiConsole.MarkupLine("h to toggle help view: [red]Disabled[/] ");
}
// Render current validation state
var validation = board.ValidateDetail();
var validationText = "";
switch (validation)
{
case Board.ValidationType.RowsAndColumns:
validationText = "Row/Column conflict! ";
break;
case Board.ValidationType.Adjacency:
validationText = "Diagonal conflict! ";
break;
case Board.ValidationType.Colors:
validationText = "At least one Color has no Queen!";
break;
}
if (validation != Board.ValidationType.None)
{
AnsiConsole.MarkupLine($"State: [red]{validationText}[/]");
}
var result = Console.ReadKey();
var exit = false;
switch (result.Key)
{
case ConsoleKey.UpArrow:
if (cursorY > 0)
{
cursorY--;
}
break;
case ConsoleKey.DownArrow:
if (cursorY < boardSize - 1)
{
cursorY++;
}
break;
case ConsoleKey.LeftArrow:
if (cursorX >= 3)
{
cursorX -= 3;
}
break;
case ConsoleKey.RightArrow:
if (cursorX < (boardSize * 3) - 3)
{
cursorX += 3;
}
break;
case ConsoleKey.Enter:
if (board.Grid[cursorX / 3, cursorY].HasQueen)
{
board.Grid[cursorX / 3, cursorY].HasQueen = false;
}
else
{
board.Grid[cursorX / 3, cursorY].HasQueen = true;
}
break;
case ConsoleKey.Escape:
{
exit = true;
RenderMainMenu();
}
break;
case ConsoleKey.H:
{
toggleHelp = !toggleHelp;
break;
}
case ConsoleKey.R:
{
board.ResetQueens();
break;
}
}
if (!exit)
{
RenderGame(toggleHelp);
}
}
private void RenderBlock(int r, int c)
{
if (currentBoard!.Grid[r / 3, c].HasQueen)
{
return;
}
var color = GetColorAt(r / 3, c);
var foregroundColor = Color.White;
// If the color is bright use black as forground color
if (color.R + color.G + color.B > 500)
{
foregroundColor = Color.Black;
}
Console.SetCursorPosition(r, c);
AnsiConsole.Write(new Markup(" • ", new Style(foreground: foregroundColor, background: color)));
}
private void RenderQueen(int r, int c, Decoration? decoration = null)
{
var color = GetColorAt(r / 3, c);
var foregroundColor = Color.White;
// If the color is bright use black as forground color
if (color.R + color.G + color.B > 500)
{
foregroundColor = Color.Black;
}
Console.SetCursorPosition(r, c);
AnsiConsole.Write(new Markup(" ♛ ", new Style(foreground: foregroundColor, background: color, decoration)));
}
private void MoveCursor(int r, int c)
{
RenderQueen(r, c, Decoration.SlowBlink);
Console.SetCursorPosition(0, currentBoard!.Size + 1);
}
private Color GetColorAt(int r, int c)
{
currentBoard!.colorMap.TryGetValue(currentBoard.Grid[r, c].Color, out var color);
return color;
}
private void RenderWinScreen()
{
stopwatch.Stop();
var time = stopwatch.Elapsed.ToString("mm\\:ss\\.ff");
AnsiConsole.Clear();
AnsiConsole.MarkupLine("Congratulations! You won the game! :birthday_cake: ");
AnsiConsole.MarkupLine($"Time: {time}");
var back = AnsiConsole.Prompt(
new SelectionPrompt<string>()
.Title("Press Enter to go back")
.AddChoices(new[] { "Back" }));
RenderMainMenu();
}
private void ExitGame()
{
AnsiConsole.MarkupLine("Exiting game... :waving_hand: ");
Environment.Exit(0);
}
}