In [1]:
from IPython.display import HTML
In [9]:
%run "../display_utilities.py"
In [2]:
board_bit_str = '000000000000000000000000000000000000011011000000000000000000000000000000000000000000000000000000000000000000'
red_car_end_a = 12
In [3]:
def move_car_left(board,end_a):
new_board = list(board)
new_board[end_a - 1] = HORIZONTAL_CAR
new_board[end_a + 1] = BLANK_SPACE
return new_board
def move_car_right(board,end_b):
new_board = list(board)
new_board[end_b + 1] = HORIZONTAL_CAR
new_board[end_b - 1] = BLANK_SPACE
return new_board
def move_car_up(board,end_a):
new_board = list(board)
new_board[end_a - 6] = VERTICAL_CAR
new_board[end_a + 6] = BLANK_SPACE
return new_board
def move_car_down(board, end_b):
new_board = list(board)
new_board[end_b + 6] = VERTICAL_CAR
new_board[end_b - 6] = BLANK_SPACE
return new_board
def move_truck_left(board, end_a):
new_board = list(board)
new_board[end_a - 1] = HORIZONTAL_TRUCK
new_board[end_a + 2] = BLANK_SPACE
return new_board
def move_truck_right(board, end_b):
new_board = list(board)
new_board[end_b + 1] = HORIZONTAL_CAR
new_board[end_b - 2] = BLANK_SPACE
return new_board
def move_truck_up(board, end_a):
new_board = list(board)
new_board[end_a - 6] = VERTICAL_TRUCK
new_board[end_a + 12] = BLANK_SPACE
return new_board
def move_truck_down(board,end_b):
new_board = list(board)
new_board[end_b + 6] = VERTICAL_TRUCK
new_board[end_b - 12] = VERTICAL_TRUCK
return new_board
In [6]:
# find all the pieces within the board encoded as a single string
BLANK_SPACE = '000'
VERTICAL_CAR = '001'
HORIZONTAL_CAR = '011'
VERTICAL_TRUCK = '100'
HORIZONTAL_TRUCK = '010'
board = [board_bit_str[i:i+3] for i in range(0,108,3)]
indexed_board = zip(range(36),board)
neighbors = []
occupied_spaces = [x for x in indexed_board if x[1] != BLANK_SPACE]
for indexed_space in occupied_spaces:
# if horizontal car not on left edge and space to left is blank, move car left one space. Take note if red car is moving.
if indexed_space[1] == HORIZONTAL_CAR and indexed_space[0] %6 > 0 and board[indexed_space[0] - 1] == BLANK_SPACE:
nbr_board = move_car_left(board,indexed_space[0])
if indexed_space[1] == red_car_end_a:
neighbors.append( (nbr_board, red_car_end_a - 1))
else:
neighbors.append( (nbr_board,red_car_end_a))
# if horizontal car not on right edge and space to right is blank, move car right one space. Take note if red car is moving.
if indexed_space[1] == HORIZONTAL_CAR and indexed_space[0] %6 < 4 and board[indexed_space[0] + 2] == BLANK_SPACE:
nbr_board = move_car_right(board,indexed_space[0]+1)
if indexed_space[1] == red_car_end_a:
neighbors.append( (nbr_board, red_car_end_a - 1))
else:
neighbors.append( (nbr_board,red_car_end_a))
# if vertical car not on top edge and space above is blank, move car up
if indexed_space[1] == VERTICAL_CAR and indexed_space[0] > 5 and board[indexed_space[0] - 6] == BLANK_SPACE:
neighbors.append( (move_car_up(board,indexed_space[0]), red_car_end_a) )
# if vertical car not on bottom edge and space below is blank, move car down
if indexed_space[1] == VERTICAL_CAR and indexed_space[0] < 24 and board[indexed_space[0] + 12] == BLANK_SPACE:
neighbors.append( (move_car_down(board,indexed_space[0] + 6), red_car_end_a) )
# if horizontal truck not on left edge and space to left is blank, move truck left
if indexed_space[1] == HORIZONTAL_TRUCK and indexed_space[0]%6 > 0 and board[indexed_space[0]-1] == BLANK_SPACE:
neighbors.append( (move_truck_left(board,indexed_space[0]) ,red_car_end_a ) )
# if horizontal truck not on right edge and space to right is blank, move truck right
if indexed_space[1] == HORIZONTAL_TRUCK and indexed_space[0]%6 < 3 and board[indexed_space[0]+3] == BLANK_SPACE:
neighbors.append( (move_truck_right(board,indexed_space[0]+2), red_car_end_a) )
# if vertical truck not on top edge and space above is blank, move truck up
if indexed_space[1] == VERTICAL_TRUCK and indexed_space[0] > 5 and board[indexed_space[0] - 6] == BLANK_SPACE:
neigbors.append( (move_truck_up(board,indexed_space[0]), red_car_end_a) )
# if vertical truck and not on bottom edge and space below is blank, move truck down
if indexed_space[1] == VERTICAL_TRUCK and indexed_space[0] < 18 and board[indexed_space[0] + 18] == BLANK_SPACE:
neighbors.append( (move_truck_down(board,indexed_space[0] + 12), red_car_end_a) )
In [13]:
HTML(html_table_for_board_bit_string_construction_coloring(board_bit_str,red_car_end_a))
Out[13]:
In [17]:
HTML(html_table_for_board_array_construction_coloring(neighbors[0][0],neighbors[0][1]))
Out[17]:
In [ ]: