Print a success message when Selfish Git's tests pass.
[troncode.git] / players / FollowerPlayer.py
blobdab2eb4145247911518a81221cbf1560195fec42
1 from troncode_values import *
2 import math
4 class FollowerPlayer( object ):
5 """Follower Player."""
7 # Matrix of
8 # LeftOrRight{-1,0,1} X UpOrDown{-1,0,1} -> Dir{ 0, 1, 2, 3 }
9 whereTable = [ [ 0, 3, 0 ],
10 [ 0, None, 2 ],
11 [ 2, 1, 2 ] ]
13 # Matrix of
14 # CurrentDir{0,1,2,3} X NextDir{0,1,2,3} -> Dir( -1: Left, None: Left/Right, 1: Right )
15 dirTable = [ [ 0, 1, None, -1 ],
16 [ -1, 0, 1, None ],
17 [ None, -1, 0, 1 ],
18 [ 1, None, -1, 0 ] ]
20 def __init__( self ):
21 self.m_step = 0
23 def GetColour( self ):
24 return ( 255, 128, 0 )
26 def GetName():
27 return "Follower"
28 GetName = staticmethod( GetName )
30 def GetDir( self, position, direction, gameboard ):
31 """Stick as close as possible to any wall you find."""
33 pos = gameboard.GetPlayerPositions( )
34 #print "my-position: ", position
35 #print "my-direction: ", direction
36 for p in pos:
37 if p[0] == position:
38 pass
39 else:
40 #print "your-position: ", p[0]
41 #print "your-direction: ", p[1]
42 if p[0][0] < position[0]:
43 leftOrRight = -1
44 elif p[0][0] == position[0]:
45 leftOrRight = 0
46 else:
47 leftOrRight = 1
49 if p[0][1] < position[1]:
50 upOrDown = -1
51 elif p[0][1] == position[1]:
52 upOrDown = 0
53 else:
54 upOrDown = 1
56 #print "LeftOrRight: ", leftOrRight
57 #print "UpOrDown: ", upOrDown
59 where = FollowerPlayer.whereTable[ leftOrRight + 1 ][upOrDown + 1 ]
60 #print "---> where: ", where
61 if where is None:
62 pass
63 elif where == -1 :
64 pass
65 else:
66 dir = FollowerPlayer.dirTable[ direction ][ where ]
67 #print "---> dir: ", dir
68 if dir==-1 or dir is None:
69 pass
70 else:
71 return dir
73 self.m_step += 1
75 ret_dir = direction
77 # Turn left if we can
78 if gameboard.GetRelativePixel( position, direction, 0, -1 ) == 0:
79 ret_dir = gameboard.TurnLeft( ret_dir )
81 # Don't enter tunnels of width 1
82 if ( gameboard.GetRelativePixel( position, ret_dir, 1, 1 ) > 0 and
83 gameboard.GetRelativePixel( position, ret_dir, 1, -1 ) > 0 ):
84 ret_dir = gameboard.TurnRight( ret_dir )
86 # Avoid immediate death by turning right
87 for i in range( 4 ):
88 if gameboard.GetRelativePixel( position, ret_dir, 1, 0 ) == 0:
89 break
90 ret_dir = gameboard.TurnRight( ret_dir )
92 return ret_dir