forked from KeithGalli/Connect4-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainfile.swift
More file actions
274 lines (234 loc) · 8.27 KB
/
mainfile.swift
File metadata and controls
274 lines (234 loc) · 8.27 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import Foundation
let ROW_COUNT = 6
let COLUMN_COUNT = 7
let PLAYER = 0
let AI = 1
let EMPTY = 0
let PLAYER_PIECE = 1
let AI_PIECE = 2
let WINDOW_LENGTH = 4
func createBoard() -> [[Int]] {
return Array(repeating: Array(repeating: 0, count: COLUMN_COUNT), count: ROW_COUNT)
}
func dropPiece(board: inout [[Int]], row: Int, col: Int, piece: Int) {
board[row][col] = piece
}
func isValidLocation(board: [[Int]], col: Int) -> Bool {
return board[ROW_COUNT - 1][col] == 0
}
func getNextOpenRow(board: [[Int]], col: Int) -> Int? {
for r in 0..<ROW_COUNT {
if board[r][col] == 0 {
return r
}
}
return nil
}
func printBoard(board: [[Int]]) {
print("")
for row in board.reversed() {
print(row)
}
}
func winningMove(board: [[Int]], piece: Int) -> Bool {
// Horizontal check
for c in 0..<(COLUMN_COUNT - 3) {
for r in 0..<ROW_COUNT {
if board[r][c] == piece && board[r][c+1] == piece && board[r][c+2] == piece && board[r][c+3] == piece {
return true
}
}
}
// Vertical check
for c in 0..<COLUMN_COUNT {
for r in 0..<(ROW_COUNT - 3) {
if board[r][c] == piece && board[r+1][c] == piece && board[r+2][c] == piece && board[r+3][c] == piece {
return true
}
}
}
// Positive diagonal check
for c in 0..<(COLUMN_COUNT - 3) {
for r in 0..<(ROW_COUNT - 3) {
if board[r][c] == piece && board[r+1][c+1] == piece && board[r+2][c+2] == piece && board[r+3][c+3] == piece {
return true
}
}
}
// Negative diagonal check
for c in 0..<(COLUMN_COUNT - 3) {
for r in 3..<ROW_COUNT {
if board[r][c] == piece && board[r-1][c+1] == piece && board[r-2][c+2] == piece && board[r-3][c+3] == piece {
return true
}
}
}
return false
}
func evaluateWindow(window: [Int], piece: Int) -> Int {
var score = 0
let oppPiece = (piece == AI_PIECE) ? PLAYER_PIECE : AI_PIECE
if window.filter({ $0 == piece }).count == 4 {
score += 100
} else if window.filter({ $0 == piece }).count == 3 && window.filter({ $0 == EMPTY }).count == 1 {
score += 5
} else if window.filter({ $0 == piece }).count == 2 && window.filter({ $0 == EMPTY }).count == 2 {
score += 2
}
if window.filter({ $0 == oppPiece }).count == 3 && window.filter({ $0 == EMPTY }).count == 1 {
score -= 4
}
return score
}
func scorePosition(board: [[Int]], piece: Int) -> Int {
var score = 0
// Score center column
let centerArray = board.map { $0[COLUMN_COUNT / 2] }
let centerCount = centerArray.filter { $0 == piece }.count
score += centerCount * 3
// Score horizontal
for r in 0..<ROW_COUNT {
let rowArray = board[r]
for c in 0..<(COLUMN_COUNT - 3) {
let window = Array(rowArray[c..<(c + WINDOW_LENGTH)])
score += evaluateWindow(window: window, piece: piece)
}
}
// Score vertical
for c in 0..<COLUMN_COUNT {
let colArray = board.map { $0[c] }
for r in 0..<(ROW_COUNT - 3) {
let window = Array(colArray[r..<(r + WINDOW_LENGTH)])
score += evaluateWindow(window: window, piece: piece)
}
}
// Score positive diagonal
for r in 0..<(ROW_COUNT - 3) {
for c in 0..<(COLUMN_COUNT - 3) {
let window = (0..<WINDOW_LENGTH).map { board[r + $0][c + $0] }
score += evaluateWindow(window: window, piece: piece)
}
}
// Score negative diagonal
for r in 0..<(ROW_COUNT - 3) {
for c in 0..<(COLUMN_COUNT - 3) {
let window = (0..<WINDOW_LENGTH).map { board[r + 3 - $0][c + $0] }
score += evaluateWindow(window: window, piece: piece)
}
}
return score
}
func isTerminalNode(board: [[Int]]) -> Bool {
return winningMove(board: board, piece: PLAYER_PIECE) || winningMove(board: board, piece: AI_PIECE) || getValidLocations(board: board).isEmpty
}
func getValidLocations(board: [[Int]]) -> [Int] {
return (0..<COLUMN_COUNT).filter { isValidLocation(board: board, col: $0) }
}
func minimax(board: [[Int]], depth: Int, alpha: Int, beta: Int, maximizingPlayer: Bool) -> (Int?, Int) {
var alpha = alpha
var beta = beta
let validLocations = getValidLocations(board: board)
let isTerminal = isTerminalNode(board: board)
if depth == 0 || isTerminal {
if isTerminal {
if winningMove(board: board, piece: AI_PIECE) {
return (nil, 100000000000000)
} else if winningMove(board: board, piece: PLAYER_PIECE) {
return (nil, -10000000000000)
} else {
return (nil, 0)
}
} else {
return (nil, scorePosition(board: board, piece: AI_PIECE))
}
}
if maximizingPlayer {
var value = Int.min
var column = validLocations.randomElement()
for col in validLocations {
if let row = getNextOpenRow(board: board, col: col) {
var boardCopy = board
dropPiece(board: &boardCopy, row: row, col: col, piece: AI_PIECE)
let newScore = minimax(board: boardCopy, depth: depth - 1, alpha: alpha, beta: beta, maximizingPlayer: false).1
if newScore > value {
value = newScore
column = col
}
alpha = max(alpha, value)
if alpha >= beta {
break
}
}
}
return (column, value)
} else {
var value = Int.max
var column = validLocations.randomElement()
for col in validLocations {
if let row = getNextOpenRow(board: board, col: col) {
var boardCopy = board
dropPiece(board: &boardCopy, row: row, col: col, piece: PLAYER_PIECE)
let newScore = minimax(board: boardCopy, depth: depth - 1, alpha: alpha, beta: beta, maximizingPlayer: true).1
if newScore < value {
value = newScore
column = col
}
beta = min(beta, value)
if alpha >= beta {
break
}
}
}
return (column, value)
}
}
var board = createBoard()
var gameOver = false
var turn = 0
print("Board created")
printBoard(board: board)
while true {
printBoard(board: board)
// Player's turn
print("Enter the column (0-\(COLUMN_COUNT - 1)) where you want to drop your piece:")
if let input = readLine(), let playerCol = Int(input), playerCol >= 0, playerCol < COLUMN_COUNT {
if let playerRow = getNextOpenRow(board: board, col: playerCol) {
dropPiece(board: &board, row: playerRow, col: playerCol, piece: PLAYER_PIECE)
print("Player dropped piece in column \(playerCol)")
printBoard(board: board)
// Check for player win
if winningMove(board: board, piece: PLAYER_PIECE) {
print("Player wins!")
break
}
} else {
print("Column \(playerCol) is full. Try a different column.")
continue
}
} else {
print("Invalid input. Please enter a number between 0 and \(COLUMN_COUNT - 1).")
continue
}
// AI's turn
print("AI is making a move...")
let minimaxResult = minimax(board: board, depth: 4, alpha: Int.min, beta: Int.max, maximizingPlayer: true)
// Check if the result is valid
if let aiCol = minimaxResult.0, let aiRow = getNextOpenRow(board: board, col: aiCol) {
dropPiece(board: &board, row: aiRow, col: aiCol, piece: AI_PIECE)
print("AI dropped piece in column \(aiCol)")
printBoard(board: board)
// Check for AI win
if winningMove(board: board, piece: AI_PIECE) {
print("AI wins!")
break
}
// Check for draw
if getValidLocations(board: board).isEmpty {
print("The game is a draw!")
break
}
} else {
print("No valid moves for AI.")
break
}
}