Linted master
authorNeil Smith <neil.git@njae.me.uk>
Fri, 12 May 2017 13:58:44 +0000 (14:58 +0100)
committerNeil Smith <neil.git@njae.me.uk>
Fri, 12 May 2017 13:58:44 +0000 (14:58 +0100)
nac-trinket/main.py
nac-trinket/menace.py
nac-trinket/nac.py

index 44d0774ea35ed9c14d61be01cc7f88e9df05a416..df9da0e9ea992b106f23bfb803ccc8e3bf29c688 100644 (file)
@@ -1,6 +1,5 @@
 from menace import * 
 import pprint
-import random
 
 pp = pprint.PrettyPrinter()
 
@@ -9,8 +8,8 @@ p2 = new_menace()
 
 epochs = 10
 for i in range(epochs):
-  print('Starting epoch', i)
-  train_players(p1, p2, rounds=10**5)
+    print('Starting epoch', i)
+    train_players(p1, p2, rounds=10**5)
     
 # pp.pprint(p1)
 # pp.pprint(p2)
@@ -18,6 +17,3 @@ for i in range(epochs):
 ph = new_human()
 
 game_with_players(p1, ph, report_result_for=ph)
-
-
-
index f8adde21071f89865425109af67349d27141e15e..bb75c0656585b84e243c6e034776e4b7e9e5864f 100644 (file)
@@ -1,7 +1,5 @@
 from nac import *
 
-import itertools
-import functools
 import collections
 import random
 
@@ -10,10 +8,11 @@ INITIAL_BEAD_COUNT = 3
 
 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)
@@ -35,6 +34,7 @@ def new_menace():
         boxes[b] = box
     return boxes
 
+
 def menace_move(game):
     board, r, f = canonical(game['board'])
     player = active_player(game)
@@ -44,6 +44,7 @@ def menace_move(game):
     moved_board = untransform(cmove_board, r, f)
     return moved_board.index(token)
 
+
 def new_human():
     return {'human?': True}
 
@@ -71,6 +72,7 @@ def human_move(game):
             print('Please enter a number.')
     return move
 
+
 def make_move(game):
     if game['player1_active']:
         active = game['player1']
@@ -90,6 +92,7 @@ def play_game(game):
     while not game_finished(game):
         make_move(game)
 
+
 def winning_player(game):
     if winner(game['board']) is None:
         return None
@@ -98,6 +101,7 @@ def winning_player(game):
     else:
         return game['player2']
 
+
 def losing_player(game):
     if winner(game['board']) is None:
         return None
@@ -111,6 +115,7 @@ def winning_moves(game):
     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?']]
@@ -132,6 +137,7 @@ def update_winner(game):
         cmove = cmove_board.index('+')
         player[board][cmove] += 1
 
+
 def update_loser(game, allow_drop_move=False):
     player = losing_player(game)
     moves = losing_moves(game)
@@ -150,7 +156,6 @@ def update_loser(game, allow_drop_move=False):
                 player[board][cmove] -= 1
 
 
-
 def count_wins(p1, p2, plays=1000):
     wins = 0
     draws = 0
@@ -172,20 +177,19 @@ def game_with_players(p1, p2, report_result_for=None):
     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
index 64cf504f147c6211df7653ce9e524f96fab8a4c3..3683aad1073ab4a7387f234ee1895c27db2583aa 100644 (file)
@@ -1,7 +1,5 @@
 import itertools
-import functools
-import collections
-import random
+
 
 def xo_count(board):
     xs = 0
@@ -13,18 +11,20 @@ def xo_count(board):
             os += 1
     return xs, os
 
+
 def valid_board(board):
     xs, os = xo_count(board)
     return (xs - os) == 0 or (xs - os) == 1
 
+
 def empty_board():
     return tuple('.' * 9)
 
 
 def all_boards():
     return [b for b in itertools.product('.xo', repeat=9)
-           if valid_board(b)
-           ]
+            if valid_board(b)]
+
 
 def winner(board):
     winning_player = None
@@ -51,6 +51,7 @@ def show_board(b):
     s = ''.join(b)
     return s[0:3] + '\n' + s[3:6] + '\n' + s[6:9]
 
+
 def show_boards(bs):
     rows = [[], [], []]
     for b in bs:
@@ -71,6 +72,7 @@ inverse_rotation = {t: f for f, t in rotation.items()}
 # 6 7 8    8 7 6
 reflection = {0: 2, 1: 1, 2: 0, 3: 5, 4: 4, 5: 3, 6: 8, 7: 7, 8: 6}
 
+
 def rotate(board, n=1):
     b = board
     for _ in range(n):
@@ -84,29 +86,34 @@ def reflect(board, r=True):
     else:
         return board
 
+
 def transform(board, n, r):
     b = rotate(board, n)
     return reflect(b, r)
 
+
 def untransform(board, n, r):
     b = reflect(board, r)
     return rotate(b, abs(4-n))
 
+
 def all_transforms(board):
     return [(transform(board, rot, ref), rot, ref)
             for rot in range(4)
             for ref in [False, True]]
 
+
 def score(board):
     return ''.join(board)
 
+
 def canonical(board):
     return max(all_transforms(board), key=lambda brf: score(brf[0]))
 
+
 def non_winning_boards():
     return set([canonical(b)[0] for b in all_boards()
-           if not winner(b)
-           ])
+                if not winner(b)])
 
 
 def successors(board):
@@ -133,9 +140,9 @@ def vacants(board):
 def apply_move(board, position, piece):
     return tuple(board[:position] + (piece,) + board[position+1:])
 
+
 def token_for_player(is_player_1):
     if is_player_1:
         return 'x'
     else:
         return 'o'
-        
\ No newline at end of file