source: CMTManagement/utils/chess3.py @ 421

Last change on this file since 421 was 421, checked in by garonne, 17 years ago
  • Property svn:executable set to *
File size: 3.7 KB
Line 
1#!/usr/bin/env python
2'''This demonstrates controlling a screen oriented application (curses).
3It starts two instances of gnuchess and then pits them against each other.
4'''
5import pexpect
6import string
7import ANSI
8
9REGEX_MOVE = '(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[C)(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[C)'
10REGEX_MOVE_PART = '(?:[0-9]|\x1b\[C)(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[C)'
11
12class 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')
77import sys, os
78print 'Starting...'
79white = Chess()
80white.do_move('b2b4')
81white.read_until_cursor (19,60)
82c1 = white.term.get_abs(17,58)
83c2 = white.term.get_abs(17,59)
84c3 = white.term.get_abs(17,60)
85c4 = white.term.get_abs(17,61)
86fout = open ('log','a')
87fout.write ('Computer:%s%s%s%s\n' %(c1,c2,c3,c4))
88fout.close()
89white.do_move('c2c4')
90white.read_until_cursor (19,60)
91c1 = white.term.get_abs(17,58)
92c2 = white.term.get_abs(17,59)
93c3 = white.term.get_abs(17,60)
94c4 = white.term.get_abs(17,61)
95fout = open ('log','a')
96fout.write ('Computer:%s%s%s%s\n' %(c1,c2,c3,c4))
97fout.close()
98white.do_scan ()
99
100#white.do_move ('b8a6')
101#move_white = white.get_computer_move()
102#print 'move white:', move_white
103
104sys.exit(1)
105
106
107
108black = Chess()
109white = Chess()
110white.child.expect ('Your move is')
111white.switch()
112
113move_white = white.get_first_computer_move()
114print 'first move white:', move_white
115
116black.do_first_move (move_white)
117move_black = black.get_first_computer_move()
118print 'first move black:', move_black
119
120white.do_move (move_black)
121
122done = 0
123while 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
134g.quit()
135
136
Note: See TracBrowser for help on using the repository browser.