-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzarus.py
144 lines (124 loc) · 4.41 KB
/
puzarus.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
import traceback
import sys
import db_puz
def display_puzzle(puzzle):
"""
Display the puzzle with the numbers and letters
"""
flag = 0
print()
for row in puzzle:
print(" ", end='')
for cell in row:
if cell in matches:
print("{:>2}".format(matches[cell]), end=' ')
else:
if cell != 0:
print("{:>2}".format(cell), end=' ')
flag = 1
else:
print(" ", end='')
print("\n")
if flag == 0:
print("Completed!\n")
def add_matches(number, letter):
"""
Add a new match between a number and a letter
"""
global matches
matches[number] = letter
print(f"{number} now matches {letter}")
def remove_matches(letter):
"""
Remove a match between a number and a letter
"""
global matches
matches = {key:val for key, val in matches.items() if val != letter}
print(f"{letter} no longer corresponds to any number")
# begin
# init puzzle data
puzzle = db_puz.puzzle_22026
matches_start = db_puz.matches_22026
matches = matches_start.copy()
display_puzzle(puzzle)
# User interaction section
while True:
command = input("Enter a command (or \"h\" for help): ")
cchain = command.strip().split()
if len(cchain) == 0:
cchain.append('REFRESH')
else:
cchain[0] = cchain[0].upper()
# screen refresh
if cchain[0] == "REFRESH":
display_puzzle(puzzle)
# add new match(es)
elif cchain[0] == "A":
try:
for a in range(1, len(cchain), 2):
if not cchain[a].isdigit():
print("{} is not an integer, skipping".format(cchain[a]))
continue
else:
current_number = int(cchain[a])
current_letter = cchain[a+1].upper()
if len(current_letter) > 1:
print("{} is not a single letter, skipping".format(current_letter))
continue
if current_number in matches:
print("{} already corresponds to {}, skipping".format(current_number, matches[current_number]))
elif current_letter in matches.values():
print("{} already corresponds to {}, skipping".format(current_letter, list(matches.keys())[list(matches.values()).index(current_letter)]))
elif (current_number < 1) or (current_number > 26):
print("Number must be between 1 and 26.")
else:
add_matches(current_number, current_letter)
except Exception: # PEP 8 E722
traceback.print_exc()
display_puzzle(puzzle)
# remove match(es)
elif cchain[0] == "R":
try:
for a in range(1, len(cchain)):
current_letter = cchain[a].upper()
if (not current_letter.isalpha() or (len(current_letter) > 1)):
print("{} is not a single letter, skipping".format(cchain[a]))
elif current_letter not in matches.values():
print("{} has no correspondence, skipping".format(current_letter))
else:
remove_matches(current_letter)
except Exception:
traceback.print_exc()
display_puzzle(puzzle)
# print current matches
elif cchain[0] == "P":
print()
print(matches)
print()
for key in matches:
print("{} {} ".format(key, matches[key]), end='')
print("\n\n")
# revert to initial matches (start over)
elif cchain[0] == "X":
matches = matches_start.copy()
display_puzzle(puzzle)
# quit
elif cchain[0] == "Q":
print("Bye!")
sys.exit(0)
# help
elif cchain[0] == "H":
print('\n'
'\n'
' A: add matches\n'
' usage: A number letter [number letter] [...]\n'
' R: remove matches\n'
' usage: R letter [letter] [...]\n'
' P: print current solution (matches)\n'
' REFRESH | <empty>: refresh display\n'
' X: reset all matches (revert to start)\n'
' H: this text\n'
'\n')
# what?
else:
print("Invalid command")