-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHangmanMaxima.wxm
77 lines (65 loc) · 2.36 KB
/
HangmanMaxima.wxm
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
/* [wxMaxima batch file version 1] [ DO NOT EDIT BY HAND! ]*/
/* [ Created with wxMaxima version 22.04.0 ] */
/* [wxMaxima: input start ] */
set_random_state(make_random_state(true))$
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
random_word():=block(
words_list:sublist(apropos(""),lambda([x],unique(map(alphacharp,charlist(string(x))))=[true])),
len:length(words_list),
words_list[random(len)])$
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hangman():=block(
/* Choose a random word from the list */
word:string(random_word()),
/* Create a string of underscores */
hidden_word: makelist("_", slength(word)),
/* Set the number of remaining guesses */
guesses_left: 6,
/* Loop until the player wins or loses */
while (guesses_left > 0 and member("_",hidden_word)) do (
/* Display the hidden word and guesses left */
print("Hidden word: " , hidden_word),
print("Guesses left", guesses_left),
/* Ask the player to guess a letter */
guess: read("Guess a letter: "),
/* Check if the guessed letter is in the word */
if member(guess,charlist(word)) then (
/* Replace the corresponding underscore with the guessed letter */
for i:1 thru slength(word) do (
if (charlist(word)[i] = guess) then (
hidden_word[i]: guess
)
)
) else (
/* Subtract one from the number of remaining guesses */
guesses_left: guesses_left - 1
)
),
/* Display the result of the game */
if member("_",hidden_word) then (
print("You lost! The word was ", word),
apply(describe,[word])
) else (
print("You won! The word was ", word),
apply(describe,[word])
))$
/* [wxMaxima: input end ] */
/* [wxMaxima: question start ] */
<math><st>Guess a letter: </st><st> </st></math>
/* [wxMaxima: question end ] */
/* [wxMaxima: answer start ] */
"z";
/* [wxMaxima: answer end ] */
/* [wxMaxima: input start ] */
hangman();
/* [wxMaxima: input end ] */
/* [wxMaxima: question start ] */
<math><st>Guess a letter: </st><st> </st></math>
/* [wxMaxima: question end ] */
/* [wxMaxima: answer start ] */
"b";
/* [wxMaxima: answer end ] */
/* Old versions of Maxima abort on loading files that end in a comment. */
"Created with wxMaxima 22.04.0"$