X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=src%2Flink%2FexternAgent.py;fp=src%2Flink%2FexternAgent.py;h=0000000000000000000000000000000000000000;hb=041f195b018bb74676017dd0a09f8a079611ba4b;hp=6112482fac8da3de59d794b200d7bb9e055bbdf0;hpb=699697dcaa730ccc26cd570cbf8975e2591344c0;p=progcomp10.git diff --git a/src/link/externAgent.py b/src/link/externAgent.py deleted file mode 100644 index 6112482..0000000 --- a/src/link/externAgent.py +++ /dev/null @@ -1,122 +0,0 @@ -'''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 - -import sys, subprocess - -class externAgent (BaseAgent): - - def __init__ (self, externName): - BaseAgent.__init__(self) - try: - self.process = subprocess.Popen(externName, stdin=subprocess.PIPE, - stdout=subprocess.PIPE, stderr=subprocess.PIPE, - universal_newlines=True) - except Exception, e: - print ("Error spawning \"%s\": " % externName), e - - 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 - raise ValueError( "tried to convert \"%s\" into an item!" % str ); - return None - - def itemToString( self, item ): - return rpsStrings[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 - # # raise an exception - # raise ValueError("tried to convert '%d' to Rock/Paper/Scissors string!" % item) - - def resultToString( self, result ): - return adtStrings[result] - - #if result == Attacker: - # return "Attacker" - #elif result == Defender: - # return "Defender" - #elif result == Tie: - # return "Tie" - #else: - # # Something has gone wrong! - # print "Error: tried to convert '%d' to Attacker/Defender/Tie string!" % result - # # raise an exception - # raise ValueError("tried to convert '%d' to Attacker/Defender/Tie string!" % result) - - def Attack (self, foe): - self.process.stdin.write ( ' '.join( ["ATTACK", repr(foe), "\r\n"] ) ) - #print >>sys.stderr, self.process.stderr.readlines() - result = self.process.stdout.readline().split() - try: - attack, bluff = self.stringToItem( result[1] ), self.stringToItem( result[2] ) - return attack, bluff - except: - #agent is insane - print "Agent is insane:", self - pass - - def Defend (self, foe, bluff ): - self.process.stdin.write ( ' '.join( ["DEFEND", repr(foe), self.itemToString( bluff ), "\r\n"] ) ) - #print >>sys.stderr, self.process.stderr.readlines() - result = self.process.stdout.readline().split() - try: - defence = self.stringToItem( result[1] ) - return defence - except: - #agent is insane - print "Agent is insane:", self - pass - - def Results (self, foe, isInstigatedByYou, winner, attItem, defItem, bluffItem, pointDelta): - - BaseAgent.Results (self, foe, isInstigatedByYou, winner, attItem, - defItem, bluffItem, pointDelta) - - string = ' '.join( [ "RESULTS", repr(foe), repr(isInstigatedByYou), - self.resultToString(winner), - self.itemToString( attItem ), - self.itemToString( defItem ), - self.itemToString( bluffItem ), repr(pointDelta), - "\r\n" ] ) - - #string = "RESULTS %s %s %s %s %s %s %d\r\n" % (foe, isInstigatedByYou, - # self.resultToString(winner), - # self.itemToString( attItem ), - # self.itemToString( defItem ), - # self.itemToString( bluffItem ), pointDelta) - #print string - - self.process.stdin.write ( string ) - self.process.stdout.readline() # read and discard (should be "OK") - - def __del__(self): - try: - self.process.communicate( "BYE\r\n" ) - except Exception, e: - print "Error in BYE:", self, ":", e - - try: - self.process.kill() - except: - None \ No newline at end of file