#! /usr/bin/python -O # -*- coding: iso-8859-15 -*- # # A short description goes here. # Copyright (C) 2007 MiKael NAVARRO # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # """A simple Awalé game. Copyright (C) 2007 MiKael NAVARRO Replay pyAwale logs. """ # Specific variables for pydoc __author__ = "MiKael Navarro " # Include directives import sys import os import re HAVE_PSYCO = True try: # Psyco optim import psyco psyco.profile(0.2) except: HAVE_PSYCO = False if __debug__: from pprint import pprint as pp # Path to awale module lib_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../src/") sys.path.append(lib_path) import awale # Re-use some functions from pyawale script from pyawale import human_sow as player_sow from pyawale import computer_sow as pyawale_sow from pyawale import display # Global variables INIT_REGEXP=r"\s*Initial board \(algo='(\w*)', level=(\d)\)" SOUTH_REGEXP=r"\s*South player sown from '(.)'" NORTH_REGEXP=r".*North player sown from '(.)'" # # Log parsing. # def parse_pyawale_log(log_filename): """Parse given log file to return needed info for pyAwale engine.""" pyawale_cmds = { "algo_type" : '', "algo_depth" : 0, "south_sown" : [], "north_sown" : [] } try: log_file = open(log_filename, 'r') for line in log_file: #print line if re.match(r"\s*Initial board", line): init_match = re.match(INIT_REGEXP, line) pyawale_cmds["algo_type"] = init_match.group(1) pyawale_cmds["algo_depth"] = int(init_match.group(2))*3 elif re.match(r"\s*South player sown", line): south_match = re.match(SOUTH_REGEXP, line) pyawale_cmds["south_sown"].append(south_match.group(1)) elif re.match(r"^.*North player sown", line): north_match = re.match(NORTH_REGEXP, line) pyawale_cmds["north_sown"].append(north_match.group(1)) else: pass finally: log_file.close() print pyawale_cmds return pyawale_cmds # # Cmds replay. # def replay_pyawale_log(awale_board, pyawale_cmds, check_sown=False): """Replay pyAwale cmds.""" for scup in pyawale_cmds["south_sown"]: #print scup try: # Replay player sown player_sow(awale_board, awale.SOUTH_PLAYER, scup) # Computer sown idx, elapsed_time = pyawale_sow(awale_board, awale.NORTH_PLAYER) if check_sown: ncup = pyawale_cmds["north_sown"].pop(0) if awale.COMPUTER_CUPS[idx-6] != ncup: print "Best Sown Not Match" break except awale.NoMoreMove, msg: # no more seeds print msg break except awale.EndOfGame, msg: # eog print msg break display(awale_board) # # Main entry point. # def main(options): """Replay pyAwale logs.""" # Parse logs to get settings/history pyawale_cmds = parse_pyawale_log(options.log) if __debug__: pp(pyawale_cmds) # Init pyAwale board awale_board = awale.Awale(algo=pyawale_cmds["algo_type"], depth=pyawale_cmds["algo_depth"]) # Then, replay the party replay_pyawale_log(awale_board, pyawale_cmds, options.check) # # External entry point. # if __name__ == "__main__": # Get options from optparse import OptionParser, make_option option_list = [ make_option("-l", "--log", action="store", dest="log", help="log file"), make_option("-c", "--check", action="store_true", dest="check", default=False, help="check computer sown"), ] parser = OptionParser(usage="python -O %prog [options] ... [args] ...", description="Replay pyAwale logs.", option_list=option_list) (options, args) = parser.parse_args() # Compulsory arguments if not options.log: print "Missing log argument (see --help)!" sys.exit(1) # Process start here main(options)