This repository has been archived by the owner on Feb 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisualize.py
95 lines (79 loc) · 2.75 KB
/
Visualize.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
from graphics import *
import numpy as np
import time
MAP_PATH = './map.dat'
BLOCK_SYMBOL = 'b'
GRID_SIZE = 15
OUTPUT_FILE = './output.txt'
def tokenize(string):
string = string.replace(' ', '')
return string.split(',')
class Map:
BLOCK_SIZE = GRID_SIZE
def __init__(self, filePath):
self.filePath = filePath
self.map =[[]]
self.win = ''
self.read_map()
self.map_objects = []
self.size = 0
def read_map(self):
content = ''
with open(self.filePath, 'r') as content_file:
content = content_file.read()
self.map = np.array([list(c) for c in content.split('\n')[1:]])
def draw_map(self):
mapShape = self.map.shape
x = mapShape[0] * self.BLOCK_SIZE
y = mapShape[1] * self.BLOCK_SIZE
self.size = mapShape[1]
self.win = GraphWin(self.filePath, x, y)
def draw_blocks(self):
for i in range(len(self.map)):
for j in range(len(self.map)):
if self.map[i][j] == BLOCK_SYMBOL:
corner1 = Point(j*self.BLOCK_SIZE, i*self.BLOCK_SIZE)
corner2 = Point((j+1)*self.BLOCK_SIZE, (i+1)*self.BLOCK_SIZE)
blockRectangle = Rectangle(corner1, corner2)
blockRectangle.setFill('black')
blockRectangle.draw(self.win)
def draw_child(self, child_str='2, Peaceful, 5, 5, 1, 21, 51, 31'):
if child_str.find('#') != -1:
return False
tokens = tokenize(child_str)
self.draw_circle(tokens)
self.draw_id(tokens)
return True
def draw_circle(self, tokens):
x, y = float(tokens[2]), float(self.size) - float(tokens[3])
center = Point(x * self.BLOCK_SIZE, y * self.BLOCK_SIZE)
circle = Circle(center, float(tokens[4])* self.BLOCK_SIZE)
if tokens[1] == 'Peaceful':
circle.setFill('blue')
elif tokens[1] == 'Coward':
circle.setFill('green')
elif tokens[1] == 'Angry':
circle.setFill('red')
circle.draw(self.win)
self.map_objects.append(circle)
def draw_id(self, tokens):
x, y = float(tokens[2]), float(self.size) - float(tokens[3])
center = Point(x * self.BLOCK_SIZE, y * self.BLOCK_SIZE)
id_text = Text(center, tokens[0])
id_text.draw(self.win)
self.map_objects.append(id_text)
def clean(self):
for o in self.map_objects:
o.undraw()
self.map_objects = []
map = Map(MAP_PATH)
map.draw_map()
map.draw_blocks()
input_txt = ''
with open(OUTPUT_FILE, 'r') as content_file:
input_txt = content_file.read()
for i in input_txt.split('\n'):
if not map.draw_child(i):
time.sleep(0.1)
map.clean()
input()