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