Exemplo n.º 1
0
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 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.')