-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
166 lines (140 loc) · 5.58 KB
/
game.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
class InvalidMove(Exception):
...
class Game:
def __init__(self):
self.nrows = 8
self.ncols = 8
self.cell_size = 2
self.board = dict()
self.pos_moves = [(x, y) for x in range(-2, 3, 1) for y in range(-2, 3, 1) if x ** 2 + y ** 2 == 5]
self.occupied = list()
self.next_moves = dict()
self.solution = []
def play(self):
self.set_board()
self.make_move("Enter the knight's starting position: ")
while True:
doit = input("Do you want to try the puzzle? (y/n): ")
if doit in ['y', 'n']:
break
print("Invalid input!")
if not self.solve():
self.exit(solution=False)
return
if doit == 'y':
self.fill_board()
while self.next_moves:
self.draw_board()
self.make_move("Enter your next move: ", False)
self.fill_board()
else:
self.fill_solution()
self.exit(manual=doit == "y")
def solve(self):
if not self.next_moves:
if len(self.occupied) == len(self.board):
self.solution = self.occupied.copy()
return True
return False
_next_moves = sorted(self.next_moves.items(), key=lambda x: x[1])
s_index = 0
if len(_next_moves) > 1 and _next_moves[0][1] == 0:
s_index = 1
for i in range(s_index, len(_next_moves)):
self.occupied.append(_next_moves[i][0])
self.get_possible_moves()
_solution = self.solve()
self.occupied.pop(-1)
if _solution:
self.next_moves = dict(_next_moves)
return True
return False
def make_move(self, message, first=True):
while True:
try:
x, y = self.get_values(message)
if not self.in_board(x, y):
raise ValueError
if not first and ((x, y) in self.occupied or not self.is_available(x, y)):
raise InvalidMove
self.occupied.append((x, y))
self.get_possible_moves()
break
except ValueError:
print("Invalid dimensions!")
except InvalidMove:
print("Invalid move!", end=" ")
def is_available(self, x, y):
return (x - self.occupied[-1][0], y - self.occupied[-1][1]) in self.pos_moves
@staticmethod
def get_values(message):
return (int(i) for i in input(message).split())
def set_board(self):
while True:
try:
x, y = self.get_values("Enter your board dimensions: ")
if x > 0 and y > 0:
self.ncols, self.nrows = x, y
self.cell_size = len(str(x * y))
self.fill_board()
break
else:
raise ValueError
except ValueError:
print("Invalid dimensions!")
def in_board(self, x, y):
return 1 <= x <= self.ncols and 1 <= y <= self.nrows
def fill_board(self):
for i in range(self.ncols):
for j in range(self.nrows):
self.update_cell((i + 1, j + 1), "_" * self.cell_size)
for point in self.occupied:
self.update_cell(point, " " * (self.cell_size - 1) + "*")
for point, count in self.next_moves.items():
self.update_cell(point, " " * (self.cell_size - 1) + str(count))
if self.occupied:
self.update_cell(self.occupied[-1], " " * (self.cell_size - 1) + "X")
def fill_solution(self):
for i, point in enumerate(self.solution):
self.update_cell(point, " " * (self.cell_size - len(str(i + 1))) + str(i + 1))
def update_cell(self, point, value):
self.board[point] = value
def get_possible_moves(self):
self.next_moves = dict()
for x, y in self.pos_moves:
nx = self.occupied[-1][0] + x
ny = self.occupied[-1][1] + y
if self.in_board(nx, ny) and (nx, ny) not in self.occupied:
self.count_ahead_moves((nx, ny))
def count_ahead_moves(self, point):
count = 0
for x, y in self.pos_moves:
nx = point[0] + x
ny = point[1] + y
if self.in_board(nx, ny) and (nx, ny) not in self.occupied:
count += 1
self.next_moves[point] = count
def draw_board(self):
md = len(str(self.nrows))
board_list = [" " * md + "-" * (self.ncols * (self.cell_size + 1) + 3)]
for i in range(self.nrows, 0, -1):
ld = len(str(i))
rnum = " " * (md - ld) + str(i)
line = rnum + "| " + " ".join([self.board[(r + 1, i)]
for r in range(self.ncols)]) + " |"
board_list += [line]
board_list += [" " * md + "-" * (self.ncols * (self.cell_size + 1) + 3)]
board_list += [" " * (md + 2) + " ".join(" " * (self.cell_size - len(str(r))) + str(r) for r in range(1, self.ncols + 1))]
print("\n".join(board_list))
def exit(self, solution=True, manual=True):
if not solution:
print("No solution exists!")
elif not manual:
print("Here's the solution!")
self.draw_board()
elif len(self.occupied) == len(self.board):
print("What a great tour! Congratulations!")
else:
print("No more possible moves!")
print(f"Your knight visited {len(self.occupied)} squares!")
Game().play()