In [1]:
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
import os

from IPython.display import SVG
from IPython.display import HTML

#os.chdir('./RushHour/RushHourPy')
os.chdir('../..')

In [2]:
pwd()


Out[2]:
'C:\\Users\\chaithcock\\Documents\\repos\\RushHour\\RushHourPy'

In [3]:
%run constants.py
%run numpy_neighbors.py
%run numpy_display.py

In [15]:
v1 = np.array([blank]*36).reshape(6,6)

v1[2,3:5] = hcar
v1[4,1:3] = hcar
#v1[2:4,0] = vcar
#v1[3:,5] = vtruck
v1


Out[15]:
array([[0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 0, 6, 6, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 6, 6, 0, 0, 0],
       [0, 0, 0, 0, 0, 0]])

In [6]:
HTML(svg_from_state(board_to_int(v1),4))


Out[6]:
XL

In [55]:
#%run numpy_neighbors.py
%run numpy_display.py

svg = svg_neighborhood(board_to_int(v1),4)
HTML(svg)


Out[55]:
XLXLXLXLXL

In [79]:
def svg_neighborhood(v,red_col):
    ''''
        Input: v: integer encoding of ndarray
               red_col: rightmost column (0...5) covered by the red car

        Ouptput:    string consisting of svg markup


    '''


    (all_nbrs,right_nbrs,up_nbrs,left_nbrs,down_nbrs) = state_nbrs(v, int(red_col) )

    ordered_nbrs = [[],[],[],[]]
    ordered_nbrs[0] = sorted(list(right_nbrs), reverse=True)
    ordered_nbrs[1] = sorted(list(up_nbrs))
    ordered_nbrs[2] = sorted(list(left_nbrs))
    ordered_nbrs[3] = sorted(list(down_nbrs))



    # use nested SVG to map these out.

    r = 200
    cx = cy = 200

    
    
    # define lower and upper bounds for 4 regions of unit circle    
    a = [-45+10,45+10,135+10,225+10]
    b = [45-10,135-10,225-10,-45-10]
    counts = [len(right_nbrs), len(up_nbrs),len(left_nbrs),len(down_nbrs)]
    nbrs_x = [ [], [], [], [] ]
    nbrs_y = [ [], [], [], [] ]

    for i in range(4):
        n = counts[i]
        if n > 0:
            c = a[i]
            d = b[i]
            denom = max(1,n-1)
            rads =  [(math.pi/180)*(c+((d-c)/(denom))*i)  for i in range(n)]
            nbrs_x[i] = [ cy + r*math.cos(rad) for rad in rads]
            nbrs_y[i] = [ cy + r*math.sin(rad) for rad in rads]
           
   
    #n = len(all_nbrs)
    #b = 360 - 360/(n-1)
    #a = 0
    #rads = [(math.pi/180)*(a+((b-a)/(n-1))*i)  for i in range(n)]
    #nbrs_x = [ cy + r*math.cos(rad) for rad in rads]
    #nbrs_y = [ cy + r*math.sin(rad) for rad in rads]

    svg = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="600" height="600">'

    for i in range(4):
        for j in range( counts[i]):
            x = nbrs_x[i][j]
            y = nbrs_y[i][j]
            states,red_col_y = ordered_nbrs[i][j]

            svg += '<line stroke="black" stroke-width="2" x1="%d" x2="%d" y1="%d" y2="%d" />'%(cx+45,x+45,cy+45,y+45)

            svg += '<svg x="%d" y = "%d">'%(x,y)
            svg += '<g transform = "scale(.5)">'
            svg += svg_from_state(states,red_col_y)
            svg += '</g></svg>'


    svg += '<svg x="%d" y = "%d">'%(cx,cy)
    svg += '<g transform = "scale(.5)">'
    svg += svg_from_state(v,red_col)
    svg += '</g></svg>'


    svg += '</svg>'



    return svg


v1 = np.array([blank]*36).reshape(6,6)

v1[2,3:5] = hcar 
v1_red_car = 4

v1[4,1:3] = hcar
v1[2:4,0] = vcar
#v1[3:,5] = vtruck

svg = svg_neighborhood(board_to_int(v1),4)
HTML(svg)


Out[79]:
XLKXLKXLKXLKXLKXLKXLK

In [60]:
(all_nbrs,right_nbrs,up_nbrs,left_nbrs,down_nbrs) = state_nbrs(board_to_int(v1), 4 )

In [61]:
all_nbrs


Out[61]:
{(972777526759784448, 5),
 (7782220157002186752, 4),
 (7782220214078275584, 4),
 (62257761256017494016, 3)}

In [73]:
right_nbrs, sorted(right_nbrs,key=lambda state: state[0],reverse=True),left_nbrs


Out[73]:
({(972777526759784448, 5), (7782220157002186752, 4)},
 [(7782220157002186752, 4), (972777526759784448, 5)],
 {(7782220214078275584, 4), (62257761256017494016, 3)})

