-
Notifications
You must be signed in to change notification settings - Fork 14
/
hangchicken.py
150 lines (132 loc) · 2.59 KB
/
hangchicken.py
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
from typing import List, Tuple
from string import ascii_lowercase
import types
import random
import dataclasses
import streamlit as st
from gamestate import persistent_game_state
PROTOTYPE = """
┏━━┑
┃ O>
┃>╦╧╦<
┃ ╠═╣
┃ ╨ ╨
┻━━━━
"""
STEPS = [
"""
┏━━┑
┃
┃
┃
┃
┻━━━━
""",
"""
┏━━┑
┃ O
┃
┃
┃
┻━━━━
""",
"""
┏━━┑
┃ O>
┃
┃
┃
┻━━━━
""",
"""
┏━━┑
┃ O>
┃ ╔╧╗
┃ ╚═╝
┃
┻━━━━
""",
"""
┏━━┑
┃ O>
┃>╦╧╗
┃ ╚═╝
┃
┻━━━━
""",
"""
┏━━┑
┃ O>
┃>╦╧╦<
┃ ╚═╝
┃
┻━━━━
""",
"""
┏━━┑
┃ O>
┃>╦╧╦<
┃ ╠═╝
┃ ╨
┻━━━━
""",
"""
┏━━┑
┃ O>
┃>╦╧╦<
┃ ╠═╣
┃ ╨ ╨
┻━━━━
"""
]
MIN_LENGTH = 3
MAX_LENGTH = 8
@st.cache
def get_words() -> List[str]:
with open('words1000.txt') as f:
words = [line.strip() for line in f]
words = [w for w in words if MIN_LENGTH <= len(w) <= MAX_LENGTH]
words = [w for w in words if all('a' <= c <= 'z' for c in w)]
return words
@dataclasses.dataclass
class GameState:
game_number: int
word: str
guessed: Tuple[str, ...] = ()
step: int = 0
game_over: bool = False
state = persistent_game_state(initial_state=GameState(0, random.choice(get_words())))
if st.button("new game"):
state.guessed = ()
state.step = 0
state.game_number += 1
state.word = random.choice(get_words())
state.game_over = False
if not state.game_over:
guess = st.text_input("guess a letter", max_chars=1, key=state.game_number)
if not guess:
st.write("please guess")
elif guess < 'a' or guess > 'z':
st.write("please guess a lowercase letter!")
elif guess in state.guessed:
st.write(f"you already guessed **{guess}**")
elif guess not in state.word:
st.write(f"the word has no **{guess}**")
state.step += 1
state.guessed += (guess,)
else:
st.write("good guess")
state.guessed += (guess,)
if state.step == len(STEPS) - 1:
st.markdown(f"you lose, the word was **{state.word}**")
state.game_over = True
elif all(c in state.guessed for c in state.word):
st.markdown(f"**YOU WIN**")
state.game_over = True
# Show the chicken
st.text(STEPS[state.step])
# Show the word
chars = [c if c in state.guessed else "_" for c in state.word]
st.text(" ".join(chars))
# Show the guessed letters
st.text(f'guessed: {" ".join(state.guessed)}')