From 562e390db115cb30535574dacb5f85e7c91553d5 Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Tue, 23 Apr 2024 14:38:31 +0200 Subject: [PATCH] added Minimax Algorithm to the game --- opponent/minimax.go | 84 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/opponent/minimax.go b/opponent/minimax.go index b84e088..ada729f 100644 --- a/opponent/minimax.go +++ b/opponent/minimax.go @@ -1 +1,85 @@ package opponent + +import ( + "math" + "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 { + // check if the game has ended + 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 := math.MinInt64 + 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 := math.MaxInt64 + 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" + } +}