3 '''This demonstrates controlling a screen oriented application (curses).
4 It starts two instances of gnuchess and then pits them against each other.
17 def __init__(self, engine="/usr/local/bin/gnuchess -a -h 1"):
18 self.child = pexpect.spawn(engine)
19 self.term = ANSI.ANSI()
21 #self.child.expect ('Chess')
22 # if self.child.after != 'Chess':
23 # raise IOError, 'incompatible chess program'
24 #self.term.process_list (self.child.before)
25 #self.term.process_list (self.child.after)
27 self.last_computer_move = ''
29 def read_until_cursor(self, r, c, e=0):
30 '''Eventually something like this should move into the screen class or
31 a subclass. Maybe a combination of pexpect and screen...
33 fout = open('log', 'a')
34 while self.term.cur_r != r or self.term.cur_c != c:
36 k = self.child.read(1, 10)
37 except Exception as e:
38 print 'EXCEPTION, (r,c):(%d,%d)\n' % (self.term.cur_r, self.term.cur_c)
41 fout.write('(r,c):(%d,%d)\n' % (self.term.cur_r, self.term.cur_c))
46 if self.term.cur_r == r and self.term.cur_c == c:
49 print 'DIDNT EVEN HIT.'
53 def expect_region(self):
54 '''This is another method that would be moved into the
60 fout = open('log', 'a')
62 c = self.child.read(1, 10)
64 fout.write('(r,c):(%d,%d)\n' % (self.term.cur_r, self.term.cur_c))
69 def do_move(self, move, e=0):
71 self.read_until_cursor(19, 60, e)
72 self.child.sendline(move)
74 def wait(self, color):
76 r = self.term.get_region(14, 50, 14, 60)[0]
82 def parse_computer_move(self, s):
87 def get_computer_move(self, e=0):
89 self.read_until_cursor(19, 60, e)
91 r = self.term.get_region(17, 50, 17, 62)[0]
92 cm = self.parse_computer_move(r)
97 self.child.sendline('switch')
99 def set_depth(self, depth):
100 self.child.sendline('depth')
101 self.child.expect('depth=')
102 self.child.sendline('%d' % depth)
105 self.child.sendline('quit')
111 fout = open('moves.log', 'a')
119 white.read_until_cursor(19, 60, 1)
125 move_white = white.get_computer_move(1)
126 LOG('move white:' + move_white)
128 black.do_move(move_white)
130 move_black = black.get_computer_move()
131 LOG('move black:' + move_black)
133 white.do_move(move_black, 1)