Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def minimax(board, depth, alpha, beta, max_player):
- counter = 0
- source_row = 0
- source_col = 0
- drop_phase = True
- b = copy.deepcopy(board)
- for i in range(5):
- for j in range(5):
- if b[i][j] == self.my_piece:
- counter = counter + 1
- if counter < 4:
- drop_phase = True
- else:
- drop_phase = False
- if drop_phase:
- valid_locations = get_valid_locations(b)
- if depth == 0 or (self.game_value(b)) == 1 or (self.game_value(b) == -1):
- if self.game_value(b) == 1 or self.game_value(b) == -1:
- if heuristic_game_value(b) == 1:
- return (None, None, 1, False)
- elif heuristic_game_value(b) == -1:
- return (None, None, -1, False)
- elif depth == 0:
- return (None, None, self.score_board(b, self.my_piece), False)
- if max_player:
- value = -1
- row,col = random.choice(valid_locations)[0],[1]
- for i in valid_locations:
- r, c = i[0], i[1]
- b_copy = copy.deepcopy(b)
- move = [(r, c)]
- b_copy = test_place(b_copy, move, self.my_piece)
- temp = minimax(b_copy, depth-1, alpha, beta, False)
- new_score = temp[2]
- if new_score > value:
- value = new_score
- row = r
- col = c
- alpha = max(alpha, value)
- if alpha >= beta:
- break
- return row, col, value, drop_phase
- else: #Min Player
- value = 1
- row,col = random.choice(valid_locations)[0],[1]
- for i in valid_locations:
- r, c = i[0], i[1]
- b_copy = copy.deepcopy(b)
- move = [(r, c)]
- b_copy = test_place(b_copy, move, self.my_piece)
- temp = minimax(b_copy, depth-1, alpha, beta, True)
- new_score = temp[2]
- if new_score < value:
- value = new_score
- row = r
- col = c
- beta = min(beta, value)
- if alpha >= beta:
- break
- return row, col, value, drop_phase
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement