|
1
|
|
|
from collections import namedtuple |
|
2
|
|
|
from enum import Enum |
|
3
|
|
|
|
|
4
|
|
|
import toolz |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
class Direction(Enum): |
|
8
|
|
|
North = 1 |
|
9
|
|
|
South = 2 |
|
10
|
|
|
East = 3 |
|
11
|
|
|
West = 4 |
|
12
|
|
|
|
|
13
|
|
|
def opposite(self): |
|
14
|
|
|
if self == Direction.North: |
|
15
|
|
|
return Direction.South |
|
16
|
|
|
elif self == Direction.South: |
|
17
|
|
|
return Direction.North |
|
18
|
|
|
elif self == Direction.West: |
|
19
|
|
|
return Direction.East |
|
20
|
|
|
elif self == Direction.East: |
|
21
|
|
|
return Direction.West |
|
22
|
|
|
else: |
|
23
|
|
|
raise TypeError("Invalid direction") |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
Glue = namedtuple("Glue", ('label', 'strength')) |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
Tile = namedtuple("Tile", ("name", "label", "tilecolor", "textcolor", "concentration", "glues")) |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
def dirbind(tile, direction=Direction.North): |
|
33
|
|
|
return tile.glues[direction].strength |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
def dirlabel(tile, direction=Direction.North): |
|
37
|
|
|
return tile.glues[direction].label |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
northbind = toolz.partial(dirbind, direction=Direction.North) |
|
41
|
|
|
southbind = toolz.partial(dirbind, direction=Direction.South) |
|
42
|
|
|
eastbind = toolz.partial(dirbind, direction=Direction.East) |
|
43
|
|
|
westbind = toolz.partial(dirbind, direction=Direction.West) |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
northlabel = toolz.partial(dirlabel, direction=Direction.North) |
|
47
|
|
|
southlabel = toolz.partial(dirlabel, direction=Direction.South) |
|
48
|
|
|
eastlabel = toolz.partial(dirlabel, direction=Direction.East) |
|
49
|
|
|
westlabel = toolz.partial(dirlabel, direction=Direction.West) |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
def new_tile(name, label="", tilecolor="white", textcolor="black", concentration=1, glues=None): |
|
53
|
|
|
if glues is None: |
|
54
|
|
|
glues = { |
|
55
|
|
|
Direction.North: Glue("", 0), |
|
56
|
|
|
Direction.South: Glue("", 0), |
|
57
|
|
|
Direction.West: Glue("", 0), |
|
58
|
|
|
Direction.East: Glue("", 0), |
|
59
|
|
|
} |
|
60
|
|
|
# TODO: validate |
|
61
|
|
|
return Tile(name, label, tilecolor, textcolor, concentration, glues) |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
def format_tile(tile): |
|
65
|
|
|
return ("TILENAME {t.name}\n" |
|
66
|
|
|
"LABEL {t.label}\n" |
|
67
|
|
|
"TILECOLOR {t.tilecolor}\n" |
|
68
|
|
|
"TEXTCOLOR {t.textcolor}\n" |
|
69
|
|
|
"CONCENTRATION {t.concentration}\n" |
|
70
|
|
|
"NORTHBIND {northbind}\n" |
|
71
|
|
|
"SOUTHBIND {southbind}\n" |
|
72
|
|
|
"WESTBIND {westbind}\n" |
|
73
|
|
|
"EASTBIND {eastbind}\n" |
|
74
|
|
|
"NORTHLABEL {northlabel}\n" |
|
75
|
|
|
"SOUTHLABEL {southlabel}\n" |
|
76
|
|
|
"WESTLABEL {westlabel}\n" |
|
77
|
|
|
"EASTLABEL {eastlabel}\n" |
|
78
|
|
|
"CREATE".format(t=tile, |
|
79
|
|
|
northbind=northbind(tile), |
|
80
|
|
|
southbind=southbind(tile), |
|
81
|
|
|
eastbind=eastbind(tile), |
|
82
|
|
|
westbind=westbind(tile), |
|
83
|
|
|
northlabel=northlabel(tile), |
|
84
|
|
|
southlabel=southlabel(tile), |
|
85
|
|
|
eastlabel=eastlabel(tile), |
|
86
|
|
|
westlabel=westlabel(tile), |
|
87
|
|
|
)) |
|
88
|
|
|
|