Print a success message when Selfish Git's tests pass.
[troncode.git] / players / FanRandomPlayer.py
blob3cc4dca3cbb4bbef065832ed275159a747032a7a
1 from troncode_values import *
2 import random
3 import math
5 DANGER = 0
6 SAFE = 1
8 class FanRandomPlayer( object ):
10 def __init__( self ):
11 self.step = 0
12 self.last = 0
14 def GetColour( self ):
15 return ( 255, 255, 0 )
17 def GetName():
18 return "Fan Zhang's Random Player!"
19 GetName = staticmethod( GetName )
21 def ProbeEnemies(self, position, gameboard):
22 enemies = gameboard.GetPlayerPositions(position)
23 (my_x, my_y) = position
24 for (epos, edir) in enemies:
25 (x, y) = epos
26 dis = math.sqrt((my_x-x)*(my_x-x) + (my_y-y)*(my_y-y))
27 if (dis < 3):
28 return (DANGER, epos, edir)
29 return (SAFE, None, None)
31 def GetDir( self, position, direction, gameboard ):
33 ret_dir = direction
35 #starting direction
36 (x, y) = position
37 ax = math.fabs(100-x)
38 ay = math.fabs(100-y)
39 if self.step==0 and x < y and x < ay and x < ax:
40 ret_dir = DIR_LEFT
41 elif self.step==0 and y < x and y < ax and y < ay:
42 ret_dir = DIR_DOWN
43 elif self.step==0 and ax < ay and ax < y and ax < x:
44 ret_dir = DIR_RIGHT
45 elif self.step==0 and ay < ax and ay < x and ay < y:
46 ret_dir = DIR_UP
48 #when meet the wall
49 if (x == 0 or x == 199) and y > ay:
50 ret_dir = DIR_DOWN
51 elif (x == 0 or x == 199) and y <= ay:
52 ret_dir = DIR_UP
53 elif (y == 0 or y == 199) and x > ax:
54 ret_dir = DIR_LEFT
55 elif (y == 0 or y == 199) and x <= ax:
56 ret_dir = DIR_RIGHT
58 #normal direction for marching
59 if self.last == 1:
60 ret_dir = gameboard.TurnLeft( ret_dir )
61 self.last = 0
62 else:
63 ret_dir = gameboard.TurnLeft( ret_dir )
64 self.last = 1
66 player = self.ProbeEnemies(position, gameboard)
68 if player[0] == DANGER:
69 ret_dir = gameboard.TurnLeft( ret_dir )
71 # Don't enter tunnels of width 1
72 if ( gameboard.GetRelativePixel( position, ret_dir, 1, 1 ) > 0 and
73 gameboard.GetRelativePixel( position, ret_dir, 1, -1 ) > 0 ):
74 ret_dir = gameboard.TurnRight( ret_dir )
76 # Avoid immediate death by turning right
77 for i in range( 4 ):
78 if gameboard.GetRelativePixel( position, ret_dir, 1, 0 ) == 0:
79 break
80 ret_dir = gameboard.TurnRight( ret_dir )
82 self.step = self.step+1
83 return ret_dir