def ai_move(self):
     player_obj = self.players[self.game.current_player]
     try:
         i, j = player_obj.get_move(self.game)
         player = "Dark" if self.game.current_player == 1 else "Light"
         player = "{} {}".format(player_obj.name, player)
         self.log("{}: {},{}".format(player, i, j))
         self.game.play(i, j)
         self.draw_board()
         if not get_possible_moves(self.game.board,
                                   self.game.current_player):
             if get_score(self.game.board)[0] > get_score(
                     self.game.board)[1]:
                 self.shutdown("Dark wins!")
             elif get_score(self.game.board)[0] < get_score(
                     self.game.board)[1]:
                 self.shutdown("White wins!")
             else:
                 self.shutdown("Tie!")
         elif isinstance(self.players[self.game.current_player],
                         AiPlayerInterface):
             self.root.after(1, lambda: self.ai_move())
         else:
             self.root.bind("<Button-1>", lambda e: self.mouse_pressed(e))
     except AiTimeoutError:
         self.shutdown("Game Over, {} lost (timeout)".format(
             player_obj.name))
Beispiel #2
0
def compute_utility(board, color):
    if color == 2:
        co = 1
    elif color == 1:
        co = 2
    diff = get_score(board)[color - 1] - get_score(board)[co - 1]
    return diff
Beispiel #3
0
def compute_heuristic(board, color):
    #IMPLEMENT

    #1. Stable position (Corners & X and C squares & Edges)
    #The four corners are the most important position that need to occupy, so increase these positions' weight
    #The opponent can take the corner when one of three position that adjacent to the corner is occupied, so lower these positions' weight
    #The opponent cannot surround edge positions, so increase these positions' weight

    #2. Mobilitiy
    #Count the number of moves that the player can make for the current board.

    #3. In the opening, mid-game, and end-game, use different weight strategy.

    corners = compute_corners(board, color) * 20

    xc_squares = -compute_xc_squares(board, color) * 10
    edge_count, frontier_count = compute_edge(board, color)

    utility = compute_utility(board, color) * 2
    mobilitiy = compute_mobility(board, color)

    game = get_score(board)[0] + get_score(board)[1]
    open_game = len(board) // 3
    mid_game = len(board) // 3 * 2
    end_game = len(board)

    return corners + xc_squares + edge_count + utility + mobilitiy
Beispiel #4
0
def play_game(game, player1, player2):
    players = [None, player1, player2]

    while True:
        player_obj = players[game.current_player]
        possible_moves = game.get_possible_moves()
        if not possible_moves:
            p1score, p2score = get_score(game.board)
            print("FINAL: {} (dark) {}:{} {} (light)".format(
                player1.name, p1score, p2score, player2.name))
            player1.kill(game)
            player2.kill(game)
            break
        else:
            color = "dark" if game.current_player == 1 else "light"
            try:
                i, j = player_obj.get_move(game)
                print("{} ({}) plays {},{}".format(player_obj.name, color, i,
                                                   j))
                game.play(i, j)
            except AiTimeoutError:
                p1score, p2score = get_score(game.board)
                print("{} ({}) timed out!".format(player_obj.name, color))
                print("FINAL: {} (dark) {}:{} {} (light)".format(
                    player_obj.name, p1score, p2score, player2.name))
                player1.kill(game)
                player2.kill(game)
                break
Beispiel #5
0
def compute_utility(board, color):
    #IMPLEMENT
    (num_dark, num_light) = get_score(board)
    if color == 1:  # dark
        return num_dark - num_light
    if color == 2:  # light
        return num_light - num_dark
