Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Side by Side Diff: third_party/pexpect/examples/chess2.py

Issue 1398903002: Add third_party/pexpect (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@end-to-end-test
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/pexpect/examples/chess.py ('k') | third_party/pexpect/examples/chess3.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 import sys
32 import time
33
34 class Chess:
35
36 def __init__(self, engine = "/usr/local/bin/gnuchess -a -h 1"):
37 self.child = pexpect.spawn (engine)
38 self.term = ANSI.ANSI ()
39
40 #self.child.expect ('Chess')
41 #if self.child.after != 'Chess':
42 # raise IOError, 'incompatible chess program'
43 #self.term.process_list (self.child.before)
44 #self.term.process_list (self.child.after)
45
46 self.last_computer_move = ''
47
48 def read_until_cursor (self, r,c, e=0):
49 '''Eventually something like this should move into the screen class or
50 a subclass. Maybe a combination of pexpect and screen...
51 '''
52 fout = open ('log','a')
53 while self.term.cur_r != r or self.term.cur_c != c:
54 try:
55 k = self.child.read(1, 10)
56 except Exception as e:
57 print('EXCEPTION, (r,c):(%d,%d)\n' %(self.term.cur_r, self.t erm.cur_c))
58 sys.stdout.flush()
59 self.term.process (k)
60 fout.write ('(r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c ))
61 fout.flush()
62 if e:
63 sys.stdout.write (k)
64 sys.stdout.flush()
65 if self.term.cur_r == r and self.term.cur_c == c:
66 fout.close()
67 return 1
68 print('DIDNT EVEN HIT.')
69 fout.close()
70 return 1
71
72 def expect_region (self):
73 '''This is another method that would be moved into the
74 screen class.
75 '''
76 pass
77 def do_scan (self):
78 fout = open ('log','a')
79 while 1:
80 c = self.child.read(1,10)
81 self.term.process (c)
82 fout.write ('(r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c ))
83 fout.flush()
84 sys.stdout.write (c)
85 sys.stdout.flush()
86
87 def do_move (self, move, e = 0):
88 time.sleep(1)
89 self.read_until_cursor (19,60, e)
90 self.child.sendline (move)
91
92 def wait (self, color):
93 while 1:
94 r = self.term.get_region (14,50,14,60)[0]
95 r = r.strip()
96 if r == color:
97 return
98 time.sleep (1)
99
100 def parse_computer_move (self, s):
101 i = s.find ('is: ')
102 cm = s[i+3:i+9]
103 return cm
104 def get_computer_move (self, e = 0):
105 time.sleep(1)
106 self.read_until_cursor (19,60, e)
107 time.sleep(1)
108 r = self.term.get_region (17,50,17,62)[0]
109 cm = self.parse_computer_move (r)
110 return cm
111
112 def switch (self):
113 print('switching')
114 self.child.sendline ('switch')
115
116 def set_depth (self, depth):
117 self.child.sendline ('depth')
118 self.child.expect ('depth=')
119 self.child.sendline ('%d' % depth)
120
121 def quit(self):
122 self.child.sendline ('quit')
123
124 def LOG (s):
125 print(s)
126 sys.stdout.flush ()
127 fout = open ('moves.log', 'a')
128 fout.write (s + '\n')
129 fout.close()
130
131 print('Starting...')
132
133 black = Chess()
134 white = Chess()
135 white.read_until_cursor (19,60,1)
136 white.switch()
137
138 done = 0
139 while not done:
140 white.wait ('Black')
141 move_white = white.get_computer_move(1)
142 LOG ( 'move white:'+ move_white )
143
144 black.do_move (move_white)
145 black.wait ('White')
146 move_black = black.get_computer_move()
147 LOG ( 'move black:'+ move_black )
148
149 white.do_move (move_black, 1)
150
151 g.quit()
152
153
OLDNEW
« no previous file with comments | « third_party/pexpect/examples/chess.py ('k') | third_party/pexpect/examples/chess3.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698