-
Notifications
You must be signed in to change notification settings - Fork 0
/
HardAi.kt
212 lines (189 loc) · 7.45 KB
/
HardAi.kt
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
// Importing necessary packages
package com.dac.tictactoe.ai
// Defining the HardAI class
class HardAI {
internal var player = 'x'
internal var opponent = 'o'
internal inner class Move(var row: Int, var col: Int)
internal fun isMovesLeft(board: Array<CharArray>): Boolean {
for (i in 0..2)
for (j in 0..2)
if (board[i][j] == '_')
return true
return false
}
// Evaluation function
private fun evaluate(board: Array<CharArray>, symbol: Char): Int {
var score = 0
// Check for potential wins or blocks for the player
for (row in 0 until 3) {
val rowOccupiedByPlayer = (0 until 3).all { board[row][it] == player }
if (rowOccupiedByPlayer) {
score -= 100 // Player has a winning possibility
}
val rowOccupiedByOpponent = (0 until 3).all { board[row][it] == opponent }
if (rowOccupiedByOpponent) {
score += 100 // AI has a winning possibility
}
}
for (col in 0 until 3) {
val colOccupiedByPlayer = (0 until 3).all { board[it][col] == player }
if (colOccupiedByPlayer) {
score -= 100 // Player has a winning possibility
}
val colOccupiedByOpponent = (0 until 3).all { board[it][col] == opponent }
if (colOccupiedByOpponent) {
score += 100 // AI has a winning possibility
}
}
val mainDiagonalOccupiedByPlayer = (0 until 3).all { board[it][it] == player }
if (mainDiagonalOccupiedByPlayer) {
score -= 100 // Player has a winning possibility
}
val mainDiagonalOccupiedByOpponent = (0 until 3).all { board[it][it] == opponent }
if (mainDiagonalOccupiedByOpponent) {
score += 100 // AI has a winning possibility
}
val antiDiagonalOccupiedByPlayer = (0 until 3).all { board[it][2 - it] == player }
if (antiDiagonalOccupiedByPlayer) {
score -= 100 // Player has a winning possibility
}
val antiDiagonalOccupiedByOpponent = (0 until 3).all { board[it][2 - it] == opponent }
if (antiDiagonalOccupiedByOpponent) {
score += 100 // AI has a winning possibility
}
// Additional evaluation factors
// Center control
if (board[1][1] == symbol) {
score += 10
}
// Corner control
if (board[0][0] == symbol || board[0][2] == symbol || board[2][0] == symbol || board[2][2] == symbol) {
score += 5
}
// Edge control
if (board[0][1] == symbol || board[1][0] == symbol || board[1][2] == symbol || board[2][1] == symbol) {
score += 3
}
return score
}
// Minimax algorithm implementation
internal fun minimax(board: Array<CharArray>, depth: Int, isMax: Boolean): Int {
val score = evaluate(board, player)
if (score == 100 || score == -100)
return score
if (!isMovesLeft(board) || depth == 4) // Increased depth for deeper search
return 0
var bestVal = if (isMax) Int.MIN_VALUE else Int.MAX_VALUE
for (i in 0 until 3) {
for (j in 0 until 3) {
if (board[i][j] == '_') {
if (isMax) {
board[i][j] = player
bestVal = maxOf(bestVal, minimax(board, depth + 1, !isMax))
} else {
board[i][j] = opponent
bestVal = minOf(bestVal, minimax(board, depth + 1, !isMax))
}
board[i][j] = '_'
}
}
}
return bestVal
}
// Finding the best move
internal fun findBestMove(board: Array<CharArray>): Int {
// Check if AI has a winning possibility and prioritize it
for (i in 0 until 3) {
for (j in 0 until 3) {
if (board[i][j] == '_') {
board[i][j] = opponent
if (evaluate(board, opponent) == 100) {
board[i][j] = '_'
return i * 3 + j + 1
}
board[i][j] = '_'
}
}
}
// If AI doesn't have a winning possibility, defend against the player
for (i in 0 until 3) {
for (j in 0 until 3) {
if (board[i][j] == '_') {
board[i][j] = player
if (evaluate(board, player) == -100) {
board[i][j] = '_'
return i * 3 + j + 1
}
board[i][j] = '_'
}
}
}
// If neither player nor AI has a high chance to win, find the best move for the AI
var bestVal = Int.MIN_VALUE
var bestMove = -1
for (i in 0 until 3) {
for (j in 0 until 3) {
if (board[i][j] == '_') {
board[i][j] = player
val moveVal = minimax(board, 0, false)
board[i][j] = '_'
if (moveVal > bestVal) {
bestVal = moveVal
bestMove = i * 3 + j + 1
}
}
}
}
return bestMove
}
// Constructing the game board
internal fun constructBoard(player1: MutableList<Int>, comp: MutableList<Int>): Int {
val board = Array(3) { CharArray(3) }
if (player1.isEmpty() && comp.isEmpty()) {
// If both player1 and comp lists are empty, choose a random position
val randomPosition = (1..9).random()
return randomPosition
}
for (cellId in 1..9) {
if (player1.contains(cellId)) {
when (cellId) {
1 -> board[0][0] = 'x'
2 -> board[0][1] = 'x'
3 -> board[0][2] = 'x'
4 -> board[1][0] = 'x'
5 -> board[1][1] = 'x'
6 -> board[1][2] = 'x'
7 -> board[2][0] = 'x'
8 -> board[2][1] = 'x'
9 -> board[2][2] = 'x'
}
} else if (comp.contains(cellId)) {
when (cellId) {
1 -> board[0][0] = 'o'
2 -> board[0][1] = 'o'
3 -> board[0][2] = 'o'
4 -> board[1][0] = 'o'
5 -> board[1][1] = 'o'
6 -> board[1][2] = 'o'
7 -> board[2][0] = 'o'
8 -> board[2][1] = 'o'
9 -> board[2][2] = 'o'
}
} else {
when (cellId) {
1 -> board[0][0] = '_'
2 -> board[0][1] = '_'
3 -> board[0][2] = '_'
4 -> board[1][0] = '_'
5 -> board[1][1] = '_'
6 -> board[1][2] = '_'
7 -> board[2][0] = '_'
8 -> board[2][1] = '_'
9 -> board[2][2] = '_'
}
}
}
return findBestMove(board)
}
}