def next_move(game: Game_IJK) -> None:
    '''board: list of list of strings -> current state of the game
           current_player: int -> player who will make the next move either ('+') or -'-')
           deterministic: bool -> either True or False, indicating whether the game is deterministic or not
    '''
    global InitialPlayer
    InitialPlayer = game.getCurrentPlayer()
    board = game.getGame()
    player = game.getCurrentPlayer()
    deterministic = game.getDeterministic()

    # You'll want to put in your fancy AI code here. For right now this just
    boards = []
    level = 0
    max_score = -math.inf
    min_score = math.inf
    final_move = ""
    if player == "+":
        boards = succesor_boards(True, game, board)
    else:
        boards = succesor_boards(False, game, board)

    for board in boards:
        alpha = -math.inf
        beta = math.inf
        score = compute_score(False, level, 3, board[1], alpha, beta, game)
        if score > max_score:
            # print(score)
            # print("Final score")
            max_score = score
            final_move = board[0]
    yield final_move
def next_move(game: Game_IJK) -> None:
    """ game: current-state of game
        yield a single character as one of the following moves - 'U', 'L', 'R', 'D', 'S'
    """
    """board: list of list of chars -> current state of the game
       current_player: int -> player who will make the next move either 1('+') or -1('-')
    """

    move = (
        input(
            "Please enter move for player %s (U, D, L, R): " % game.getCurrentPlayer()
        )
        .strip()
        .upper()
    )
    while move not in "UDLR":
        move = (
            input(
                "Bad move! Please try again and enter move for player %s (U, D, L, R): "
                % game.getCurrentPlayer()
            )
            .strip()
            .upper()
        )
    yield move
Beispiel #3
0
def next_move(game: Game_IJK) -> None:
    '''board: list of list of strings -> current state of the game
           current_player: int -> player who will make the next move either ('+') or -'-')
           deterministic: bool -> either True or False, indicating whether the game is deterministic or not
    '''
    global InitialPlayer
    InitialPlayer = game.getCurrentPlayer()
    board = game.getGame()
    player = game.getCurrentPlayer()
    deterministic = game.getDeterministic()

    # You'll want to put in your fancy AI code here. For right now this just
    boards = []
    level = 0
    max_score = -math.inf
    min_score = math.inf
    final_move = ""
    if deterministic:
        " Minimax  - alpha/beta pruning"
        if player == "+":
            boards = succesor_boards(True, game, board)
        else:
            boards = succesor_boards(False, game, board)
        for board in boards:
            alpha = -math.inf
            beta = math.inf
            score = compute_score(False, False, level, 3, board[1], alpha,
                                  beta, game, InitialPlayer, "")
            if score > max_score:
                # print(score)
                # print("Final score")
                max_score = score
                final_move = board[0]

    else:
        " Expectiminimax - alpha/beta pruning"
        moves = ['U', 'D', 'L', 'R']
        empty_tiles = get_empty_tiles(board)
        for move in moves:
            score = 0
            if player == "+":
                boards = chance_boards(True, game, board, move, empty_tiles)
            else:
                boards = chance_boards(False, game, board, move, empty_tiles)
            for board1 in boards:
                alpha = -math.inf
                beta = math.inf
                score += compute_score(False, False, level, 3, board1[1],
                                       alpha, beta, game, InitialPlayer, "")
            avg_score = score / empty_tiles
            if avg_score > max_score:
                # print(score)
                # print("Final score")
                max_score = avg_score
                final_move = move

    yield final_move
Beispiel #4
0
def next_move(game: Game_IJK)-> None:

    '''board: list of list of strings -> current state of the game
       current_player: int -> player who will make the next move either ('+') or -'-')
       deterministic: bool -> either True or False, indicating whether the game is deterministic or not
    '''

    board = game.getGame()
    player = game.getCurrentPlayer()
    deterministic = game.getDeterministic()
    
    print(player)

    # You'll want to put in your fancy AI code here. For right now this just 
    # returns a random move.
    
    if player == '+':
       temp = 'max'
    else:
       temp = 'min'
    
    if deterministic == True:
        yield choice(game,temp,4)#random.choice(['U', 'D', 'L', 'R'])
    else:
        yield choice(game,temp,5)#random.choice(['U', 'D', 'L', 'R'])
