First attempt at a matlab link.
[progcomp10.git] / src / link / externAgent.py
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.
3
4 Licensed under an MIT-style license: see the LICENSE file for details.
5 '''
6
7 from uccProgComp import BaseAgent, LearningAgent, RandomAttack
8 from rpsconst import *
9 #from pexpect import pexpect
10
11 import sys, subprocess
12
13 class externAgent (BaseAgent):
14
15     def __init__ (self, externName):
16         BaseAgent.__init__(self)
17         try:
18             self.process = subprocess.Popen(externName, stdin=subprocess.PIPE, 
19                                             stdout=subprocess.PIPE, stderr=subprocess.PIPE,
20                                             universal_newlines=True)
21         except Exception, e:
22             print ("Error spawning \"%s\": " % externName), e
23             
24     def stringToItem( self, str ):
25         if str == "Rock":
26             return Rock
27         elif str == "Paper":
28             return Paper
29         elif str == "Scissors":
30             return Scissors
31         else:
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 );
35             return None
36
37     def itemToString( self, item ):
38         return rpsStrings[item]
39         
40         #if item == Rock:
41         #    return "Rock"
42         #elif item == Paper:
43         #    return "Paper"
44         #elif item == Scissors:
45         #    return "Scissors"
46         #else:
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)
51         
52     def resultToString( self, result ):
53         return adtStrings[result]
54     
55         #if result == Attacker:
56         #    return "Attacker"
57         #elif result == Defender:
58         #    return "Defender"
59         #elif result == Tie:
60         #    return "Tie"
61         #else:
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)
66         
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()
71         #print result
72         try:
73             attack, bluff = self.stringToItem( result[1] ), self.stringToItem( result[2] )
74             return attack, bluff
75         except:
76             #agent is insane
77             print "Agent is insane:", self
78             pass
79         
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()
84         #print result
85         try:
86             defence = self.stringToItem( result[1] )
87             return defence
88         except Exception, e:
89             #agent is insane
90             print "Agent is insane:", self, ":", e
91             pass
92
93     def Results (self, foe, isInstigatedByYou, winner, attItem, defItem, bluffItem, pointDelta):
94         
95         BaseAgent.Results (self, foe, isInstigatedByYou, winner, attItem, 
96                            defItem, bluffItem, pointDelta)
97         
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),
103                              "\r\n" ] )
104         
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)
110         #print string
111         
112         self.process.stdin.write ( string )
113         self.process.stdout.readline() # read and discard (should be "OK")
114         
115     def __del__(self):
116         try:
117             self.process.communicate( "BYE\r\n" )
118         except Exception, e:
119             print "Error in BYE:", self, ":", e
120             
121         try:
122             self.process.kill()
123         except:
124             None

UCC git Repository :: git.ucc.asn.au