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 | import sys, os, time
|
---|
9 |
|
---|
10 | class Chess:
|
---|
11 |
|
---|
12 | def __init__(self, engine = "/usr/local/bin/gnuchess -a -h 1"):
|
---|
13 | self.child = pexpect.spawn (engine)
|
---|
14 | self.term = ANSI.ANSI ()
|
---|
15 |
|
---|
16 | #self.child.expect ('Chess')
|
---|
17 | #if self.child.after != 'Chess':
|
---|
18 | # raise IOError, 'incompatible chess program'
|
---|
19 | #self.term.process_list (self.child.before)
|
---|
20 | #self.term.process_list (self.child.after)
|
---|
21 |
|
---|
22 | self.last_computer_move = ''
|
---|
23 |
|
---|
24 | def read_until_cursor (self, r,c, e=0):
|
---|
25 | '''Eventually something like this should move into the screen class or
|
---|
26 | a subclass. Maybe a combination of pexpect and screen...
|
---|
27 | '''
|
---|
28 | fout = open ('log','a')
|
---|
29 | while self.term.cur_r != r or self.term.cur_c != c:
|
---|
30 | try:
|
---|
31 | k = self.child.read(1, 10)
|
---|
32 | except Exception, e:
|
---|
33 | print 'EXCEPTION, (r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c)
|
---|
34 | sys.stdout.flush()
|
---|
35 | self.term.process (k)
|
---|
36 | fout.write ('(r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c))
|
---|
37 | fout.flush()
|
---|
38 | if e:
|
---|
39 | sys.stdout.write (k)
|
---|
40 | sys.stdout.flush()
|
---|
41 | if self.term.cur_r == r and self.term.cur_c == c:
|
---|
42 | fout.close()
|
---|
43 | return 1
|
---|
44 | print 'DIDNT EVEN HIT.'
|
---|
45 | fout.close()
|
---|
46 | return 1
|
---|
47 |
|
---|
48 | def expect_region (self):
|
---|
49 | '''This is another method that would be moved into the
|
---|
50 | screen class.
|
---|
51 | '''
|
---|
52 | pass
|
---|
53 | def do_scan (self):
|
---|
54 | fout = open ('log','a')
|
---|
55 | while 1:
|
---|
56 | c = self.child.read(1,10)
|
---|
57 | self.term.process (c)
|
---|
58 | fout.write ('(r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c))
|
---|
59 | fout.flush()
|
---|
60 | sys.stdout.write (c)
|
---|
61 | sys.stdout.flush()
|
---|
62 |
|
---|
63 | def do_move (self, move, e = 0):
|
---|
64 | time.sleep(1)
|
---|
65 | self.read_until_cursor (19,60, e)
|
---|
66 | self.child.sendline (move)
|
---|
67 |
|
---|
68 | def wait (self, color):
|
---|
69 | while 1:
|
---|
70 | r = self.term.get_region (14,50,14,60)[0]
|
---|
71 | r = r.strip()
|
---|
72 | if r == color:
|
---|
73 | return
|
---|
74 | time.sleep (1)
|
---|
75 |
|
---|
76 | def parse_computer_move (self, s):
|
---|
77 | i = s.find ('is: ')
|
---|
78 | cm = s[i+3:i+9]
|
---|
79 | return cm
|
---|
80 | def get_computer_move (self, e = 0):
|
---|
81 | time.sleep(1)
|
---|
82 | self.read_until_cursor (19,60, e)
|
---|
83 | time.sleep(1)
|
---|
84 | r = self.term.get_region (17,50,17,62)[0]
|
---|
85 | cm = self.parse_computer_move (r)
|
---|
86 | return cm
|
---|
87 |
|
---|
88 | def switch (self):
|
---|
89 | print 'switching'
|
---|
90 | self.child.sendline ('switch')
|
---|
91 |
|
---|
92 | def set_depth (self, depth):
|
---|
93 | self.child.sendline ('depth')
|
---|
94 | self.child.expect ('depth=')
|
---|
95 | self.child.sendline ('%d' % depth)
|
---|
96 |
|
---|
97 | def quit(self):
|
---|
98 | self.child.sendline ('quit')
|
---|
99 |
|
---|
100 | def LOG (s):
|
---|
101 | print s
|
---|
102 | sys.stdout.flush ()
|
---|
103 | fout = open ('moves.log', 'a')
|
---|
104 | fout.write (s + '\n')
|
---|
105 | fout.close()
|
---|
106 |
|
---|
107 | print 'Starting...'
|
---|
108 |
|
---|
109 | black = Chess()
|
---|
110 | white = Chess()
|
---|
111 | white.read_until_cursor (19,60,1)
|
---|
112 | white.switch()
|
---|
113 |
|
---|
114 | done = 0
|
---|
115 | while not done:
|
---|
116 | white.wait ('Black')
|
---|
117 | move_white = white.get_computer_move(1)
|
---|
118 | LOG ( 'move white:'+ move_white )
|
---|
119 |
|
---|
120 | black.do_move (move_white)
|
---|
121 | black.wait ('White')
|
---|
122 | move_black = black.get_computer_move()
|
---|
123 | LOG ( 'move black:'+ move_black )
|
---|
124 |
|
---|
125 | white.do_move (move_black, 1)
|
---|
126 |
|
---|
127 | g.quit()
|
---|
128 |
|
---|
129 |
|
---|