Beispiel #5
0
def evaluate(game: Game_IJK, player) -> (int, int):
    up, low = 0, 0
    gamestate = game.state()
    if (gamestate == 'C'):
        up = float("inf")
        low = float("-inf")
        return float("inf")
    elif (gamestate == 'c'):
        return float("-inf")
    elif (gamestate == 'Tie'):
        return 0
    adjscore = 0
    board = game.getGame()
    for row in board:
        for e in row:
            if (e != ' '):
                if (e >= 'a'):
                    low += (2**(ord(e) - ord('a') + 1) - 1)
                else:
                    up += (2**(ord(e) - ord('A') + 1) - 1)
    for i in range(6):
        for j in range(6):
            if (j + 1 < 6
                    and abs(ord(board[i][j + 1]) - ord(board[i][j])) == 32):
                adjscore += (2**(ord(board[i][j + 1]) - ord('a') + 2) - 1)
            elif (i + 1 < 6
                  and abs(ord(board[i + 1][j]) - ord(board[i][j])) == 32):
                adjscore += (2**(ord(board[i + 1][j]) - ord('a') + 2) - 1)
    return up - low + (adjscore
                       if game.getCurrentPlayer() == player else -adjscore)
Beispiel #6
0
def next_move(game: Game_IJK) -> None:
    '''board: list of list of strings -> current state of the game
       current_player: int -> player who will make the next move either ('+') or -'-')
       deterministic: bool -> either True or False, indicating whether the game is deterministic or not
    '''

    board = game.getGame()
    player = game.getCurrentPlayer()
    deterministic = game.getDeterministic()

    # You'll want to put in your fancy AI code here. For right now this just
    # returns a random move.
    #print('Board: ', board)
    if (player == '+'):
        current_player = 1
    else:
        current_player = -1
    #print(merge(board, current_player))
    #children(board, current_player)
    #board = [['J', 'J', ' ', ' '], [' ', ' ', ' ', ' '], [' ', ' ', ' ', ' '], ['i', ' ', ' ', ' ']]
    if (deterministic == True):
        score, move = Decision(board, current_player, True)
        #print('MOVE', move, score)
        return move
    else:
        score, move = Decision_nondet(board, current_player, True)
        #print('MOVE: ', move, score)
        return move
def next_move(game: Game_IJK) -> None:
    '''board: list of list of strings -> current state of the game
	   current_player: int -> player who will make the next move either ('+') or -'-')
	   deterministic: bool -> either True or False, indicating whether the game is deterministic or not
	'''

    board = game.getGame()
    player = game.getCurrentPlayer()
    deterministic = game.getDeterministic()

    moves = getAvailableMoves(game)
    maxUtility = -np.inf
    nextMove = -1

    for move in moves:
        child = getChild(game, move)
        utility = MinimaxAB(board=child,
                            node=player,
                            deterministic=deterministic)

        if utility >= maxUtility:
            maxUtility = utility
            nextMove = move

    yield nextMove
def next_move(game: Game_IJK) -> None:
    '''board: list of list of strings -> current state of the game
       current_player: int -> player who will make the next move either ('+') or -'-')
       deterministic: bool -> either True or False, indicating whether the game is deterministic or not
    '''

    board = game.getGame()
    player = game.getCurrentPlayer()
    deterministic = game.getDeterministic()

    # You'll want to put in your fancy AI code here. For right now this just
    # returns a random move.

    depth = 0

    alpha = -math.inf
    beta = math.inf
    mv = ''
    to_be_done = ''

    unchanged_board = board

    for m in moves:
        alpha, mv = max([alpha, mv], Min(m, game, depth, alpha, beta))
        to_be_done = mv
    return to_be_done
