chess2.py :  » Development » Pexpect » pexpect-2.3 » examples » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » Pexpect 
Pexpect » pexpect 2.3 » examples » chess2.py
#!/usr/bin/env python

'''This demonstrates controlling a screen oriented application (curses).
It starts two instances of gnuchess and then pits them against each other.
'''

import pexpect
import string
import ANSI
import sys, os, time

class Chess:

        def __init__(self, engine = "/usr/local/bin/gnuchess -a -h 1"):
                self.child = pexpect.spawn (engine)
                self.term = ANSI.ANSI ()
             
                #self.child.expect ('Chess')
                #if self.child.after != 'Chess':
                #        raise IOError, 'incompatible chess program'
                #self.term.process_list (self.child.before)
                #self.term.process_list (self.child.after)

                self.last_computer_move = ''

        def read_until_cursor (self, r,c, e=0):
            '''Eventually something like this should move into the screen class or
            a subclass. Maybe a combination of pexpect and screen...
            '''
            fout = open ('log','a')
            while self.term.cur_r != r or self.term.cur_c != c:
                try:
                    k = self.child.read(1, 10)
                except Exception, e:
                    print 'EXCEPTION, (r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c)
                    sys.stdout.flush()
                self.term.process (k)
                fout.write ('(r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c))
                fout.flush()
                if e:
                    sys.stdout.write (k)
                    sys.stdout.flush()
                if self.term.cur_r == r and self.term.cur_c == c:
                    fout.close()
                    return 1
            print 'DIDNT EVEN HIT.'
            fout.close()
            return 1

        def expect_region (self):
            '''This is another method that would be moved into the
            screen class.
            '''
            pass
        def do_scan (self):
            fout = open ('log','a')
            while 1:
                c = self.child.read(1,10)
                self.term.process (c)
                fout.write ('(r,c):(%d,%d)\n' %(self.term.cur_r, self.term.cur_c))
                fout.flush()
                sys.stdout.write (c)
                sys.stdout.flush()

        def do_move (self, move, e = 0):
                time.sleep(1)
                self.read_until_cursor (19,60, e)
                self.child.sendline (move)

        def wait (self, color):
            while 1:
                r = self.term.get_region (14,50,14,60)[0]
                r = r.strip()
                if r == color:
                    return
                time.sleep (1)

        def parse_computer_move (self, s):
                i = s.find ('is: ')
                cm = s[i+3:i+9]        
                return cm
        def get_computer_move (self, e = 0):
                time.sleep(1)
                self.read_until_cursor (19,60, e)
                time.sleep(1)
                r = self.term.get_region (17,50,17,62)[0]
                cm = self.parse_computer_move (r)
                return cm

        def switch (self):
                print 'switching'
                self.child.sendline ('switch')

        def set_depth (self, depth):
                self.child.sendline ('depth')
                self.child.expect ('depth=')
                self.child.sendline ('%d' % depth)

        def quit(self):
                self.child.sendline ('quit')

def LOG (s):
    print s
    sys.stdout.flush ()
    fout = open ('moves.log', 'a')
    fout.write (s + '\n')
    fout.close()

print 'Starting...'

black = Chess()
white = Chess()
white.read_until_cursor (19,60,1)
white.switch()

done = 0
while not done:
    white.wait ('Black')
    move_white = white.get_computer_move(1)
    LOG ( 'move white:'+ move_white )

    black.do_move (move_white)
    black.wait ('White')
    move_black = black.get_computer_move()
    LOG ( 'move black:'+ move_black )
   
    white.do_move (move_black, 1)

g.quit()


www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.