In [69]:
html = '<table><tr>'
html += '<td>' + svg_from_state(board_to_int(v1),int(4)) + '</td>'
for x,red_col in sorted(left_nbrs,reverse=True):
    html += '<td> '+ svg_from_state(x,red_col) + '<td>'
html += '</tr></table>'
HTML(html)


Out[69]:
XL XL XL

In [35]:
i = 0
n = counts[i]
if n > 0:
    rads = [(math.pi/180)*(a[i]+((b[i]-a[i])/(n+1))*i)  for i in range(n)]
n,a[i],b[i],rads


Out[35]:
(2, -35, 35, [-0.6108652381980153, 1.3671745807288913])

In [36]:
i = 2
n = counts[i]
if n > 0:
    rads = [(math.pi/180)*(a[i]+((b[i]-a[i])/(n+1))*i)  for i in range(n)]
    nbrs_x[i] = [ cy + r*math.cos(rad) for rad in rads]
    nbrs_y[i] = [ cy + r*math.sin(rad) for rad in rads]
n,a[i],b[i],rads


Out[36]:
(2, 145, 215, [-0.6108652381980153, 1.3671745807288913])

In [42]:
a_i = a[0]
b_i = b[0]
n = 2
[(math.pi/180)*(a_i+((b_i-a_i)/(n+1))*i)  for i in range(n)],a,b
nbrs_x[i] = [ cy + r*math.cos(rad) for rad in rads]
nbrs_y[i] = [ cy + r*math.sin(rad) for rad in rads]


Out[42]:
([-0.6108652381980153, -0.20362174606600514],
 [-35, 55, 145, 235],
 [35, 125, 215, -55])

In [44]:
a_i = a[2]
b_i = b[2]
n = 2
[(math.pi/180)*(a_i+((b_i-a_i)/(n+1))*i)  for i in range(n)],a,b


Out[44]:
([2.530727415391778, 2.937970907523788],
 [-35, 55, 145, 235],
 [35, 125, 215, -55])

In [4]:
HTML(svg_from_state(276745027360252628944933584371200, 2))


Out[4]:
XLKJHZWRT

In [5]:
HTML(svg_neighborhood(276745027360252628944933584371200, 2))


Out[5]:
XLKJHZWRTXLKJHZWRTXLKJHZWRTXLKJHZWRT

In [28]:
# Function Inputs

v = 276745027360252628944933584371200
red_col = 2

(all_nbrs,right_nbrs,up_nbrs,left_nbrs,down_nbrs) = state_nbrs(v, int(red_col) )

ordered_nbrs = [[],[],[],[]]
ordered_nbrs[0] = sorted(list(right_nbrs), reverse=True)
ordered_nbrs[1] = sorted(list(up_nbrs))
ordered_nbrs[2] = sorted(list(left_nbrs))
ordered_nbrs[3] = sorted(list(down_nbrs))



# use nested SVG to map these out.

r = 200
cx = cy = 200

    

# define lower and upper bounds for 4 regions of unit circle    
a = [-45+10,45+10,135+10,225+10]
b = [45-10,135-10,225-10,-45-10]
counts = [len(right_nbrs), len(up_nbrs),len(left_nbrs),len(down_nbrs)]
nbrs_x = [ [], [], [], [] ]
nbrs_y = [ [], [], [], [] ]

for i in range(4):
    n = counts[i]
    if n > 0:
        c = a[i]
        d = b[i]
        denom = max(1,n-1)
        rads =  [(math.pi/180)*(c+((d-c)/(denom))*i)  for i in range(n)]
        nbrs_x[i] = [ cy + r*math.cos(rad) for rad in rads]
        nbrs_y[i] = [ cy + r*math.sin(rad) for rad in rads]

        
        
svg = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="600" height="600">'



for i in range(4):
    for j in range( counts[i]):
        x = nbrs_x[i][j]
        y = nbrs_y[i][j]
        states,red_col_y = ordered_nbrs[i][j]

        svg += '<line stroke="black" stroke-width="2" x1="%d" x2="%d" y1="%d" y2="%d" />'%(cx+45,x+45,cy+45,y+45)

        svg += '<svg x="%d" y = "%d">'%(x,y)
        svg += '<g transform = "scale(.5)">'
        svg += svg_from_state(states,red_col_y)
        svg += '</g></svg>'


svg += '<svg x="%d" y = "%d">'%(cx,cy)
svg += '<g transform = "scale(.5)">'
svg += svg_from_state(v,red_col)
svg += '</g></svg>'


svg += '</svg>'



HTML(svg)


Out[28]:
XLKJHZWRTXLKJHZWRTXLKJHZWRTXLKJHZWRT

In [23]:
rads


Out[23]:
[-0.6108652381980153, 0.0, 0.6108652381980153]

In [21]:
b


Out[21]:
[35, 125, 215, -55]

In [ ]: