Require using the higher roll value when only one move may be played

This commit is contained in:
Trevor Slocum 2024-01-31 11:28:43 -08:00
parent 37b7f72a0f
commit 6dafbdfa2a
2 changed files with 100 additions and 0 deletions

View File

@ -49,6 +49,55 @@ func NewBoard(variant int8) Board {
return Board{0, -2, 0, 0, 0, 0, 5, 0, 3, 0, 0, 0, -5, 5, 0, 0, 0, -3, 0, -5, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0}
}
func (b Board) String() string {
var board []byte
for i, v := range b {
if i != 0 {
board = append(board, ',')
}
board = append(board, []byte(fmt.Sprintf("%d", v))...)
}
variant := "Backgammon"
switch b[SpaceVariant] {
case VariantAceyDeucey:
variant = "Acey-deucey"
case VariantTabula:
variant = "Tabula"
}
entered1, entered2 := "Y", "Y"
if b[SpaceEnteredPlayer] == 0 {
entered1 = "N"
}
if b[SpaceEnteredOpponent] == 0 {
entered2 = "N"
}
off1 := b[SpaceHomePlayer]
off2 := b[SpaceHomeOpponent] * -1
var rolls []byte
if b[SpaceRoll1] != 0 {
rolls = append(rolls, []byte(fmt.Sprintf("%d", b[SpaceRoll1]))...)
}
if b[SpaceRoll2] != 0 {
if len(rolls) != 0 {
rolls = append(rolls, ' ')
}
rolls = append(rolls, []byte(fmt.Sprintf("%d", b[SpaceRoll2]))...)
}
if b[SpaceRoll3] != 0 {
if len(rolls) != 0 {
rolls = append(rolls, ' ')
}
rolls = append(rolls, []byte(fmt.Sprintf("%d", b[SpaceRoll3]))...)
}
if b[SpaceRoll4] != 0 {
if len(rolls) != 0 {
rolls = append(rolls, ' ')
}
rolls = append(rolls, []byte(fmt.Sprintf("%d", b[SpaceRoll4]))...)
}
return fmt.Sprintf("Board: %s\nVariant: %s\nEntered: %s / %s\nOff: %d / %d\nRolls: %s", board, variant, entered1, entered2, off1, off2, rolls)
}
func (b Board) SetValue(space int, value int8) Board {
b[space] = value
return b
@ -455,6 +504,30 @@ func (b Board) Available(player int8) ([][4][2]int8, []Board) {
newMoves = append(newMoves, allMoves[i])
}
}
if maxLen == 1 && len(newMoves) > 1 {
moved := b[SpaceRoll1] == 0
if !moved {
moved = b[SpaceRoll2] == 0
}
if !moved && (b[SpaceVariant] == VariantTabula || b[SpaceRoll1] == b[SpaceRoll2]) {
moved = b[SpaceRoll3] == 0
}
if !moved && b[SpaceVariant] != VariantTabula && b[SpaceRoll1] == b[SpaceRoll2] {
moved = b[SpaceRoll4] == 0
}
if !moved {
var highestRoll int8
var highestIndex int
for i, move := range newMoves {
roll := b.spaceDiff(player, move[0][0], move[0][1])
if roll > highestRoll {
highestRoll = roll
highestIndex = i
}
}
newMoves = [][4][2]int8{newMoves[highestIndex]}
}
}
return newMoves, boards
}

View File

@ -5,6 +5,33 @@ import (
"testing"
)
func TestAvailableHighRoll(t *testing.T) {
b := Board{0, 0, 0, 0, -2, -2, -2, -2, -2, -1, -2, 0, -2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 0, 0, 1, 1, 0}
available, _ := b.Available(1)
type testCase struct {
moves [4][2]int8
found int
}
var testCases = []*testCase{
{
moves: [4][2]int8{{13, 9}},
},
}
for i, testCase := range testCases {
for _, moves := range available {
if movesEqual(moves, testCase.moves) {
testCase.found++
continue
}
t.Errorf("unexpected available moves for test case %d: %+v", i, moves)
}
if testCase.found != 1 {
t.Errorf("unexpected number of found moves for test case %d: expected 1 move, got %d", i, testCase.found)
}
}
}
func TestMoveBackgammon(t *testing.T) {
b := NewBoard(VariantBackgammon)
b[SpaceRoll1] = 1