Beispiel #6
0
def compute_heuristic(board, color):  #not implemented, optional
    # IMPLEMENT
    # Board size
    d = len(board)

    # Minimize the number of disks the opponent
    result = get_score(board)
    if color == 1:
        utility = result[0] - result[1]
    else:
        utility = result[1] - result[0]

    # Minimize the number of moves the opponent can make
    dark_moves = len(get_possible_moves(board, 1))
    light_moves = len(get_possible_moves(board, 2))
    if color == 1:
        mobility = dark_moves - light_moves
    else:
        mobility = light_moves - dark_moves

    # Check board size to prevent index out of range
    if d < 4:
        return utility + mobility

    # Now the rest satisfies board size >= 4
    dark = 0
    light = 0

    # Highly value taking corner fields
    corners = [(0, 0), (d - 1, 0), (0, d - 1), (d - 1, d - 1)]
    for (column, row) in corners:
        if board[column][row] == 1:
            dark += 500
        elif board[column][row] == 2:
            light += 500

    # Highly penalize taking the fields next to the corners
    near_corners = [(1, 0), (0, 1), (1, d - 1), (d - 1, 1), (0, d - 2),
                    (d - 2, 0), (d - 1, d - 2), (d - 2, d - 1)]
    for (column, row) in near_corners:
        if board[column][row] == 1:
            dark -= 50
        elif board[column][row] == 2:
            light -= 50

    # Value other border tiles than remaining tiles
    for i in range(2, d - 2):
        edges = [(0, i), (i, 0), (i, d - 1), (d - 1, i)]
        for (column, row) in edges:
            if board[column][row] == 1:
                dark += 50
            elif board[column][row] == 2:
                light += 50

    if color == 1:
        weight = dark - light
    else:
        weight = light - dark

    return utility + mobility + weight
def compute_utility(board, color):
    disk_num = get_score(board)
    if (color == 1):
        ut = disk_num[0] - disk_num[1]
    else:
        ut = disk_num[1] - disk_num[0]
    return ut
Beispiel #8
0
def compute_utility(board, color):
    scores = get_score(board)
    if color==1:
      utility = scores[0]-scores[1]
    else:
      utility = scores[1] - scores[0]
    return utility
def select_move_minimax(board, color, move_num, time_remaining, time_opponent,
                        ply):
    moves = get_possible_moves(board, color)

    if not isinstance(moves, list):
        score1, score2 = get_score(board)
        boardScore = [score1, score2]
        return boardScore, None

    # if time_remaining < 10:
    #    return (0, max(moves, key=lambda move: self.greedy(board, color, move)) )

    return_move = moves[0]
    bestscore = -float('inf')

    #ply = 4
    #will define ply later;
    for move in moves:
        newboard = board[:]
        newboard = play_move(newboard, color, move[0], move[1])

        if (color == 1):
            opp = 2
        else:
            opp = 1
        score = minimax_min_node(newboard, opp, move_num, ply)
        if score > bestscore:
            bestscore = score
            return_move = move
    #return (bestscore,return_move)
    return return_move
Beispiel #10
0
def compute_utility(board, color):
    #IMPLEMENT
    score1, score2 = get_score(board)
    if color == 1:
        return score1 - score2
    else:
        return score2 - score1
Beispiel #11
0
 def draw_board(self):
     self.draw_grid()
     self.draw_disks()
     player = "Dark" if self.game.current_player == 1 else "Light"
     self.move_label["text"] = player
     self.score_label["text"] = "Dark {} : {} Light".format(
         *get_score(self.game.board))
Beispiel #12
0
def compute_utility(board, color):
    #IMPLEMENT
    dark, light = get_score(board)
    if(color == 1): #dark
        return dark - light
    if(color == 2): #light
        return light - dark
Beispiel #13
0
    def get_move(self, manager):
        white_score, dark_score = get_score(manager.board)
        self.process.stdin.write("SCORE {} {}\n".format(
            white_score, dark_score).encode("ASCII"))
        self.process.stdin.flush()
        self.process.stdin.write("{}\n".format(str(
            manager.board)).encode("ASCII"))
        self.process.stdin.flush()

        timer = Timer(AiPlayerInterface.TIMEOUT, lambda: self.timeout())
        self.timed_out = False
        timer.start()
        start = datetime.datetime.now()
        # Wait for the AI call
        move_s = self.process.stdout.readline().decode("ASCII")
        last = datetime.datetime.now() - start
        print(last)

        if self.timed_out:
            raise AiTimeoutError
        timer.cancel()
        i_s, j_s = move_s.strip().split()
        i = int(i_s)
        j = int(j_s)
        return i, j