Beispiel #9
0
def next_move(game: Game_IJK) -> None:
    '''board: list of list of strings -> current state of the game
       current_player: int -> player who will make the next move either ('+') or -'-')
       deterministic: bool -> either True or False, indicating whether the game is deterministic or not
    '''

    beta = math.inf
    alpha = -1 * math.inf

    # depth
    future_step = 3

    board = game.getGame()
    player = game.getCurrentPlayer()
    deterministic = game.getDeterministic()

    if player == '+':
        # succ = [game,'move']
        succ = successor(game)

        best, c = max(min_value(i, future_step, alpha, beta) for i in succ)
        yield best[1]

    else:
        yield random.choice(['U', 'L', 'D', 'R'])
Beispiel #10
0
def next_move(game: Game_IJK) -> None:
    '''board: list of list of strings -> current state of the game
       current_player: int -> player who will make the next move either ('+') or -'-')
       deterministic: bool -> either True or False, indicating whether the game is deterministic or not
    '''

    board = game.getGame()
    player = game.getCurrentPlayer()
    deterministic = game.getDeterministic()

    # You'll want to put in your fancy AI code here. For right now this just
    # returns a random move.

    moves = ['U', 'L', 'R', 'D']
    value = []

    #start with minimax algorithm with current game state
    for i in ['U', 'L', 'R', 'D']:
        child = successor(copy.deepcopy(game), i)
        value.append(minimax(child, 3, False, float('-inf'), float('inf')))
    #get index number with highest value
    k = np.argmax(value)

    # You'll want to put in your fancy AI code here. For right now this just
    # returns a random move.

    #choose a move with highest minimax utility value
    yield moves[k]
Beispiel #11
0
def next_move(game: Game_IJK)-> None:

    
    board = game.getGame()
    player = game.getCurrentPlayer()
    deterministic = game.getDeterministic()
    print( "Player" , player )
    yield moves_run( board , game , player )
Beispiel #12
0
def next_move(game: Game_IJK) -> None:
    ''' game: current-state of game
        yield a single character as one of the following moves - 'U', 'L', 'R', 'D', 'S'
    '''
    '''board: list of list of chars -> current state of the game
       current_player: int -> player who will make the next move either 1('+') or -1('-')
    '''

    print('Please enter move for player %s (U, D, L, R): ' %
          game.getCurrentPlayer())
    move = input().strip().upper()
    while (move not in "UDLR"):
        print(
            'Bad move! Please try again and enter move for player %s (U, D, L, R): '
            % game.getCurrentPlayer())
        move = input().strip().upper()

    yield move
def next_move(game: Game_IJK) -> None:
    '''board: list of list of strings -> current state of the game
       current_player: int -> player who will make the next move either ('+') or -'-')
       deterministic: bool -> either True or False, indicating whether the game is deterministic or not
    '''

    board = game.getGame()
    player = game.getCurrentPlayer()
    deterministic = game.getDeterministic()

    # You'll want to put in your fancy AI code here. For right now this just
    # returns a random move.
    yield MINIMAX_Decision(game, deterministic, h=5)
Beispiel #14
0
def next_move(game: Game_IJK) -> None:
    """board: list of list of strings -> current state of the game
       current_player: int -> player who will make the next move either ('+') or -'-')
       deterministic: bool -> either True or False, indicating whether the game is deterministic or not
    """

    board = game.getGame()
    player = game.getCurrentPlayer()
    deterministic = game.getDeterministic()

    if deterministic == True:
        move = findOptimalMoveDet(game, board, 4, player)
    elif deterministic == False:
        move = findOptimalMoveNonDet(game, board, player)
    yield move
