source: CMTManagement/utils/chess.py@ 421

Last change on this file since 421 was 421, checked in by garonne, 18 years ago
  • Property svn:executable set to *
File size: 3.3 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 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')
76import sys, os
77print 'Starting...'
78white = Chess()
79white.child.echo = 1
80white.child.expect ('Your move is')
81white.set_depth(2)
82white.switch()
83
84move_white = white.get_first_computer_move()
85print 'first move white:', move_white
86
87white.do_move ('e7e5')
88move_white = white.get_computer_move()
89print 'move white:', move_white
90white.do_move ('f8c5')
91move_white = white.get_computer_move()
92print 'move white:', move_white
93white.do_move ('b8a6')
94move_white = white.get_computer_move()
95print 'move white:', move_white
96
97sys.exit(1)
98
99
100
101black = Chess()
102white = Chess()
103white.child.expect ('Your move is')
104white.switch()
105
106move_white = white.get_first_computer_move()
107print 'first move white:', move_white
108
109black.do_first_move (move_white)
110move_black = black.get_first_computer_move()
111print 'first move black:', move_black
112
113white.do_move (move_black)
114
115done = 0
116while 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
127g.quit()
128
129
Note: See TracBrowser for help on using the repository browser.