Updated colours of old players to be white so it's easier to watch the new ones.
[troncode.git] / players / TunnelMaker2Player.py
blob74636f015beba21ce59708dcea536323cab3054d
1 from troncode_values import *
3 MODE_START = 0
4 MODE_NORMAL = 3
5 MODE_SEQUENCE = 5
7 COLLAPSE_SEQUENCE = "LL-RZ3"
8 SEQUENCE_STEP_AWAY = "LR-Z3"
9 SEQUENCE_STEP_IN = "RL-Z3"
11 class TunnelMaker2Player( object ):
12 """Edmund's player to create tunnels to trap opponents."""
14 def __init__( self ):
15 """Do any required setup here."""
16 self.state = MODE_START
18 def GetColour( self ):
19 """Return the colour this light cycle should be displayed on screen."""
20 return ( 0, 255, 255 )
22 def GetName():
23 """Return the name of this player as it should be displayed."""
24 return "TunnelMaker,v2"
25 GetName = staticmethod( GetName )
28 def DebugPrint( self, what ) :
29 # if ( True ) :
30 if ( False ) :
31 print what
33 def IssueMove( self, gameboard, position, new_direction ) :
34 """Move in the direction indicated, if not blocked"""
36 for i in range (4) :
37 if gameboard.GetRelativePixel( position, new_direction, 1, 0 ) == 0:
38 return new_direction
39 else :
40 new_direction = gameboard.TurnLeft( new_direction )
42 self.DebugPrint("Trying " + str(new_direction) + "...")
43 return new_direction
45 def CountOccupied(self, gameboard, position, direction, forward_range, right_range) :
46 """How many of this block are occupied?"""
48 count= 0
49 for df in forward_range :
50 for ds in right_range :
51 if gameboard.GetRelativePixel( position, direction, df, ds ) > 0:
52 count+= 1
54 return count
57 def GetDir( self, position, direction, gameboard ):
58 """Repeatedly create a tunnel to lure an opposing player into, then close it
59 so they can't get out"""
61 if (self.state == MODE_START or self.state == MODE_NORMAL) :
62 # Skim this obstacle
63 if (gameboard.GetRelativePixel( position, direction, 1, 0 ) > 0 and
64 gameboard.GetRelativePixel( position, direction, 1, -1 ) == 0) :
65 self.state = MODE_SEQUENCE
66 self.sequence = SEQUENCE_STEP_AWAY
67 self.DebugPrint("skim")
68 return self.GetDir ( position, direction, gameboard )
70 # Found "the side", so collapse the tunnel
71 if (gameboard.GetRelativePixel( position, direction, 1, 0 ) > 0 or
72 gameboard.GetRelativePixel( position, direction, 1, -1 ) > 0) :
73 self.state = MODE_SEQUENCE
74 self.sequence = COLLAPSE_SEQUENCE;
75 self.DebugPrint("jaws")
76 self.PrintRadar(gameboard, position, direction)
77 return self.GetDir ( position, direction, gameboard )
79 # Too close -> step out
80 if (gameboard.GetRelativePixel( position, direction, 0, 1 ) > 0 or
81 gameboard.GetRelativePixel( position, direction, 0, 2 ) > 0) :
82 self.state = MODE_SEQUENCE
83 self.sequence = SEQUENCE_STEP_AWAY
84 self.DebugPrint("step out")
85 return self.GetDir ( position, direction, gameboard )
87 # Turn right if there's space
88 elif (self.state == MODE_NORMAL and
89 self.CountOccupied( gameboard, position, direction,
90 range(-2, 1), range(1, 4)) == 0) :
91 self.DebugPrint("right")
92 return self.IssueMove(gameboard, position, gameboard.TurnRight( direction ) )
94 else :
95 return self.IssueMove(gameboard, position, direction)
98 if (self.state == MODE_SEQUENCE) :
99 action = self.sequence[0]
100 self.sequence = self.sequence[1:]
101 self.DebugPrint("S[" + action + "]")
102 if (action == "Z") :
103 self.state = int(self.sequence[0])
104 self.DebugPrint("Z = " + str(self.state))
105 return self.GetDir ( position, direction, gameboard )
106 elif (action == "R") :
107 return self.IssueMove(gameboard, position, gameboard.TurnRight( direction ) )
108 elif (action == "L") :
109 return self.IssueMove(gameboard, position, gameboard.TurnLeft( direction ) )
110 elif (action == "-") :
111 return self.IssueMove(gameboard, position, direction);
112 else :
113 print "Gack!"
115 print "no direction!!!\a"
118 def PrintRadar( self, gameboard, position, direction ) :
119 """Debug dump of what we can see"""
121 # print "" + str(gameboard.GetRelativePixel(position, direction, 2, -1)) + "-" + \
122 # str(gameboard.GetRelativePixel(position, direction, 2, 0)) + "-" + \
123 # str(gameboard.GetRelativePixel(position, direction, 2, 1))
125 # print str(gameboard.GetRelativePixel(position, direction, 1, -1)) + "-" + \
126 # str(gameboard.GetRelativePixel(position, direction, 1, 0)) + "-" + \
127 # str(gameboard.GetRelativePixel(position, direction, 1, 1))
128 # print str(gameboard.GetRelativePixel(position, direction, 0, -1)) + "-" + \
129 # "^" + "-" + \
130 # str(gameboard.GetRelativePixel(position, direction, 0, 1))