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()
72 attack, bluff = self.stringToItem( result[1] ), self.stringToItem( result[2] )
76 print "Agent is insane:", self
79 def Defend (self, foe, bluff ):
80 self.process.stdin.write ( ' '.join( ["DEFEND", repr(foe), self.itemToString( bluff ), "\r\n"] ) )
81 #print >>sys.stderr, self.process.stderr.readlines()
82 result = self.process.stdout.readline().split()
84 defence = self.stringToItem( result[1] )
88 print "Agent is insane:", self
91 def Results (self, foe, isInstigatedByYou, winner, attItem, defItem, bluffItem, pointDelta):
93 BaseAgent.Results (self, foe, isInstigatedByYou, winner, attItem,
94 defItem, bluffItem, pointDelta)
96 string = ' '.join( [ "RESULTS", repr(foe), repr(isInstigatedByYou),
97 self.resultToString(winner),
98 self.itemToString( attItem ),
99 self.itemToString( defItem ),
100 self.itemToString( bluffItem ), repr(pointDelta),
103 #string = "RESULTS %s %s %s %s %s %s %d\r\n" % (foe, isInstigatedByYou,
104 # self.resultToString(winner),
105 # self.itemToString( attItem ),
106 # self.itemToString( defItem ),
107 # self.itemToString( bluffItem ), pointDelta)
110 self.process.stdin.write ( string )
111 self.process.stdout.readline() # read and discard (should be "OK")
115 self.process.communicate( "BYE\r\n" )
117 print "Error in BYE:", self, ":", e