|
| 1 | +package engine |
| 2 | + |
| 3 | +import "github.com/RubikNube/GoInGo/cmd/game" |
| 4 | + |
| 5 | +// AlphaBetaEngine implements Engine using an evaluation function and alpha-beta pruning. |
| 6 | +type AlphaBetaEngine struct{} |
| 7 | + |
| 8 | +// Move in AlphaBetaEngine uses alpha-beta pruning to select the best move or pass if no beneficial move exists. |
| 9 | +func (e *AlphaBetaEngine) Move(board game.Board, player game.FieldState, ko *game.Point) *game.Point { |
| 10 | + bestScore := -1 << 30 |
| 11 | + var bestMove *game.Point |
| 12 | + depth := 3 // Shallow for performance; increase for stronger play |
| 13 | + moveFound := false |
| 14 | + |
| 15 | + for i := 0; i < 9; i++ { |
| 16 | + for j := 0; j < 9; j++ { |
| 17 | + if board[i][j] != game.Empty { |
| 18 | + continue |
| 19 | + } |
| 20 | + pt := game.Point{Row: i, Col: j} |
| 21 | + if ko != nil && pt.Row == ko.Row && pt.Col == ko.Col { |
| 22 | + continue |
| 23 | + } |
| 24 | + var nextBoard game.Board |
| 25 | + copy(nextBoard[:], board[:]) |
| 26 | + nextBoard[pt.Row][pt.Col] = player |
| 27 | + opp := game.Black |
| 28 | + if player == game.Black { |
| 29 | + opp = game.White |
| 30 | + } |
| 31 | + for _, n := range game.Neighbors(pt) { |
| 32 | + if nextBoard[n.Row][n.Col] == opp { |
| 33 | + group, libs := game.Group(nextBoard, n) |
| 34 | + if len(libs) == 0 { |
| 35 | + for stonePt := range group { |
| 36 | + nextBoard[stonePt.Row][stonePt.Col] = game.Empty |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + _, libs := game.Group(nextBoard, pt) |
| 42 | + if len(libs) == 0 { |
| 43 | + continue |
| 44 | + } |
| 45 | + score := -alphaBeta(nextBoard, opp, player, ko, depth-1, -1<<30, 1<<30) |
| 46 | + moveFound = true |
| 47 | + if score > bestScore { |
| 48 | + bestScore = score |
| 49 | + move := pt |
| 50 | + bestMove = &move |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + // Pass if no move found or if passing is as good or better than any move |
| 55 | + passScore := -alphaBeta(board, opponent(player), player, ko, depth-1, -1<<30, 1<<30) |
| 56 | + if !moveFound || passScore >= bestScore { |
| 57 | + return nil // pass |
| 58 | + } |
| 59 | + return bestMove |
| 60 | +} |
| 61 | + |
| 62 | +// opponent returns the opposite FieldState (Black <-> White). |
| 63 | +func opponent(player game.FieldState) game.FieldState { |
| 64 | + if player == game.Black { |
| 65 | + return game.White |
| 66 | + } |
| 67 | + return game.Black |
| 68 | +} |
| 69 | + |
| 70 | +// alphaBeta is a minimax search with alpha-beta pruning and pass support. |
| 71 | +func alphaBeta(board game.Board, player, opp game.FieldState, ko *game.Point, depth, alpha, beta int) int { |
| 72 | + if depth == 0 { |
| 73 | + return evaluate(board, player, opp) |
| 74 | + } |
| 75 | + foundMove := false |
| 76 | + for i := 0; i < 9; i++ { |
| 77 | + for j := 0; j < 9; j++ { |
| 78 | + if board[i][j] != game.Empty { |
| 79 | + continue |
| 80 | + } |
| 81 | + pt := game.Point{Row: i, Col: j} |
| 82 | + if ko != nil && pt.Row == ko.Row && pt.Col == ko.Col { |
| 83 | + continue |
| 84 | + } |
| 85 | + var nextBoard game.Board |
| 86 | + copy(nextBoard[:], board[:]) |
| 87 | + nextBoard[pt.Row][pt.Col] = player |
| 88 | + for _, n := range game.Neighbors(pt) { |
| 89 | + if nextBoard[n.Row][n.Col] == opp { |
| 90 | + group, libs := game.Group(nextBoard, n) |
| 91 | + if len(libs) == 0 { |
| 92 | + for stonePt := range group { |
| 93 | + nextBoard[stonePt.Row][stonePt.Col] = game.Empty |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + _, libs := game.Group(nextBoard, pt) |
| 99 | + if len(libs) == 0 { |
| 100 | + continue |
| 101 | + } |
| 102 | + foundMove = true |
| 103 | + score := -alphaBeta(nextBoard, opp, player, ko, depth-1, -beta, -alpha) |
| 104 | + if score > alpha { |
| 105 | + alpha = score |
| 106 | + } |
| 107 | + if alpha >= beta { |
| 108 | + return alpha |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + // Consider passing if no move found or passing is better |
| 113 | + passScore := -alphaBeta(board, opp, player, ko, depth-1, -beta, -alpha) |
| 114 | + if !foundMove || passScore > alpha { |
| 115 | + alpha = passScore |
| 116 | + } |
| 117 | + return alpha |
| 118 | +} |
| 119 | + |
| 120 | +// evaluate is a sophisticated evaluation function considering liberties, groups, and captures. |
| 121 | +func evaluate(board game.Board, player, opp game.FieldState) int { |
| 122 | + playerStones, oppStones := 0, 0 |
| 123 | + playerLibs, oppLibs := 0, 0 |
| 124 | + playerGroups, oppGroups := 0, 0 |
| 125 | + playerCapturable, oppCapturable := 0, 0 |
| 126 | + |
| 127 | + visited := make(map[game.Point]bool) |
| 128 | + for i := 0; i < 9; i++ { |
| 129 | + for j := 0; j < 9; j++ { |
| 130 | + pt := game.Point{Row: i, Col: j} |
| 131 | + if visited[pt] || board[i][j] == game.Empty { |
| 132 | + continue |
| 133 | + } |
| 134 | + group, libs := game.Group(board, pt) |
| 135 | + for stone := range group { |
| 136 | + visited[stone] = true |
| 137 | + } |
| 138 | + if board[i][j] == player { |
| 139 | + playerStones += len(group) |
| 140 | + playerLibs += len(libs) |
| 141 | + playerGroups++ |
| 142 | + if len(libs) == 1 { |
| 143 | + playerCapturable += len(group) |
| 144 | + } |
| 145 | + } else if board[i][j] == opp { |
| 146 | + oppStones += len(group) |
| 147 | + oppLibs += len(libs) |
| 148 | + oppGroups++ |
| 149 | + if len(libs) == 1 { |
| 150 | + oppCapturable += len(group) |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + // Weighted sum: stones, liberties, groups, capturability |
| 156 | + return (playerStones-oppStones)*10 + |
| 157 | + (playerLibs-oppLibs)*2 + |
| 158 | + (oppCapturable-playerCapturable)*8 + |
| 159 | + (playerGroups - oppGroups) |
| 160 | +} |
0 commit comments