forked from rowingsjr/hangman_flask_app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_logic.py
150 lines (133 loc) · 4.49 KB
/
game_logic.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
import random
import json
class HangmanGame:
def __init__(self, word_list):
self.word_list = word_list
self.chosen_word = random.choice(self.word_list)
self.lives = 6
self.display = ["_" for _ in self.chosen_word]
self.game_over = False
self.message = ""
self.stages = ['''
+---+
| |
O |
/|\ |
/ \ |
|
=========
Sorry, GAME OVER...
''', '''
+---+
| |
O |
/|\ |
/ |
|
=========
Uh oh, there the right leg, you're down to one more try!
''', '''
+---+
| |
O |
/|\ |
|
|
=========
Here comes the left arm!
''', '''
+---+
| |
O |
/| |
|
|
=========
I see a right arm!
''', '''
+---+
| |
O |
| |
|
|
=========
Here comes the Mid-Section!
''', '''
+---+
| |
O |
|
|
|
=========
Oh man! There goes the Head!
''', '''
+---+
| |
|
|
|
|
=========
Uh Oh, there goes the platform!
''']
# Inside the HangmanGame class
def guess_letter(self, guess):
if guess in self.chosen_word:
for index, letter in enumerate(self.chosen_word):
if letter == guess:
self.display[index] = guess
# Check if the game has been won
if "_" not in self.display:
self.game_over = True
self.message = "Congratulations, you've won!"
return ''.join(self.display), self.lives, self.message, self.game_over
else:
self.lives -= 1
if self.lives == 0:
self.game_over = True
self.message = "Game over. The word was '{}'.".format(self.chosen_word)
return ''.join(self.display), self.lives, self.message, self.game_over
def to_json(self):
# Serialize game state into a JSON-serializable dictionary
return {
'chosen_word': self.chosen_word,
'lives': self.lives,
'display': self.display,
'game_over': self.game_over,
'message': self.message,
'stages': self.stages
}
@classmethod
def from_json(cls, data):
# Create a new instance of the game without choosing a new word
game = cls.__new__(cls)
# Manually set the attributes from the provided JSON data
game.chosen_word = data['chosen_word']
game.lives = data['lives']
game.display = data['display']
game.game_over = data['game_over']
game.message = data['message']
game.stages = data['stages']
# No need to pass a word list since we're restoring a game, not starting a new one
return game
def load_word_list(file_path):
with open(file_path, 'r') as file:
word_list = [line.strip() for line in file]
return word_list
def new_game(word_list):
chosen_word = random.choice(word_list)
display = ['_' for _ in chosen_word]
game_state = {
'chosen_word': chosen_word,
'display': display,
'lives': 6,
'guesses': [],
'game_over': False,
'message': 'Guess a letter!'
}
return game_state
# Example usage in your Flask app
# word_list = load_word_list('path/to/your/wordlist.txt')
# game = HangmanGame(word_list)