Home | History | Annotate | Download | only in examples
      1 #!/usr/bin/env python
      2 
      3 '''This demonstrates controlling a screen oriented application (curses).
      4 It starts two instances of gnuchess and then pits them against each other.
      5 '''
      6 
      7 import pexpect
      8 import string
      9 import ANSI
     10 import sys, os, time
     11 
     12 class Chess:
     13 
     14         def __init__(self, engine = "/usr/local/bin/gnuchess -a -h 1"):
     15                 self.child = pexpect.spawn (engine)
     16                 self.term = ANSI.ANSI ()
     17              
     18                 #self.child.expect ('Chess')
     19                 #if self.child.after != 'Chess':
     20                 #        raise IOError, 'incompatible chess program'
     21                 #self.term.process_list (self.child.before)
     22                 #self.term.process_list (self.child.after)
     23 
     24                 self.last_computer_move = ''
     25 
     26         def read_until_cursor (self, r,c, e=0):
     27             '''Eventually something like this should move into the screen class or
     28             a subclass. Maybe a combination of pexpect and screen...
     29             '''
     30             fout = open ('log','a')
     31             while self.term.cur_r != r or self.term.cur_c != c:
     32                 try:
     33                     k = self.child.read(1, 10)
     34                 except Exception, e:
     35                     print 'EXCEPTION, (r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c)
     36                     sys.stdout.flush()
     37                 self.term.process (k)
     38                 fout.write ('(r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c))
     39                 fout.flush()
     40                 if e:
     41                     sys.stdout.write (k)
     42                     sys.stdout.flush()
     43                 if self.term.cur_r == r and self.term.cur_c == c:
     44                     fout.close()
     45                     return 1
     46             print 'DIDNT EVEN HIT.'
     47             fout.close()
     48             return 1
     49 
     50         def expect_region (self):
     51             '''This is another method that would be moved into the
     52             screen class.
     53             '''
     54             pass
     55         def do_scan (self):
     56             fout = open ('log','a')
     57             while 1:
     58                 c = self.child.read(1,10)
     59                 self.term.process (c)
     60                 fout.write ('(r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c))
     61                 fout.flush()
     62                 sys.stdout.write (c)
     63                 sys.stdout.flush()
     64 
     65         def do_move (self, move, e = 0):
     66                 time.sleep(1)
     67                 self.read_until_cursor (19,60, e)
     68                 self.child.sendline (move)
     69 
     70         def wait (self, color):
     71             while 1:
     72                 r = self.term.get_region (14,50,14,60)[0]
     73                 r = r.strip()
     74                 if r == color:
     75                     return
     76                 time.sleep (1)
     77 
     78         def parse_computer_move (self, s):
     79                 i = s.find ('is: ')
     80                 cm = s[i+3:i+9]        
     81                 return cm
     82         def get_computer_move (self, e = 0):
     83                 time.sleep(1)
     84                 self.read_until_cursor (19,60, e)
     85                 time.sleep(1)
     86                 r = self.term.get_region (17,50,17,62)[0]
     87                 cm = self.parse_computer_move (r)
     88                 return cm
     89 
     90         def switch (self):
     91                 print 'switching'
     92                 self.child.sendline ('switch')
     93 
     94         def set_depth (self, depth):
     95                 self.child.sendline ('depth')
     96                 self.child.expect ('depth=')
     97                 self.child.sendline ('%d' % depth)
     98 
     99         def quit(self):
    100                 self.child.sendline ('quit')
    101 
    102 def LOG (s):
    103     print s
    104     sys.stdout.flush ()
    105     fout = open ('moves.log', 'a')
    106     fout.write (s + '\n')
    107     fout.close()
    108 
    109 print 'Starting...'
    110 
    111 black = Chess()
    112 white = Chess()
    113 white.read_until_cursor (19,60,1)
    114 white.switch()
    115 
    116 done = 0
    117 while not done:
    118     white.wait ('Black')
    119     move_white = white.get_computer_move(1)
    120     LOG ( 'move white:'+ move_white )
    121 
    122     black.do_move (move_white)
    123     black.wait ('White')
    124     move_black = black.get_computer_move()
    125     LOG ( 'move black:'+ move_black )
    126    
    127     white.do_move (move_black, 1)
    128 
    129 g.quit()
    130 
    131 
    132