forked from dotnet/dotnet-console-games
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
232 lines (215 loc) · 5.79 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
using System;
using System.Globalization;
using static Towel.Statics;
Exception? exception = null;
const string menu = @"
Sliding Puzzle
Choose Puzzle Size:
[1] 3 x 3
[2] 4 x 4
[3] 5 x 5
[escape] close";
string info = @"
Solve the puzzle by getting the tiles in
least-to-greatest order with the space in
the lower right. Use the arrow keys or WASD
to slide the tiles into the space. Press
[escape] to return to the menu. ";
string youWon = @"
*************** You Won! ***************
Press [enter] to return to the menu...
";
try
{
while (true)
{
Menu:
Console.Clear();
Console.Write(menu);
int[,]? board = null;
var (row, column) = (0, 0);
while (board is null)
{
Console.CursorVisible = false;
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.D1 or ConsoleKey.NumPad1: board = new int[3, 3]; break;
case ConsoleKey.D2 or ConsoleKey.NumPad2: board = new int[4, 4]; break;
case ConsoleKey.D3 or ConsoleKey.NumPad3: board = new int[5, 5]; break;
case ConsoleKey.Escape: return;
}
}
Initialize(board);
while (IsSolved(board))
{
Initialize(board);
}
Console.Clear();
while (!IsSolved(board))
{
Render(board);
Console.Write(info);
var space = FindFlatLength(board);
Console.CursorVisible = false;
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.A or ConsoleKey.LeftArrow:
if (space.Column < board.GetLength(1) - 1)
{
(board[space.Row, space.Column], board[space.Row, space.Column + 1]) = (board[space.Row, space.Column + 1], board[space.Row, space.Column]);
}
break;
case ConsoleKey.D or ConsoleKey.RightArrow:
if (space.Column > 0)
{
(board[space.Row, space.Column], board[space.Row, space.Column - 1]) = (board[space.Row, space.Column - 1], board[space.Row, space.Column]);
}
break;
case ConsoleKey.W or ConsoleKey.UpArrow:
if (space.Row < board.GetLength(0) - 1)
{
(board[space.Row, space.Column], board[space.Row + 1, space.Column]) = (board[space.Row + 1, space.Column], board[space.Row, space.Column]);
}
break;
case ConsoleKey.S or ConsoleKey.DownArrow:
if (space.Row > 0)
{
(board[space.Row, space.Column], board[space.Row - 1, space.Column]) = (board[space.Row - 1, space.Column], board[space.Row, space.Column]);
}
break;
case ConsoleKey.Escape:
goto Menu;
}
}
Render(board);
Console.Write(youWon);
GetEnterOrEscape:
Console.CursorVisible = false;
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.Enter or ConsoleKey.Escape: break;
default: goto GetEnterOrEscape;
}
}
}
catch (Exception e)
{
exception = e;
throw;
}
finally
{
Console.CursorVisible = true;
Console.ResetColor();
Console.Clear();
Console.WriteLine(exception?.ToString() ?? "Sliding Puzzle was closed.");
}
static void Render(int[,] board)
{
int space = board.FlatLength();
Console.SetCursorPosition(0, 0);
Console.WriteLine();
Console.WriteLine(" Sliding Puzzle");
Console.WriteLine();
int tileWidth = board.FlatLength().ToString().Length;
Console.WriteLine($" ╔{new string('═', board.GetLength(1) * (tileWidth + 1) + 1)}╗");
for (int r = 0; r < board.GetLength(0); r++)
{
Console.Write(" ║ ");
for (int c = 0; c < board.GetLength(1); c++)
{
if (board[r, c] == space)
{
Console.Write(new string(' ', tileWidth));
}
else
{
string value = board[r, c].ToString(CultureInfo.InvariantCulture);
if (value.Length < 2 && board.FlatLength() > 9)
{
Console.Write('0');
}
Console.Write(value);
}
Console.Write(' ');
}
Console.WriteLine('║');
}
Console.WriteLine($" ╚{new string('═', board.GetLength(1) * (tileWidth + 1) + 1)}╝");
}
static void Initialize(int[,] board)
{
for (int i = 0, k = 1; i < board.FlatLength(); i++)
{
board.FlatSet(i, k++);
}
Shuffle(0, board.FlatLength() - 1, i => board.FlatGet(i), (i, v) => board.FlatSet(i, v));
if (!IsSolvable(board))
{
if (board[0, 0] != board.FlatLength() && board[0, 1] != board.FlatLength())
{
(board[0, 0], board[0, 1]) = (board[0, 1], board[0, 0]);
}
else
{
(board[1, 0], board[1, 1]) = (board[1, 1], board[1, 0]);
}
}
}
static bool IsSolvable(int[,] board) =>
board.GetLength(1) % 2 is 1
? Inversions(board) % 2 is 0
: (Inversions(board) + board.GetLength(0) - (FindFlatLength(board).Row + 1)) % 2 is 0;
static (int Row, int Column) FindFlatLength(int[,] board)
{
for (int r = 0; r < board.GetLength(0); r++)
{
for (int c = 0; c < board.GetLength(1); c++)
{
if (board[r, c] == board.FlatLength())
{
return (r, c);
}
}
}
throw new Exception("bug. could not find (board.FlatLength()) in board");
}
static bool IsSolved(int[,] board)
{
for (int i = 0; i < board.FlatLength(); i++)
{
for (int j = i + 1; j < board.FlatLength(); j++)
{
if (board.FlatGet(i) > board.FlatGet(j))
{
return false;
}
}
}
return true;
}
static int Inversions(int[,] board)
{
int inversions = 0;
for (int i = 0; i < board.FlatLength(); i++)
{
for (int j = i + 1; j < board.FlatLength(); j++)
{
if (!(board.FlatGet(i) == board.FlatLength() || board.FlatGet(j) == board.FlatLength()) &&
board.FlatGet(i) > board.FlatGet(j))
{
inversions++;
}
}
}
return inversions;
}
public static class Extensions
{
public static int FlatLength<T>(this T[,] array2d) =>
array2d.GetLength(0) * array2d.GetLength(1);
public static T FlatGet<T>(this T[,] array2d, int index) =>
array2d[index / array2d.GetLength(0), index % array2d.GetLength(0)];
public static void FlatSet<T>(this T[,] array2d, int index, T value) =>
array2d[index / array2d.GetLength(0), index % array2d.GetLength(0)] = value;
}