1 | #!/usr/bin/env python
|
---|
2 | '''This demonstrates controlling a screen oriented application (curses).
|
---|
3 | It starts two instances of gnuchess and then pits them against each other.
|
---|
4 | '''
|
---|
5 | import pexpect
|
---|
6 | import string
|
---|
7 | import ANSI
|
---|
8 |
|
---|
9 | REGEX_MOVE = '(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[C)(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[C)'
|
---|
10 | REGEX_MOVE_PART = '(?:[0-9]|\x1b\[C)(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[C)'
|
---|
11 |
|
---|
12 | class 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 | while 1:
|
---|
26 | self.child.read(1, 60)
|
---|
27 | self.term.process (c)
|
---|
28 | if self.term.cur_r == r and self.term.cur_c == c:
|
---|
29 | return 1
|
---|
30 |
|
---|
31 | def do_first_move (self, move):
|
---|
32 | self.child.expect ('Your move is')
|
---|
33 | self.child.sendline (move)
|
---|
34 | self.term.process_list (self.before)
|
---|
35 | self.term.process_list (self.after)
|
---|
36 | return move
|
---|
37 |
|
---|
38 | def do_move (self, move):
|
---|
39 | read_until_cursor (19,60)
|
---|
40 | #self.child.expect ('\[19;60H')
|
---|
41 | self.child.sendline (move)
|
---|
42 | print 'do_move' move
|
---|
43 | return move
|
---|
44 |
|
---|
45 | def get_first_computer_move (self):
|
---|
46 | self.child.expect ('My move is')
|
---|
47 | self.child.expect (REGEX_MOVE)
|
---|
48 | # print '', self.child.after
|
---|
49 | return self.child.after
|
---|
50 |
|
---|
51 | def get_computer_move (self):
|
---|
52 | print 'Here'
|
---|
53 | i = self.child.expect (['\[17;59H', '\[17;58H'])
|
---|
54 | print i
|
---|
55 | if i == 0:
|
---|
56 | self.child.expect (REGEX_MOVE)
|
---|
57 | if len(self.child.after) < 4:
|
---|
58 | self.child.after = self.child.after + self.last_computer_move[3]
|
---|
59 | if i == 1:
|
---|
60 | self.child.expect (REGEX_MOVE_PART)
|
---|
61 | self.child.after = self.last_computer_move[0] + self.child.after
|
---|
62 | print '', self.child.after
|
---|
63 | self.last_computer_move = self.child.after
|
---|
64 | return self.child.after
|
---|
65 |
|
---|
66 | def switch (self):
|
---|
67 | self.child.sendline ('switch')
|
---|
68 |
|
---|
69 | def set_depth (self, depth):
|
---|
70 | self.child.sendline ('depth')
|
---|
71 | self.child.expect ('depth=')
|
---|
72 | self.child.sendline ('%d' % depth)
|
---|
73 |
|
---|
74 | def quit(self):
|
---|
75 | self.child.sendline ('quit')
|
---|
76 | import sys, os
|
---|
77 | print 'Starting...'
|
---|
78 | white = Chess()
|
---|
79 | white.child.echo = 1
|
---|
80 | white.child.expect ('Your move is')
|
---|
81 | white.set_depth(2)
|
---|
82 | white.switch()
|
---|
83 |
|
---|
84 | move_white = white.get_first_computer_move()
|
---|
85 | print 'first move white:', move_white
|
---|
86 |
|
---|
87 | white.do_move ('e7e5')
|
---|
88 | move_white = white.get_computer_move()
|
---|
89 | print 'move white:', move_white
|
---|
90 | white.do_move ('f8c5')
|
---|
91 | move_white = white.get_computer_move()
|
---|
92 | print 'move white:', move_white
|
---|
93 | white.do_move ('b8a6')
|
---|
94 | move_white = white.get_computer_move()
|
---|
95 | print 'move white:', move_white
|
---|
96 |
|
---|
97 | sys.exit(1)
|
---|
98 |
|
---|
99 |
|
---|
100 |
|
---|
101 | black = Chess()
|
---|
102 | white = Chess()
|
---|
103 | white.child.expect ('Your move is')
|
---|
104 | white.switch()
|
---|
105 |
|
---|
106 | move_white = white.get_first_computer_move()
|
---|
107 | print 'first move white:', move_white
|
---|
108 |
|
---|
109 | black.do_first_move (move_white)
|
---|
110 | move_black = black.get_first_computer_move()
|
---|
111 | print 'first move black:', move_black
|
---|
112 |
|
---|
113 | white.do_move (move_black)
|
---|
114 |
|
---|
115 | done = 0
|
---|
116 | while not done:
|
---|
117 | move_white = white.get_computer_move()
|
---|
118 | print 'move white:', move_white
|
---|
119 |
|
---|
120 | black.do_move (move_white)
|
---|
121 | move_black = black.get_computer_move()
|
---|
122 | print 'move black:', move_black
|
---|
123 |
|
---|
124 | white.do_move (move_black)
|
---|
125 | print 'tail of loop'
|
---|
126 |
|
---|
127 | g.quit()
|
---|
128 |
|
---|
129 |
|
---|