1#!/usr/bin/env python 2 3'''This demonstrates controlling a screen oriented application (curses). 4It starts two instances of gnuchess and then pits them against each other. 5''' 6 7import pexpect 8import string 9import ANSI 10 11REGEX_MOVE = '(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[C)(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[C)' 12REGEX_MOVE_PART = '(?:[0-9]|\x1b\[C)(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[C)' 13 14class Chess: 15 16 def __init__(self, engine = "/usr/local/bin/gnuchess -a -h 1"): 17 self.child = pexpect.spawn (engine) 18 self.term = ANSI.ANSI () 19 20# self.child.expect ('Chess') 21 # if self.child.after != 'Chess': 22 # raise IOError, 'incompatible chess program' 23 # self.term.process_list (self.before) 24 # self.term.process_list (self.after) 25 self.last_computer_move = '' 26 def read_until_cursor (self, r,c): 27 fout = open ('log','a') 28 while 1: 29 k = self.child.read(1, 10) 30 self.term.process (k) 31 fout.write ('(r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c)) 32 fout.flush() 33 if self.term.cur_r == r and self.term.cur_c == c: 34 fout.close() 35 return 1 36 sys.stdout.write (k) 37 sys.stdout.flush() 38 39 def do_scan (self): 40 fout = open ('log','a') 41 while 1: 42 c = self.child.read(1,10) 43 self.term.process (c) 44 fout.write ('(r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c)) 45 fout.flush() 46 sys.stdout.write (c) 47 sys.stdout.flush() 48 49 def do_move (self, move): 50 self.read_until_cursor (19,60) 51 self.child.sendline (move) 52 return move 53 54 def get_computer_move (self): 55 print 'Here' 56 i = self.child.expect (['\[17;59H', '\[17;58H']) 57 print i 58 if i == 0: 59 self.child.expect (REGEX_MOVE) 60 if len(self.child.after) < 4: 61 self.child.after = self.child.after + self.last_computer_move[3] 62 if i == 1: 63 self.child.expect (REGEX_MOVE_PART) 64 self.child.after = self.last_computer_move[0] + self.child.after 65 print '', self.child.after 66 self.last_computer_move = self.child.after 67 return self.child.after 68 69 def switch (self): 70 self.child.sendline ('switch') 71 72 def set_depth (self, depth): 73 self.child.sendline ('depth') 74 self.child.expect ('depth=') 75 self.child.sendline ('%d' % depth) 76 77 def quit(self): 78 self.child.sendline ('quit') 79import sys, os 80print 'Starting...' 81white = Chess() 82white.do_move('b2b4') 83white.read_until_cursor (19,60) 84c1 = white.term.get_abs(17,58) 85c2 = white.term.get_abs(17,59) 86c3 = white.term.get_abs(17,60) 87c4 = white.term.get_abs(17,61) 88fout = open ('log','a') 89fout.write ('Computer:%s%s%s%s\n' %(c1,c2,c3,c4)) 90fout.close() 91white.do_move('c2c4') 92white.read_until_cursor (19,60) 93c1 = white.term.get_abs(17,58) 94c2 = white.term.get_abs(17,59) 95c3 = white.term.get_abs(17,60) 96c4 = white.term.get_abs(17,61) 97fout = open ('log','a') 98fout.write ('Computer:%s%s%s%s\n' %(c1,c2,c3,c4)) 99fout.close() 100white.do_scan () 101 102#white.do_move ('b8a6') 103#move_white = white.get_computer_move() 104#print 'move white:', move_white 105 106sys.exit(1) 107 108 109 110black = Chess() 111white = Chess() 112white.child.expect ('Your move is') 113white.switch() 114 115move_white = white.get_first_computer_move() 116print 'first move white:', move_white 117 118black.do_first_move (move_white) 119move_black = black.get_first_computer_move() 120print 'first move black:', move_black 121 122white.do_move (move_black) 123 124done = 0 125while not done: 126 move_white = white.get_computer_move() 127 print 'move white:', move_white 128 129 black.do_move (move_white) 130 move_black = black.get_computer_move() 131 print 'move black:', move_black 132 133 white.do_move (move_black) 134 print 'tail of loop' 135 136g.quit() 137 138 139