-
Notifications
You must be signed in to change notification settings - Fork 0
/
Play_tools.py
179 lines (148 loc) · 6.03 KB
/
Play_tools.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import math
from utils import string_modify, string_switch
import string
Cars_id = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "X"]
Trucks_id = ["O", "P", "Q", "R"]
class Board:
def __init__(self, _cur_board):
self.board = str(_cur_board)
self.side = int(math.sqrt(len(self.board)))
self.VehicleHash = {}
for i in range(0, len(self.board)-1):
length = 0
if self.board[i] == '.':
continue
if self.board[i] in self.VehicleHash:
continue
if self.board[i] in Cars_id:
# it is a car -> length 2
length = 2
if self.board[i] in Trucks_id:
# it is a truck -> length 3
length = 3
if (i + 1) % self.side != 0 and self.board[i] == self.board[i + 1]:
# horizontal
self.VehicleHash[self.board[i]] = Vehicle(self.board[i], 'H', length, i, self)
if (i + self.side) < len(self.board) and self.board[i] == self.board[i + self.side]:
# vertical
self.VehicleHash[self.board[i]] = Vehicle(self.board[i], 'V', length, i, self)
def is_target_empty(self, _target):
if not self.check_point(_target):
return False
if self.board[_target] == '.':
return True
return False
def get_board_side(self):
return self.side
def set_board(self, _target, _vid):
# self.board[_target] = _vid
self.board = string_modify(self.board, _target, _vid)
def check_point(self, _p):
if (_p >= 0) and (_p < len(self.board)):
return True
return False
def print_board(self):
for r in range(self.side):
for c in range(self.side):
print(self.board[r * self.side + c], end='\t')
print('\n')
def get_vehicle(self, _id):
return self.VehicleHash.get(_id)
def get_board(self):
return self.board
def update_board(self, _command):
self.get_vehicle(_command[0]).move(_command[1], int(_command[2])-int('0'))
class Vehicle:
def __init__(self, _id, _direction, _len, _pos, _obj_board):
self.id = _id
self.direction = _direction
self.length = _len
self.top_left = _pos
self.board = _obj_board
if self.direction == 'H':
self.bottom_right = self.top_left + self.length - 1
else:
self.bottom_right = self.top_left + (self.length * self.board.get_board_side()) - self.board.get_board_side()
def move_vehicle(self, _direction, _steps):
if self.check_move_validity(_direction, _steps):
self.move(_direction, _steps)
else:
print('-E- can\'t move\n')
def check_move_validity(self, _direction, _steps):
if _direction == 'U':
target = self.top_left - (self.board.get_board_side())
for step in range(1, _steps):
if not self.board.is_target_empty(target):
return False
target = target - self.board.get_board_side()
return True
if _direction == 'D':
target = self.bottom_right + (self.board.get_board_side())
for step in range(1, _steps):
if not self.board.is_target_empty(target):
return False
target = target + self.board.get_board_side()
return True
if _direction == 'L':
target = self.top_left - 1
for step in range(1, _steps):
if not self.board.is_target_empty(target):
return False
target = target - 1
return True
if _direction == 'R':
target = self.bottom_right + 1
for step in range(1, _steps):
if not self.board.is_target_empty(target):
return False
target = target + 1
return True
def move(self, _direction, _steps):
if _direction == 'U':
target = self.top_left - (self.board.get_board_side())
old_pos = self.bottom_right
for step in range(0, _steps):
self.board.set_board(target, self.id)
self.board.set_board(old_pos, '.')
target = target - self.board.get_board_side()
old_pos = old_pos - self.board.get_board_side()
self.top_left = target + self.board.get_board_side()
self.bottom_right = old_pos
return self.board
if _direction == 'D':
target = self.bottom_right + (self.board.get_board_side())
old_pos = self.top_left
for step in range(0, _steps):
self.board.set_board(target, self.id)
self.board.set_board(old_pos, '.')
target = target + self.board.get_board_side()
old_pos = old_pos + self.board.get_board_side()
self.bottom_right = target - self.board.get_board_side()
self.top_left = old_pos
return self.board
if _direction == 'L':
target = self.top_left - 1
old_pos = self.bottom_right
for step in range(0, _steps):
self.board.set_board(target, self.id)
self.board.set_board(old_pos, '.')
target = target - 1
old_pos = old_pos - 1
self.top_left = target + 1
self.bottom_right = old_pos
return self.board
if _direction == 'R':
target = self.bottom_right + 1
old_pos = self.top_left
for step in range(0, _steps):
self.board.set_board(target, self.id)
self.board.set_board(old_pos, '.')
target = target + 1
old_pos = old_pos + 1
self.bottom_right = target - 1
self.top_left = old_pos
return self.board
def get_direction(self):
return self.direction
def get_length(self):
return self.length