3 '''This demonstrates controlling a screen oriented application (curses).
4 It starts two instances of gnuchess and then pits them against each other.
14 def __init__(self, engine = "/usr/local/bin/gnuchess -a -h 1"):
15 self.child = pexpect.spawn (engine)
16 self.term = ANSI.ANSI ()
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)
24 self.last_computer_move = ''
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...
30 fout = open ('log','a')
31 while self.term.cur_r != r or self.term.cur_c != c:
33 k = self.child.read(1, 10)
35 print 'EXCEPTION, (r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c)
38 fout.write ('(r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c))
43 if self.term.cur_r == r and self.term.cur_c == c:
46 print 'DIDNT EVEN HIT.'
50 def expect_region (self):
51 '''This is another method that would be moved into the
56 fout = open ('log','a')
58 c = self.child.read(1,10)
60 fout.write ('(r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c))
65 def do_move (self, move, e = 0):
67 self.read_until_cursor (19,60, e)
68 self.child.sendline (move)
70 def wait (self, color):
72 r = self.term.get_region (14,50,14,60)[0]
78 def parse_computer_move (self, s):
82 def get_computer_move (self, e = 0):
84 self.read_until_cursor (19,60, e)
86 r = self.term.get_region (17,50,17,62)[0]
87 cm = self.parse_computer_move (r)
92 self.child.sendline ('switch')
94 def set_depth (self, depth):
95 self.child.sendline ('depth')
96 self.child.expect ('depth=')
97 self.child.sendline ('%d' % depth)
100 self.child.sendline ('quit')
105 fout = open ('moves.log', 'a')
106 fout.write (s + '\n')
113 white.read_until_cursor (19,60,1)
119 move_white = white.get_computer_move(1)
120 LOG ( 'move white:'+ move_white )
122 black.do_move (move_white)
124 move_black = black.get_computer_move()
125 LOG ( 'move black:'+ move_black )
127 white.do_move (move_black, 1)