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