Beispiel #15
0
def next_move(game: Game_IJK)-> None:

    '''board: list of list of strings -> current state of the game
       current_player: int -> player who will make the next move either ('+') or -'-')
       deterministic: bool -> either True or False, indicating whether the game is deterministic or not
    '''

    board = game.getGame()
    player = game.getCurrentPlayer()
    deterministic = game.getDeterministic()

    # You'll want to put in your fancy AI code here. For right now this just 
    # returns a random move    
    direction, child, utility = decision(game)
    yield direction
#    yield random.choice(['U', 'D', 'L', 'R'])
def next_move(game: Game_IJK) -> None:
    '''board: list of list of strings -> current state of the game
       current_player: int -> player who will make the next move either ('+') or -'-')
       deterministic: bool -> either True or False, indicating whether the game is deterministic or not
    '''
    global maxdepth
    board = game.getGame()
    player = game.getCurrentPlayer()
    deterministic = game.getDeterministic()

    # You'll want to put in your fancy AI code here. For right now this just
    # returns a random move.
    if deterministic == False:
        if countboard(board) > 8:
            maxdepth = 0
        elif countboard(board) > 5:
            maxdepth = 1
        else:
            maxdepth = 2
        if player == '+':

            start = time.time()
            best_move, _ = maximizechance(game, board)
            end = time.time()
            print(start - end)

            yield (best_move)
        if player == '-':
            best_move, _ = minimizechance(game, board)

            yield (best_move)

    if deterministic == True:
        if player == "+":
            maxdepth = 7
            best_move, _ = Max(game, board)

            yield (best_move)

        if player == "-":
            maxdepth = 7
            best_move, _ = Min(game, board)

            yield (best_move)
def next_move(game: Game_IJK) -> None:
    '''board: list of list of strings -> current state of the game
       current_player: int -> player who will make the next move either ('+') or -'-')
       deterministic: bool -> either True or False, indicating whether the game is deterministic or not
    '''

    board = game.getGame()
    player = game.getCurrentPlayer()
    deterministic = game.getDeterministic()

    if player == '+':
        Ismax = True
    else:
        Ismax = False
    depth = 5
    if (deterministic == True):
        score, move = Mininax_AlphaBeta(board, depth, Ismax, -math.inf,
                                        math.inf, game)
        #print('MOVE', move, score)

        return move
Beispiel #18
0
def next_move(game: Game_IJK):
    '''board: list of list of strings -> current state of the game
       current_player: int -> player who will make the next move either ('+') or -'-')
       deterministic: bool -> either True or False, indicating whether the game is deterministic or not
    '''
    board = game.getGame()
    player = game.getCurrentPlayer()
    deterministic = game.getDeterministic()

    # You'll want to put in your fancy AI code here. For right now this just
    # returns a random move.

    moves = ['U', 'D', 'L', 'R']
    best_move = moves[0]
    best_utility = float('-inf')
    for move in moves:
        clone = game.makeMove(move)
        utility = min_utility(clone, 0, -float('inf'), float('inf'))
        if utility > best_utility:
            best_utility = utility
            best_move = move
    yield best_move
Beispiel #19
0
def next_move(game: Game_IJK) -> None:
    '''board: list of list of strings -> current state of the game
       current_player: int -> player who will make the next move either ('+') or -'-')
       deterministic: bool -> either True or False, indicating whether the game is deterministic or not
    '''

    board = game.getGame()
    player = game.getCurrentPlayer()
    deterministic = game.getDeterministic()

    # You'll want to put in your fancy AI code here. For right now this just
    # returns a random move.
    if (deterministic):
        depth = 6
    else:
        depth = 0
    mins = {}
    w = copy.deepcopy(game)
    w = w.makeMove('U')
    s = copy.deepcopy(game)
    s = w.makeMove('D')
    a = copy.deepcopy(game)
    a = w.makeMove('L')
    d = copy.deepcopy(game)
    d = w.makeMove('R')
    mins['U'] = MIN_Value(w, float("-inf"), float("inf"), depth, player)
    mins['D'] = MIN_Value(s, float("-inf"), float("inf"), depth, player)
    mins['L'] = MIN_Value(a, float("-inf"), float("inf"), depth, player)
    mins['R'] = MIN_Value(d, float("-inf"), float("inf"), depth, player)
    move = max(mins, key=lambda k: mins[k])
    for i in range(6):
        for j in range(6):
            if (j + 1 < 6
                    and abs(ord(board[i][j + 1]) - ord(board[i][j])) == 32):
                move = 'R'
            elif (i + 1 < 6
                  and abs(ord(board[i + 1][j]) - ord(board[i][j])) == 32):
                move = 'D'
    yield move
