Add bear off test case

This commit is contained in:
Trevor Slocum 2024-08-19 19:55:21 -07:00
parent 7895e575d4
commit 91065b63f1
2 changed files with 41 additions and 0 deletions

View file

@ -370,6 +370,7 @@ func (b Board) _available(player int8) [][2]int8 {
var moves [][2]int8
// Enter board from home space.
if b[SpaceVariant] != VariantBackgammon && ((player == 1 && b[SpaceEnteredPlayer] == 0) || (player == 2 && b[SpaceEnteredOpponent] == 0)) && b[homeSpace] != 0 {
for space := int8(1); space < 25; space++ {
v := b[space]
@ -378,10 +379,14 @@ func (b Board) _available(player int8) [][2]int8 {
}
}
}
for from := int8(0); from < 28; from++ {
// Skip invalid spaces, spaces without player checkers and non-bar spaces when a checker is on the bar.
if from == SpaceHomePlayer || from == SpaceHomeOpponent || from == opponentBarSpace || checkers(player, b[from]) == 0 || (onBar && from != barSpace) {
continue
}
// Iterate over destination spaces to determine available moves.
if player == 1 && b[SpaceVariant] != VariantTabula {
for to := int8(0); to < from; to++ {
if to == SpaceBarPlayer || to == SpaceBarOpponent || to == SpaceHomeOpponent || (to == SpaceHomePlayer && !mayBearOff()) {

View file

@ -63,6 +63,42 @@ func TestMoveBackgammon(t *testing.T) {
}
}
func TestBearOffBackgammon(t *testing.T) {
b := Board{0, 0, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, -3, 0, 0, -3, -6, -2, 0, 0, 0, 3, 3, 0, 0, 1, 1, 0}
available, _ := b.Available(1)
got, expected := len(available), 1
if got != expected {
t.Errorf("unexpected number of move combinations: expected %d: got %d", expected, got)
}
var foundMoves [][2]int8
for i := 0; i < 4; i++ {
move := available[0][i]
if move[0] == 0 && move[1] == 0 {
break
}
foundMoves = append(foundMoves, move)
}
got, expected = len(foundMoves), 2
if got != expected {
t.Errorf("unexpected number of legal moves: expected %d: got %d", expected, got)
}
var found30 bool
var found41 bool
for _, move := range foundMoves {
if move[0] == 3 && move[1] == 0 {
found30 = true
} else if move[0] == 4 && move[1] == 1 {
found41 = true
}
}
if !found30 {
t.Errorf("expected move 3/0 was not found")
}
if !found41 {
t.Errorf("expected move 4/1 was not found")
}
}
func TestMoveTabula(t *testing.T) {
{
b := NewBoard(VariantTabula)