-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathProgram.cs
341 lines (319 loc) · 8.29 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Text;
(int Left, int Top)[] directions =
[
( 1, 0), // right
(-1, 0), // left
( 0, 1), // down
( 0, -1), // up
( 1, 1), // down-right
(-1, -1), // up-left
( 1, -1), // up-right
(-1, 1), // down-left
];
Console.OutputEncoding = Encoding.UTF8;
PlayAgain:
(int Left, int Top) cursor = default;
bool?[,] board;
HashSet<(int Left, int Top)> validMoves = [];
bool turn = true;
bool? playerColor = null;
Console.Clear();
InitializeBoard();
RenderBoard();
Console.Write($"""
When you place a piece on the board all
your oponent pieces you jump will be converted
into your pieces. Have the most pieces at the
end of the game and you win. You and your
opponent will be randomly assigned a color of
black or white. The white player always moves first.
Controls:
- enter: start match
- escape: close game
""");
MainMenuInput:
Console.CursorVisible = false;
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.Escape: goto Close;
case ConsoleKey.Enter: break;
default: goto MainMenuInput;
}
playerColor = Random.Shared.Next(2) is 0;
Console.Clear();
RenderBoard();
Console.Write($"""
You are {(playerColor.Value ? "● white" : "○ black")} and your opponent
is {(playerColor.Value ? "○ black" : "● white")}.
Controls:
- enter: continue
- escape: close game
""");
ColorConfirmInput:
Console.CursorVisible = false;
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.Escape: goto Close;
case ConsoleKey.Enter: break;
default: goto ColorConfirmInput;
}
while (true)
{
UpdateValidMoves(turn);
if (validMoves.Count is 0)
{
turn = !turn;
UpdateValidMoves(turn);
if (validMoves.Count is 0)
{
Console.Clear();
RenderBoard();
int playerScore = GetScore(playerColor.Value);
int opponentScore = GetScore(!playerColor.Value);
string endGameState =
playerScore > opponentScore ? "Win" :
playerScore < opponentScore ? "Lose" :
"Tie";
Console.Write($"""
No more valid moves. Game Over.
You {endGameState}!
Controls:
- enter: continue
- escape: close game
""");
GameOverInput:
Console.CursorVisible = false;
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.Escape: goto Close;
case ConsoleKey.Enter: goto PlayAgain;
default: goto GameOverInput;
}
}
Console.Clear();
RenderBoard();
Console.Write($"""
{(turn == playerColor ? "Your Opponent" : "You")} has no valid moves.
Turn skipped.
Controls:
- enter: continue
- escape: close game
""");
ConfirmTurnSkip:
Console.CursorVisible = false;
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.Escape: goto Close;
case ConsoleKey.Enter: break;
default: goto ConfirmTurnSkip;
}
goto SkipTurn;
}
SkipTurn:
if (turn == playerColor)
{
Console.Clear();
RenderBoard();
Console.Write($"""
Controls:
- arrow keys: move cursor
- enter: place piece at '+' valid move
- escape: close game
""");
Console.SetCursorPosition(2 * cursor.Left, cursor.Top);
Console.CursorVisible = false;
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.LeftArrow: cursor.Left = cursor.Left <= 0 ? board.GetLength(0) - 1 : cursor.Left - 1; break;
case ConsoleKey.RightArrow: cursor.Left = cursor.Left >= board.GetLength(0) - 1 ? 0 : cursor.Left + 1; break;
case ConsoleKey.UpArrow: cursor.Top = cursor.Top <= 0 ? board.GetLength(1) - 1 : cursor.Top - 1; break;
case ConsoleKey.DownArrow: cursor.Top = cursor.Top >= board.GetLength(1) - 1 ? 0 : cursor.Top + 1; break;
case ConsoleKey.Escape: goto Close;
case ConsoleKey.Enter:
if (validMoves.Contains(cursor))
{
PlaceMove(cursor, playerColor.Value);
Console.Clear();
RenderBoard();
Console.Write($"""
You played a piece.
Controls:
- enter: continue
- escape: close game
""");
MoveConfirmInput:
Console.CursorVisible = false;
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.Escape: goto Close;
case ConsoleKey.Enter: break;
default: goto MoveConfirmInput;
}
turn = !turn;
}
break;
}
}
else
{
ComputerMove();
Console.Clear();
RenderBoard();
Console.Write($"""
Your opponent played a piece.
Controls:
- enter: continue
- escape: close game
""");
OpponentMoveConfirmInput:
Console.CursorVisible = false;
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.Escape: goto Close;
case ConsoleKey.Enter: turn = !turn; break;
default: goto OpponentMoveConfirmInput;
}
}
}
Close:
Console.Clear();
Console.WriteLine("Reversi was closed.");
Console.CursorVisible = true;
void InitializeBoard()
{
bool? _ = null, w = true, b = false;
board = new bool?[,]
{
{ _, _, _, _, _, _, _, _ },
{ _, _, _, _, _, _, _, _ },
{ _, _, _, _, _, _, _, _ },
{ _, _, _, w, b, _, _, _ },
{ _, _, _, b, w, _, _, _ },
{ _, _, _, _, _, _, _, _ },
{ _, _, _, _, _, _, _, _ },
{ _, _, _, _, _, _, _, _ },
};
}
void RenderBoard()
{
StringBuilder render = new();
render.AppendLine(" Reversi");
render.AppendLine();
render.AppendLine(" ┌─────────────────┐");
for (int i = 0; i < board.GetLength(1); i++)
{
render.Append(' ');
render.Append('│');
for (int j = 0; j < board.GetLength(0); j++)
{
render.Append(
cursor == (j, i) ? '[' :
cursor == (j - 1, i) ? ']' :
' ');
render.Append(
validMoves.Contains((j, i)) ? '+' :
board[j, i] is null ? ' ' :
board[j, i]!.Value ? '●' : '○');
}
render.Append(cursor == (board.GetLength(0) - 1, i) ? ']' : ' ');
render.Append('│');
render.AppendLine();
}
render.AppendLine(" └─────────────────┘");
if (playerColor is not null)
{
render.AppendLine($" ●: {GetScore(true)} ○: {GetScore(false)}");
}
Console.SetCursorPosition(0, 0);
Console.Write(render);
}
int GetScore(bool color)
{
int score = 0;
for (int i = 0; i < board.GetLength(1); i++)
{
for (int j = 0; j < board.GetLength(0); j++)
{
if (board[j, i] == color)
{
score++;
}
}
}
return score;
}
void UpdateValidMoves(bool color)
{
validMoves.Clear();
for (int i = 0; i < board.GetLength(1); i++)
{
for (int j = 0; j < board.GetLength(0); j++)
{
if (board[j, i] is null)
{
foreach (var direction in directions)
{
bool jump = false;
(int Left, int Top) location = (j + direction.Left, i + direction.Top);
while (
location.Left >= 0 && location.Left < board.GetLength(0) &&
location.Top >= 0 && location.Top < board.GetLength(1) &&
board[location.Left, location.Top] == !color)
{
jump = true;
location = (location.Left + direction.Left, location.Top + direction.Top);
}
if (location.Left >= 0 && location.Left < board.GetLength(0) &&
location.Top >= 0 && location.Top < board.GetLength(1) &&
board[location.Left, location.Top] == color &&
jump)
{
validMoves.Add((j, i));
}
}
}
}
}
}
void PlaceMove((int Left, int Top) move, bool color)
{
board[move.Left, move.Top] = color;
foreach (var direction in directions)
{
bool jump = false;
(int Left, int Top) location = (move.Left + direction.Left, move.Top + direction.Top);
while (
location.Left >= 0 && location.Left < board.GetLength(0) &&
location.Top >= 0 && location.Top < board.GetLength(1) &&
board[location.Left, location.Top] == !color)
{
jump = true;
location = (location.Left + direction.Left, location.Top + direction.Top);
}
if (location.Left >= 0 && location.Left < board.GetLength(0) &&
location.Top >= 0 && location.Top < board.GetLength(1) &&
board[location.Left, location.Top] == color &&
jump)
{
location = (move.Left + direction.Left, move.Top + direction.Top);
while (
location.Left >= 0 && location.Left < board.GetLength(0) &&
location.Top >= 0 && location.Top < board.GetLength(1) &&
board[location.Left, location.Top] == !color)
{
board[location.Left, location.Top] = color;
location = (location.Left + direction.Left, location.Top + direction.Top);
}
}
}
validMoves.Clear();
}
void ComputerMove()
{
(int Left, int Top)[] validMovesArray = [.. validMoves];
(int Left, int Top) move = validMovesArray[Random.Shared.Next(validMovesArray.Length)];
cursor = move;
PlaceMove(move, !playerColor.Value);
}