forked from DeepSpace2/wordle-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.py
executable file
·69 lines (62 loc) · 2.56 KB
/
play.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
#!/usr/bin/env python3
import random
import sys
from datetime import datetime
import wordle
from cli import CLIPlayer
def print_help_exit():
print("Usage: python3 play.py [-h|--help] [--today|DAY|SOLUTION] [--hints]")
print()
print("Option\t\t\tBehaviour (* = mutually-exclusive)")
print("------\t\t\t----------------------------------")
print("none\t\t\tUse a random solution from the official Wordle dictionary")
print("--today\t\t\t* Use today's official Wordle solution")
print("DAY (number)\t\t* Use the official solution from this DAY")
print("SOLUTION (string)\t* Use a given SOLUTION (must be 5-letter word)")
print("--hints\t\t\tAfter each guess, report number of possible words remaining")
print("-h, --help\t\tPrint this help text and quit")
exit()
if __name__=="__main__":
game = wordle.Game()
player = CLIPlayer()
fixed_solution = None
hints = False
for arg in sys.argv[1:]:
if arg == "-h" or arg == "--help":
print_help_exit()
elif arg == "--today" and fixed_solution == None:
delta = (datetime.utcnow() - datetime(2021, 6, 19)).days % len(game.VALID_SOLUTIONS)
fixed_solution = game.VALID_SOLUTIONS[delta]
player.GAME_NUMBER = delta
elif arg.isdigit() and int(arg) >= 0 and fixed_solution == None:
delta = int(arg) % len(game.VALID_SOLUTIONS)
fixed_solution = game.VALID_SOLUTIONS[delta]
player.GAME_NUMBER = delta
elif arg.isalpha() and len(arg) == game.LENGTH and fixed_solution == None:
arg = arg.upper()
# fixed solution must be in official guesses list, but doesn't have to be in solutions list
if arg in game.VALID_GUESSES:
fixed_solution = arg
player.warn(f"Solution will be { arg }")
else:
player.warn(f"Invalid solution { arg }, must be a valid guess")
print_help_exit()
elif arg == "--hints":
hints = True
else:
player.warn(f"Invalid argument { arg }")
print_help_exit()
while True:
try:
game.play(player, random.choice(game.VALID_SOLUTIONS) if fixed_solution == None else fixed_solution, hints=hints)
except (KeyboardInterrupt, EOFError):
print()
player.quit()
if fixed_solution != None:
exit()
try:
player.again()
print()
except (KeyboardInterrupt, EOFError):
print()
exit()