Skip to content

Update connect4_with_ai.py #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions connect4_with_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,22 @@ def minimax(board, depth, alpha, beta, maximizingPlayer):
return (None, score_position(board, AI_PIECE))
if maximizingPlayer:
value = -math.inf
column = random.choice(valid_locations)
best_col = []
for col in valid_locations:
row = get_next_open_row(board, col)
b_copy = board.copy()
drop_piece(b_copy, row, col, AI_PIECE)
new_score = minimax(b_copy, depth-1, alpha, beta, False)[1]
if new_score > value:
value = new_score
column = col
best_col = [col]
elif new_score == value:
best_col.append(col)
alpha = max(alpha, value)
if alpha >= beta:
break

column = random.choice(best_col)
return column, value

else: # Minimizing player
Expand Down