#!/usr/bin/python #!/usr/local/pypy-1.5/bin/pypy '''Some tests for board_mod.Board - more or less a tournament between the different computer players''' try: import cProfile as profile except ImportError: import profile import sys import pprint import random import collections import board_mod import player_varieties_mod def main(): '''Main program. Test board_mod''' counts = collections.defaultdict(int) player_varieties = [ player_variety for player_variety in player_varieties_mod.PLAYER_VARIETIES if player_variety[0] != 'human' ] print player_varieties for rep in xrange(10000): print rep winner = one_run(player_varieties) counts[winner] += 1 if winner == 'bad': print '%s result' sys.exit(1) print '%s win' % winner pprint.pprint(counts) print print def one_run(player_varieties): '''Run a single random game''' board = board_mod.Board() moveno = 0 mode = None test_x = random.randrange(2) while True: result = board.game_result() if result == '.': if test_x: if moveno % 2 == 0: # player X will be strong_move for this game board.strong_randomfirst_move('x') else: # player O flips around between random, straightforward, strong and strong-firstrandom if mode == None: mode = random.randrange(4) #mode = 3 print 'O is %s' % player_varieties[mode][1] dispatch(board, mode, 'o') else: if moveno % 2 == 0: # player X flips around between random, straightforward, strong and strong-firstrandom if mode == None: mode = random.randrange(4) #mode = 3 print 'X is %s' % player_varieties[mode][1] dispatch(board, mode, 'x') else: # player O will be strong_move for this game board.strong_randomfirst_move('o') print board print else: return find_result(test_x, result) moveno += 1 def find_result(test_x, result): ''' Figure out if this was a good result or not, taking into account that we don't always have what we're testing on x or always on o ''' if result == 'c': return 'cat' else: if test_x: if result == 'x': return 'good' else: return 'bad' else: if result == 'o': return 'good' else: return 'bad' def dispatch(board, mode, player): '''Call the appropriate mover''' if mode == 0: board.random_move(player) elif mode == 1: board.straightforward_move(player) elif mode == 2: board.strong_move(player) else: board.strong_randomfirst_move(player) if False: profile.run('main()', 'ttt.prof') else: main()