-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
167 lines (129 loc) · 4.38 KB
/
main.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
import random
import easygui
import pygame
pygame.init()
pygame.font.init()
WIDTH = 600
win = pygame.display.set_mode((WIDTH, WIDTH))
TILE_SIZE = WIDTH // 3
font = pygame.font.SysFont("Arial", TILE_SIZE // 2)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
class Tile:
def __init__(self, x, y, num, win):
self.x = x
self.y = y
self.num = num
self.win = win
def draw(self):
pygame.draw.rect(self.win, WHITE, (self.x, self.y, TILE_SIZE, TILE_SIZE))
# Draw a border
pygame.draw.rect(self.win, BLACK, (self.x, self.y, TILE_SIZE, TILE_SIZE), 5)
text = font.render(str(self.num), True, BLACK)
text_surf = text.get_rect(
center=(self.x + TILE_SIZE // 2, self.y + TILE_SIZE // 2)
)
self.win.blit(text, text_surf)
def update(self, tiles):
if pygame.mouse.get_pressed()[0]:
pos = pygame.mouse.get_pos()
if (
self.x < pos[0] < self.x + TILE_SIZE
and self.y < pos[1] < self.y + TILE_SIZE
):
self.move(tiles)
def move(self, tiles):
current = tiles.numbers.index(self.num)
right = current + 1
left = current - 1
up = current - 3
down = current + 3
# Right
if right < len(tiles.numbers) and right % 3 != 0 and tiles.numbers[right] == 0:
tiles.numbers[right] = self.num
tiles.numbers[current] = 0
# Left
elif left >= 0 and left % 3 != 2 and tiles.numbers[left] == 0:
tiles.numbers[left] = self.num
tiles.numbers[current] = 0
# Up
elif up >= 0 and tiles.numbers[up] == 0:
tiles.numbers[up] = self.num
tiles.numbers[current] = 0
# Down
elif down < len(tiles.numbers) and tiles.numbers[down] == 0:
tiles.numbers[down] = self.num
tiles.numbers[current] = 0
tiles.make_tiles()
if tiles.check_win():
easygui.msgbox("You won! Press OK to play another Game.", "You won!")
tiles.shuffle()
class Tiles:
def __init__(self, win):
self.win = win
self.shuffle()
def shuffle(self, nums=None):
if nums:
self.numbers = []
if len(set(nums)) != len(nums):
self.shuffle()
return
for i in nums:
if 0 <= int(i) <= 8:
self.numbers.append(int(i))
else:
self.numbers = list(range(1, 9))
random.shuffle(self.numbers)
self.numbers.append(0)
solvable = self.check_solvable(self.numbers)
if not solvable:
self.shuffle()
self.make_tiles()
def make_tiles(self):
self.tiles = []
for i, num in enumerate(self.numbers):
if num != 0:
self.tiles.append(
Tile(TILE_SIZE * (i % 3), TILE_SIZE * (i // 3), num, self.win)
)
def check_win(self):
if self.numbers[-1] != 0:
return False
for i, num in enumerate(self.numbers[:-1]):
if num != i + 1:
return False
return True
def check_solvable(self, puzzle: list):
inversions = 0
puzzle = [i for i in puzzle if i != 0]
for i in range(len(puzzle)):
for j in range(i + 1, len(puzzle)):
if puzzle[j] > puzzle[i]:
inversions += 1
return inversions % 2 == 0
def solve(self):
random.choice(self.tiles).move()
def draw(self):
for tile in self.tiles:
tile.draw()
tile.update(self)
tiles = Tiles(win)
while True:
win.fill((128, 128, 128))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
tiles.shuffle()
elif event.key == pygame.K_e:
nums = easygui.multenterbox(
f"Replace Empty cell with 0. If what you entered is not valid or is not solvable it will generate a random one.",
fields=[str(i) for i in range(1, 10)],
)
tiles.shuffle(nums)
elif event.key == pygame.K_s:
tiles.solve()
tiles.draw()
pygame.display.flip()