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             fout = open ('log','a')
     28             while 1:
     29                 k = self.child.read(1, 10)
     30                 self.term.process (k)
     31                 fout.write ('(r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c))
     32                 fout.flush()
     33                 if self.term.cur_r == r and self.term.cur_c == c:
     34                     fout.close()
     35                     return 1
     36                 sys.stdout.write (k)
     37                 sys.stdout.flush()
     38 
     39 	def do_scan (self):
     40             fout = open ('log','a')
     41             while 1:
     42                 c = self.child.read(1,10)
     43                 self.term.process (c)
     44                 fout.write ('(r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c))
     45                 fout.flush()
     46                 sys.stdout.write (c)
     47                 sys.stdout.flush()
     48 
     49 	def do_move (self, move):
     50                 self.read_until_cursor (19,60)
     51 		self.child.sendline (move)
     52 		return move
     53 	
     54 	def get_computer_move (self):
     55 		print 'Here'
     56 		i = self.child.expect (['\[17;59H', '\[17;58H'])
     57 		print i
     58 		if i == 0:
     59 			self.child.expect (REGEX_MOVE)
     60 			if len(self.child.after) < 4:
     61 				self.child.after = self.child.after + self.last_computer_move[3]
     62 		if i == 1:
     63 			self.child.expect (REGEX_MOVE_PART)
     64 			self.child.after = self.last_computer_move[0] + self.child.after
     65 		print '', self.child.after
     66 		self.last_computer_move = self.child.after
     67 		return self.child.after
     68 
     69 	def switch (self):
     70 		self.child.sendline ('switch')
     71 
     72 	def set_depth (self, depth):
     73 		self.child.sendline ('depth')
     74 		self.child.expect ('depth=')
     75 		self.child.sendline ('%d' % depth)
     76 
     77 	def quit(self):
     78 		self.child.sendline ('quit')
     79 import sys, os
     80 print 'Starting...'
     81 white = Chess()
     82 white.do_move('b2b4')
     83 white.read_until_cursor (19,60)
     84 c1 = white.term.get_abs(17,58)
     85 c2 = white.term.get_abs(17,59)
     86 c3 = white.term.get_abs(17,60)
     87 c4 = white.term.get_abs(17,61)
     88 fout = open ('log','a')
     89 fout.write ('Computer:%s%s%s%s\n' %(c1,c2,c3,c4))
     90 fout.close()
     91 white.do_move('c2c4')
     92 white.read_until_cursor (19,60)
     93 c1 = white.term.get_abs(17,58)
     94 c2 = white.term.get_abs(17,59)
     95 c3 = white.term.get_abs(17,60)
     96 c4 = white.term.get_abs(17,61)
     97 fout = open ('log','a')
     98 fout.write ('Computer:%s%s%s%s\n' %(c1,c2,c3,c4))
     99 fout.close()
    100 white.do_scan ()
    101 
    102 #white.do_move ('b8a6')
    103 #move_white = white.get_computer_move()
    104 #print 'move white:', move_white
    105 
    106 sys.exit(1)
    107 
    108 
    109 
    110 black = Chess()
    111 white = Chess()
    112 white.child.expect ('Your move is')
    113 white.switch()
    114 
    115 move_white = white.get_first_computer_move()
    116 print 'first move white:', move_white
    117 
    118 black.do_first_move (move_white)
    119 move_black = black.get_first_computer_move()
    120 print 'first move black:', move_black
    121 
    122 white.do_move (move_black)
    123 
    124 done = 0
    125 while not done:
    126     move_white = white.get_computer_move()
    127     print 'move white:', move_white
    128 
    129     black.do_move (move_white)
    130     move_black = black.get_computer_move()
    131     print 'move black:', move_black
    132    
    133     white.do_move (move_black)
    134     print 'tail of loop'
    135 
    136 g.quit()
    137 
    138 
    139