85 lines
2.2 KiB
Go
85 lines
2.2 KiB
Go
package opponent
|
|
|
|
import (
|
|
"tictactoe/gameControl"
|
|
)
|
|
|
|
type NextMove struct {
|
|
Row int
|
|
Col int
|
|
score int
|
|
}
|
|
|
|
// iterative implementation of minimax algorithm
|
|
func Minimax(board [3][3]string, depth int, isMaximizingPlayer bool, player string) NextMove {
|
|
// Evaluate the Board based on who would win the current board
|
|
if gameControl.PlayerHasWon(board, "X") {
|
|
return NextMove{score: -10 + depth}
|
|
} else if gameControl.PlayerHasWon(board, "O") {
|
|
return NextMove{score: 10 - depth}
|
|
} else if gameControl.BoardisFull(board) {
|
|
return NextMove{score: 0}
|
|
}
|
|
// check who's turn it is
|
|
if isMaximizingPlayer {
|
|
// set max score
|
|
bestScore := -10000
|
|
var bestMove NextMove
|
|
// iterate through all fields
|
|
for i := 0; i < 3; i++ {
|
|
for j := 0; j < 3; j++ {
|
|
// check if field is empty
|
|
if board[i][j] == " " {
|
|
// make a move on this field
|
|
board[i][j] = player
|
|
// continue with the other player
|
|
move := Minimax(board, depth+1, false, SwitchPlayer(player))
|
|
// reset the field again
|
|
board[i][j] = " "
|
|
// if this move was better then the move before change values
|
|
if move.score > bestScore {
|
|
bestScore = move.score
|
|
bestMove = NextMove{Row: i, Col: j, score: move.score}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// return best possible Move
|
|
return bestMove
|
|
} else {
|
|
// set min score
|
|
bestScore := +10000
|
|
var bestMove NextMove
|
|
// iterate through all fields
|
|
for i := 0; i < 3; i++ {
|
|
for j := 0; j < 3; j++ {
|
|
// check if field is empty
|
|
if board[i][j] == " " {
|
|
// make a move on this field
|
|
board[i][j] = player
|
|
// continue with the other player
|
|
move := Minimax(board, depth+1, true, SwitchPlayer(player))
|
|
// reset the field again
|
|
board[i][j] = " "
|
|
// if this move was better then the move before change values
|
|
if move.score < bestScore {
|
|
bestScore = move.score
|
|
bestMove = NextMove{Row: i, Col: j, score: move.score}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// return best possible Move
|
|
return bestMove
|
|
}
|
|
}
|
|
|
|
// this function changes the player for the minimax algorthim
|
|
func SwitchPlayer(player string) string {
|
|
if player == "X" {
|
|
return "O"
|
|
} else {
|
|
return "X"
|
|
}
|
|
}
|