def main(file_path=FILE_LOCATION, game_number=16): network = Neural_Network() board = parse_training_file(file_path) inp = simulate(board) instance = inp[0][int(game_number)] # Some move within a game b = Board(instance) # Gather neural network outputs for each possible move on board outputs = gather_outputs(b, network) b.print_board() # Call with additional parameter False to prevent PDF output draw_graph(outputs)
def main(): board = Board() # for i in range(20): # board.move(i, 0, board._next_player) m = winning_moves(board, 3, should_print=True) print(len(m))
def make_move(brd: Board) -> (int, int): player = brd.get_next_player() while True: move = input("Type your move " + get_player_string(player) + " (X,Y): ") if validate_input(move): break print("Invalid move.") coordinates = move.split(",") x = int(coordinates[0]) - 1 y = int(coordinates[1]) - 1 return x, y
def simulate(moves: MovesStruct, should_print: bool = False) -> TrainingDataStruct: board = Board() all_boards = [board.get_board()] p = -1 for x, y in moves: assert board.move(x, y, p) all_boards.append(board.get_board()) if should_print: board.print_board() winner, _ = board.decide_winner() if winner != 0: return all_boards, winner p = -p raise ValueError( 'Winner still not determined after all moves have been made.')
class Gomoku: board = Board() def make_move(self, x: int, y: int, player: int) -> bool: if self.board.move(x, y, player): self.board.print_board() return True return False def check_for_winner(self) -> bool: winning_data = self.board.decide_winner() if winning_data[1] is None: return False winner, winning_moves = winning_data print_winning_message(winner, winning_moves) return True
def make_move(self, brd: Board) -> MoveStruct: if self.is_new_game(): self.set_player_number_for_computer(brd) self.create_node(brd) else: last_move = brd.get_last_move() assert last_move is not None, "Expected to have received a move but was None" node_found = self.update_node_to_child(last_move) if not node_found: self.create_node(brd, last_move) if GEN_GRAPH: use_graph_gen(self.node._board, self.neural_network) self.node = self.node.get_play() x, y = self.node.get_move() return x, y
def nn(self, board: Board, player) -> float: return use_network(board.get_board(), self.training_input, self.heuristic, self.keep_prob, self.tf_output, self.sess, player)
def set_player_number_for_computer(self, board: Board): if board.get_last_move() is None: self.player_int = INT_PLAYER_1 else: self.player_int = INT_PLAYER_2
def winning_moves(board: Board, depth: int, should_print: bool = False) -> List[Tuple[int, int]]: """ Searches the board for winning moves to the given depth. Total number of moves checked = reduce(lambda x, y: x + len(moves) - i, range(i+1)) """ # All possible moves that can be made on the current board. cs = moves(board) # No moves can be made anyway, so we can't give a list of winning moves. if len(cs) == 0: return [] if depth == 0: # Returns a list of moves that have a higher than 0.7 probability of being in our favour, # Or will literally win the game. good_moves = [] for move in cs: board.move(move[0], move[1], board.get_next_player()) raw_board = board.get_board() if nn(raw_board) > 0.7 or board.decide_winner() != 0: good_moves.append(move) board.reverse_move() return good_moves rs = [] """ For all winning moves, if the move after could lead to a winning move for the other player, remove it from the list of winning moves. """ for i, c in enumerate(cs): if should_print: print('%d/%d' % (i, len(cs))) x, y = c p = board.get_next_player() board.move(x, y, p) ms = winning_moves(board, depth - 1) rx, ry, rp = board.reverse_move() boolean = (rx, ry, rp) == (x, y, p) assert boolean if len(ms) == 0: rs.append(c) return rs
def moves(board: Board) -> List[Tuple[int, int]]: """ Returns the number of moves that can be made on the board currently, as boards. """ return board.get_possible_moves()
def brain_restart(): global board, COMPUTER board = Board() COMPUTER = computer.Computer() pp.pipeOut("OK")
import pisqpipe as pp from gomokuapp.board import Board from pisqpipe import DEBUG_EVAL from players import computer __author__ = "Jan Stránský (https://github.com/stranskyjan/pbrain-pyrandom), Modified for AlphaGomoku" pp.infotext = 'name="pbrain-alphagomoku", ' \ 'author="Matthew Boakes, Harry Clarke, Matthew Clayton, Max Harris, and Jamie Pont", ' \ 'version="1.0", ' \ 'country="United Kingdom", ' \ 'www="https://git.cs.kent.ac.uk/rg399/AlphaGomoku"' MAX_BOARD = 100 board = Board() COMPUTER = computer.Computer() def brain_init(): if pp.width < 5 or pp.height < 5: pp.pipeOut("ERROR size of the board") return if pp.width > MAX_BOARD or pp.height > MAX_BOARD: pp.pipeOut("ERROR Maximal board size is {}".format(MAX_BOARD)) return pp.pipeOut("OK") def brain_restart(): global board, COMPUTER