X-Git-Url: https://git.ucc.asn.au/?p=progcomp10.git;a=blobdiff_plain;f=src%2Flink%2FexternAgent.py;fp=src%2Flink%2FexternAgent.py;h=10118c1d5f53fbb21884f2bed1ad206a5ee52de8;hp=0000000000000000000000000000000000000000;hb=cd42b53c196672694396e695ae17fd94ba7d58b4;hpb=e9a8105a8f22404f4ac550d79954eaa6b7f5d8ff diff --git a/src/link/externAgent.py b/src/link/externAgent.py new file mode 100644 index 0000000..10118c1 --- /dev/null +++ b/src/link/externAgent.py @@ -0,0 +1,56 @@ +'''externAgent.py - a bot shell for talking to external I/O bots. +Written by Daniel Axtens for the UCC Programming Competition in 2010. + +Licensed under an MIT-style license: see the LICENSE file for details. +''' + +from uccProgComp import BaseAgent, LearningAgent, RandomAttack +from rpsconst import * +from pexpect import pexpect + +class externAgent (BaseAgent): + + def __init__ (self, externName): + BaseAgent.__init__(self) + self.process = pexpect.spawn(externName) + self.process.delaybeforesend=0 + + + def stringToItem( self, str ): + if str == "Rock": + return Rock + elif str == "Paper": + return Paper + elif str == "Scissors": + return Scissors + else: + # Something has gone wrong! + print "Error: tried to convert \"%s\" into an item!" % str + return None + + def itemToString( self, item ): + if item == Rock: + return "Rock" + elif item == Paper: + return "Paper" + elif item == Scissors: + return "Scissors" + else: + # Something has gone wrong! + print "Error: tried to convert '%d' to Rock/Paper/Scissors string!" % item + + def Attack (self, foe): + self.process.sendline( "ATTACK %s" % foe ) + self.process.expect( "ATTACKING (.+) (.+)\n" ) + attack, bluff = self.process.match.groups() + attack, bluff = attack.strip(), bluff.strip() + return self.stringToItem(attack), self.stringToItem(bluff) + + def Defend( self, foe, bluff ): + #print "DEFEND %s %s" % (foe, self.itemToString(bluff)) + self.process.sendline( "DEFEND %s %s" % (foe, self.itemToString(bluff) ) ) + self.process.expect( "DEFENDING (.+)" ) + #print '------------------ ', self.process.match.groups()[0].strip() + defence = self.process.match.groups()[0].strip() + return self.stringToItem(defence) + \ No newline at end of file