In [3]:
import os
import pandas as pd
from IPython.display import HTML
In [4]:
os.getcwd()
Out[4]:
In [5]:
os.chdir('..\data')
In [6]:
os.getcwd()
Out[6]:
In [7]:
filepath = '.\game_states_2_cars_2_trucks.csv'
In [8]:
states_2_2 = pd.read_csv(filepath)
In [9]:
states_2_2
Out[9]:
In [10]:
s = """<table>
<tr>
<th>header 1</th>
<th>header 2</th>
</tr>
<tr><td>foo</td>
<td>bar</td>
</tr>
</table>"""
In [11]:
h = HTML(s); h
Out[11]:
In [12]:
x = states_2_2['game_hash_top'][0]
In [13]:
x
Out[13]:
In [14]:
board_top_bit_string = bin(x)[2:].zfill(54)
In [15]:
board_top = [board_top_bit_string[i:i+3] for i in range(0,54,3)]
In [16]:
board_bottom_bit_string = bin(states_2_2['game_hash_bottom'][0])[2:].zfill(54)
In [17]:
board_bottom = [board_bottom_bit_string[i:i+3] for i in range(0,54,3)]
In [18]:
board_bottom
Out[18]:
In [19]:
board_bit_string = board_top + board_bottom
In [20]:
red_car_end_a = states_2_2['red_car_end_a'][0]; red_car_end_a
Out[20]:
In [21]:
str = """
<table>
<tr>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
</tr>
<tr>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
</tr>
<tr>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#FF0000" style="width:30px; height:30px; vertical-align:middle; text-align:center">X</td>
<td bgcolor="#FF0000" style="width:30px; height:30px; vertical-align:middle; text-align:center">X</td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
</tr>
<tr>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
</tr>
<tr>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
</tr>
<tr>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
<td bgcolor="#E6E6E6" style="width:30px; height:30px"></td>
</tr>
</table>
"""
In [22]:
h = HTML(str);h
Out[22]:
Now given an array of bit strings, we need to convert those strings into the table above. We need to know the position of the red
In [23]:
car_symbols = ['Q','A','B','C','D','E','F','G','H','J','K','L']
truck_symbols = ['T','R','W','Z']
car_colors = ['#7FFF00','##7FFFD4','#D2691E','#8B008B','#BDB76B','#8B0000','#FF1493','#1E90FF','#FFD700','#ADFF2F','#CD5C5C','#F0E68C']
truck_colors = ['#F08080','#FFA07A','#FF00FF','#00FA9A']
RED_COLOR = '#FF0000'
RED_SYMBOL = 'X'
BLANK_COLOR = "#E6E6E6"
In [24]:
def html_for_bit_string(board_bit_string,red_car_end_a):
board_symbols = [""] * 36
board_colors = [""] * 36
car_index = 0
truck_index = 0
board_symbols[red_car_end_a] = RED_SYMBOL
board_symbols[red_car_end_a+1] = RED_SYMBOL
board_colors[red_car_end_a] = RED_COLOR
board_colors[red_car_end_a+1] = RED_COLOR
board_string_split = [ board_bit_string[i:i+3] for i in range(0,108,3)]
for i in range(36):
if board_symbols[i] == "":
# empty space
if board_string_split[i] == "000":
board_colors[i] = BLANK_COLOR
# vertical car
if board_string_split[i] == "001":
board_symbols[i] = car_symbols[car_index]
board_symbols[i+6] = car_symbols[car_index]
board_colors[i] = car_colors[car_index]
board_colors[i+6] = car_colors[car_index]
car_index = car_index + 1
# vertical truck
if board_string_split[i] == "010":
board_symbols[i] = truck_symbols[truck_index]
board_symbols[i+6] = truck_symbols[truck_index]
board_symbols[i+12] = truck_symbols[truck_index]
board_colors[i] = truck_colors[truck_index]
board_colors[i+6] = truck_colors[truck_index]
board_colors[i+12] = truck_colors[truck_index]
truck_index = truck_index + 1
# horizontal car
if board_string_split[i] == "011":
board_symbols[i] = car_symbols[car_index]
board_symbols[i+1] = car_symbols[car_index]
board_colors[i] = car_colors[car_index]
board_colors[i+1] = car_colors[car_index]
car_index = car_index + 1
# horizontal truck
if board_string_split[i] == "100":
board_symbols[i] = truck_symbols[truck_index]
board_symbols[i+1] = truck_symbols[truck_index]
board_symbols[i+2] = truck_symbols[truck_index]
board_colors[i] = truck_colors[truck_index]
board_colors[i+1] = truck_colors[truck_index]
board_colors[i+2] = truck_colors[truck_index]
truck_index = truck_index + 1
html_data = zip(board_colors, board_symbols)
html_cells = ['<td bgcolor="%s" style="width:30px; height:30px; vertical-align:middle; text-align:center">%s</td>' %x for x in html_data]
return '<table>' + ''.join(['<tr>' + ''.join(html_cells[i:i+6]) + '</tr>' for i in range(0,35,6)]) + '</table>'
#return board_string_split
In [25]:
HTML(html_for_bit_string( board_bit_string,12))
Out[25]:
In [26]:
board_bottom_bit_string = bin(states_2_2['game_hash_bottom'][0])[2:].zfill(54)
In [27]:
def html_for_index(df_game_states,index):
top_board_bit_str = bin(df_game_states['game_hash_top'][index])[2:].zfill(54)
bottom_board_bit_str = bin(df_game_states['game_hash_bottom'][index])[2:].zfill(54)
board_bit_str = top_board_bit_str + bottom_board_bit_str
red_car_end_a = red_car_end_a = df_game_states['red_car_end_a'][index]
return html_for_bit_string(board_bit_str, red_car_end_a)
In [28]:
HTML(html_for_index(states_2_2,4))
Out[28]:
In [ ]: