Red is not feminine enough. Made it pink.
[progcomp2012.git] / agents / asmodeus / asmodeus.py
1 #!/usr/bin/python -u
2
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.
5
6 '''
7  asmodeus.py - A sample Stratego AI for the UCC Programming Competition 2012
8
9  Written in python, the slithery language 
10
11  author Sam Moore (matches) [SZM]
12  website http://matches.ucc.asn.au/stratego
13  email [email protected] or [email protected]
14  git git.ucc.asn.au/progcomp2012.git
15 '''
16
17 from basic_python import *
18 from path import *
19
20
21
22 class Asmodeus(BasicAI):
23         " A slightly more advanced python based AI who calculates the optimum score for each move "
24         def __init__(self):
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}    
32
33         def MakeMove(self):
34                 #sys.stderr.write("Asmodeus MakingMove...\n")
35                 "Over-rides the default BasicAI.MakeMove function"
36
37                 moveList = []
38
39                 for unit in self.units:
40                         if unit.mobile() == False:
41                                 continue
42
43                         for enemy in self.enemyUnits:
44                                 if enemy == unit:
45                                         continue
46                                 path = PathFinder().pathFind((unit.x, unit.y), (enemy.x, enemy.y), self.board)
47
48                                 #sys.stderr.write("Computed path: " + str(path) + "\n")
49                                 if path == False or len(path) <= 0:
50                                         continue
51                                 score = self.CalculateScore(unit, enemy)
52
53                                 score = float(score / float(len(path) + 1))
54                                 moveList.append([unit, path, enemy, score])
55
56
57                 
58                 if len(moveList) <= 0:
59                         #sys.stderr.write("NO Moves!\n")
60                         return BasicAI.MakeMove(self)
61
62                 moveList.sort(key = lambda e : e[len(e)-1], reverse=True)
63
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]
66                 return True     
67
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]
77                 else:
78                         return self.suicideScores[attacker.rank]
79                 
80 if __name__ == "__main__":
81         asmodeus = Asmodeus()
82         if asmodeus.Setup():
83                 while asmodeus.MoveCycle():
84                         pass
85

UCC git Repository :: git.ucc.asn.au