def get_next_state(self, board, player, move): if (action == 8 * 8): return (board, player) b = Board() b.pieces = np.copy(board) move = (int(action / 8), action % 8) b.executeMove(player, move) return (b.pieces, -player)
def test_obstacles_error(self): with self.assertRaises(ValueError): Board(6, [(1, 1), (1, 1)]) with self.assertRaises(ValueError): Board(6, [(2, 2)]) with self.assertRaises(ValueError): Board(6, [(2, 3)])
def getNextState(self, board, player, action): # if player takes action on board, return next (board,player) # action must be a valid move if action == self.n * self.n: return (board, -player) b = Board(self.n) b.pieces = np.copy(board) move = (int(action / self.n), action % self.n) b.execute_move(move, player) return (b.pieces, -player)
def getValidMoves(self, board, player): # return a fixed size binary vector valids = [0] * self.getActionSize() b = Board(self.n) b.pieces = np.copy(board) legalMoves = b.get_legal_moves(player) if len(legalMoves) == 0: valids[-1] = 1 return np.array(valids) for x, y in legalMoves: valids[self.n * x + y] = 1 return np.array(valids)
def test_eight_board(self): eight_board = Board(8) ans = np.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, -1, 1, 0, 0, 0], [0, 0, 0, 1, -1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]) self.assertTrue(np.array_equal(eight_board.pieces, ans))
def test_obstacles(self): board = Board(6, [(1, 1), (0, 0), (4, 5)]) ans = np.array([ [2, 0, 0, 0, 0, 0], [0, 2, 0, 0, 0, 0], [0, 0, -1, 1, 0, 0], [0, 0, 1, -1, 0, 0], [0, 0, 0, 0, 0, 2], [0, 0, 0, 0, 0, 0], ]) self.assertTrue(np.array_equal(board.pieces, ans))
def getGameEnded(self, board, player): # return 0 if not ended, 1 if player 1 won, -1 if player 1 lost # player = 1 b = Board(self.n) b.pieces = np.copy(board) if b.has_legal_moves(player): return 0 if b.has_legal_moves(-player): return 0 if b.countDiff(player) > 0: return 1 return -1
def getScore(self, board, player): b = Board(self.n) b.pieces = np.copy(board) return b.countDiff(player)
def getInitBoard(self): # return initial board (numpy board) b = Board(self.n) return np.array(b.pieces)
def test_four_board(self): four_board = Board(4) ans = np.array([[0, 0, 0, 0], [0, -1, 1, 0], [0, 1, -1, 0], [0, 0, 0, 0]]) self.assertTrue(np.array_equal(four_board.pieces, ans))
def test_seven_board(self): with self.assertRaises(ValueError): Board(7)
Author: Caleb Spradlin Date: 25/10/19 This file contains the main driver for the OthelloZero system, intiializes a board, and a game to run the board """ import argparse, copy, signal, sys, timeit, imp from OthelloGame import OthelloGame as Game from OthelloLogic import Board from OthelloIO import get_char_col, split_string from alphabeta import alphabeta if __name__ == "__main__": ab = alphabeta() currentPlayer = 0 ME = 1 OPP = -1 bd = Board() g = Game() color = g.initColor() g.display(bd) game_time = 300.0 time = {-1: game_time, 1: game_time} if color == -1: currentPlayer = ME else: currentPlayer = OPP while g.isNotOver is not False: if currentPlayer == ME: start_time = timeit.default_timer() move = g.makeMove(bd, color, ab, time)
This file contains the main driver for the OthelloZero system, intiializes a board, and a game to run the board """ import argparse, copy, signal, sys, timeit, imp from OthelloGame import OthelloGame as Game from OthelloLogic import Board from OthelloIO import get_char_col, split_string from alphabeta import alphabeta from train import train from othellonnet import nnet_wrap if __name__ == "__main__": ab = alphabeta() currentPlayer = 0 ME = 1 OPP = -1 bd = Board() g = Game() nnet = nnet_wrap(g) tr = train(g, nnet, bd) tr.learn() color = g.initColor() g.display(bd) game_time = 300.0 time = {-1: game_time, 1: game_time} if color == -1: currentPlayer = ME else: currentPlayer = OPP while g.isNotOver is not False: