-
Notifications
You must be signed in to change notification settings - Fork 0
/
mrowka_langtona.py
50 lines (49 loc) · 1.65 KB
/
mrowka_langtona.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
"""Mrówka Langtona"""
import tkinter
coord_moves = {
"R": {(0, -1): (-1, 0), (-1, 0): (0, 1), (0, 1): (1, 0), (1, 0): (0, -1)},
"L": {(0, -1): (1, 0), (1, 0): (0, 1), (0, 1): (-1, 0), (-1, 0): (0, -1)},
}
def create_board(size):
return [[0 for _ in range(size)] for _ in range(size)]
def rgb_int_to_hex(number):
return "#{n:02x}{n:02x}{n:02x}".format(n=number)
size = 200
pattern = "RLR"
window_size = 600
steps_number = 15000
steps_render = 1000
board = create_board(size)
x, y = size // 2, size // 2
coordinates = (0, -1)
color_step = int(255 / (len(pattern) - 1))
colors = [rgb_int_to_hex(color_step * i) for i in range(len(pattern))][::-1]
root = tkinter.Tk()
canvas = tkinter.Canvas(root, width=window_size, height=window_size, bg="#fff")
canvas.pack()
def redraw(window_size, size, canvas, colors, board):
s = window_size / size
canvas.delete("all")
for y, row in enumerate(board):
for x, field in enumerate(row):
color = colors[field]
if field:
canvas.create_rectangle(
s * x, s * y, s + (s * x), s + (s * y), fill=color, outline=color
)
canvas.update()
try:
for i in range(steps_number):
field = board[y][x]
direction = pattern[field]
board[y][x] = (field + 1) % len(pattern)
coordinates = coord_moves[direction][coordinates]
y, x = y + coordinates[0], x + coordinates[1]
if y < 0 or x < 0:
raise IndexError
if not (i % steps_render):
redraw(window_size, size, canvas, colors, board)
except IndexError:
print(i)
redraw(window_size, size, canvas, colors, board)
root.mainloop()