Work towards C SDK (#11). Frenchie is still broken.
[progcomp10.git] / src / link / externAgent.py
1 '''externAgent.py - a bot shell for talking to external I/O bots.
2 Written by Daniel Axtens <[email protected]> for the UCC Programming Competition in 2010.
3
4 Licensed under an MIT-style license: see the LICENSE file for details.
5 '''
6
7 from uccProgComp import BaseAgent, LearningAgent, RandomAttack
8 from rpsconst import *
9 from pexpect import pexpect
10
11 class externAgent (BaseAgent):
12     
13     def __init__ (self, externName):
14         BaseAgent.__init__(self)
15         self.process = pexpect.spawn(externName)
16         self.process.delaybeforesend=0
17         
18         
19     def stringToItem( self, str ):
20         if str == "Rock":
21             return Rock
22         elif str == "Paper":
23             return Paper
24         elif str == "Scissors":
25             return Scissors
26         else:
27             # Something has gone wrong!
28             print "Error: tried to convert \"%s\" into an item!" % str
29             return None
30
31     def itemToString( self, item ):
32         if item == Rock:
33             return "Rock"
34         elif item == Paper:
35             return "Paper"
36         elif item == Scissors:
37             return "Scissors"
38         else:
39             # Something has gone wrong!
40             print "Error: tried to convert '%d' to Rock/Paper/Scissors string!" % item
41         
42     def Attack (self, foe):
43         self.process.sendline( "ATTACK %s" % foe )
44         self.process.expect( "ATTACKING (.+) (.+)\r\n" )
45         attack, bluff = self.process.match.groups()
46         attack, bluff = attack.strip(), bluff.strip()
47         return self.stringToItem(attack), self.stringToItem(bluff)
48         
49     def Defend( self, foe, bluff ):
50         #print "DEFEND %s %s" % (foe, self.itemToString(bluff))
51         self.process.sendline( "DEFEND %s %s" % (foe, self.itemToString(bluff) ) )
52         self.process.expect( "DEFENDING (.+)\r\n" )
53         #print '------------------ ', self.process.match.groups()[0].strip()
54         defence = self.process.match.groups()[0].strip()
55         return self.stringToItem(defence)
56         
57     def __del__(self):
58         self.process.close(True)

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