From 695ea428ae2167035fb1e4794619a6a200d88b1f Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Mon, 8 Jan 2024 10:34:45 -0800 Subject: [PATCH] Fix space value calculations when playing tabula --- analysis.go | 2 +- board.go | 22 +++++++++++----------- cmd/tabula/main.go | 13 +++++++------ 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/analysis.go b/analysis.go index 386abba..624ca70 100644 --- a/analysis.go +++ b/analysis.go @@ -88,7 +88,7 @@ func (a *Analysis) _analyze() { } checkers := checkers(o, a.Board[move[1]]) if checkers == 1 { - hs += PseudoPips(a.player, move[1]) + hs += PseudoPips(a.player, move[1], a.Board[SpaceVariant]) } a.Board = a.Board.UseRoll(move[0], move[1], a.player).Move(move[0], move[1], a.player) } diff --git a/board.go b/board.go index 3a3e4d2..5d5dc0d 100644 --- a/board.go +++ b/board.go @@ -521,18 +521,18 @@ func (b Board) Pips(player int8) int { var pips int if b[SpaceVariant] != VariantBackgammon { if player == 1 && b[SpaceEnteredPlayer] == 0 { - pips += int(checkers(player, b[SpaceHomePlayer])) * PseudoPips(player, SpaceHomePlayer) + pips += int(checkers(player, b[SpaceHomePlayer])) * PseudoPips(player, SpaceHomePlayer, b[SpaceVariant]) } else if player == 2 && b[SpaceEnteredOpponent] == 0 { - pips += int(checkers(player, b[SpaceHomeOpponent])) * PseudoPips(player, SpaceHomeOpponent) + pips += int(checkers(player, b[SpaceHomeOpponent])) * PseudoPips(player, SpaceHomeOpponent, b[SpaceVariant]) } } if player == 1 { - pips += int(checkers(player, b[SpaceBarPlayer])) * PseudoPips(player, SpaceBarPlayer) + pips += int(checkers(player, b[SpaceBarPlayer])) * PseudoPips(player, SpaceBarPlayer, b[SpaceVariant]) } else { - pips += int(checkers(player, b[SpaceBarOpponent])) * PseudoPips(player, SpaceBarOpponent) + pips += int(checkers(player, b[SpaceBarOpponent])) * PseudoPips(player, SpaceBarOpponent, b[SpaceVariant]) } for space := int8(1); space < 25; space++ { - pips += int(checkers(player, b[space])) * PseudoPips(player, space) + pips += int(checkers(player, b[space])) * PseudoPips(player, space, b[SpaceVariant]) } return pips } @@ -542,7 +542,7 @@ func (b Board) Blots(player int8) int { var pips int for space := int8(1); space < 25; space++ { if checkers(player, b[space]) == 1 { - pips += PseudoPips(o, space) + pips += PseudoPips(o, space, b[SpaceVariant]) } } return pips @@ -776,19 +776,19 @@ func opponent(player int8) int8 { return 1 } -func spaceValue(player int8, space int8) int { +func spaceValue(player int8, space int8, variant int8) int { if space == SpaceHomePlayer || space == SpaceHomeOpponent || space == SpaceBarPlayer || space == SpaceBarOpponent { return 25 - } else if player == 1 { + } else if player == 1 || variant == VariantTabula { return int(space) } else { return int(25 - space) } } -func PseudoPips(player int8, space int8) int { - v := 6 + spaceValue(player, space) + int(math.Exp(float64(spaceValue(player, space))*0.2))*2 - if space == SpaceHomePlayer || space == SpaceHomeOpponent || (player == 1 && (space > 6 || space == SpaceBarPlayer)) || (player == 2 && (space < 19 || space == SpaceBarOpponent)) { +func PseudoPips(player int8, space int8, variant int8) int { + v := 6 + spaceValue(player, space, variant) + int(math.Exp(float64(spaceValue(player, space, variant))*0.2))*2 + if space == SpaceHomePlayer || space == SpaceHomeOpponent || (variant == VariantTabula && space < 13) || (variant != VariantTabula && ((player == 1 && (space > 6 || space == SpaceBarPlayer)) || (player == 2 && (space < 19 || space == SpaceBarOpponent)))) { v += 24 } return v diff --git a/cmd/tabula/main.go b/cmd/tabula/main.go index 37aa9cd..023ab00 100644 --- a/cmd/tabula/main.go +++ b/cmd/tabula/main.go @@ -18,10 +18,13 @@ func main() { flag.Parse() if pips { - fmt.Println("| Space | Pseudopips |") - fmt.Println("| --- | --- |") - for space := int8(1); space <= 25; space++ { - fmt.Printf("| %d | %d |\n", space, tabula.PseudoPips(1, space)) + for player := int8(1); player < 3; player++ { + fmt.Printf("Player %d:\n", player) + fmt.Println("| Space | Pseudopips |") + fmt.Println("| --- | --- |") + for space := int8(1); space <= 25; space++ { + fmt.Printf("| %d | %d |\n", space, tabula.PseudoPips(player, space, tabula.VariantBackgammon)) + } } return } @@ -37,8 +40,6 @@ func main() { analysis := make([]*tabula.Analysis, 0, tabula.AnalysisBufferSize) - // should be 3/1 then 5/0 in acey, then fix backgammon movement - // test cases for issues first then fix until tests pass b := tabula.Board{0, 5, 2, 5, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, -2, -2, -2, -3, -3, -2, 0, 1, 0, 0, 0, 5, 2, 0, 0, 1, 1, 1} log.Println(b[24]) b.Print()