From ed1f6e56c3b94ed3f25baa309a8f5181b6cfca5f Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Sun, 10 Dec 2023 19:08:01 -0800 Subject: [PATCH] Support choosing doubles when playing acey-deucey --- board.go | 30 ++++++++++++++++++++++++++++-- board_test.go | 14 +++++++------- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/board.go b/board.go index ef3476d..06d284a 100644 --- a/board.go +++ b/board.go @@ -158,7 +158,7 @@ func (b Board) HaveRoll(from int, to int, player int) bool { playerDelta = 1 playerHomeEnd = 19 } - if b.MayBearOff(player) { + if b.MayBearOff(player) && !b.Acey() { allowGreater := true for checkSpace := int8(0); checkSpace < 6-delta; checkSpace++ { if b.Checkers(player, playerHomeEnd+int(checkSpace)*playerDelta) != 0 { @@ -187,7 +187,7 @@ func (b Board) UseRoll(from int, to int, player int) Board { playerHomeEnd = 19 } var allowGreater bool - if b.MayBearOff(player) { + if b.MayBearOff(player) && !b.Acey() { allowGreater = true for checkSpace := int8(0); checkSpace < 6-delta; checkSpace++ { if b.Checkers(player, playerHomeEnd+int(checkSpace)*playerDelta) != 0 { @@ -509,6 +509,32 @@ func (b Board) Analyze(available [][][]int) []*Analysis { }) return result } +func (b Board) ChooseDoubles() (int, [][]*Analysis) { + if !b.Acey() { + return 0, nil + } + + var allAnalysis = make([][]*Analysis, 6) + for i := 0; i < 6; i++ { + doubles := int8(i + 1) + bc := b + bc[SpaceRoll1], bc[SpaceRoll2], bc[SpaceRoll3], bc[SpaceRoll4] = doubles, doubles, doubles, doubles + available, _ := bc.Available(1) + allAnalysis[i] = bc.Analyze(available) + } + + bestDoubles := 6 + bestScore := math.MaxFloat64 + for i := 0; i < 6; i++ { + if len(allAnalysis[i]) == 0 { + continue + } else if allAnalysis[i][0].Score < bestScore { + bestDoubles = i + 1 + bestScore = allAnalysis[i][0].Score + } + } + return bestDoubles, allAnalysis +} func (b Board) Print() { log.Printf("%+v", b) diff --git a/board_test.go b/board_test.go index 17ace79..62272a5 100644 --- a/board_test.go +++ b/board_test.go @@ -6,7 +6,7 @@ import ( ) func TestMove(t *testing.T) { - b := NewBoard() + b := NewBoard(false) b[SpaceRoll1] = 1 b[SpaceRoll2] = 2 got, expected := b[24], int8(2) @@ -37,7 +37,7 @@ func TestMove(t *testing.T) { } func TestPast(t *testing.T) { - b := NewBoard() + b := NewBoard(false) got, expected := b.Past(), false if got != expected { t.Errorf("unexpected past value: expected %v: got %v", expected, got) @@ -63,7 +63,7 @@ func TestPast(t *testing.T) { } func TestBlots(t *testing.T) { - b := NewBoard() + b := NewBoard(false) got, expected := b.Blots(1), 0 if got != expected { t.Errorf("unexpected blots value: expected %v: got %v", expected, got) @@ -97,7 +97,7 @@ func TestBlots(t *testing.T) { } func TestAnalyze(t *testing.T) { - b := NewBoard() + b := NewBoard(false) b = b.Move(24, 23, 1) b = b.Move(1, 2, 2) b[SpaceRoll1], b[SpaceRoll2] = 1, 2 @@ -130,7 +130,7 @@ func TestAnalyze(t *testing.T) { } for _, c := range cases { t.Run(fmt.Sprintf("%d-%d", c.roll1, c.roll2), func(t *testing.T) { - board := NewBoard() + board := NewBoard(false) board[SpaceRoll1] = c.roll1 board[SpaceRoll2] = c.roll2 board[SpaceRoll3] = c.roll3 @@ -161,7 +161,7 @@ func BenchmarkAvailable(b *testing.B) { } for _, c := range cases { b.Run(fmt.Sprintf("%d-%d", c.roll1, c.roll2), func(b *testing.B) { - board := NewBoard() + board := NewBoard(false) board[SpaceRoll1] = c.roll1 board[SpaceRoll2] = c.roll2 board[SpaceRoll3] = c.roll3 @@ -197,7 +197,7 @@ func BenchmarkAnalyze(b *testing.B) { } for _, c := range cases { b.Run(fmt.Sprintf("%d-%d", c.roll1, c.roll2), func(b *testing.B) { - board := NewBoard() + board := NewBoard(false) board[SpaceRoll1] = c.roll1 board[SpaceRoll2] = c.roll2 board[SpaceRoll3] = c.roll3