-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.py
executable file
·842 lines (779 loc) · 34.7 KB
/
board.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
import sys
import math
from tile import tile
import threading
import random
import coordinate
from copy import deepcopy
from implementation import *
class board():
EXT_ERROR = -1
_lock = threading.RLock()
_defaultX = 10
_defaultY = 10
_tile = tile()
_instance = None
car_default_initial_placement = [4, 4]
__carPlacement = None
def __init__(self,boardSize = None,carPlacement = None,random_values = 0, preMade_data = None,param_tile = None):
"""
This method initializes a singleton of a board,a two dimensional array of various tiles and a car tile
:param boardSize: dictionary of the form {'x': Int, 'y':Int}
:param carPlacement: dictionary of the form {'x': Int, 'y':Int}
:param random_values: tells the generator whether to randomize tile values
:return:
"""
# To save the amount of memory, tile instance can be set as an input
if param_tile is None:
self._tile = tile()
else:
self._tile = tile
# board data should be received at real time environment, but should be generated
# randomly for testing purposes
if preMade_data is not None:
self._instance = preMade_data
elif board._instance is None :
xVal = None # Setted x value
yVal = None # Setted y value
if boardSize == None or type(boardSize) != type({}):
if random_values == 0:
board._instance =self.init_board(board._defaultX, board._defaultY)
elif random_values == 1:
board._instance =self.init_board(board._defaultX, board._defaultY,random_values = 1)
xVal = board._defaultX
yVal = board._defaultY
else:
try:
xVal = boardSize['x']
yVal = boardSize['y']
if (xVal<= 0) or (yVal <= 0):
raise self.impossible_action_exception("illegal board size")
board._instance =self.init_board(xVal, yVal)
except:
board._instance =self.init_board(board._defaultX, board._defaultY)
if carPlacement == None or type(carPlacement) != type({}):
flag = self.initial_car_placement(int((xVal-1)/2), int((yVal-1)/2))
print flag
else:
try:
flag = self.initial_car_placement(carPlacement['x'],carPlacement['y'])
except:
# A more - permissive form that allows to receive illegal car placement
# flag = self.initial_car_placement(self.car_default_initial_placement[0], self.car_default_initial_placement[1])
# less permisive form - raises an excpetion
raise self.impossible_action_exception("Cannot place car outside of the board borders")
if flag == False:
raise self.impossible_action_exception("Error car's initial placement")
def __str__(self):
"""
This methoud prints out the _instance board as a string representation
:return: None
"""
string_model = ''
for row in self._instance:
for cell in row:
string_model += str(cell) + ' '
string_model = string_model[:-1] + '\n'
return string_model
def impossible_action_exception(self,message):
"""
prints error message and exits
"""
print ('board exception',message)
sys.exit(self.EXT_ERROR)
def failure_exception(self,message):
"""
prints error message and exits
"""
print ('Failure :',message)
sys.exit(self.EXT_ERROR)
def isObstacle(self,x_loc,y_loc):
"""
This method returns boolean values if the tile in coordinate <x,y> is an obstacle
Note: every tile that is not free or unmapped will be considered an obstacle
:param x_loc: legal coordinate
:param y_loc: legal coordinate
:return: False if tile is unmapped or free, True if not, -1 in case of an exception
"""
try:
unmapped_val = self._tile.get_UnmappedVal()
free_val = self._tile.get_FreeVal()
current_val = self.get_cell_val(x_loc, y_loc)
if current_val == -1 or (current_val != unmapped_val and current_val !=free_val):
return True
return False
except:
return -1
def isObstacle_adjacent(self,which):
"""
This method gets a direction (1-4 clockwise) and returns if the cell in that direction is an obstacle, i.e
can't go through this place
:param which: Int ranged 1-4
:return: True if wall, False otherwise. raises exception on error
"""
pass
if which<1 or which >4 :
raise self.impossible_action_exception("Adjacent cell represenation is from 1 to 4")
free_val = self._tile.get_FreeVal()
unmapped_val = self._tile.get_UnmappedVal()
car_location = self.get_car_placement()
locX = car_location['x']
locY = car_location['y']
if which == 1:
if locY-1 < 0:
return True
return (self.get_cell_val(locX,locY-1) != free_val) and (self.get_cell_val(locX,locY-1) != unmapped_val)
elif which == 2:
if locX+1 >= self.get_board_size()['x']:
return True
return (self.get_cell_val(locX+1,locY) != free_val) and (self.get_cell_val(locX+1,locY) != unmapped_val)
elif which == 3:
if locY+1 >= self.get_board_size()['y']:
return True
return (self.get_cell_val(locX,locY+1) != free_val) and (self.get_cell_val(locX,locY+1) != unmapped_val)
elif which == 4:
if locX-1 < 0:
return True
return (self.get_cell_val(locX-1,locY) != free_val) and (self.get_cell_val(locX-1,locY) != unmapped_val)
def get_board_size(self):
"""
This method returns a dictionary with the size of the board
:return: dictionary of the form {'x':Int, 'y':Int}
"""
try:
return {'y':len(self._instance) ,'x':len(self._instance[0])}
except:
return {'x':-1,'y':-1}
def get_cell_val(self,locationX,locationY):
"""
Given an X and Y placement within the board, returns the value of the given cell
:param locationX:
:param locationY:
:return: cell's value upon success, -1 upon ERROR
"""
try:
xLen = len(self._instance[0])
yLen = len(self._instance)
except:
return -1
if ((locationX < 0) or (locationX >= xLen)) or ((locationY < 0) or (locationY >= yLen)) :
raise self.failure_exception("Error while trying to get cell")
else:
return self._instance[locationY][locationX]
def tile_list_generator(self,length,val):
"""
This method creates tile of specific val and length
:param length: length of the returned list
:val: value to fill each cell within the list
:return: list
"""
result = []
if (length <=0) or (val<0):
return []
else:
for i in range(length):
result.append(val)
return result
def get_tile(self):
return self._tile
def get_xy_map(self,sizeX,sizeY,premade_location = None):
"""
This method is responsible for for cropping a map sized X*Y around the car or around some default location
In case there is not enough space around some side, it will fill it with unreachable placement
:param premade_location: allows you to get the map around some default location rather than the car's location
it is a dictionary of the form {'x':int,'y':int}
:param sizeX: number of cells on the right/left of the car
:param sizeY: number of cells on the front/back of the car
:return: a two dimensional array where the car is in the middle of it, raise exception in case of an error
"""
if (sizeX < 0) or (sizeY <0):
return []
result = []
unreachable_val = self._tile.get_UnreachableVal()
xBoard_size = len(self._instance[0])
yBoard_size = len(self._instance)
car_place_dict = self.get_car_placement()
if premade_location is None:
carX_placement = car_place_dict['x']
carY_placement = car_place_dict['y']
else:
try:
carX_placement = premade_location['x']
carY_placement = premade_location['y']
except:
carX_placement = car_place_dict['x']
carY_placement = car_place_dict['y']
if carX_placement == -1 or carY_placement == -1:
raise self.impossible_action_exception("illegal size arguments")
# checking if unreachable spaces are needed
missing_space_left = self.tile_list_generator(((min(0,(carX_placement-sizeX))) * -1),unreachable_val)
missing_space_right = self.tile_list_generator((max(0,carX_placement+sizeX - xBoard_size+1)),unreachable_val)
"""
missing_space_front = self.tile_list_generator((min(0,carY_placement-sizeY) * -1),unreachable_val)
missing_space_rear = self.tile_list_generator((max(0,carY_placement+sizeY-yBoard_size+1)),unreachable_val)
"""
# the board will be filled with rows of unreachable tiles if needed
missing_rows_front = (min(0,carY_placement-sizeY) * -1)
missing_rows_rear = (max(0,carY_placement+sizeY-yBoard_size+1))
xIteration_from = max(0,carX_placement-sizeX)
xIteration_to = min(xBoard_size-1,carX_placement+sizeX)
yIteration_from = max(0,carY_placement-sizeY)
yIteration_to = min(yBoard_size-1,carY_placement+sizeY)
for i in range(yIteration_from,yIteration_to+1):
original_piece = missing_space_left + self._instance[i][xIteration_from:xIteration_to+1] + missing_space_right
result.append(original_piece)
if missing_rows_front !=0 or missing_rows_rear != 0:
unreachable_row = self.tile_list_generator(sizeX*2+1,unreachable_val)
rear_completion = []
front_completion = []
for i in range(missing_rows_front):
front_completion.append(unreachable_row)
for i in range(missing_rows_rear):
rear_completion.append(unreachable_row)
result = front_completion + result + rear_completion
tempBoard = board()
tempBoard._instance = result
return tempBoard
def init_board(self, xVal, yVal,random_values=0):
if random_values == 1:
mapping_vals = self._tile.get_tile_mapping().values()
car_val = self._tile.get_CarVal()
mapping_vals.remove(car_val)
return [[(random.choice(mapping_vals)) for i in range(xVal)] for j in range(yVal)]
return [[self._tile.data for i in range(xVal)] for j in range(yVal)]
def set_cell(self,locX,locY,val):
"""
setting a cel positioned [x.y] to value val (val needs to be legal tile value)
:param locX: cell row
:param locY: cell column
:param val: new cell's value
:return: True upon success, exception if illegal location, False if value is not found
"""
legal_values = self._tile.get_tile_mapping()
for key in legal_values:
if legal_values[key] == val:
try:
self._instance[locY][locX] = val
except:
raise self.impossible_action_exception("Error while trying to set cell")
return True
return False
def get_car_placement(self):
"""
This method is responsible for locating the car within the board,
!! returns the first instance it sees, if two cells with the same value exist it will return the upper most left one !!
:return: dictionary containing the x and y points {'x':Int,'y':Int} the key's values would be -1
if they cannot be found
"""
#return self.__carPlacement
# previous implementation
carVal = self._tile.get_CarVal()
for row in range(len(self._instance)) :
for cell in range(len(self._instance[row])):
if self._instance[row][cell] == carVal:
return {'x':cell,'y':row}
return {'x':-1,'y':-1}
def initial_car_placement(self,locX,locY):
"""
Assuming car initial position
If cell is free, placing the car upon the data board
:param locX: the car's place within the x axis
:param locY: the car's place within the y axis
:return: True upon success, False otherwise
"""
current_cell_value = self.get_cell_val(locX,locY)
if (current_cell_value == -1):
return False
elif (current_cell_value == self._tile.get_FreeVal()):
return False
car_val = self._tile.get_CarVal()
self.__carPlacement = {'x':locX,'y':locY,'val':car_val}
return self.set_cell(locX,locY,car_val)
def expand_board(self,addX,addY,placementX,placementY):
pass
def replace_two_tile(self,locX1,locX2,locY1,locY2,val1,val2):
"""
This method is designed to replace two tiles with a value without having to take a risk,
one or more tile would be changed as a result of multi-tasking
note that this method does not check if the vales are legal
:param locX1: first location
:param locX2: second location
:param locY1:
:param locY2:
:param val1: replace tile1_val to
:param val2: replace tile2_val to
:return: True if successful, False otherwise
"""
with self._lock:
return (self.set_cell(locX1,locY1,val1)) and (self.set_cell(locX2,locY2,val2))
def randomize_walls(self,number=None):
"""
This method creates random walls
:return: None
"""
if number == None:
number = self._defaultX
board_size = self.get_board_size()
wall_val = self._tile.get_WallVal()
car_val = self._tile.get_CarVal()
for x in range(number):
flag= False
while flag==False:
randX = random.randint(0,board_size['x']-1)
randY = random.randint(0,board_size['y']-1)
if self.get_cell_val(randX,randY) != car_val:
flag = self.set_cell(randX,randY,wall_val)
return None
def insert_row_front(self,data = None):
"""
This method adds a row of tiles at the upper bound of the board
:param data: contains pre-made data, if None the we will randomize one
:return: True if successful, False otherwise
"""
if data == None:
data =[]
for i in range(len(self._instance[0])):
data.append(random.choice(self._tile.get_tile_mapping().values()))
else:
if len(data) != len(self._instance[0]):
return False
try:
self._instance.insert(0,data)
except:
return False
return True
def insert_row_right(self,data = None):
"""
This method adds a column of tiles at the right bound of the board
:param data: contains pre-made data, if None the we will randomize one
:return: True if successful, False otherwise
"""
if data == None:
data =[]
for i in range(len(self._instance[0])):
data.append(random.choice(self._tile.get_tile_mapping().values()))
else:
if len(data) != len(self._instance[0]):
return False
try:
for index in range (len(self._instance)):
self._instance[index].append(data[index])
except:
return False
return True
def insert_row_back(self,data = None):
"""
This method adds a row of tiles at the rear bound of the board
:param data: contains pre-made data, if None the we will randomize one
:return: True if successful, False otherwise
"""
if data == None:
data =[]
for i in range(len(self._instance[0])):
data.append(random.choice(self._tile.get_tile_mapping().values()))
else:
if len(data) != len(self._instance[0]):
return False
try:
self._instance.append(data)
except:
return False
return True
def insert_row_left(self,data = None):
"""
This method adds a column of tiles at the left bound of the board
:param data: contains pre-made data, if None the we will randomize one
:return: True if successful, False otherwise
"""
if data == None:
data =[]
for i in range(len(self._instance[0])):
data.append(random.choice(self._tile.get_tile_mapping().values()))
else:
if len(data) != len(self._instance[0]):
return False
try:
for index in range (len(self._instance)):
self._instance[index].insert(0,data[index])
except:
return False
return True
def get_obstacles_map(self):
"""
This method takes the board instance and returns a 2d array composed of
tile that are obstacles and tile that are car/not obstacles
:return:
"""
temp_board = deepcopy(self._instance)
wall_val = self._tile.get_WallVal()
car_val = self._tile.get_CarVal()
otherwise_val = self._tile.get_UnreachableVal()
for yIndex in range(0,len(temp_board)):
for xIndex in range(0,len(temp_board[0])):
if temp_board[yIndex][xIndex] !=wall_val and temp_board[yIndex][xIndex] != car_val:
temp_board[yIndex][xIndex] = otherwise_val
return temp_board
def calculate_distance(self,x1,x2,y1,y2):
distance = math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))
rnd_distance = round(distance,3)
return rnd_distance
def get_nearest_tileVal(self,tileVal):
"""
This method returns the coordinate of the closest tileVal according to the car's position
The main algorithm is checking the nearest cells clockwise,and expand if necessary
:param tileVal: legal tileVal to be found
:return: the tile coordinate if exists, -1 other if no such can be found
"""
carPlacement = self.get_car_placement()
boardSize = self.get_board_size()
if carPlacement['x'] == -1 or carPlacement['y'] == -1:
return -1
carX = carPlacement['x']
carY = carPlacement['y']
boardX = boardSize['x']
boardY = boardSize['y']
# We need to find the maximum number of iterations allows
iterationNumber = max(carX,(boardX-carX),carY,(boardY-carY))
for ind in range(1,iterationNumber):
if self.get_cell_val(carX,carY-ind) == tileVal:
return coordinate.coordinate(carX,carY-ind)
elif self.get_cell_val(carX+ind,carY-ind) == tileVal:
return coordinate.coordinate(carX+ind,carY-ind)
elif self.get_cell_val(carX+ind,carY) == tileVal:
return coordinate.coordinate(carX+ind,carY)
elif self.get_cell_val(carX+ind,carY+ind) == tileVal:
return coordinate.coordinate(carX+ind,carY+ind)
elif self.get_cell_val(carX,carY+ind) == tileVal:
return coordinate.coordinate(carX,carY+ind)
elif self.get_cell_val(carX-ind,carY+ind) == tileVal:
return coordinate.coordinate(carX-ind, carY)
elif self.get_cell_val(carX-ind, carY) == tileVal:
return coordinate.coordinate(carX-ind, carY)
elif self.get_cell_val(carX-ind, carY-ind) == tileVal:
return coordinate.coordinate(carX-ind, carY-ind)
return -1
def get_obstacles_locations(self):
"""
:return: an array depicting the coordinates where obstacles are
"""
raise self.impossible_action_exception('need to be implemented')
def get_obstacles_coordinates_list(self):
"""
for each tile valued as wall, return its location
:return: list of Coordinates
"""
wall_val = self._tile.get_WallVal()
indexX = 0
indexY = 0
obstacles_list = []
for indexY in range(0, len(self._instance)):
for indexX in range(0, len(self._instance[0])):
if self.get_cell_val(indexX, indexY) == wall_val:
new_co = coordinate.coordinate(indexX, indexY)
obstacles_list.append(new_co)
return obstacles_list
def is_all_wall_ahead(self, direction):
raise self.impossible_action_exception('need to be implemented')
def set_board(self,data_board):
"""
This method uses a lock to replace the data resides in _instance
:param data_board: needs to be 2d array
:return: True upon success, False otherwise
"""
try:
# Checking if the input is a 2d array, in some old fashioned manner
data_board[0][0]
except:
return False
with self._lock:
try:
self._instance = data_board
return True
except:
return False
return False
def is_dest_unreacable(self, dest, cost_so_far):
for i in range(-1,2):
for j in range(-1,2):
if ((dest[0]+i,dest[1]+j) in cost_so_far):
return False
return True
def is_mapping_done(self):
"""
This method is designed to decide weather the mapping is done.
if all 4 corners are unreachable the mapping is probably done.
:return: True upon board surrounded by obstacles, False otherwise
:raises: Failure exception
"""
car_loc = self.get_car_placement()
bor = self.get_board_size()
height = bor["x"]
width = bor["y"]
diagram = GridWithWeights(height, width)
for i in range(width):
for j in range(height):
if (self._instance[i][j] == self.get_tile().get_WallVal()):
diagram.walls.append((i, j))
came_from, cost_so_far = a_star_search(diagram, (car_loc["x"], car_loc["y"]),
(bor["x"]-1, bor["y"]-1))
if self.is_dest_unreacable((bor["x"]-1, bor["y"]-1), cost_so_far):
came_from, cost_so_far = a_star_search(diagram, (car_loc["x"], car_loc["y"]),
(bor["x"]-1, 0))
if self.is_dest_unreacable((bor["x"]-1, 0), cost_so_far):
came_from, cost_so_far = a_star_search(diagram, (car_loc["x"], car_loc["y"]),
(0, bor["y"]-1))
if self.is_dest_unreacable((0, bor["y"]-1), cost_so_far):
came_from, cost_so_far = a_star_search(diagram, (car_loc["x"], car_loc["y"]),
(0, 0))
if self.is_dest_unreacable((0, 0), cost_so_far):
return True, "Done mapping!"
return False, "Keep mapping!"
"""
flag = True
current_board = self.get_board_size()
boardSize_x = current_board['x']
boardSize_y = current_board['y']
if boardSize_x == -1 or boardSize_y == -1:
self.failure_exception('Illegal board size: {0}*{1}'.format(boardSize_x, boardSize_y))
for row in range(1, boardSize_y-1):
try:
# Note galpo: Every row might not be at the same length !
row_first = self.isObstacle(0,row)
row_last = self.isObstacle(boardSize_x-1, row)
if row_last == -1:
self.failure_exception('Error while trying to figure if tile <{0},{1}> is an obstacle'.format(row, row_last))
elif row_first == -1 :
self.failure_exception('Error while trying to figure if tile <{0},{1}> is an obstacle'.format(row, row_first))
elif not row_first or not row_last :
flag = False
break
except:
self.failure_exception(sys.exc_info()[1])
if not flag:
return flag
else:
for index in range(0,boardSize_x):
first_row_instance = self.isObstacle(index,0)
last_row_instance = self.isObstacle(index,boardSize_x-1)
if first_row_instance == -1 :
self.failure_exception('Error while trying to figure if tile <{0},{1}> is an obstacle'.format(index, 0))
elif last_row_instance == -1 :
self.failure_exception('Error while trying to figure if tile <{0},{1}> is an obstacle'.format(index, boardSize_x-1))
elif not first_row_instance or not last_row_instance:
flag = False
return flag
return flag
"""
if __name__ == '__main__':
newBoard = board(random_values=1)
print newBoard
#print newBoard.get_nearest_tileVal(newBoard._tile.get_WallVal())
newBoard.set_board(([[2,3],[4,5]]))
print newBoard
sys.exit()
for c in newBoard.get_obstacles_coordinates_list():
print (c.get_x(),c.get_y())
sys.exit()
newBoard.randomize_walls()
print newBoard
print '*********'
newBoard.insert_row_right()
print newBoard
'''
#############################################################
#########Methods cemetery####################################
#############################################################
def set_initial_direction(self):
"""
This method sets the cars direction
:return:
"""
directions = self._tile.get_car_directions()
self.__CAR_INITIAL_DIRECTION = directions[directions.keys()[0]]
def set_movement_cell(self,val,locX,locY):
"""
In case of changing the car's direction, this method change the tile value
:param val: tile value representing the new direction
:return: True upon success, False if the value is illegal
"""
if self._tile.isDirection_available(val):
try:
self._instance[locY][locX] = val
except:
raise self.impossible_action_exception("Error while trying to set cell")
return True
return False
def move_LU(self):
"""
This method moves to to the left in a diagonal matter
relatively to the car's direction !!!
:return: True upon success, False otherwise
"""
location_data = self.__carPlacement
xLoc = location_data['x']
yLoc = location_data['y']
current_direction = location_data['val']
direction_dictionary = self._tile.get_directions_dict()
if current_direction == direction_dictionary['LU']:
# turn left
xNext = xLoc - 1
yNext = yLoc
directionNext = direction_dictionary['LC']
pass
elif current_direction == direction_dictionary['CU']:
# left upper corner
xNext = xLoc - 1
yNext = yLoc - 1
directionNext = direction_dictionary['LU']
pass
elif current_direction == direction_dictionary['RU']:
# center upper
xNext = xLoc
yNext = yLoc - 1
directionNext = direction_dictionary['CU']
pass
elif current_direction == direction_dictionary['RC']:
# right upper corner
xNext = xLoc + 1
yNext = yLoc - 1
directionNext = direction_dictionary['RU']
pass
elif current_direction == direction_dictionary['RB']:
# right
xNext = xLoc + 1
yNext = yLoc
directionNext = direction_dictionary['RC']
pass
elif current_direction == direction_dictionary['CB']:
# right bottom corner
xNext = xLoc + 1
yNext = yLoc + 1
directionNext = direction_dictionary['RB']
pass
elif current_direction == direction_dictionary['LB']:
# rear
xNext = xLoc
yNext = yLoc + 1
directionNext = direction_dictionary['CB']
pass
elif current_direction == direction_dictionary['LC']:
# left bottom corner
xNext = xLoc - 1
yNext = yLoc + 1
directionNext = direction_dictionary['LB']
pass
def move_front_tile(self):
"""
This method moves the car one tile ahead and updates the tile it has been to to MAPPED
:return: new location if possible,otherwise -1
! Note : lock is needed in the tile setting area
"""
current_location = self.get_car_placement()
locX = current_location['x']
locY = current_location['y']
if (locX == -1 or locY == -1) or (locY-1< 0) :
return False
else:
mapped_val = self._tile.get_FreeVal()
car_val = self._tile.get_CarVal()
unmapped_val = self._tile.get_UnmappedVal()
# checking if the next step is reachable
if self.get_cell_val(locX,locY-1) != mapped_val and self.get_cell_val(locX,locY-1) != unmapped_val:
return False
if self.replace_two_tile(locX,locX,locY-1,locY,car_val,mapped_val) == False:
error_val = self._tile.get_ErrorVal()
self.set_cell(locX,locY,car_val)
self.set_cell(locX,locY-1,error_val)
print 'Error - Could not place the car in location X:{0} Y:{1}.\nCar moved back to location X:{2} Y:{3}\n'.format(locX,locY-1,locX,locY)
return False
return self.get_car_placement()
def move_left_tile(self):
"""
This method moves the car one tile to the left
:return: new location if possible,otherwise -1
"""
current_location = self.get_car_placement()
locX = current_location['x']
locY = current_location['y']
if (locX == -1 or locY == -1) or (locX-1< 0) :
return False
else:
mapped_val = self._tile.get_FreeVal()
car_val = self._tile.get_CarVal()
unmapped_val = self._tile.get_UnmappedVal()
# checking if the next step is reachable
if self.get_cell_val(locX-1,locY) != mapped_val and self.get_cell_val(locX-1,locY) != unmapped_val:
return False
if (self._tile.isVal_legal(mapped_val) == False) or (self._tile.isVal_legal(car_val) == False):
return False
# (self.set_cell(locX,locY-1,car_val)==False ) or (self.set_cell(locX,locY,mapped_val)==False ) - had been replaced by an atomic method
if self.replace_two_tile(locX-1,locX,locY,locY,car_val,mapped_val) == False:
error_val = self._tile.get_ErrorVal()
self.set_cell(locX,locY,car_val)
self.set_cell(locX,locY-1,error_val)
print 'Error - Could not place the car in location X:{0} Y:{1}.\nCar moved back to location X:{2} Y:{3}\n'.format(locX,locY-1,locX,locY)
return False
return self.get_car_placement()
def move_right_tile(self):
"""
This method moves the car one tile to the right
:return: new location if possible,otherwise False
"""
board_size = self.get_board_size()
row_length = board_size['x']
if row_length == -1:
return False
current_location = self.get_car_placement()
locX = current_location['x']
locY = current_location['y']
if (locX == -1 or locY == -1) or (locX+1 >= row_length) :
return False
else:
mapped_val = self._tile.get_FreeVal()
car_val = self._tile.get_CarVal()
unmapped_val = self._tile.get_UnmappedVal()
# checking if the next step is reachable
if self.get_cell_val(locX+1,locY) != mapped_val and self.get_cell_val(locX+1,locY) != unmapped_val:
return False
if (self._tile.isVal_legal(mapped_val) == False) or (self._tile.isVal_legal(car_val) == False):
return False
# (self.set_cell(locX,locY-1,car_val)==False ) or (self.set_cell(locX,locY,mapped_val)==False ) - had been replaced by an atomic method
if self.replace_two_tile(locX+1,locX,locY,locY,car_val,mapped_val) == False:
error_val = self._tile.get_ErrorVal()
self.set_cell(locX,locY,car_val)
self.set_cell(locX,locY-1,error_val)
print 'Error - Could not place the car in location X:{0} Y:{1}.\nCar moved back to location X:{2} Y:{3}\n'.format(locX,locY-1,locX,locY)
return False
return self.get_car_placement()
def move_back_tile(self):
"""
This method moves the car one tile to the back
:return: new location if possible,otherwise -1
"""
board_size = self.get_board_size()
column_length = board_size['y']
if column_length == -1:
return False
current_location = self.get_car_placement()
locX = current_location['x']
locY = current_location['y']
if (locX == -1 or locY == -1) or (locY+1 >= column_length) :
return False
else:
mapped_val = self._tile.get_FreeVal()
car_val = self._tile.get_CarVal()
unmapped_val = self._tile.get_UnmappedVal()
# checking if the next step is reachable
if self.get_cell_val(locX,locY+1) != mapped_val and self.get_cell_val(locX,locY+1) != unmapped_val:
return False
if (self._tile.isVal_legal(mapped_val) == False) or (self._tile.isVal_legal(car_val) == False):
return False
# (self.set_cell(locX,locY-1,car_val)==False ) or (self.set_cell(locX,locY,mapped_val)==False ) - had been replaced by an atomic method
if self.replace_two_tile(locX,locX,locY+1,locY,car_val,mapped_val) == False:
error_val = self._tile.get_ErrorVal()
self.set_cell(locX,locY,car_val)
self.set_cell(locX,locY-1,error_val)
print 'Error - Could not place the car in location X:{0} Y:{1}.\nCar moved back to location X:{2} Y:{3}\n'.format(locX,locY-1,locX,locY)
return False
return self.get_car_placement()
'''