@@ -46,39 +46,31 @@ def actions(self, state):
4646                if  state [0 ][i ][len (state [0 ][i ])- 1 ]== '' :
4747                    action .append (((i ,len (state [0 ][i ])- 1 ),'left' ))
4848        else :
49-             point = list (state [1 ])
50-             if  state [2 ]== 'up' :
51-                 if  point [0 ]> 0  and  state [0 ][point [0 ]- 1 ][point [1 ]]== ''  or  point [0 ]== 0 :
52-                     action .append ((tuple (point ),'up' ))
53-                 else :
54-                     if  point [1 ]> 0  and  state [0 ][point [0 ]][point [1 ]- 1 ]== ''  or  point [1 ]== 0 :
55-                         action .append ((tuple (point ),'left' ))
56-                     if  point [1 ]< len (state [0 ][0 ])- 1  and  state [0 ][point [0 ]][point [1 ]+ 1 ]== ''  or  point [1 ]== len (state [0 ][0 ])- 1 :
57-                         action .append ((tuple (point ),'right' ))
58-             elif  state [2 ]== 'down' :
59-                 if  point [0 ]< len (state [0 ])- 1  and  state [0 ][point [0 ]+ 1 ][point [1 ]]== ''  or  point [0 ]== len (state [0 ])- 1 :
60-                     action .append ((tuple (point ),'down' ))
61-                 else :
62-                     if  point [1 ]> 0  and  state [0 ][point [0 ]][point [1 ]- 1 ]== ''  or  point [1 ]== 0 :
63-                         action .append ((tuple (point ),'left' ))
64-                     if  point [1 ]< len (state [0 ][0 ])- 1  and  state [0 ][point [0 ]][point [1 ]+ 1 ]== ''  or  point [1 ]== len (state [0 ][0 ])- 1 :
65-                         action .append ((tuple (point ),'right' ))
66-             elif  state [2 ]== 'left' :
67-                 if  point [1 ]> 0  and  state [0 ][point [0 ]][point [1 ]- 1 ]== ''  or  point [1 ]== 0 :
68-                     action .append ((tuple (point ),'left' ))
69-                 else :
70-                     if  point [0 ]< len (state [0 ])- 1  and  state [0 ][point [0 ]+ 1 ][point [1 ]]== ''  or  point [0 ]== len (state [0 ])- 1 :
71-                         action .append ((tuple (point ),'down' ))
72-                     if  point [0 ]< len (state [0 ])- 1  and  state [0 ][point [0 ]- 1 ][point [1 ]]== ''  or  point [0 ]== 0 :
73-                         action .append ((tuple (point ),'up' ))
49+             direction = state [2 ]
50+             current_i , current_j  =  state [1 ]
51+             height  =  len (state [0 ])
52+             width = len (state [0 ][0 ])
53+             # Calculate possible turning directions (90-degree turns) 
54+             if  direction  in  ['up' , 'down' ]:
55+                 possible_dirs  =  ['left' , 'right' ]
7456            else :
75-                 if  point [1 ]< len (state [0 ][0 ])- 1  and  state [0 ][point [0 ]][point [1 ]+ 1 ]== ''  or  point [1 ]== len (state [0 ][0 ])- 1 :
76-                     action .append ((tuple (point ),'right' ))
57+                 possible_dirs  =  ['up' , 'down' ]
58+             
59+             # Validate each possible direction 
60+             for  new_dir  in  possible_dirs :
61+                 di , dj  =  0 , 0 
62+                 if  new_dir  ==  'up' : di  =  - 1 
63+                 elif  new_dir  ==  'down' : di  =  1 
64+                 elif  new_dir  ==  'left' : dj  =  - 1 
65+                 else : dj  =  1 
66+                 
67+                 ni , nj  =  current_i  +  di , current_j  +  dj 
68+                 # Check next tile validity 
69+                 if  0  <=  ni  <  height  and  0  <=  nj  <  width :
70+                     if  state [0 ][ni ][nj ] ==  '' :
71+                         action .append (((current_i , current_j ), new_dir ))
7772                else :
78-                     if  point [0 ]< len (state [0 ])- 1  and  state [0 ][point [0 ]+ 1 ][point [1 ]]== ''  or  point [0 ]== len (state [0 ])- 1 :
79-                         action .append ((tuple (point ),'down' ))
80-                     if  point [0 ]> 0  and  state [0 ][point [0 ]- 1 ][point [1 ]]== ''  or  point [0 ]== 0 :
81-                         action .append ((tuple (point ),'up' ))
73+                     action .append (((current_i , current_j ), new_dir ))
8274        return  action 
8375
8476    def  result (self , state , action ):
0 commit comments