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 get_state_space(board, player, depth): count = 0 if not board.game_over() and depth > 0: moves = [] # [(board,player) , ...] # search siblings for i in range(0, 6): if board.check_move(player, i): count += 1 board_copy = Board(board) next_player = board_copy.move(player, i) moves.append((board_copy, next_player)) # search sibling children for move in moves: count += get_state_space(move[0], move[1], depth - 1) return count
def alphabeta(self, board, alpha, beta, player, depth): value = 0 # cound does not update correct when threaded, only works with serial move self.search_count += 1 # traverse entire game to find best move if board.game_over() or depth == 0: value = self.eval_heuristic(board) elif player == self.player: cut = False value = -48 i = 0 while i < 6 and not cut: board_copy = Board(board) if board_copy.check_move(self.player, i): next_player = board_copy.move(self.player, i) value = max( value, self.alphabeta(board_copy, alpha, beta, next_player, depth - 1)) alpha = max(value, alpha) if alpha >= beta: cut = True else: # penalize no moves alpha = -48 i += 1 else: # opponent cut = False value = 48 i = 0 # for each opponent move, check if its valid, if so get the value of the next possible move while i < 6 and not cut: board_copy = Board(board) # if i is a valid move if board_copy.check_move(self.opponent, i): next_player = board_copy.move(self.opponent, i) value = min( value, self.alphabeta(board_copy, alpha, beta, next_player, depth - 1)) beta = min(value, beta) if alpha >= beta: cut = True else: # no moves beta = 48 i += 1 return value
def move_minimax(self, board, player, depth=0): if game_over(board): return game_score(board, self.player) moves = possible_moves(board) # first move if len(moves) == 9: # angles move = choice([0, 2, 6, 8]) return [0, move] if player == self.player: maxEval = [-inf, None] for move in moves: new_board = deepcopy(board) row, col = move // 3, move % 3 new_board[row][col] = player score = self.move_minimax(new_board, player * -1, depth + 1) if score[0] >= maxEval[0]: maxEval = score maxEval[1] = move return maxEval else: minEval = [inf, None] for move in moves: new_board = deepcopy(board) row, col = move // 3, move % 3 new_board[row][col] = player score = self.move_minimax(new_board, player * -1, depth + 1) if score[0] <= minEval[0]: minEval = score minEval[1] = move return minEval
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!'