In [3]:
import os
import pandas as pd
from IPython.display import HTML

In [4]:
os.getcwd()


Out[4]:
'C:\\Users\\cliff\\repositories\\RushHour\\RushHourPy\\analysis_prototypes'

In [5]:
os.chdir('..\data')

In [6]:
os.getcwd()


Out[6]:
'C:\\Users\\cliff\\repositories\\RushHour\\RushHourPy\\data'

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]:
game_number comb_class_id game_hash_top game_hash_bottom is_goal_state optimal_neighbor red_car_end_a connected_component_id topo_class_hash degree
0 1 36 7760490508431362 0 False NaN 12 NaN 51673825284 NaN
1 2 36 7760392260530176 0 False NaN 12 NaN 55968792576 NaN
2 3 36 7760357967900672 0 False NaN 12 NaN 55968792576 NaN
3 4 36 7760353681321984 0 False NaN 12 NaN 55968792576 NaN
4 5 36 7760353337390080 70368744177664 False NaN 12 NaN 51673827328 NaN
5 6 36 7760353145499648 0 False NaN 12 NaN 55968792576 NaN
6 7 36 7760353102508160 8796093022208 False NaN 12 NaN 51673825536 NaN
7 8 36 7760353073147920 1099511627776 False NaN 12 NaN 51673825312 NaN
8 9 36 7760353069477890 137438953472 False NaN 12 NaN 51673825284 NaN
9 10 36 7760353068955936 0 False NaN 12 NaN 52344913920 NaN
10 11 36 7760353068954624 70369012613120 False NaN 12 NaN 51673827328 NaN
11 12 36 7760353068953892 0 False NaN 12 NaN 52344913920 NaN
12 13 36 7760353068953728 8796126576640 False NaN 12 NaN 51673825536 NaN
13 14 36 7760353068953616 1099515822080 False NaN 12 NaN 51673825312 NaN
14 15 36 7760353068953602 137439477760 False NaN 12 NaN 51673825284 NaN
15 16 36 7760353068953600 10273836649938944 False NaN 12 NaN 51740934144 NaN
16 17 36 7760353068953600 4503616807305216 False NaN 12 NaN 51673956352 NaN
17 18 36 7760353068953600 1284229581242368 False NaN 12 NaN 51740934144 NaN
18 19 36 7760353068953600 562952100913152 False NaN 12 NaN 51673841664 NaN
19 20 36 7760353068953600 160528697655296 False NaN 12 NaN 51740934144 NaN
20 21 36 7760353068953600 70369012614144 False NaN 12 NaN 51673827328 NaN
21 22 36 7760353068953600 20066087206912 False NaN 12 NaN 51740934144 NaN
22 23 36 7760353068953600 8796126576768 False NaN 12 NaN 51673825536 NaN
23 24 36 7760353068953600 1099515822096 False NaN 12 NaN 51673825312 NaN
24 25 36 7760353068953600 137439477762 False NaN 12 NaN 51673825284 NaN
25 26 36 7760353068953600 39191576576 False NaN 12 NaN 51682213888 NaN
26 27 36 7760353068953600 4898947072 False NaN 12 NaN 51682213888 NaN
27 28 36 7760353068953600 612368384 False NaN 12 NaN 51682213888 NaN
28 29 36 7760353068953600 76546048 False NaN 12 NaN 51682213888 NaN
29 30 36 7760353068953600 149504 False NaN 12 NaN 51674873856 NaN
... ... ... ... ... ... ... ... ... ... ...
116620 116621 36 27 4906174464 True NaN 16 NaN 147849216 NaN
116621 116622 36 27 4906043648 True NaN 16 NaN 147849216 NaN
116622 116623 36 27 4906027296 True NaN 16 NaN 147849216 NaN
116623 116624 36 27 4906025252 True NaN 16 NaN 147849216 NaN
116624 116625 36 27 4901193736 True NaN 16 NaN 143654920 NaN
116625 116626 36 27 4901062920 True NaN 16 NaN 143654920 NaN
116626 116627 36 27 4899358721 True NaN 16 NaN 143654913 NaN
116627 116628 36 27 4899227905 True NaN 16 NaN 143654913 NaN
116628 116629 36 27 4899211553 True NaN 16 NaN 143654913 NaN
116629 116630 36 27 4899060000 True NaN 16 NaN 144179200 NaN
116630 116631 36 27 4899057956 True NaN 16 NaN 144179200 NaN
116631 116632 36 27 4898961188 True NaN 16 NaN 144179200 NaN
116632 116633 36 27 4899096792 True NaN 16 NaN 144179200 NaN
116633 116634 36 27 4899096603 True NaN 16 NaN 144179200 NaN
116634 116635 36 27 4898965787 True NaN 16 NaN 144179200 NaN
116635 116636 36 27 612780033 True NaN 16 NaN 143654913 NaN
116636 116637 36 27 612649217 True NaN 16 NaN 143654913 NaN
116637 116638 36 27 612632865 True NaN 16 NaN 143654913 NaN
116638 116639 36 27 612481312 True NaN 16 NaN 144179200 NaN
116639 116640 36 27 612479268 True NaN 16 NaN 144179200 NaN
116640 116641 36 27 612382500 True NaN 16 NaN 144179200 NaN
116641 116642 36 27 612518104 True NaN 16 NaN 144179200 NaN
116642 116643 36 27 612517915 True NaN 16 NaN 144179200 NaN
116643 116644 36 27 612387099 True NaN 16 NaN 144179200 NaN
116644 116645 36 27 76658976 True NaN 16 NaN 144179200 NaN
116645 116646 36 27 76656932 True NaN 16 NaN 144179200 NaN
116646 116647 36 27 76560164 True NaN 16 NaN 144179200 NaN
116647 116648 36 27 76695768 True NaN 16 NaN 144179200 NaN
116648 116649 36 27 76695579 True NaN 16 NaN 144179200 NaN
116649 116650 36 27 76564763 True NaN 16 NaN 144179200 NaN

116650 rows × 10 columns


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]:
header 1 header 2
foo bar

In [12]:
x = states_2_2['game_hash_top'][0]

In [13]:
x


Out[13]:
7760490508431362

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]:
['000',
 '000',
 '000',
 '000',
 '000',
 '000',
 '000',
 '000',
 '000',
 '000',
 '000',
 '000',
 '000',
 '000',
 '000',
 '000',
 '000',
 '000']

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]:
12

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]:
X X

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]:
XX

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]:
QQTTT
R
XXR
R

In [ ]: