generated from cisc204/model-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
viz.py
117 lines (101 loc) · 3.72 KB
/
viz.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
from sty import fg, bg, rs
import random
class Path:
"""
Class representing a path in a level.
self.type - The type of tile. Can be one of S, C, B, M, O, blank
self.link - Whether the tile links up, down, left, right
self.water - Whether or not the tile contains water.
self.row - The row that the tile is in.
self.col - The column that the tile is in.
"""
def __init__(self, row, col) -> None:
self.type = "blank"
self.link = [False, False, False, False] # [U, D, L, R]
self.water = False
self.row = row
self.col = col
def convert_solution(sol, level_layout):
"""
Convert the dictionary output from the SAT solver
into an easier-to-use list.
sol - A solution to the level.
level_layout - The layout of the level.
"""
n_row = len(level_layout)
n_col = len(level_layout[0])
level = [[Path(r, c) for c in range(n_col)] for r in range(n_row)]
for k, v in sol.items():
prop = getattr(k, 'prop', None)
if v and prop == 'tile':
level[k.row][k.col].type = level_layout[k.row][k.col]
elif prop == 'link':
if k.direction == 'V':
level[k.row + 1][k.col].link[0] = v
level[k.row][k.col].link[1] = v
if v:
level[k.row + 1][k.col].water = True
level[k.row][k.col].water = True
elif k.direction == 'H':
level[k.row][k.col + 1].link[2] = v
level[k.row][k.col].link[3] = v
if v:
level[k.row][k.col + 1].water = True
level[k.row][k.col].water = True
return level
def viz_level(level):
"""
Visualize a solution to a level.
level - The output of convert_solution
"""
n_row = len(level)
n_col = len(level[0])
# Get character to be printed
for r in range(n_row):
for c in range(n_col):
path = level[r][c]
char = ' '
# Straight tile
if path.type == '-':
if path.link == [False, False, True, True]:
char = '━'
elif path.link == [True, True, False, False]:
char = '┃'
else:
char = random.choice("━┃")
# Curved tile
elif path.type == 'L':
if path.link == [False, True, False, True]:
char = '┏'
elif path.link == [False, True, True, False]:
char = '┓'
elif path.link == [True, False, False, True]:
char = '┗'
elif path.link == [True, False, True, False]:
char = '┛'
else:
char = random.choice("┏┓┗┛")
# Bridge tile
elif path.type == '+':
char = '╋'
# Moat tile
elif path.type == '#':
char = '#'
# Ocean tile
elif path.type == 'U':
if path.link == [True, False, False, False]:
char = 'U'
elif path.link == [False, True, False, False]:
char = '∩'
elif path.link == [False, False, True, False]:
char = '⊃'
elif path.link == [False, False, False, True]:
char = '⊂'
else:
char = random.choice("U∩⊂⊃")
# Color the tiles that contain water
if path.water:
print(bg.blue + fg.white + char + rs.bg + rs.fg, end='')
else:
print(fg.white + char + rs.fg, end='')
print()