예제 #1
0
 def eval_heuristic(self, board):
     score = board.get_score(self.player)
     pieces = 0
     if self.horde:
         if self.relative_horde:
             pieces = (pieces - board.get_pieces(self.opponent))
         else:
             pieces = board.get_pieces(self.player)
     if self.relative_score:
         score = (score - board.get_score(self.opponent))
     return score + pieces
예제 #2
0
def ai_battle():
    #tests the different heuristics by having ai play each other
    P1 = 0
    P2 = 1
    # multiprocess computaion
    parallel = True
    lookahead = 8  # AI lookahead depth, set to negative to search entire game
    board = Board()
    ai = AI(P1, lookahead, relative_score=False)
    #ai_horder = AI(P2, lookahead, horde=True) # hordes pieces on its side
    ai_relative = AI(P2,
                     lookahead,
                     relative_score=True,
                     horde=True,
                     relative_horde=True)  #horde relative is better then not
    next = random.randint(0, 1)

    starting_ai = next
    ai_cur = None  # ai with current turn

    while not board.game_over():

        if not board.has_move(next):
            next = (next + 1) % 2
        if next == ai.player:
            ai_cur = ai
        else:
            ai_cur = ai_relative
        move = ai_cur.move(board, parallel)
        print board
        ## get the move for the ai player
        #if ai.player == ai_basic.player:
        #   print 'Basic picked ', move+1
        #else:
        #   print 'Horder picked ', move+1
        next = board.move(ai_cur.player, move)

    print '         FINAL'
    print board
    p1_score = board.get_score(P1)
    p2_score = board.get_score(P2)

    if next == ai:
        print 'P1 Started'
    else:
        print 'P2  Started'
    if p1_score > p2_score:
        print 'P1 Wins!'
    elif p1_score < p2_score:
        print 'P2 Wins!'
    else:
        print 'It\'s a tie !'
    def play(self, board, silent=False):
        if not silent:
            print(board)
        turns = board.get_all_legal_moves(board.grid)
        t = self.choose_turn(turns, board)
        if self.previous_state:
            if hash_board(board.grid) in self.q_table:
                # with open("q_update.txt", "a") as f:
                #     f.write("old_q " + str(self.q_table[self.previous_state][self.previous_action]))
                #     f.write("\nmax " + str(max([self.q_table[hash_board(board.grid)][a]
                #             for a in self.q_table[hash_board(board.grid)]])))
                #     f.write("\ndifference " + str(max([self.q_table[hash_board(board.grid)][a]
                #             for a in self.q_table[hash_board(board.grid)]])
                #                 - self.q_table[self.previous_state][self.previous_action]))
                self.q_table[self.previous_state][self.previous_action] += (
                    LEARNING_RATE *
                    ((get_score(board.grid, board.turn) -
                      get_score(self.previous_grid, board.turn)) +
                     DISCOUNT * self.q_table[hash_board(board.grid)][str(t)] -
                     self.q_table[self.previous_state][self.previous_action]))
                # f.write("\n" + str(self.q_table[self.previous_state][self.previous_action]))
                # f.write("\n\n")
            else:
                self.q_table[self.previous_state][self.previous_action] += (
                    LEARNING_RATE *
                    (get_score(board.grid, board.turn) -
                     self.q_table[self.previous_state][self.previous_action]))
        # sleep(1)

        self.previous_grid = board.grid
        self.previous_state = hash_board(board.grid)
        self.previous_action = str(t)
        if self.previous_state not in self.q_table:
            action_dict = dict()
            for turn in turns:
                action_dict[str(turn)] = 0
            self.q_table[self.previous_state] = action_dict
        if not silent:
            print("Q-learn moves " + " to ".join([translate(c)
                                                  for c in t]) + "\n")
        for i in range(0, len(t) - 1):  # loop is here to support double jumps
            board.move(t[i], t[i + 1])
예제 #4
0
def main():
    P1 = 0
    P2 = 1
    # multiprocess computaion
    parallel = True
    lookahead = 6  # AI lookahead depth, set to negative to search entire game
    board = Board()
    # ai Player
    ai = AI(P2, lookahead)
    # starting player is random
    current_player = random.randint(0, 1)
    next = (current_player + 1) % 2
    move = 0
    while not board.game_over() and move != 'quit':
        print board
        print '\nP' + str(current_player + 1) + '\'s Turn'
        # if the current player has a move, else switch
        if board.has_move(current_player):
            # not ai turn, user turn
            if current_player != ai.player:
                move = ''
                next = current_player
                while current_player == next and board.has_move(
                        current_player) and move != 'quit':
                    move = get_user_move(board, current_player)
                    if not board.check_move(current_player, move):
                        print 'No pieces', move
                    if move != 'quit':
                        next = board.move(current_player, move)
                        print board
                        print 'Play again!'
                        print '\nP' + str(current_player + 1)

            else:
                # AI turn
                move = ai.move(board, parallel)
                # get the move for the ai player
                print '\tAI picked ', move + 1
                next = board.move(ai.player, move)
                # while AI has another move
                while ai.player == next and board.has_move(
                        ai.player) and move != 'quit':
                    print board
                    print '\tAI Playing Again...'
                    move = ai.move(board, parallel)
                    print '\tAI picked ', move + 1
                    next = board.move(ai.player, move)
            # set player to the next
            current_player = next
        else:
            print '\n P' + str(current_player + 1) + ' has no moves!'
            current_player = (current_player + 1) % 2

    # If game is over and user did not quit
    if move != 'quit':
        print '         FINAL'
        print board
        p1_score = board.get_score(P1)
        p2_score = board.get_score(P2)
        if p1_score > p2_score:
            print 'Player 1 Wins!'
        elif p1_score < p2_score:
            print 'Player 2 Wins!'
        else:
            print 'It\'s a tie !'
    print 'Goodbye!'