Make the previous winner, TunnelMaker, gold.
[troncode.git] / players / TunnelMakerPlayer.py
blob1063dd76ba05b73c21f3c51e243f3c9047865538
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 TunnelMakerPlayer( object ):
12 """An example player to get you started on writing your own. This player
13 creates tunnels to trap opponents."""
15 def __init__( self ):
16 """Do any required setup here."""
17 self.state = MODE_START
19 def GetColour( self ):
20 """Return the colour this light cycle should be displayed on screen."""
21 return ( 218, 165, 32 )
23 def GetName():
24 """Return the name of this player as it should be displayed."""
25 return "Edmund's Tunnel Maker"
26 GetName = staticmethod( GetName )
29 def DebugPrint( self, what ) :
30 # if ( True ) :
31 if ( False ) :
32 print what
34 def IssueMove( self, gameboard, position, new_direction ) :
35 """Move in the direction indicated, if not blocked"""
37 for i in range (4) :
38 if gameboard.GetRelativePixel( position, new_direction, 1, 0 ) == 0:
39 return new_direction
40 else :
41 new_direction = gameboard.TurnLeft( new_direction )
43 self.DebugPrint("Trying " + str(new_direction) + "...")
44 return new_direction
46 def CountOccupied(self, gameboard, position, direction, forward_range, right_range) :
47 """How many of this block are occupied?"""
49 count= 0
50 for df in forward_range :
51 for ds in right_range :
52 if gameboard.GetRelativePixel( position, direction, df, ds ) > 0:
53 count+= 1
55 return count
58 def GetDir( self, position, direction, gameboard ):
59 """Repeatedly create a tunnel to lure an opposing player into, then close it
60 so they can't get out"""
62 if (self.state == MODE_START or self.state == MODE_NORMAL) :
63 # Skim this obstacle
64 if (gameboard.GetRelativePixel( position, direction, 1, 0 ) > 0 and
65 gameboard.GetRelativePixel( position, direction, 1, -1 ) == 0) :
66 self.state = MODE_SEQUENCE
67 self.sequence = SEQUENCE_STEP_AWAY
68 self.DebugPrint("skim")
69 return self.GetDir ( position, direction, gameboard )
71 # Found "the side", so collapse the tunnel
72 if (gameboard.GetRelativePixel( position, direction, 1, 0 ) > 0 or
73 gameboard.GetRelativePixel( position, direction, 1, -1 ) > 0) :
74 self.state = MODE_SEQUENCE
75 self.sequence = COLLAPSE_SEQUENCE;
76 self.DebugPrint("jaws")
77 self.PrintRadar(gameboard, position, direction)
78 return self.GetDir ( position, direction, gameboard )
80 # Too close -> step out
81 if (gameboard.GetRelativePixel( position, direction, 0, 1 ) > 0 or
82 gameboard.GetRelativePixel( position, direction, 0, 2 ) > 0) :
83 self.state = MODE_SEQUENCE
84 self.sequence = SEQUENCE_STEP_AWAY
85 self.DebugPrint("step out")
86 return self.GetDir ( position, direction, gameboard )
88 # Turn right if there's space
89 elif (self.state == MODE_NORMAL and
90 self.CountOccupied( gameboard, position, direction,
91 range(-2, 1), range(1, 4)) == 0) :
92 self.DebugPrint("right")
93 return gameboard.TurnRight( direction )
95 else :
96 return self.IssueMove(gameboard, position, direction)
99 if (self.state == MODE_SEQUENCE) :
100 action = self.sequence[0]
101 self.sequence = self.sequence[1:]
102 self.DebugPrint("S[" + action + "]")
103 if (action == "Z") :
104 self.state = int(self.sequence[0])
105 self.DebugPrint("Z = " + str(self.state))
106 return self.GetDir ( position, direction, gameboard )
107 elif (action == "R") :
108 return gameboard.TurnRight( direction )
109 elif (action == "L") :
110 return gameboard.TurnLeft( direction )
111 elif (action == "-") :
112 return self.IssueMove(gameboard, position, direction);
113 else :
114 print "Gack!"
116 print "no direction!!!\a"
119 def PrintRadar( self, gameboard, position, direction ) :
120 """Debug dump of what we can see"""
122 # print "" + str(gameboard.GetRelativePixel(position, direction, 2, -1)) + "-" + \
123 # str(gameboard.GetRelativePixel(position, direction, 2, 0)) + "-" + \
124 # str(gameboard.GetRelativePixel(position, direction, 2, 1))
126 # print str(gameboard.GetRelativePixel(position, direction, 1, -1)) + "-" + \
127 # str(gameboard.GetRelativePixel(position, direction, 1, 0)) + "-" + \
128 # str(gameboard.GetRelativePixel(position, direction, 1, 1))
129 # print str(gameboard.GetRelativePixel(position, direction, 0, -1)) + "-" + \
130 # "^" + "-" + \
131 # str(gameboard.GetRelativePixel(position, direction, 0, 1))