Advertisement
Guest User

Teeko Minimax

a guest
Apr 2nd, 2020
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.75 KB | None | 0 0
  1.  
  2. def minimax(board, depth, alpha, beta, max_player):
  3.            counter = 0
  4.            source_row = 0
  5.            source_col = 0
  6.            drop_phase = True
  7.            b = copy.deepcopy(board)
  8.            for i in range(5):
  9.                for j in range(5):
  10.                    if b[i][j] == self.my_piece:
  11.                        counter = counter + 1
  12.            if counter < 4:
  13.                drop_phase = True
  14.            else:
  15.                drop_phase = False
  16.                
  17.            if drop_phase:    
  18.                valid_locations = get_valid_locations(b)
  19.                if depth == 0 or (self.game_value(b)) == 1 or (self.game_value(b) == -1):
  20.                    if self.game_value(b) == 1 or self.game_value(b) == -1:
  21.                        if heuristic_game_value(b) == 1:
  22.                            return (None, None, 1, False)
  23.                        elif heuristic_game_value(b) == -1:
  24.                            return (None, None, -1, False)
  25.                    elif depth == 0:
  26.                        return (None, None, self.score_board(b, self.my_piece), False)
  27.                if max_player:
  28.                    value = -1
  29.                    row,col = random.choice(valid_locations)[0],[1]
  30.                    for i in valid_locations:
  31.                        r, c = i[0], i[1]
  32.                        b_copy = copy.deepcopy(b)
  33.                        move = [(r, c)]
  34.                        b_copy = test_place(b_copy, move, self.my_piece)
  35.                        temp = minimax(b_copy, depth-1, alpha, beta, False)
  36.                        new_score = temp[2]
  37.                        if new_score > value:
  38.                            value = new_score
  39.                            row = r
  40.                            col = c
  41.                        alpha = max(alpha, value)
  42.                        if alpha >= beta:
  43.                            break
  44.                    return row, col, value, drop_phase
  45.                
  46.                else: #Min Player
  47.                    value = 1
  48.                    row,col = random.choice(valid_locations)[0],[1]
  49.                    for i in valid_locations:
  50.                        r, c = i[0], i[1]
  51.                        b_copy = copy.deepcopy(b)
  52.                        move = [(r, c)]
  53.                        b_copy = test_place(b_copy, move, self.my_piece)
  54.                        temp = minimax(b_copy, depth-1, alpha, beta, True)
  55.                        new_score = temp[2]
  56.                        if new_score < value:
  57.                            value = new_score
  58.                            row = r
  59.                            col = c
  60.                        beta = min(beta, value)
  61.                        if alpha >= beta:
  62.                            break
  63.                    return row, col, value, drop_phase
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement