-
Notifications
You must be signed in to change notification settings - Fork 0
/
Start-Frankenstein.ps1
408 lines (356 loc) · 10.7 KB
/
Start-Frankenstein.ps1
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<#
.SYNOPSIS
Starts a game of Frankenstein's Monster (based on Hangman).
.DESCRIPTION
Players discover puzzle words or phrases by suggesting letters.
Each incorrect guess adds an element to the monster diagram.
The game ends when a solution is guessed, or a diagram is completed.
Various word and phrase dictionaries can be found online.
A dictionary can be a simple list of words or phrases, one per line.
For example: frankenstein
tic-tac-toe
powershell programming
An optional category can be specified by adding a comma separator.
For example: classic games, frankenstein
classic games, tic-tac-toe
computers, powershell programming
Please consider giving to cancer research.
.PARAMETER path
Specifies the path to a dictionary file of puzzle words and phrases.
.INPUTS
None.
.OUTPUTS
A whole lot of fun.
.EXAMPLE
.\Start-Frankenstein.ps1
Starts the program with the default options.
.EXAMPLE
.\Start-Frankenstein.ps1 -path ".\dictionary.txt"
Starts the program with a custom dictionary of puzzle words and phrases.
.NOTES
MIT License
Copyright (c) 2023 TigerPointe Software, LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
If you enjoy this software, please do something kind for free.
History:
01.00 2023-Apr-03 Scott S. Initial release.
01.01 2023-Apr-04 Scott S. Fix whitespace.
.LINK
https://en.wikipedia.org/wiki/Hangman_(game)
.LINK
https://en.wikipedia.org/wiki/ASCII_art
.LINK
https://braintumor.org/
.LINK
https://www.cancer.org/
#>
param
(
# Defines the default path to a dictionary file of puzzle words and phrases
# (the default data file must be placed in the same folder as this script)
[string]$path = ".\frankenstein.txt"
)
# Gets a new mask value that includes the current guess
function Get-NewMask
{
param
(
[string]$word # the original puzzle word value
, [string]$mask # the mask with placeholders
, [string]$guess # the letter guess
)
# Replace matching characters in the mask with the guess value
# (starts with the puzzle word and copies back the mask characters)
$chars = $word.ToCharArray();
for ($i = 0; $i -lt $chars.Length; $i++)
{
if ($chars[$i] -ne $guess)
{
$chars[$i] = $mask.Substring($i, 1);
}
}
return ($chars -join ""); # returns the updated mask value
}
# Read the entire dictionary file
Write-Host -Object "Loading ...";
Start-Sleep -Milliseconds 500;
$words = (Get-Content -Path $path -ErrorAction Stop);
# Define the list of special non-alphabetic characters
$special = " !`"#$%&'()*+,-./0123456789:;<=>?@[\]^_``{|}~".ToCharArray();
# Define the ASCII art (originally created by Scott S.)
$ascii = @"
( )===========( )
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
( )===========( )
( )===========( )
| | ^^^^^ | |
| | {|_ _|} | |
| | O| O |O | |
| | | - | | |
| | =\___/= | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
( )===========( )
( )===========( )
| | ^^^^^ | |
| | {|_ _|} | |
| | O| O |O | |
| | | - | | |
| | =\___/= | |
| | _.| |._ | |
| | [ \./ ] | |
| | | . | | |
| | | . | | |
| | |=O=| | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
( )===========( )
( )===========( )
| | ^^^^^ | |
| | {|_ _|} | |
| | O| O |O | |
| | | - | | |
| | =\___/= | |
| | _.| |._ | |
| | [ \./ ] | |
| | | | . | | |
| | | | . | | |
| | |_|=O=| | |
| | (_' | |
| | | |
| | | |
| | | |
| | | |
| | | |
( )===========( )
( )===========( )
| | ^^^^^ | |
| | {|_ _|} | |
| | O| O |O | |
| | | - | | |
| | =\___/= | |
| | _.| |._ | |
| | [ \./ ] | |
| | | | . | | | |
| | | | . | | | |
| | |_|=O=|_| | |
| | (_' '_) | |
| | | |
| | | |
| | | |
| | | |
| | | |
( )===========( )
( )===========( )
| | ^^^^^ | |
| | {|_ _|} | |
| | O| O |O | |
| | | - | | |
| | =\___/= | |
| | _.| |._ | |
| | [ \./ ] | |
| | | | . | | | |
| | | | . | | | |
| | |_|=O=|_| | |
| | (_' | '_) | |
| | | | | |
| | | | | |
| | | | | |
| | .|_| | |
| | (___' | |
( )===========( )
( )===========( )
| | ^^^^^ | |
| | {|_ .|} | |
| | O| O |O | |
| | | - | | |
| | =\___/= | |
| | _.| |._ | |
| | [ \./ ] | |
| | | | . | | | |
| | | | . | | | |
| | |_|=O=|_| | |
| | (_' | '_) | |
| | | | | | |
| | | | | | |
| | | | | | |
| | .|_|_|. | |
| | (___'___) | |
( )===========( )
( )===========( )
| | ^^^^^ | |
| | {|o O|} | |
| | O| O |O | |
| | |~~~| GRRRR!
| | =\___/= | |
| | _.| |._ | |
| | [ \./ ] | |
| | | | . | | | |
| | | | . | | | |
| | |_|=O=|_| | |
| | (_' | '_) | |
| | | | | | |
| | | | | | |
| | | | | | |
| | .|_|_|. | |
| | (___'___) | |
( )===========( )
( )===========( )
| | ^^^^^ | |
| | {|_ _|} | |
| | O| O |O | |
| | | - | ZZzzzz
| | =\___/= | |
| | _.| |._ | |
| | [ \./ ] | |
| | | | . | | | |
| | | | . | | | |
| | |_|=O=|_| | |
| | (_' | '_) | |
| | | | | | |
| | | | | | |
| | | | | | |
| | .|_|_|. | |
| | (___'___) | |
( )===========( )
"@;
# Use a try-catch for exceptions because the host is cleared on each guess
# (otherwise, the exception messages get cleared with the console)
try
{
# Define the ASCII art panel variables
$height = 18; # number of text lines per panel
$width = 22; # includes the horizontal padding
$ascii = $ascii.Replace("`r", ""); # removes the carriage returns
$lines = $ascii.Split("`n"); # splits on the line feeds
# Loop while more games are selected
$more = "Y";
while ($more -eq "Y")
{
# Initialize the loop control variables
$solved = $false; # not solved
$count = 0; # no guesses
$maximum = 8; # wrong guesses, number of ASCII art panels minus one
# Initialize the puzzle variables (includes an optional category)
$remain = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; # holds the remaining letters
$category = "";
$word = (Get-Random -InputObject $words).Trim().ToUpper();
$idx = $word.LastIndexOf(","); # finds optional category separator
if ($idx -ge 0)
{
$category = "The category is $($word.Substring(0, $idx).Trim())";
$idx++;
$word = $word.Substring($idx, ($word.Length - $idx)).Trim();
}
# Create a mask and replace the special characters to support phrases
$mask = ("." * $word.Length); # repeats a placeholder for each character
foreach ($chr in $special)
{
$mask = (Get-NewMask -word $word -mask $mask -guess $chr);
}
# Loop while not solved and remaining guesses are available
while ((-not $solved) -and ($count -lt $maximum))
{
# Check for a solved puzzle and skip to the winning count value
if ($mask -eq $word)
{
$solved = $true;
$count = $maximum;
}
# Display the ASCII art panel for the current count value
Clear-Host;
Write-Host;
for ($i = 0; $i -lt $height; $i++)
{
$line = $lines[($count * $height) + $i];
$line = $line.PadRight($width);
if ($i -eq 3) { $line = "$line Don't Wake the FRANKENSTEIN MONSTER"; }
elseif ($i -eq 4) { $line = "$line $category"; }
elseif ($i -eq 7) { $line = "$line $mask"; }
elseif ($i -eq 11) { $line = "$line Remaining Letters:"; }
elseif ($i -eq 12) { $line = "$line $remain"; }
Write-Host $line;
}
# If solved, exit the loop (winning screen has been drawn)
if ($solved) { continue; }
# Otherwise, check for remaining guesses
if ($count -lt ($maximum - 1))
{
# Read the next guess (limit to the remaining letters only)
$guess = "";
while (($guess.Length -ne 1) -or (-not $remain.Contains($guess)))
{
$guess = Read-Host -Prompt `
"Please choose one of the remaining letters";
$guess = $guess.ToUpper();
}
$remain = $remain.Replace($guess, " ");
# Check for a correct guess and update the mask
if (($guess -ne " ") -and ($word.Contains($guess)))
{
$mask = (Get-NewMask -word $word -mask $mask -guess $guess);
continue;
}
}
# Increment the counter for an incorrect guess
$count++;
} # end solved-count-maximum loop
# Display the solution message
$message = "Oh no!";
if ($solved) { $message = "Congratulations!"; }
Write-Host "$message The solution was $word.";
# Prompt for another game
$more = "";
while (($more.Length -ne 1) -or (-not ("YN").Contains($more)))
{
$more = Read-Host -Prompt "Do you want to play another game? (Y/N)";
$more = $more.ToUpper();
}
} # end more loop
}
catch
{
Write-Error $_;
}