-
Notifications
You must be signed in to change notification settings - Fork 0
/
HanochViewer.py
executable file
·148 lines (110 loc) · 3.71 KB
/
HanochViewer.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
#!/usr/bin/python3
import pygame
from pygame import Color
from enum import Enum
from random import randint
import re
import sys
size = width, height = [1024, 640]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Hanoch")
clock = pygame.time.Clock()
class Colors(Enum):
WHITE = (255,255,255)
BLACK = (0, 0, 0)
def gera_cor_aleatoria():
return tuple([randint(0, 255) for i in range (0, 4)])
class Rect(pygame.Rect):
def __init__(self, left, top, width, height, color=None):
self.color = color or Colors.BLACK.value
super().__init__(left, top, width, height)
def arrasta(self, left, top):
self.move_ip(left - self.left,
top - self.top)
def desenha(self):
pygame.draw.rect(pygame.display.get_surface(),
self.color, self)
class Torre(Rect):
def __init__(self, n):
self.discos = []
self.n = n
self.width = 5
self.top = 40
self.height = height - self.top
self.left = ((1024 / 4) * self.n) - (self.width / 2)
super().__init__(self.left, self.top, self.width, self.height)
def empilha_disco(self, d):
d.move_para_torre(self)
self.discos.append(d)
def desempilha_disco(self):
if (self.discos):
return self.discos.pop()
return
class Disco(Rect):
def __init__(self, peso):
self.peso = peso
self.width = (200 * self.peso) / 16
self.height = 20
self.color = gera_cor_aleatoria()
super().__init__(0, 0, self.width, self.height, self.color)
def move_para_torre(self, torre):
pos_x = ((width / 4) * torre.n) - (self.width / 2)
pos_y = height - (self.height * (len(torre.discos) + 1))
self.arrasta(pos_x, pos_y)
self.desenha()
def coloca_ndiscos(torre, ndiscos):
for i in range(ndiscos, 1 - 1, -1):
d = Disco(i)
torre.empilha_disco(d)
def move_disco(origem, destino):
disco = origem.desempilha_disco()
if disco:
destino.empilha_disco(disco)
def quit_on_close():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
def traduz_comandos(comando):
comandos = {
'A': 0,
'B': 1,
'C': 2,
'->': 1,
'<-': -1
}
return comandos.get(comando)
def run():
torres = [Torre(i) for i in range(1, 3 + 1)]
move_pattern = re.compile("(A|B|C) (->|<-) (A|B|C)")
for line in sys.stdin:
quit_on_close()
if 'DISCOS:' in line:
ndiscos = int(line.split()[1])
coloca_ndiscos(torres[0], ndiscos)
else:
m = move_pattern.search(line)
if m:
origem = direcao = destino = 0
origem = traduz_comandos(m.group(1))
direcao = traduz_comandos(m.group(2))
destino = traduz_comandos(m.group(3))
if direcao == 1:
move_disco(torres[origem],
torres[destino])
else:
move_disco(torres[destino],
torres[origem])
screen.fill(Colors.WHITE.value)
for torre in torres:
torre.desenha()
for disco in torre.discos:
disco.desenha()
pygame.display.update()
pygame.display.flip()
#clock.tick(2)
while True:
quit_on_close()
clock.tick(1)
if __name__ == '__main__':
run()