Home | History | Annotate | Download | only in examples
      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 
      7 import pexpect
      8 import string
      9 import ANSI
     10 
     11 REGEX_MOVE = '(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[C)(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[C)'
     12 REGEX_MOVE_PART = '(?:[0-9]|\x1b\[C)(?:[a-z]|\x1b\[C)(?:[0-9]|\x1b\[C)'
     13 
     14 class 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')
     78 import sys, os
     79 print 'Starting...'
     80 white = Chess()
     81 white.child.echo = 1
     82 white.child.expect ('Your move is')
     83 white.set_depth(2)
     84 white.switch()
     85 
     86 move_white = white.get_first_computer_move()
     87 print 'first move white:', move_white
     88 
     89 white.do_move ('e7e5')
     90 move_white = white.get_computer_move()
     91 print 'move white:', move_white
     92 white.do_move ('f8c5')
     93 move_white = white.get_computer_move()
     94 print 'move white:', move_white
     95 white.do_move ('b8a6')
     96 move_white = white.get_computer_move()
     97 print 'move white:', move_white
     98 
     99 sys.exit(1)
    100 
    101 
    102 
    103 black = Chess()
    104 white = Chess()
    105 white.child.expect ('Your move is')
    106 white.switch()
    107 
    108 move_white = white.get_first_computer_move()
    109 print 'first move white:', move_white
    110 
    111 black.do_first_move (move_white)
    112 move_black = black.get_first_computer_move()
    113 print 'first move black:', move_black
    114 
    115 white.do_move (move_black)
    116 
    117 done = 0
    118 while 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 
    129 g.quit()
    130 
    131 
    132