Cleanup bot code.
authorW. Trevor King <wking@drexel.edu>
Tue, 7 Dec 2010 17:51:24 +0000 (12:51 -0500)
committerW. Trevor King <wking@drexel.edu>
Tue, 7 Dec 2010 17:51:24 +0000 (12:51 -0500)
bots/hole_bot.py [new file with mode: 0755]
bots/p1.py [new file with mode: 0755]
bots/p2.py [new file with mode: 0755]
p1.py [deleted file]
p2.py [deleted file]

diff --git a/bots/hole_bot.py b/bots/hole_bot.py
new file mode 100755 (executable)
index 0000000..5274c69
--- /dev/null
@@ -0,0 +1,80 @@
+#!/usr/bin/python
+
+"""Go all (or not) depending on whats in the hole.
+"""
+
+import logging
+import os
+import sys
+
+
+class RunComplete (Exception):
+    """Raised after receiving the `END` tag to abort execution.
+    """
+    pass
+
+
+class HoleBot (object):
+    def __init__(self, stdin=None, stdout=None, log_level=logging.WARN):
+        if stdin == None:
+            stdin = sys.stdin
+        self.stdin = stdin
+        if stdout == None:
+            stdout = sys.stdout
+        self.stdout = stdout
+        log_file = '%s_%d.log' % (self.__class__.__name__, os.getpid())
+        logging.basicConfig(filename=log_file, level=log_level)
+        self.log = logging
+
+    def run(self):
+        state = self._initial_state()
+        while True:
+            line = self.stdin.readline().strip()
+            if not line:
+                continue  # skip blank lines
+            tag,type,data = self._parse(line)
+            try:
+                self._process(tag, type, data, state)
+            except RunComplete:
+                break
+
+    def _parse(self, line):
+        self.log.debug('read: %s' % line)
+        fields = line.split(' ')
+        try:
+            tag  = fields[0]
+            type = fields[1]
+            data = fields[2:]
+        except:
+            self.log.debug('tag only: %s' % str(fields))
+            tag  = fields[0]
+            type = data = None
+        self.log.debug('parsed tag %s, type %s, data %s'
+                       % (repr(tag), repr(type), repr(data)))
+        return (tag, type, data)
+
+    def _process(self, tag, type, data, state):
+        if tag == 'END':
+            raise RunComplete
+        elif tag == 'MOVE':
+            bet = self._bet(state)
+            self.log.info('bet %s' % bet)
+            self.stdout.write('%s\n' % bet)
+            self.stdout.flush()
+        elif tag == 'INFO' and type == 'HOLE':
+            self._process_hole(data, state)
+            self.log.info('processed hole %s, state: %s' % (data, state))
+
+    def _initial_state(self):
+        return {'all_in': False}
+
+    def _bet(self, state):
+        if state['all_in']:
+            bet = 'A'
+        else:
+            bet = 'f'
+        state['all_in'] = False
+        return bet
+
+    def _process_hole(self, data, state):
+        raise NotImplementedError
diff --git a/bots/p1.py b/bots/p1.py
new file mode 100755 (executable)
index 0000000..b6e7b74
--- /dev/null
@@ -0,0 +1,17 @@
+#!/usr/bin/python
+
+import logging
+
+from hole_bot import HoleBot
+
+
+class AceBot (HoleBot):
+    """Go all in whenever there is an ace in the hole.
+    """
+    def _process_hole(self, hole, state):
+        state['all_in'] = 'A' in [card[0] for card in hole]
+
+
+if __name__ == '__main__':
+    b = AceBot()#log_level=logging.INFO)
+    b.run()
diff --git a/bots/p2.py b/bots/p2.py
new file mode 100755 (executable)
index 0000000..49c348d
--- /dev/null
@@ -0,0 +1,17 @@
+#!/usr/bin/python
+
+import logging
+
+from hole_bot import HoleBot
+
+
+class PairBot (HoleBot):
+    """Go all in on any pocket pair.
+    """
+    def _process_hole(self, hole, state):
+        state['all_in'] = hole[0][0] == hole[1][0]
+
+
+if __name__ == '__main__':
+    b = PairBot()#log_level=logging.INFO)
+    b.run()
diff --git a/p1.py b/p1.py
deleted file mode 100755 (executable)
index 7b9e762..0000000
--- a/p1.py
+++ /dev/null
@@ -1,40 +0,0 @@
-#!/usr/bin/python
-import sys
-import os
-
-IN, OUT = sys.stdin, sys.stdout
-
-FOUT  = open("test_anyAce.txt",'a')
-allin = False
-
-while True:
-   std_in = IN.readline().strip()
-   if std_in:
-        FOUT.write(std_in+'\n')
-
-        std_in = std_in.split(' ')
-        try   : 
-            tag  = std_in[0]
-            type = std_in[1]
-            data = std_in[2:]
-        except: 
-            #FOUT.write(str(std_in))
-            tag  = std_in[0]
-
-        #FOUT.write(tag+' '+type+' '+str(data)+' '+'\n')
-        if tag == "END" : FOUT.close(); exit()
-        if tag == "MOVE": 
-            # You must return a valid bet here
-            if allin: OUT.write("A\n")
-            else    : OUT.write("f\n")
-
-            OUT.flush()
-            allin = False
-
-        elif tag == "INFO" and type == "HOLE":
-            # Handle new information
-            # Save info to a file and go all in on any Ace
-            h1, h2 = data[-1], data[-2]
-            #FOUT.write(h1+h2+'\n')
-
-            if h1[0]=='A' or h2[0]=='A':  allin = True
diff --git a/p2.py b/p2.py
deleted file mode 100755 (executable)
index 6377776..0000000
--- a/p2.py
+++ /dev/null
@@ -1,46 +0,0 @@
-#!/usr/bin/python
-import sys
-import os
-
-IN, OUT = sys.stdin, sys.stdout
-
-FOUT  = open("test_anyPair.txt",'w')
-allin = False
-
-while True:
-   std_in = IN.readline().strip()
-   if std_in:
-        std_in = std_in.split(' ')
-        try   : 
-            tag  = std_in[0]
-            type = std_in[1]
-            data = std_in[2:]
-        except: 
-            FOUT.write(str(std_in))
-            tag  = std_in[0]
-
-        #FOUT.write(tag+' '+type+' '+str(data)+' '+'\n')
-
-        if tag == "MOVE": 
-            # You must return a valid bet here
-            if allin: OUT.write("A\n")
-            else    : OUT.write("f\n")
-            OUT.flush()
-            allin = False
-
-        elif tag == "INFO" and type == "HOLE":
-            # Handle new information
-            # Save info to a file and go all in on any pair
-            h1, h2 = data[-1], data[-2]
-
-            if h1[0] == h2[0] and h1[0]=="A":  allin = True
-
-                
-            
-
-            
-        
-
-
-
-