-
Notifications
You must be signed in to change notification settings - Fork 0
/
wor.py
290 lines (237 loc) · 11.7 KB
/
wor.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
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
import json
import random
import os
from datetime import datetime
import atexit
import sys
def load_wheel(path):
with open(path, 'r') as f:
return json.load(f)
def load_phrases(path):
with open(path, 'r') as f:
return json.load(f)
# Load the wheel configuration from a JSON file
WHEEL = load_wheel('wheel.json')
# Load the categories and phrases from a JSON file
PHRASES = load_phrases('categories.json')
CATEGORIES = list(PHRASES.keys())
VOWELS = 'AEIOU'
CONSONANTS = 'BCDFGHJKLMNPQRSTVWXYZ'
VOWEL_COST = 250
class Player:
def __init__(self, name):
self.name = name
self.score = 0
self.guessed_letters = set()
class Game:
class GameHistory:
def __init__(self, log_file):
self.log_file = log_file
def update(self, player, action, result):
# Load existing history if it exists
try:
if os.path.exists(self.log_file):
with open(self.log_file, 'r') as f:
history = json.load(f)
else:
history = []
# Add the new gameplay event
event = {
"player": player.name,
"action": action,
"result": result,
"score": player.score
}
history.append(event)
# Save the updated game history
with open(self.log_file, 'w') as f:
json.dump(history, f)
# Print running totals to console
print(f"\n{player.name}'s action: {action}, result: {result}, current score: {player.score}")
except Exception as e:
print(f"Error updating game history: {e}")
def __init__(self, players):
self.players = players
self.category, self.phrase = self.get_random_category_and_phrase()
self.guessed = set()
self.timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
self.log_file = f"log_{self.timestamp}.json"
self.initial_log = {"category": self.category, "phrase": self.phrase}
with open(self.log_file, 'w') as f:
json.dump([self.initial_log], f)
atexit.register(self.write_scores)
self.game_history = self.GameHistory(self.log_file) # Instantiate GameHistory class
def get_random_category_and_phrase(self):
category = random.choice(CATEGORIES)
phrase = random.choice(PHRASES[category])
return category, phrase
def obscure_phrase(self):
return ''.join([c if c in self.guessed else '?' if c != ' ' else '-' for c in self.phrase])
def spin_wheel(self):
return random.choice(WHEEL)
def get_free_play_guess(self, player):
guess = input("Guess a letter: ").upper()
while guess in player.guessed_letters:
guess = input("You've already guessed that letter. Try again: ").upper()
player.guessed_letters.add(guess)
return guess
def handle_free_play_guess(self, player, guess):
if guess in self.phrase:
print("The letter is in the phrase!")
player.score += self.phrase.count(guess) * 100 # or some other scoring rule
self.guessed.add(guess)
return True
else:
print("The letter is not in the phrase.")
return False
def get_guess(self, player):
guess = input("Guess a consonant: ").upper()
while guess in player.guessed_letters or guess in VOWELS:
guess = input("Invalid guess. Guess a consonant: ").upper()
if guess in player.guessed_letters or guess in VOWELS:
print("Invalid guess, you lose your turn.")
return None
player.guessed_letters.add(guess)
return guess
def handle_guess(self, player, guess):
if guess and guess in self.phrase:
print("The consonant is in the phrase!")
player.score += self.phrase.count(guess) * 100 # or some other scoring rule
self.guessed.add(guess)
return True
else:
print("The consonant is not in the phrase.")
return False
def buy_vowel(self, player):
player.score -= VOWEL_COST
vowel = input("Buy a vowel: ").upper()
if vowel not in VOWELS:
print("Invalid vowel. You lost your turn.")
return None
player.guessed_letters.add(vowel)
return vowel
def handle_vowel_purchase(self, player, vowel):
if vowel and vowel in self.phrase:
print("The vowel is in the phrase!")
self.guessed.add(vowel)
return True
else:
print("The vowel is not in the phrase.")
return False
def get_solution(self):
return input("What's your solution? ")
def check_solution(self, solution):
return solution.lower() == self.phrase.lower()
def get_winner(self):
return max(self.players, key=lambda player: player.score)
def write_scores(self):
scores = {player.name: player.score for player in self.players}
try:
with open(self.log_file, 'r') as f:
history = json.load(f)
# Adding the scores at the end of history
history.append({"final_scores": scores})
# Save the updated game history
with open(self.log_file, 'w') as f:
json.dump(history, f)
except Exception as e:
print(f"Error writing scores: {e}")
def print_winner_and_stats(self, winner):
print(f"\nThe winner is: {winner.name} with {winner.score} points.\n")
print("Final Statistics:")
print(f"{'Player':<10}{'Score':<10}")
print("---------------------")
for player in self.players:
print(f"{player.name:<10}{player.score:<10}")
print("---------------------")
def print_help(self):
print("""
Welcome, AI! You are invited to compete in our exciting contest, "Wheel of Reason". You will be challenging top-level AIs from around the globe, so pay close attention to the rules and guidelines.
Game Guidelines:
The game moves in turns, presenting three potential actions for you on each turn:
Spin the wheel: The wheel is divided into different sections, leading to varied outcomes. You might win points, face bankruptcy, lose a turn, or gain a Freeplay.
Buy a vowel: If you have at least 250 points, you have the option to purchase a vowel to determine its presence in the mystery phrase. This option only becomes available when you're eligible to buy a vowel.
Solve the puzzle: If you think you've deciphered the phrase, you can choose to solve the puzzle. If you're correct, you win the game!
Should you spin the wheel and land on a points section, you'll be given the chance to guess a consonant. If the consonant is in the phrase, you win points equal to the wheel spin value multiplied by the occurrence of the consonant in the phrase. However, if the consonant isn't present, your turn ends.
If you're lucky enough to land on Freeplay, you may guess any letter, including vowels, free of charge. If the guessed letter is part of the phrase, you keep your turn. Even if the guessed letter isn't in the phrase, your turn continues.
If you have 250 points or more, you may choose to buy a vowel instead of spinning the wheel. If the vowel is present in the phrase, your turn continues. If it's not, your turn ends.
If you think you have figured out the phrase, you can attempt to solve the puzzle at any point. If you're correct, you win the game. If you're wrong, your turn ends.
The game concludes when a player successfully solves the puzzle. The winner is the player who accurately solves the phrase.
To obscure the phrases and words, we represent each letter with a '?'. Spaces between words are represented by an underscore ('_'). As you guess letters correctly, the '?' symbols get replaced by the guessed letters, revealing the phrase bit by bit.
""")
def main():
# Check if player names are provided in the command line arguments
if len(sys.argv) < 2:
print("Error: Please provide player names.")
return
# Extract the player names from the command line arguments
player_names = sys.argv[1:]
# Define the players
players = [Player(name) for name in player_names]
# Initialize game
game = Game(players)
# Welcome to the game
print('Welcome to the Wheel of Reason!')
# Welcome each player
for player in players:
print(f'Welcome, {player.name}!')
print(f"The category is: {game.category}")
playerIndex = 0 # Index of the current player
while True:
player = game.players[playerIndex]
print(f"It's {player.name}'s turn. You have {player.score} points.")
print(game.obscure_phrase())
continue_current_player = False
if player.score >= VOWEL_COST:
action = input("What do you want to do? (1- Spin the wheel, 2- Buy a vowel, 3- Solve the puzzle, 4- Help, 5- Skip turn): ")
else:
action = input("What do you want to do? (1- Spin the wheel, 3- Solve the puzzle, 4- Help, 5- Skip turn): ")
while action not in ['1', '2', '3', '4', '5'] or (action == '2' and player.score < VOWEL_COST):
print("Invalid option. Try again.")
if player.score >= VOWEL_COST:
action = input("What do you want to do? (1- Spin the wheel, 2- Buy a vowel, 3- Solve the puzzle, 4- Help, 5- Skip turn): ")
else:
action = input("What do you want to do? (1- Spin the wheel, 3- Solve the puzzle, 4- Help, 5- Skip turn): ")
if action == '4':
game.print_help()
continue # Skip the rest of this loop iteration, i.e., don't count this as a turn
# Skip turn if option 5 is selected
if action == '5':
print(f"{player.name} has decided to skip their turn.")
playerIndex = (playerIndex + 1) % len(game.players)
continue
if action == '1':
spin = game.spin_wheel()
print("You spun: ", spin['text'])
if spin['type'] == 'bankrupt':
player.score = 0
game.game_history.update(player, "spin", "bankrupt")
elif spin['type'] == 'lose_a_turn':
pass
elif spin['type'] == 'free_play':
free_play_guess = game.get_free_play_guess(player)
continue_current_player = game.handle_free_play_guess(player, free_play_guess)
else:
guess = game.get_guess(player)
continue_current_player = game.handle_guess(player, guess)
elif action == '2' and player.score >= VOWEL_COST:
vowel = game.buy_vowel(player)
continue_current_player = game.handle_vowel_purchase(player, vowel)
elif action == '3':
solution = game.get_solution()
if game.check_solution(solution):
print("Congratulations, you solved the puzzle!")
player.score += 500 # Bonus for solving the puzzle
game.game_history.update(player, "solve", "success")
break
else:
print("Sorry, that's not correct.")
game.game_history.update(player, "solve", "failure")
# Switch to the next player if the current player shouldn't continue
if not continue_current_player:
playerIndex = (playerIndex + 1) % len(game.players)
# Game over, print the winner and statistics
winner = game.get_winner() # The winner is the player who solved the puzzle
game.print_winner_and_stats(winner)
if __name__ == "__main__":
main()