Skip to content

Commit

Permalink
pcplayer: fix bug in minmax engine
Browse files Browse the repository at this point in the history
  • Loading branch information
gucio321 committed Jan 17, 2024
1 parent 8899aca commit 9fd560e
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions pkg/core/pcplayer/pc_player_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (p *PCPlayer) minMax(gameBoard *board.Board, maxDepth int) (i int) {
func (a *PCPlayer) mm(
gameBoard *board.Board, l letter.Letter, currentDepth int,
maxDepth int, mutex *sync.Mutex, best *int, move *int, couldWin *bool,
) (resultCanWin bool, resultMove int) {
) {
//logger.Debugf("mm: call for %s (depth: %d)\n%s", l, currentDepth, gameBoard)
mutex.Lock()
if *best <= currentDepth && *couldWin {
Expand All @@ -128,30 +128,26 @@ func (a *PCPlayer) mm(
cp.SetIndexState(i, l)
if winner, u := cp.IsWinner(l); winner {
logger.Debugf("Can win at %d (combo %v) Depth %d", i, u, currentDepth)

return true, i
}

//logger.Debugf("re-running for opposite letter")
wg.Add(1)
go func() {
cw, m := a.mm(cp, l.Opposite(), currentDepth+1, maxDepth, mutex, best, move, couldWin)
mutex.Lock()
if cw && (!*couldWin || (*couldWin && *best > currentDepth)) {
if !*couldWin || (*couldWin && *best > currentDepth) {
//logger.Warnf("Found move %d at depth %d (Potential winner is %s)", m, currentDepth, l)
//logger.Debugf("mm depth %d: updated best move to %d (on depth %d)", currentDepth, cpMove, cpDepth)
*best = currentDepth
*move = m
*move = i
*couldWin = true
}
mutex.Unlock()
}

//logger.Debugf("re-running for opposite letter")
wg.Add(1)
go func() {
a.mm(cp, l.Opposite(), currentDepth+1, maxDepth, mutex, best, move, couldWin)
wg.Done()
}()
}

wg.Wait()

return false, 0
}

//nolint:gocyclo,funlen // https://github.com/gucio321/tic-tac-go/issues/154
Expand Down

0 comments on commit 9fd560e

Please sign in to comment.