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

Side by Side Diff: third_party/pexpect/examples/chess.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/cgishell.cgi ('k') | third_party/pexpect/examples/chess2.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
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
48 def read_until_cursor (self, r,c):
49 while 1:
50 self.child.read(1, 60)
51 self.term.process (c)
52 if self.term.cur_r == r and self.term.cur_c == c:
53 return 1
54
55 def do_first_move (self, move):
56 self.child.expect ('Your move is')
57 self.child.sendline (move)
58 self.term.process_list (self.before)
59 self.term.process_list (self.after)
60 return move
61
62 def do_move (self, move):
63 self.read_until_cursor (19,60)
64 self.child.sendline (move)
65 return move
66
67 def get_first_computer_move (self):
68 self.child.expect ('My move is')
69 self.child.expect (REGEX_MOVE)
70 return self.child.after
71
72 def get_computer_move (self):
73 print('Here')
74 i = self.child.expect (['\[17;59H', '\[17;58H'])
75 print(i)
76 if i == 0:
77 self.child.expect (REGEX_MOVE)
78 if len(self.child.after) < 4:
79 self.child.after = self.child.after + self.last_computer_move[3]
80 if i == 1:
81 self.child.expect (REGEX_MOVE_PART)
82 self.child.after = self.last_computer_move[0] + self.child.after
83 print('', self.child.after)
84 self.last_computer_move = self.child.after
85 return self.child.after
86
87 def switch (self):
88 self.child.sendline ('switch')
89
90 def set_depth (self, depth):
91 self.child.sendline ('depth')
92 self.child.expect ('depth=')
93 self.child.sendline ('%d' % depth)
94
95 def quit(self):
96 self.child.sendline ('quit')
97 import sys
98 print('Starting...')
99 white = Chess()
100 white.child.echo = 1
101 white.child.expect ('Your move is')
102 white.set_depth(2)
103 white.switch()
104
105 move_white = white.get_first_computer_move()
106 print('first move white:', move_white)
107
108 white.do_move ('e7e5')
109 move_white = white.get_computer_move()
110 print('move white:', move_white)
111 white.do_move ('f8c5')
112 move_white = white.get_computer_move()
113 print('move white:', move_white)
114 white.do_move ('b8a6')
115 move_white = white.get_computer_move()
116 print('move white:', move_white)
117
118 sys.exit(1)
119
120
121
122 black = Chess()
123 white = Chess()
124 white.child.expect ('Your move is')
125 white.switch()
126
127 move_white = white.get_first_computer_move()
128 print('first move white:', move_white)
129
130 black.do_first_move (move_white)
131 move_black = black.get_first_computer_move()
132 print('first move black:', move_black)
133
134 white.do_move (move_black)
135
136 done = 0
137 while not done:
138 move_white = white.get_computer_move()
139 print('move white:', move_white)
140
141 black.do_move (move_white)
142 move_black = black.get_computer_move()
143 print('move black:', move_black)
144
145 white.do_move (move_black)
146 print('tail of loop')
147
148 g.quit()
OLDNEW
« no previous file with comments | « third_party/pexpect/examples/cgishell.cgi ('k') | third_party/pexpect/examples/chess2.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698