From 885b621bb5a65e62bf6b7f90d2087123e1fb82ca Mon Sep 17 00:00:00 2001 From: Logan Gier Date: Sun, 1 Jan 2023 15:24:22 -0600 Subject: [PATCH] tictactoe: minor formatting and imports - Fix typo in comment - PEP 8 formatting - Update keras imports Signed-off-by: Logan Gier --- tictactoe/TicTacToeLogic.py | 40 ++++++++++++++++---------------- tictactoe/TicTacToePlayers.py | 14 ++++++----- tictactoe/keras/TicTacToeNNet.py | 8 +++---- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/tictactoe/TicTacToeLogic.py b/tictactoe/TicTacToeLogic.py index 915861e8f..7e6f703cf 100644 --- a/tictactoe/TicTacToeLogic.py +++ b/tictactoe/TicTacToeLogic.py @@ -14,23 +14,24 @@ Based on the board for the game of Othello by Eric P. Nichols. ''' + + # from bkcharts.attributes import color class Board(): - # list of all 8 directions on the board, as (x,y) offsets - __directions = [(1,1),(1,0),(1,-1),(0,-1),(-1,-1),(-1,0),(-1,1),(0,1)] + __directions = [(1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1)] def __init__(self, n=3): "Set up initial board configuration." self.n = n # Create the empty board array. - self.pieces = [None]*self.n + self.pieces = [None] * self.n for i in range(self.n): - self.pieces[i] = [0]*self.n + self.pieces[i] = [0] * self.n # add [][] indexer syntax to the Board - def __getitem__(self, index): + def __getitem__(self, index): return self.pieces[index] def get_legal_moves(self, color): @@ -43,18 +44,18 @@ def get_legal_moves(self, color): # Get all the empty squares (color==0) for y in range(self.n): for x in range(self.n): - if self[x][y]==0: - newmove = (x,y) + if self[x][y] == 0: + newmove = (x, y) moves.add(newmove) return list(moves) def has_legal_moves(self): for y in range(self.n): for x in range(self.n): - if self[x][y]==0: + if self[x][y] == 0: return True return False - + def is_win(self, color): """Check whether the given player has collected a triplet in any direction; @param color (1=white,-1=black) @@ -64,32 +65,32 @@ def is_win(self, color): for y in range(self.n): count = 0 for x in range(self.n): - if self[x][y]==color: + if self[x][y] == color: count += 1 - if count==win: + if count == win: return True # check x-strips for x in range(self.n): count = 0 for y in range(self.n): - if self[x][y]==color: + if self[x][y] == color: count += 1 - if count==win: + if count == win: return True # check two diagonal strips count = 0 for d in range(self.n): - if self[d][d]==color: + if self[d][d] == color: count += 1 - if count==win: + if count == win: return True count = 0 for d in range(self.n): - if self[d][self.n-d-1]==color: + if self[d][self.n - d - 1] == color: count += 1 - if count==win: + if count == win: return True - + return False def execute_move(self, move, color): @@ -97,9 +98,8 @@ def execute_move(self, move, color): color gives the color pf the piece to play (1=white,-1=black) """ - (x,y) = move + (x, y) = move # Add the piece to the empty square. assert self[x][y] == 0 self[x][y] = color - diff --git a/tictactoe/TicTacToePlayers.py b/tictactoe/TicTacToePlayers.py index 4ae2a595b..ecfca1af5 100644 --- a/tictactoe/TicTacToePlayers.py +++ b/tictactoe/TicTacToePlayers.py @@ -1,7 +1,7 @@ import numpy as np """ -Random and Human-ineracting players for the game of TicTacToe. +Random and Human-interacting players for the game of TicTacToe. Author: Evgeny Tyurin, github.com/evg-tyurin Date: Jan 5, 2018. @@ -9,6 +9,8 @@ Based on the OthelloPlayers by Surag Nair. """ + + class RandomPlayer(): def __init__(self, game): self.game = game @@ -16,7 +18,7 @@ def __init__(self, game): def play(self, board): a = np.random.randint(self.game.getActionSize()) valids = self.game.getValidMoves(board, 1) - while valids[a]!=1: + while valids[a] != 1: a = np.random.randint(self.game.getActionSize()) return a @@ -30,15 +32,15 @@ def play(self, board): valid = self.game.getValidMoves(board, 1) for i in range(len(valid)): if valid[i]: - print(int(i/self.game.n), int(i%self.game.n)) - while True: + print(int(i / self.game.n), int(i % self.game.n)) + while True: # Python 3.x a = input() # Python 2.x # a = raw_input() - x,y = [int(x) for x in a.split(' ')] - a = self.game.n * x + y if x!= -1 else self.game.n ** 2 + x, y = [int(x) for x in a.split(' ')] + a = self.game.n * x + y if x != -1 else self.game.n ** 2 if valid[a]: break else: diff --git a/tictactoe/keras/TicTacToeNNet.py b/tictactoe/keras/TicTacToeNNet.py index d65c5d666..faaf51cba 100644 --- a/tictactoe/keras/TicTacToeNNet.py +++ b/tictactoe/keras/TicTacToeNNet.py @@ -1,11 +1,9 @@ import sys sys.path.append('..') -from utils import * -import argparse -from tensorflow.keras.models import * -from tensorflow.keras.layers import * -from tensorflow.keras.optimizers import * +from keras.models import * +from keras.layers import * +from keras.optimizers import * """ NeuralNet for the game of TicTacToe.