def compute_utility(board, color):
    a = get_score(board)
    if color == 1:
        b = a[color] - a[1]
    else:
        b = a[color] - a[2]
    return b
def compute_utility(board, color):
    score = get_score(board)
    #returns tuple (# of dark disks, # of light disks)
    if color == 1:
      return score[0]-score[1]
    else:
      return score[1]-score[0]
Beispiel #16
0
def compute_utility(board, color):
    #IMPLEMENT
    dark, light = get_score(board)
    if color == 1:  # player is black
        return dark - light
    else:  # color == 2 (i.e., player is white)
        return light - dark
Beispiel #17
0
def compute_utility(board, color):
    disk_count = get_score(board)
    dark_disk = disk_count[0]
    light_disk = disk_count[1]
    if color == 1:
        return dark_disk - light_disk
    elif color == 2:
        return light_disk - dark_disk
Beispiel #18
0
def compute_utility(board, color):
    score = get_score(board)
    if color == 1:
        return score[0] - score[1]
    elif color == 2:
        return score[1] - score[0]
    else:
        return 0
Beispiel #19
0
def compute_utility(board, color):
    score = get_score(board)
    #light
    if color == 2:
        return score[1] - score[0]
        #dark
    elif color == 1:
        return score[0] - score[1]
Beispiel #20
0
def compute_utility(board, color):
    result = get_score(board)
    dark = result[0]
    white = result[1]
    if color == 1:
        return dark - white
    else:
        return white - dark
Beispiel #21
0
def compute_utility(board, color):

    dark, light = get_score(board)

    if color == 2:
        return light - dark
    else:
        return dark - light
def compute_utility(board, color):
    #IMPLEMENT
    scores = get_score(board)
    if color == 1:
        score_final = scores[0] - scores[1]
    elif color == 2:
        score_final = scores[1] - scores[0]
    return score_final
Beispiel #23
0
def compute_utility(board, color):
    #IMPLEMENT
    (dark, light) = get_score(board)
    if color == 1:  #dark
        return dark - light
    if color == 2:  #light
        return light - dark
    else:
        return False
Beispiel #24
0
def compute_utility(board, color):
    p1, p2 = get_score(board)

    if color == 1:
        return p1 - p2
    elif color == 2:
        return p2 - p1

    return 0
def compute_utility(board, color):
    """
    Return the utility of the given board state
    (represented as a tuple of tuples) from the perspective
    of the player "color" (1 for dark, 2 for light)
    """
    u1, u2 = get_score(board)
    if color == 1: return u1 - u2
    if color == 2: return u2 - u1
Beispiel #26
0
def compute_utility(board, color):
    #IMPLEMENT
    p1, p2 = get_score(board)
   # eprint(get_score(board))
#    eprint(color)
    if color == 1: #dark
        return p1 - p2
    else:
        return p2 - p1 #change this!
Beispiel #27
0
def compute_utility(board, color):
    #IMPLEMENT

    p1_count, p2_count = get_score(board)
    #Player color: 1 for dark (goes first), 2 for light.

    if (color == 1):
        return p1_count - p2_count
    else:
        return p2_count - p1_count
Beispiel #28
0
def compute_utility(board, color):
    # color 1 - dark
    # color 2 - light
    # IMPLEMENT
    dark_score, light_score = get_score(board)
    # default assuming color 1
    utility = dark_score - light_score
    if color == 2:
        utility = -utility
    return utility
Beispiel #29
0
def compute_utility(board, color):
    #IMPLEMENT
    # get_score(board) returns a tuple (number of dark disks, number of light disks)
    result = get_score(board)

    # utility calculated as the number of disks of the player's colour minus the number of disks of the opponent
    if color == 1:
        return result[0] - result[1]
    else:
        return result[1] - result[0]
Beispiel #30
0
def compute_utility(board, color):
    """
    Return the utility of the given board state
    (represented as a tuple of tuples) from the perspective
    of the player "color" (1 for dark, 2 for light)
    """
    points = get_score(board)  #p1 is dark, p2 is light
    if color == 1:
        return points[0] - points[1]
    else:
        return points[1] - points[0]