-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathProgram.cs
229 lines (210 loc) · 6.44 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
char[,] board = new char[20, 20];
List<(int Left, int Top)> showWordSelections = [];
List<(int Left, int Top)> selections = [];
string[] wordArray = default!;
string currentWord = default!;
(int Left, int Top) cursor = (0, 0);
InitializeWords();
PlayAgain:
InitializeBoard();
Console.Clear();
while (true)
{
RenderBoard();
Console.Write($"""
Highlight the word "{currentWord}" above.
Controls:
- arrow keys: move cursor
- enter: highlight characters
- backspace: clear highlighted characters
- home: new word search
- end: give up and show word
- escape: close game
""");
Console.SetCursorPosition(2 * cursor.Left, cursor.Top);
Console.CursorVisible = true;
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.Backspace: selections.Clear(); break;
case ConsoleKey.Home: goto PlayAgain;
case ConsoleKey.End:
selections.Clear();
selections.AddRange(showWordSelections);
Console.Clear();
RenderBoard();
Console.Write($"""
Here is where "{currentWord}" was hiding.
Controls:
- enter/home: play again
- escape: close game
""");
while (true)
{
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.Enter or ConsoleKey.Home: goto PlayAgain;
case ConsoleKey.Escape: goto Close;
}
}
case ConsoleKey.Escape: goto Close;
case ConsoleKey.Enter:
if (!selections.Remove(cursor))
{
selections.Add(cursor);
}
selections.Sort();
if (UserFoundTheWord())
{
Console.Clear();
RenderBoard();
Console.Write($"""
You found "{currentWord}"! You win!
Controls:
- enter/home: play again
- escape: close game
""");
while (true)
{
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.Enter or ConsoleKey.Home: goto PlayAgain;
case ConsoleKey.Escape: goto Close;
}
}
}
break;
}
}
Close:
Console.Clear();
Console.WriteLine("Word Search was closed.");
void InitializeWords()
{
const string wordsResource = "Word_Search.Words.txt";
Assembly assembly = Assembly.GetExecutingAssembly();
using Stream? stream = assembly.GetManifestResourceStream(wordsResource);
using StreamReader streamReader = new(stream!);
List<string> words = [];
while (!streamReader.EndOfStream)
{
string? word = streamReader.ReadLine();
if (!string.IsNullOrWhiteSpace(word) &&
word.Length < board.GetLength(0) &&
word.Length < board.GetLength(1))
{
words.Add(word.ToUpper());
}
}
wordArray = [.. words];
}
void InitializeBoard()
{
selections.Clear();
for (int i = 0; i < board.GetLength(1); i++)
{
for (int j = 0; j < board.GetLength(0); j++)
{
board[j, i] = (char)('A' + Random.Shared.Next(26));
}
}
currentWord = wordArray[Random.Shared.Next(wordArray.Length)];
// choose a random orientation for the word (down, right, left, up, down-right, down-left, up-right, or up-left)
bool r((int Left, int Top) location) => location.Left + currentWord.Length < board.GetLength(0);
bool d((int Left, int Top) location) => location.Top + currentWord.Length < board.GetLength(1);
bool l((int Left, int Top) location) => location.Left - currentWord.Length >= 0;
bool u((int Left, int Top) location) => location.Top - currentWord.Length >= 0;
bool dr((int Left, int Top) location) => d(location) && r(location);
bool dl((int Left, int Top) location) => d(location) && l(location);
bool ur((int Left, int Top) location) => u(location) && r(location);
bool ul((int Left, int Top) location) => u(location) && l(location);
(Func<(int Left, int Top), bool> Validator, (int Left, int Top) Adjustment) orientation = Random.Shared.Next(8) switch
{
0 => (d, ( 0, 1)),
1 => (r, ( 1, 0)),
2 => (u, ( 0, -1)),
3 => (l, (-1, 0)),
4 => (dr, ( 1, 1)),
5 => (dl, (-1, 1)),
6 => (ur, ( 1, -1)),
7 => (ul, (-1, -1)),
_ => throw new NotImplementedException(),
};
// choose a random starting location that is valid for the orientation
List<(int Left, int Top)> possibleLocations = [];
for (int i = 0; i < board.GetLength(1); i++)
{
for (int j = 0; j < board.GetLength(0); j++)
{
if (orientation.Validator((j, i)))
{
possibleLocations.Add((j, i));
}
}
}
(int Left, int Top) randomLocation = possibleLocations[Random.Shared.Next(possibleLocations.Count)];
showWordSelections.Clear();
for (int i = 0; i < currentWord.Length; i++)
{
showWordSelections.Add(randomLocation);
board[randomLocation.Left, randomLocation.Top] = currentWord[i];
randomLocation = (randomLocation.Left + orientation.Adjustment.Left, randomLocation.Top + orientation.Adjustment.Top);
}
}
void RenderBoard()
{
Console.CursorVisible = false;
Console.SetCursorPosition(0, 0);
for (int i = 0; i < board.GetLength(1); i++)
{
for (int j = 0; j < board.GetLength(0); j++)
{
if (selections.Contains((j, i)))
{
(Console.ForegroundColor, Console.BackgroundColor) = (Console.BackgroundColor, Console.ForegroundColor);
}
Console.Write(board[j, i]);
if (selections.Contains((j, i)))
{
(Console.ForegroundColor, Console.BackgroundColor) = (Console.BackgroundColor, Console.ForegroundColor);
}
if (j < board.GetLength(1) - 1)
{
Console.Write(' ');
}
}
Console.WriteLine();
}
}
bool UserFoundTheWord()
{
// make sure all the selections are in a straight line
if (selections.Count > 1)
{
(int Left, int Top) adjustment = (selections[1].Left - selections[0].Left, selections[1].Top - selections[0].Top);
if (adjustment.Left > 1 || adjustment.Left < -1 || adjustment.Top > 1 || adjustment.Top < -1)
{
return false;
}
for (int i = 2; i < selections.Count; i++)
{
if ((selections[i].Left - selections[i - 1].Left, selections[i].Top - selections[i - 1].Top) != adjustment)
{
return false;
}
}
}
char[] chars = selections.Select(location => board[location.Left, location.Top]).ToArray();
string charsString = new(chars);
Array.Reverse(chars);
string charsStringReverse = new(chars);
return charsString == currentWord || charsStringReverse == currentWord;
}