]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - third_party/Python/module/pexpect-2.4/examples/chess2.py
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / third_party / Python / module / pexpect-2.4 / examples / chess2.py
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
7 import pexpect
8 import string
9 import ANSI
10 import sys
11 import os
12 import time
13
14
15 class Chess:
16
17     def __init__(self, engine="/usr/local/bin/gnuchess -a -h 1"):
18         self.child = pexpect.spawn(engine)
19         self.term = ANSI.ANSI()
20
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)
26
27         self.last_computer_move = ''
28
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...
32         '''
33         fout = open('log', 'a')
34         while self.term.cur_r != r or self.term.cur_c != c:
35             try:
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)
39                 sys.stdout.flush()
40             self.term.process(k)
41             fout.write('(r,c):(%d,%d)\n' % (self.term.cur_r, self.term.cur_c))
42             fout.flush()
43             if e:
44                 sys.stdout.write(k)
45                 sys.stdout.flush()
46             if self.term.cur_r == r and self.term.cur_c == c:
47                 fout.close()
48                 return 1
49         print 'DIDNT EVEN HIT.'
50         fout.close()
51         return 1
52
53     def expect_region(self):
54         '''This is another method that would be moved into the
55         screen class.
56         '''
57         pass
58
59     def do_scan(self):
60         fout = open('log', 'a')
61         while True:
62             c = self.child.read(1, 10)
63             self.term.process(c)
64             fout.write('(r,c):(%d,%d)\n' % (self.term.cur_r, self.term.cur_c))
65             fout.flush()
66             sys.stdout.write(c)
67             sys.stdout.flush()
68
69     def do_move(self, move, e=0):
70         time.sleep(1)
71         self.read_until_cursor(19, 60, e)
72         self.child.sendline(move)
73
74     def wait(self, color):
75         while True:
76             r = self.term.get_region(14, 50, 14, 60)[0]
77             r = r.strip()
78             if r == color:
79                 return
80             time.sleep(1)
81
82     def parse_computer_move(self, s):
83         i = s.find('is: ')
84         cm = s[i + 3:i + 9]
85         return cm
86
87     def get_computer_move(self, e=0):
88         time.sleep(1)
89         self.read_until_cursor(19, 60, e)
90         time.sleep(1)
91         r = self.term.get_region(17, 50, 17, 62)[0]
92         cm = self.parse_computer_move(r)
93         return cm
94
95     def switch(self):
96         print 'switching'
97         self.child.sendline('switch')
98
99     def set_depth(self, depth):
100         self.child.sendline('depth')
101         self.child.expect('depth=')
102         self.child.sendline('%d' % depth)
103
104     def quit(self):
105         self.child.sendline('quit')
106
107
108 def LOG(s):
109     print s
110     sys.stdout.flush()
111     fout = open('moves.log', 'a')
112     fout.write(s + '\n')
113     fout.close()
114
115 print 'Starting...'
116
117 black = Chess()
118 white = Chess()
119 white.read_until_cursor(19, 60, 1)
120 white.switch()
121
122 done = 0
123 while not done:
124     white.wait('Black')
125     move_white = white.get_computer_move(1)
126     LOG('move white:' + move_white)
127
128     black.do_move(move_white)
129     black.wait('White')
130     move_black = black.get_computer_move()
131     LOG('move black:' + move_black)
132
133     white.do_move(move_black, 1)
134
135 g.quit()