3 #NOTE: The -u option is required for unbuffered stdin/stdout.
4 # If stdin/stdout are buffered, the manager program will not recieve any messages and assume that the agent has timed out.
7 asmodeus.py - A sample Stratego AI for the UCC Programming Competition 2012
9 Written in python, the slithery language
11 author Sam Moore (matches) [SZM]
12 website http://matches.ucc.asn.au/stratego
13 email progcomp@ucc.asn.au or matches@ucc.asn.au
14 git git.ucc.asn.au/progcomp2012.git
17 from basic_python import *
22 class Asmodeus(BasicAI):
23 " A slightly more advanced python based AI who calculates the optimum score for each move "
25 #sys.stderr.write("Asmodeus initialised...\n")
26 BasicAI.__init__(self)
27 self.riskScores = {'1' : 0.01 , '2' : 0.05 , '3' : 0.15 , '4' : 0.2, '5' : 0.2, '6' : 0.25, '7' : 0.25, '8' : 0.01 , '9' : 0.4, 's' : 0.01}
28 self.bombScores = {'1' : 0.0 , '2' : 0.0 , '3' : 0.05 , '4' : 0.1, '5' : 0.3, '6' : 0.4, '7' : 0.5, '8' : 1 , '9' : 0.6, 's' : 0.1}
29 self.flagScores = {'1' : 1.0 , '2' : 1.0 , '3' : 1.0 , '4' : 1.0, '5' : 1.0, '6' : 1.0, '7' : 1, '8' : 1.0 , '9' : 1.0, 's' : 1.0}
30 self.suicideScores = {'1' : 0.0 , '2' : 0.0 , '3' : 0.0, '4' : 0.0, '5' : 0.0, '6' : 0.05, '7' : 0.1, '8' : 0.0 , '9' : 0.0, 's' : 0.0}
31 self.killScores = {'1' : 1.0 , '2' : 0.9 , '3' : 0.8 , '4' : 0.5, '5' : 0.5, '6' : 0.5, '7' : 0.4, '8' : 0.9 , '9' : 0.6, 's' : 0.9}
34 #sys.stderr.write("Asmodeus MakingMove...\n")
35 "Over-rides the default BasicAI.MakeMove function"
39 for unit in self.units:
40 if unit.mobile() == False:
43 for enemy in self.enemyUnits:
46 path = PathFinder().pathFind((unit.x, unit.y), (enemy.x, enemy.y), self.board)
48 #sys.stderr.write("Computed path: " + str(path) + "\n")
49 if path == False or len(path) <= 0:
51 score = self.CalculateScore(unit, enemy)
53 score = float(score / float(len(path) + 1))
54 moveList.append([unit, path, enemy, score])
58 if len(moveList) <= 0:
59 #sys.stderr.write("NO Moves!\n")
60 return BasicAI.MakeMove(self)
62 moveList.sort(key = lambda e : e[len(e)-1], reverse=True)
64 #sys.stderr.write("Chosen move is: " + str(moveList[0][0].x) + " " + str(moveList[0][0].y) + " " + moveList[0][1][0] + " (targeting enemy with rank " + moveList[0][2].rank + " at position " + str(moveList[0][2].x) + " " + str(moveList[0][2].y) + " (my rank " + moveList[0][0].rank+")\n")
65 print str(moveList[0][0].x) + " " + str(moveList[0][0].y) + " " + moveList[0][1][0]
68 def CalculateScore(self, attacker, defender):
69 if defender.rank == '?':
70 return self.riskScores[attacker.rank]
71 elif defender.rank == 'B':
72 return self.bombScores[attacker.rank]
73 elif defender.rank == 'F':
74 return self.flagScores[attacker.rank]
75 elif defender.valuedRank() < attacker.valuedRank() or defender.rank == '1' and attacker.rank == 's':
76 return self.killScores[defender.rank]
78 return self.suicideScores[attacker.rank]
80 if __name__ == "__main__":
83 while asmodeus.MoveCycle():