from nac import *
-import itertools
-import functools
import collections
import random
def new_game(player1, player2):
return {'board': empty_board(),
- 'player1': player1,
- 'player2': player2,
- 'player1_active': True,
- 'history': []}
+ 'player1': player1,
+ 'player2': player2,
+ 'player1_active': True,
+ 'history': []}
+
def game_finished(game):
return (winner(game['board']) is not None) or (game['board'].count('.') == 0)
boxes[b] = box
return boxes
+
def menace_move(game):
board, r, f = canonical(game['board'])
player = active_player(game)
moved_board = untransform(cmove_board, r, f)
return moved_board.index(token)
+
def new_human():
return {'human?': True}
print('Please enter a number.')
return move
+
def make_move(game):
if game['player1_active']:
active = game['player1']
while not game_finished(game):
make_move(game)
+
def winning_player(game):
if winner(game['board']) is None:
return None
else:
return game['player2']
+
def losing_player(game):
if winner(game['board']) is None:
return None
return [h for h in game['history']
if h['player1?'] == game['history'][-1]['player1?']]
+
def losing_moves(game):
return [h for h in game['history']
if h['player1?'] != game['history'][-1]['player1?']]
cmove = cmove_board.index('+')
player[board][cmove] += 1
+
def update_loser(game, allow_drop_move=False):
player = losing_player(game)
moves = losing_moves(game)
player[board][cmove] -= 1
-
def count_wins(p1, p2, plays=1000):
wins = 0
draws = 0
play_game(g)
if report_result_for:
- print('\nFinal position')
- print(show_board(g['board']))
- if winner(g['board']) is None:
- print('A draw')
- elif winning_player(g) == ph:
- print('You won')
- else:
- print('You lost')
-
+ print('\nFinal position')
+ print(show_board(g['board']))
+ if winner(g['board']) is None:
+ print('A draw')
+ elif winning_player(g) == ph:
+ print('You won')
+ else:
+ print('You lost')
return g
+
def train_players(p1, p2, rounds=10000, allow_drop_move=False):
for _ in range(rounds):
g = game_with_players(p1, p2)
update_players(g, allow_drop_move=allow_drop_move)
return p1, p2
-
\ No newline at end of file