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.
4 Licensed under an MIT-style license: see the LICENSE file for details.
7 from uccProgComp import BaseAgent, LearningAgent, RandomAttack
9 from pexpect import pexpect
11 class externAgent (BaseAgent):
13 def __init__ (self, externName):
14 BaseAgent.__init__(self)
15 self.process = pexpect.spawn(externName)
16 self.process.delaybeforesend=0
19 def stringToItem( self, str ):
24 elif str == "Scissors":
27 # Something has gone wrong!
28 print "Error: tried to convert \"%s\" into an item!" % str
31 def itemToString( self, item ):
36 elif item == Scissors:
39 # Something has gone wrong!
40 print "Error: tried to convert '%d' to Rock/Paper/Scissors string!" % item
42 def Attack (self, foe):
43 self.process.sendline( "ATTACK %s" % foe )
44 self.process.expect( "ATTACKING (.+) (.+)\n" )
45 attack, bluff = self.process.match.groups()
46 attack, bluff = attack.strip(), bluff.strip()
47 return self.stringToItem(attack), self.stringToItem(bluff)
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 (.+)" )
53 #print '------------------ ', self.process.match.groups()[0].strip()
54 defence = self.process.match.groups()[0].strip()
55 return self.stringToItem(defence)