Beispiel #20
0
def next_move(game: Game_IJK) -> None:
    '''board: list of list of strings -> current state of the game
       current_player: int -> player who will make the next move either ('+') or -'-')
       deterministic: bool -> either True or False, indicating whether the game is deterministic or not
    '''

    board = game.getGame()
    player = game.getCurrentPlayer()
    deterministic = game.getDeterministic()

    # Create an object of the class Node
    node = Node(copy.deepcopy(board), player, '')
    start = time.time()
    if deterministic:
        # if deterministic, then call minimax with alpha beta pruning
        result = minimax_alpha_beta(node, 9, player, NINF, PINF, deterministic,
                                    start)
    else:
        # if non-deterministic then call normal minimax as expectation is involved
        result = minimax(node, 6, player, deterministic, start)
    print(time.time() - start)
    print(result)
    return result[0]
Beispiel #21
0
def next_move(game: Game_IJK) -> None:
    '''board: list of list of strings -> current state of the game
       current_player: int -> player who will make the next move either ('+') or -'-')
       deterministic: bool -> either True or False, indicating whether the game is deterministic or not
    '''

    board = game.getGame()
    player = game.getCurrentPlayer()
    deterministic = game.getDeterministic()

    global current_index

    if deterministic:
        fringe = [board]
        count = 1

        while len(fringe) >= 0 and count <= 85:

            board = fringe.pop(0)
            fringe += find_successors(board, player, deterministic)
            count += 1

        list_mini_max = []
        for i in fringe:
            list_mini_max.append(heuristic(i, player))

        temp, temp1 = minimax(0, 0, True, list_mini_max, -1000, 1000)

        temp_dict = {0: 'L', 1: 'R', 2: 'U', 3: 'D'}
        yield temp_dict[temp1]

    else:
        # Tuple structure : Board, max=1/min=0/chance=-1, depth, Heuristic, parent, current node index, parent node index
        fringe = [(board, 1, 1, 0, "root", 0, 0)]
        fringe1 = [(board, 1, 1, 0, "root", 0, 0)]
        tree_depth = 4
        empty_tiles = 1
        board = fringe.pop(0)
        succ = []
        current_index = 1
        succ += find_successors1(board[0], player, board[2], deterministic,
                                 tree_depth, board[5])

        for i in succ:
            fringe1.append(i)
            fringe.append(i)
            current_index += 1

        chance_flag = 0
        while len(fringe) > 0:
            fringe_temp = []
            board = fringe.pop(0)

            if board[2] < tree_depth:
                if board[1] in [1, 0]:
                    fringe_temp = [
                        chance_nodes for chance_nodes in find_chance(
                            board[0], board[1], board[2], board[5])
                    ]
                    fringe += fringe_temp
                    fringe1 += fringe_temp
                    chance_flag = board[1]

                else:
                    fringe_temp += find_successors1(board[0], chance_flag,
                                                    board[2], deterministic,
                                                    tree_depth, board[5])
                    fringe += fringe_temp
                    fringe1 += fringe_temp
            else:
                break

        count = expectiminimax(fringe1)
        dict = {0: 'L', 1: 'R', 2: 'U', 3: 'D'}

        yield dict[count]