-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai.py
More file actions
226 lines (179 loc) Β· 6.1 KB
/
Copy pathai.py
File metadata and controls
226 lines (179 loc) Β· 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
"""
Pyoneer - Connect 4 AI using Minimax with Alpha-Beta Pruning
A tiny, smart, and performant AI opponent.
"""
import math
import random
ROW_COUNT = 6
COLUMN_COUNT = 7
WINDOW_LENGTH = 4
AI_PIECE = 2
PLAYER_PIECE = 1
EMPTY = 0
# Search depth (higher = smarter but slower)
DEPTH = 5
def get_valid_locations(board):
"""Get all columns that can accept a piece."""
valid = []
for col in range(COLUMN_COUNT):
if board[ROW_COUNT - 1][col] == 0:
valid.append(col)
return valid
def get_next_open_row(board, col):
"""Find the next open row in a column."""
for r in range(ROW_COUNT):
if board[r][col] == 0:
return r
return None
def drop_piece_copy(board, row, col, piece):
"""Drop a piece on a copy of the board (doesn't modify original)."""
import numpy as np
new_board = board.copy()
new_board[row][col] = piece
return new_board
def is_terminal_node(board):
"""Check if the game is over (win or tie)."""
return (
check_win(board, PLAYER_PIECE)
or check_win(board, AI_PIECE)
or len(get_valid_locations(board)) == 0
)
def check_win(board, piece):
"""Check if a piece has won."""
# Horizontal
for c in range(COLUMN_COUNT - 3):
for r in range(ROW_COUNT):
if (
board[r][c] == piece
and board[r][c + 1] == piece
and board[r][c + 2] == piece
and board[r][c + 3] == piece
):
return True
# Vertical
for c in range(COLUMN_COUNT):
for r in range(ROW_COUNT - 3):
if (
board[r][c] == piece
and board[r + 1][c] == piece
and board[r + 2][c] == piece
and board[r + 3][c] == piece
):
return True
# Positive diagonal
for c in range(COLUMN_COUNT - 3):
for r in range(ROW_COUNT - 3):
if (
board[r][c] == piece
and board[r + 1][c + 1] == piece
and board[r + 2][c + 2] == piece
and board[r + 3][c + 3] == piece
):
return True
# Negative diagonal
for c in range(COLUMN_COUNT - 3):
for r in range(3, ROW_COUNT):
if (
board[r][c] == piece
and board[r - 1][c + 1] == piece
and board[r - 2][c + 2] == piece
and board[r - 3][c + 3] == piece
):
return True
return False
def score_window(window, piece):
"""Score a window of 4 slots."""
score = 0
opp_piece = PLAYER_PIECE if piece == AI_PIECE else AI_PIECE
if window.count(piece) == 4:
score += 100
elif window.count(piece) == 3 and window.count(EMPTY) == 1:
score += 5
elif window.count(piece) == 2 and window.count(EMPTY) == 2:
score += 2
# Block opponent
if window.count(opp_piece) == 3 and window.count(EMPTY) == 1:
score -= 4
return score
def score_position(board, piece):
"""Evaluate the entire board position."""
score = 0
# Favor center column
center_col = list(board[:, COLUMN_COUNT // 2])
center_count = center_col.count(piece)
score += center_count * 3
# Horizontal
for r in range(ROW_COUNT):
row_array = list(board[r, :])
for c in range(COLUMN_COUNT - 3):
window = row_array[c : c + WINDOW_LENGTH]
score += score_window(window, piece)
# Vertical
for c in range(COLUMN_COUNT):
col_array = list(board[:, c])
for r in range(ROW_COUNT - 3):
window = col_array[r : r + WINDOW_LENGTH]
score += score_window(window, piece)
# Positive diagonal
for r in range(ROW_COUNT - 3):
for c in range(COLUMN_COUNT - 3):
window = [board[r + i][c + i] for i in range(WINDOW_LENGTH)]
score += score_window(window, piece)
# Negative diagonal
for r in range(3, ROW_COUNT):
for c in range(COLUMN_COUNT - 3):
window = [board[r - i][c + i] for i in range(WINDOW_LENGTH)]
score += score_window(window, piece)
return score
def minimax(board, depth, alpha, beta, maximizing_player):
"""
Minimax algorithm with Alpha-Beta pruning.
Returns (column, score) tuple.
"""
valid_locations = get_valid_locations(board)
is_terminal = is_terminal_node(board)
if depth == 0 or is_terminal:
if is_terminal:
if check_win(board, AI_PIECE):
return (None, 100000000)
elif check_win(board, PLAYER_PIECE):
return (None, -100000000)
else: # Tie
return (None, 0)
else: # Depth is zero
return (None, score_position(board, AI_PIECE))
if maximizing_player:
value = -math.inf
best_col = random.choice(valid_locations)
for col in valid_locations:
row = get_next_open_row(board, col)
new_board = drop_piece_copy(board, row, col, AI_PIECE)
new_score = minimax(new_board, depth - 1, alpha, beta, False)[1]
if new_score > value:
value = new_score
best_col = col
alpha = max(alpha, value)
if alpha >= beta:
break # Beta cutoff
return best_col, value
else: # Minimizing player
value = math.inf
best_col = random.choice(valid_locations)
for col in valid_locations:
row = get_next_open_row(board, col)
new_board = drop_piece_copy(board, row, col, PLAYER_PIECE)
new_score = minimax(new_board, depth - 1, alpha, beta, True)[1]
if new_score < value:
value = new_score
best_col = col
beta = min(beta, value)
if alpha >= beta:
break # Alpha cutoff
return best_col, value
def get_best_move(board_obj):
"""
Public API: Get Pyoneer's best move.
Takes a Board object and returns the best column to play.
"""
col, _ = minimax(board_obj.board, DEPTH, -math.inf, math.inf, True)
return col