Updated colours of old players to be white so it's easier to watch the new ones.
[troncode.git] / players / BobsPlayer.py
blob752a0f23dec2710de83a988ef9e56d7a30b4cf54
1 from troncode_values import *
3 TRAP_GAP = 5
5 class BobsPlayer( object ):
7 def __init__( self ):
8 self.counter = 0
9 self.trap_gap = TRAP_GAP
11 def GetColour( self ):
12 return ( 255, 255, 255 )
14 def GetName():
15 return "Bobs Player"
16 GetName = staticmethod( GetName )
18 def GetAnyWallsInRangeAhead( self, position, direction, gameboard ):
19 rval = 0
20 for i in range( 1, self.trap_gap ):
21 if gameboard.GetRelativePixel( position, direction, i, 0 ) > 0:
22 rval = rval+1
24 return rval
26 def GetAnyWallsInRangeRight( self, position, direction, gameboard ):
27 rval = 0
29 for i in range( -1, 1 ):
30 for j in range( 1, self.trap_gap ):
31 if gameboard.GetRelativePixel( position, direction, i, j ) > 0:
32 rval = rval+1
34 return rval
36 def GetAnyWallsInRangeLeft( self, position, direction, gameboard ):
37 rval = 0
39 for i in range( -1, 1 ):
40 for j in range( -1, -self.trap_gap ):
41 if gameboard.GetRelativePixel( position, direction, i, j ) > 0:
42 rval = rval+1
44 return rval
46 def GetDir( self, position, direction, gameboard ):
47 """Turn Left Player but turn right occasionally"""
49 ret_dir = direction
51 density_ahead = self.GetAnyWallsInRangeAhead( position, direction, gameboard )
52 density_left = self.GetAnyWallsInRangeLeft( position, ret_dir, gameboard )
53 density_right = self.GetAnyWallsInRangeRight( position, ret_dir, gameboard )
55 #Avoid hitting something straight ahead up to self.trap_gap pixels away
56 if ( density_ahead > 0 ):
57 if ( density_left == 0 ) :
58 ret_dir = gameboard.TurnLeft( direction )
59 self.counter=self.counter+1
61 elif ( density_right == 0 ) :
62 ret_dir = gameboard.TurnRight( direction )
63 self.counter=self.counter-1
65 elif ( density_left < density_right ) :
66 ret_dir = gameboard.TurnLeft( direction )
67 self.counter=self.counter+1
69 elif ( density_right < density_left ) :
70 ret_dir = gameboard.TurnRight( direction )
71 self.counter=self.counter-1
73 elif ( self.counter > 2 and density_right == 0 ) :
74 ret_dir = gameboard.TurnRight( direction )
76 # if we're stuck in a box of our own making, best to try and stay alive as long as possible..
77 elif ( self.counter > 4 or self.counter < -4 ) and ( self.trap_gap > 2 ) :
78 self.trap_gap=self.trap_gap-1
80 return ret_dir