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 import sys, subprocess
13 class externAgent (BaseAgent):
15 def __init__ (self, externName):
16 BaseAgent.__init__(self)
18 self.process = subprocess.Popen(externName, stdin=subprocess.PIPE,
19 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
20 universal_newlines=True)
22 print ("Error spawning \"%s\": " % externName), e
24 def stringToItem( self, str ):
29 elif str == "Scissors":
32 # Something has gone wrong!
33 print "Error: tried to convert \"%s\" into an item!" % str
34 raise ValueError( "tried to convert \"%s\" into an item!" % str );
37 def itemToString( self, item ):
38 return rpsStrings[item]
44 #elif item == Scissors:
47 # # Something has gone wrong!
48 # print "Error: tried to convert '%d' to Rock/Paper/Scissors string!" % item
49 # # raise an exception
50 # raise ValueError("tried to convert '%d' to Rock/Paper/Scissors string!" % item)
52 def resultToString( self, result ):
53 return adtStrings[result]
55 #if result == Attacker:
57 #elif result == Defender:
62 # # Something has gone wrong!
63 # print "Error: tried to convert '%d' to Attacker/Defender/Tie string!" % result
64 # # raise an exception
65 # raise ValueError("tried to convert '%d' to Attacker/Defender/Tie string!" % result)
67 def Attack (self, foe):
68 self.process.stdin.write ( ' '.join( ["ATTACK", repr(foe), "\r\n"] ) )
69 #print >>sys.stderr, self.process.stderr.readlines()
70 result = self.process.stdout.readline().split()
73 attack, bluff = self.stringToItem( result[1] ), self.stringToItem( result[2] )
77 print "Agent is insane:", self
80 def Defend (self, foe, bluff):
81 self.process.stdin.write ( ' '.join( ["DEFEND", repr(foe), self.itemToString( bluff ), "\r\n"] ) )
82 #print >>sys.stderr, self.process.stderr.readlines()
83 result = self.process.stdout.readline().split()
86 defence = self.stringToItem( result[1] )
90 print "Agent is insane:", self, ":", e
93 def Results (self, foe, isInstigatedByYou, winner, attItem, defItem, bluffItem, pointDelta):
95 BaseAgent.Results (self, foe, isInstigatedByYou, winner, attItem,
96 defItem, bluffItem, pointDelta)
98 string = ' '.join( [ "RESULTS", repr(foe), repr(isInstigatedByYou),
99 self.resultToString(winner),
100 self.itemToString( attItem ),
101 self.itemToString( defItem ),
102 self.itemToString( bluffItem ), repr(pointDelta),
105 #string = "RESULTS %s %s %s %s %s %s %d\r\n" % (foe, isInstigatedByYou,
106 # self.resultToString(winner),
107 # self.itemToString( attItem ),
108 # self.itemToString( defItem ),
109 # self.itemToString( bluffItem ), pointDelta)
112 self.process.stdin.write ( string )
113 self.process.stdout.readline() # read and discard (should be "OK")
117 self.process.communicate( "BYE\r\n" )
119 print "Error in BYE:", self, ":", e