OLD | NEW |
(Empty) | |
| 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 PEXPECT LICENSE |
| 7 |
| 8 This license is approved by the OSI and FSF as GPL-compatible. |
| 9 http://opensource.org/licenses/isc-license.txt |
| 10 |
| 11 Copyright (c) 2012, Noah Spurrier <noah@noah.org> |
| 12 PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY |
| 13 PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE |
| 14 COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES. |
| 15 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 16 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 17 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 18 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 21 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 |
| 23 ''' |
| 24 |
| 25 from __future__ import print_function |
| 26 |
| 27 from __future__ import absolute_import |
| 28 |
| 29 import pexpect |
| 30 import ANSI |
| 31 |
| 32 REGEX_MOVE = '(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[C)(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[
C)' |
| 33 REGEX_MOVE_PART = '(?:[0-9]|\x1b\[C)(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[C)' |
| 34 |
| 35 class Chess: |
| 36 |
| 37 def __init__(self, engine = "/usr/local/bin/gnuchess -a -h 1"): |
| 38 self.child = pexpect.spawn (engine) |
| 39 self.term = ANSI.ANSI () |
| 40 |
| 41 # self.child.expect ('Chess') |
| 42 # if self.child.after != 'Chess': |
| 43 # raise IOError, 'incompatible chess program' |
| 44 # self.term.process_list (self.before) |
| 45 # self.term.process_list (self.after) |
| 46 self.last_computer_move = '' |
| 47 def read_until_cursor (self, r,c): |
| 48 fout = open ('log','a') |
| 49 while 1: |
| 50 k = self.child.read(1, 10) |
| 51 self.term.process (k) |
| 52 fout.write ('(r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c)) |
| 53 fout.flush() |
| 54 if self.term.cur_r == r and self.term.cur_c == c: |
| 55 fout.close() |
| 56 return 1 |
| 57 sys.stdout.write (k) |
| 58 sys.stdout.flush() |
| 59 |
| 60 def do_scan (self): |
| 61 fout = open ('log','a') |
| 62 while 1: |
| 63 c = self.child.read(1,10) |
| 64 self.term.process (c) |
| 65 fout.write ('(r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c)) |
| 66 fout.flush() |
| 67 sys.stdout.write (c) |
| 68 sys.stdout.flush() |
| 69 |
| 70 def do_move (self, move): |
| 71 self.read_until_cursor (19,60) |
| 72 self.child.sendline (move) |
| 73 return move |
| 74 |
| 75 def get_computer_move (self): |
| 76 print('Here') |
| 77 i = self.child.expect (['\[17;59H', '\[17;58H']) |
| 78 print(i) |
| 79 if i == 0: |
| 80 self.child.expect (REGEX_MOVE) |
| 81 if len(self.child.after) < 4: |
| 82 self.child.after = self.child.after + self.last_computer_move[3] |
| 83 if i == 1: |
| 84 self.child.expect (REGEX_MOVE_PART) |
| 85 self.child.after = self.last_computer_move[0] + self.child.after |
| 86 print('', self.child.after) |
| 87 self.last_computer_move = self.child.after |
| 88 return self.child.after |
| 89 |
| 90 def switch (self): |
| 91 self.child.sendline ('switch') |
| 92 |
| 93 def set_depth (self, depth): |
| 94 self.child.sendline ('depth') |
| 95 self.child.expect ('depth=') |
| 96 self.child.sendline ('%d' % depth) |
| 97 |
| 98 def quit(self): |
| 99 self.child.sendline ('quit') |
| 100 import sys |
| 101 print('Starting...') |
| 102 white = Chess() |
| 103 white.do_move('b2b4') |
| 104 white.read_until_cursor (19,60) |
| 105 c1 = white.term.get_abs(17,58) |
| 106 c2 = white.term.get_abs(17,59) |
| 107 c3 = white.term.get_abs(17,60) |
| 108 c4 = white.term.get_abs(17,61) |
| 109 fout = open ('log','a') |
| 110 fout.write ('Computer:%s%s%s%s\n' %(c1,c2,c3,c4)) |
| 111 fout.close() |
| 112 white.do_move('c2c4') |
| 113 white.read_until_cursor (19,60) |
| 114 c1 = white.term.get_abs(17,58) |
| 115 c2 = white.term.get_abs(17,59) |
| 116 c3 = white.term.get_abs(17,60) |
| 117 c4 = white.term.get_abs(17,61) |
| 118 fout = open ('log','a') |
| 119 fout.write ('Computer:%s%s%s%s\n' %(c1,c2,c3,c4)) |
| 120 fout.close() |
| 121 white.do_scan () |
| 122 |
| 123 #white.do_move ('b8a6') |
| 124 #move_white = white.get_computer_move() |
| 125 #print 'move white:', move_white |
| 126 |
| 127 sys.exit(1) |
| 128 |
| 129 |
| 130 |
| 131 black = Chess() |
| 132 white = Chess() |
| 133 white.child.expect ('Your move is') |
| 134 white.switch() |
| 135 |
| 136 move_white = white.get_first_computer_move() |
| 137 print('first move white:', move_white) |
| 138 |
| 139 black.do_first_move (move_white) |
| 140 move_black = black.get_first_computer_move() |
| 141 print('first move black:', move_black) |
| 142 |
| 143 white.do_move (move_black) |
| 144 |
| 145 done = 0 |
| 146 while not done: |
| 147 move_white = white.get_computer_move() |
| 148 print('move white:', move_white) |
| 149 |
| 150 black.do_move (move_white) |
| 151 move_black = black.get_computer_move() |
| 152 print('move black:', move_black) |
| 153 |
| 154 white.do_move (move_black) |
| 155 print('tail of loop') |
| 156 |
| 157 g.quit() |
OLD | NEW |