Skip calculating opponent moves when choosing doubles in acey-deucey

This commit is contained in:
Trevor Slocum 2024-04-22 13:06:57 -07:00
parent 691c7a5d52
commit 0e7e8d0792
5 changed files with 27 additions and 17 deletions

View file

@ -75,6 +75,7 @@ type Analysis struct {
player int8
hitScore int
chance int
skipOpp bool
wg *sync.WaitGroup
}
@ -97,7 +98,7 @@ func (a *Analysis) _analyze() {
}
a.Board.evaluate(a.player, hs, a)
if a.player == 1 && !a.Past {
if a.player == 1 && !a.Past && !a.skipOpp {
a.wg.Add(21)
for j := 0; j < 21; j++ {
j := j

2
bei.go
View file

@ -56,7 +56,7 @@ func (s *BEIServer) handleConnection(conn net.Conn) {
}
available, _ := b.Available(1)
b.Analyze(available, &analysis)
b.Analyze(available, &analysis, false)
var move *bei.Move
if len(analysis) > 0 {
move = &bei.Move{}

View file

@ -345,6 +345,19 @@ func (b Board) UseRoll(from int8, to int8, player int8) Board {
}
func (b Board) _available(player int8) [][2]int8 {
var bearOff int
mayBearOff := func() bool {
if bearOff != 0 {
return bearOff == 1
}
if b.MayBearOff(player) {
bearOff = 1
return true
}
bearOff = 2
return false
}
homeSpace := SpaceHomePlayer
barSpace := SpaceBarPlayer
opponentBarSpace := SpaceBarOpponent
@ -353,7 +366,6 @@ func (b Board) _available(player int8) [][2]int8 {
barSpace = SpaceBarOpponent
opponentBarSpace = SpaceBarPlayer
}
mayBearOff := b.MayBearOff(player)
onBar := b[barSpace] != 0
var moves [][2]int8
@ -372,7 +384,7 @@ func (b Board) _available(player int8) [][2]int8 {
}
if player == 1 && b[SpaceVariant] != VariantTabula {
for to := int8(0); to < from; to++ {
if to == SpaceBarPlayer || to == SpaceBarOpponent || to == SpaceHomeOpponent || (to == SpaceHomePlayer && !mayBearOff) {
if to == SpaceBarPlayer || to == SpaceBarOpponent || to == SpaceHomeOpponent || (to == SpaceHomePlayer && !mayBearOff()) {
continue
}
v := b[to]
@ -393,7 +405,7 @@ func (b Board) _available(player int8) [][2]int8 {
} else if player == 2 && to == SpaceHomePlayer {
to = SpaceHomeOpponent
}
if to == SpaceBarPlayer || to == SpaceBarOpponent || (((player == 1 && to == SpaceHomePlayer) || (player == 2 && to == SpaceHomeOpponent)) && !mayBearOff) {
if to == SpaceBarPlayer || to == SpaceBarOpponent || (((player == 1 && to == SpaceHomePlayer) || (player == 2 && to == SpaceHomeOpponent)) && !mayBearOff()) {
continue
}
v := b[to]
@ -412,16 +424,12 @@ func (b Board) _available(player int8) [][2]int8 {
func (b Board) Available(player int8) ([][4][2]int8, []Board) {
var allMoves [][4][2]int8
resultMutex := &sync.Mutex{}
movesFound := func(moves [4][2]int8) bool {
resultMutex.Lock()
for i := range allMoves {
if movesEqual(allMoves[i], moves) {
resultMutex.Unlock()
return true
}
}
resultMutex.Unlock()
return false
}
@ -692,7 +700,7 @@ func (b Board) Evaluation(player int8, hitScore int, moves [4][2]int8) *Analysis
return a
}
func (b Board) Analyze(available [][4][2]int8, result *[]*Analysis) {
func (b Board) Analyze(available [][4][2]int8, result *[]*Analysis, skipOpponent bool) {
if len(available) == 0 {
*result = (*result)[:0]
return
@ -729,6 +737,7 @@ func (b Board) Analyze(available [][4][2]int8, result *[]*Analysis) {
Past: past,
player: 1,
chance: 1,
skipOpp: skipOpponent,
result: r,
resultMutex: &sync.Mutex{},
wg: w,
@ -875,7 +884,7 @@ func (b Board) ChooseDoubles(result *[]*Analysis) int {
bc[SpaceRoll1], bc[SpaceRoll2], bc[SpaceRoll3], bc[SpaceRoll4] = doubles, doubles, doubles, doubles
available, _ = bc.Available(1)
bc.Analyze(available, result)
bc.Analyze(available, result, true)
if len(*result) > 0 && (*result)[0].Score < bestScore {
bestDoubles = i + 1
bestScore = (*result)[0].Score

View file

@ -340,7 +340,7 @@ func TestHitScore(t *testing.T) {
b := Board{0, 0, -2, -2, -2, 4, 0, -1, 0, 0, -2, 4, 0, -2, -1, 0, -2, 4, 0, 2, 0, 0, 0, 0, -1, 0, 1, 0, 4, 1, 0, 0, 1, 1, 1}
available, _ := b.Available(1)
analysis := make([]*Analysis, 0, AnalysisBufferSize)
b.Analyze(available, &analysis)
b.Analyze(available, &analysis, false)
var reached bool
minHitScore := 200
@ -363,7 +363,7 @@ func TestAnalyze(t *testing.T) {
available, _ := b.Available(1)
analysis := make([]*Analysis, 0, AnalysisBufferSize)
b.Analyze(available, &analysis)
b.Analyze(available, &analysis, false)
var blots int
for _, r := range analysis {
blots += r.Blots
@ -397,7 +397,7 @@ func TestAnalyze(t *testing.T) {
board[SpaceRoll4] = c.roll4
available, _ := board.Available(1)
board.Analyze(available, &analysis)
board.Analyze(available, &analysis, false)
})
}
}
@ -467,7 +467,7 @@ func BenchmarkAnalyze(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
board.Analyze(available, &analysis)
board.Analyze(available, &analysis, false)
}
_ = analysis

View file

@ -48,7 +48,7 @@ func main() {
for i := range available {
log.Println(available[i])
}
b.Analyze(available, &analysis)
b.Analyze(available, &analysis, false)
for i := range analysis {
log.Println(analysis[i])
}
@ -74,7 +74,7 @@ func main() {
b[tabula.SpaceRoll4] = int8(r2)
}
available, _ := b.Available(1)
b.Analyze(available, &analysis)
b.Analyze(available, &analysis, false)
log.Println("ROLL", r1, r2, analysis[0].Moves)
}
}