-
Notifications
You must be signed in to change notification settings - Fork 0
/
field.py
84 lines (72 loc) · 2.78 KB
/
field.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
"""Field implementation file."""
import os
import time
from point import Point
from probe import Probe
class Field(object):
"""Map field to the probe."""
width = 0
height = 0
probes = []
def __init__(self, width, height):
"""Overwrite default __init__ function."""
self.width = width + 1
self.height = height + 1
super(Field, self).__init__()
def _contains(self, point):
"""Check if field contains point."""
if not isinstance(point, Point):
raise ValueError('Argument point should be an Point instance')
return 0 <= point.x <= self.width and 0 <= point.y <= self.height
def validateProbePosition(self, position):
"""Validate if position is inside the field.
If the final position is not inside the field return last possible
position inside the field.
return: (True, point)
(False, point)
"""
if self._contains(position):
return (True, position)
last_x = max(0, min(self.width, position.x))
last_y = max(0, min(self.height, position.y))
return (False, Point(x=last_x, y=last_y))
def insertProbe(self, probe, drawing=True):
"""Add probe into the field."""
if not isinstance(probe, Probe):
raise ValueError('action argument should be an instance of Probe')
self.probes.append(probe)
self.drawField()
def runActionOnProbe(self, action, is_last=True, probe_id=0, drawing=True):
"""Move the probe at id or the last added probe."""
if not is_last:
raise NotImplementedError('Function not implemented yet')
if len(self.probes) <= 0:
return
probe = self.probes[-1]
probe.runAction(action)
(is_inside, in_position) = self.validateProbePosition(probe.position)
if not is_inside:
probe.position = in_position
if drawing:
self.drawField()
return probe.position, probe.direction
def point_representation(self, point):
"""Return the point representation of the desired point for the UI."""
for probe in self.probes:
if point.x <= probe.position.x < point.x + 1 and \
point.y <= probe.position.y < point.y + 1:
return probe.draw()
return ' * '
def drawField(self):
"""Return the the UI field draw."""
time.sleep(1)
os.system('clear')
final_draw = ""
for y in reversed(range(self.height)):
line_draw = ""
for x in range(self.width):
self.point_representation(Point(x, y))
line_draw += self.point_representation(Point(x, y))
line_draw += '\n'
final_draw += line_draw
print(final_draw)