Rename field acey as variant

This commit is contained in:
Trevor Slocum 2024-01-07 13:26:03 -08:00
parent 32165ed8d7
commit 3a825a2f64
6 changed files with 34 additions and 27 deletions

11
bei.go
View file

@ -86,7 +86,7 @@ func (s *BEIServer) handleConnection(conn net.Conn) {
return
}
if b[SpaceAcey] != 1 {
if b[SpaceVariant] != VariantAceyDeucey {
log.Println("error: failed to choose roll: state does not represent acey-deucey game")
conn.Close()
return
@ -163,16 +163,17 @@ func parseState(buf []byte) (Board, error) {
if state.Roll1 == state.Roll2 {
b[SpaceRoll3], b[SpaceRoll4] = int8(state.Roll1), int8(state.Roll2)
}
b[SpaceEnteredPlayer] = 1
b[SpaceEnteredOpponent] = 1
if state.Acey {
b[SpaceAcey] = 1
if int8(state.Variant) != VariantBackgammon {
b[SpaceVariant] = int8(state.Variant)
if !state.Entered1 {
b[SpaceEnteredPlayer] = 0
}
if !state.Entered2 {
b[SpaceEnteredOpponent] = 0
}
} else {
b[SpaceEnteredPlayer] = 1
b[SpaceEnteredOpponent] = 1
}
if Verbose {

View file

@ -24,20 +24,26 @@ const (
SpaceRoll4 int8 = 31
SpaceEnteredPlayer int8 = 32 // Whether the player has fully entered the board. Only used in acey-deucey games.
SpaceEnteredOpponent int8 = 33 // Whether the opponent has fully entered the board. Only used in acey-deucey games.
SpaceAcey int8 = 34 // 0 - Backgammon, 1 - Acey-deucey.
SpaceVariant int8 = 34 // 0 - Backgammon, 1 - Acey-deucey, 2 - Tabula.
)
const (
boardSpaces = 35
)
const (
VariantBackgammon int8 = 0
VariantAceyDeucey int8 = 1
VariantTabula int8 = 2
)
// Board represents the state of a game. It contains spaces for the checkers,
// as well as four "spaces" which contain the available die rolls.
type Board [boardSpaces]int8
// NewBoard returns a new board with checkers placed in their starting positions.
func NewBoard(acey bool) Board {
if acey {
func NewBoard(variant int8) Board {
if variant != VariantBackgammon {
return Board{15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -15, 0, 0, 0, 0, 0, 0, 0, 0, 1}
}
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}
@ -84,7 +90,7 @@ func checkers(player int, v int8) int8 {
}
func (b Board) MayBearOff(player int) bool {
if b[SpaceAcey] == 1 && ((player == 1 && b[SpaceEnteredPlayer] == 0) || (player == 2 && b[SpaceEnteredOpponent] == 0)) {
if b[SpaceVariant] != VariantBackgammon && ((player == 1 && b[SpaceEnteredPlayer] == 0) || (player == 2 && b[SpaceEnteredOpponent] == 0)) {
return false
}
barSpace := SpaceBarPlayer
@ -123,7 +129,7 @@ func (b Board) spaceDiff(player int, from int8, to int8) int8 {
case to == SpaceHomeOpponent:
return 25 - from
case from == SpaceHomePlayer || from == SpaceHomeOpponent:
if b[SpaceAcey] == 1 {
if b[SpaceVariant] != VariantBackgammon {
if player == 1 && from == SpaceHomePlayer && b[SpaceEnteredPlayer] == 0 {
return 25 - to
} else if player == 2 && from == SpaceHomeOpponent && b[SpaceEnteredOpponent] == 0 {
@ -169,7 +175,7 @@ func (b Board) HaveRoll(from int8, to int8, player int) bool {
playerDelta = 1
playerHomeEnd = 19
}
if b.MayBearOff(player) && b[SpaceAcey] == 0 {
if b.MayBearOff(player) && b[SpaceVariant] == VariantBackgammon {
allowGreater := true
for checkSpace := 0; checkSpace < 6-int(delta); checkSpace++ {
if checkers(player, b[playerHomeEnd+checkSpace*playerDelta]) != 0 {
@ -214,7 +220,7 @@ func (b Board) UseRoll(from int8, to int8, player int) Board {
playerHomeEnd = 19
}
var allowGreater bool
if b.MayBearOff(player) && b[SpaceAcey] == 0 {
if b.MayBearOff(player) && b[SpaceVariant] == VariantBackgammon {
allowGreater = true
for checkSpace := int8(0); checkSpace < 6-delta; checkSpace++ {
if checkers(player, b[playerHomeEnd+int(checkSpace)*playerDelta]) != 0 {
@ -258,7 +264,7 @@ func (b Board) _available(player int) [][2]int8 {
var moves [][2]int8
if b[SpaceAcey] == 1 && ((player == 1 && b[SpaceEnteredPlayer] == 0) || (player == 2 && b[SpaceEnteredOpponent] == 0)) && b[homeSpace] != 0 {
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]
if ((player == 1 && v >= -1) || (player == 2 && v <= 1)) && b.HaveRoll(homeSpace, space, player) {
@ -410,7 +416,7 @@ func (b Board) Past() bool {
func (b Board) Pips(player int) int {
var pips int
if b[SpaceAcey] == 1 {
if b[SpaceVariant] != VariantBackgammon {
if player == 1 && b[SpaceEnteredPlayer] == 0 {
pips += int(checkers(player, b[SpaceHomePlayer])) * PseudoPips(player, SpaceHomePlayer)
} else if player == 2 && b[SpaceEnteredOpponent] == 0 {
@ -632,7 +638,7 @@ func (b Board) StartingPosition(player int) bool {
}
func (b Board) ChooseDoubles(result *[]*Analysis) int {
if b[SpaceAcey] == 0 {
if b[SpaceVariant] != VariantAceyDeucey {
return 0
}

View file

@ -6,7 +6,7 @@ import (
)
func TestMove(t *testing.T) {
b := NewBoard(false)
b := NewBoard(VariantBackgammon)
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(false)
b := NewBoard(VariantBackgammon)
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(false)
b := NewBoard(VariantBackgammon)
got, expected := b.Blots(1), 0
if got != expected {
t.Errorf("unexpected blots value: expected %v: got %v", expected, got)
@ -116,7 +116,7 @@ func TestHitScore(t *testing.T) {
}
func TestAnalyze(t *testing.T) {
b := NewBoard(false)
b := NewBoard(VariantBackgammon)
b = b.Move(24, 23, 1)
b = b.Move(1, 2, 2)
b[SpaceRoll1], b[SpaceRoll2] = 1, 2
@ -150,7 +150,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(false)
board := NewBoard(VariantBackgammon)
board[SpaceRoll1] = c.roll1
board[SpaceRoll2] = c.roll2
board[SpaceRoll3] = c.roll3
@ -181,7 +181,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(false)
board := NewBoard(VariantBackgammon)
board[SpaceRoll1] = c.roll1
board[SpaceRoll2] = c.roll2
board[SpaceRoll3] = c.roll3
@ -217,7 +217,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(false)
board := NewBoard(VariantBackgammon)
board[SpaceRoll1] = c.roll1
board[SpaceRoll2] = c.roll2
board[SpaceRoll3] = c.roll3
@ -248,7 +248,7 @@ func BenchmarkChooseDoubles(b *testing.B) {
}
func BenchmarkMayBearOff(b *testing.B) {
board := NewBoard(false)
board := NewBoard(VariantBackgammon)
var allowed bool
b.ResetTimer()

View file

@ -37,7 +37,7 @@ func main() {
analysis := make([]*tabula.Analysis, 0, tabula.AnalysisBufferSize)
for r1 := 1; r1 <= 6; r1++ {
for r2 := 1; r2 <= 6; r2++ {
b := tabula.NewBoard(false)
b := tabula.NewBoard(tabula.VariantBackgammon)
b[tabula.SpaceRoll1] = int8(r1)
b[tabula.SpaceRoll2] = int8(r2)
if r1 == r2 {

2
go.mod
View file

@ -2,4 +2,4 @@ module code.rocket9labs.com/tslocum/tabula
go 1.17
require code.rocket9labs.com/tslocum/bei v0.0.0-20231223213316-ef6cb993c773
require code.rocket9labs.com/tslocum/bei v0.0.0-20240107212214-bcc7b6933a4c

4
go.sum
View file

@ -1,2 +1,2 @@
code.rocket9labs.com/tslocum/bei v0.0.0-20231223213316-ef6cb993c773 h1:cu3FKYfk5WQcuxMXrqghHAmriyFiRCZ/8aUqIKI9yLU=
code.rocket9labs.com/tslocum/bei v0.0.0-20231223213316-ef6cb993c773/go.mod h1:tS60/VNAJphKvDBkSLQhKALa15msIAuWWfEKNc4oFZc=
code.rocket9labs.com/tslocum/bei v0.0.0-20240107212214-bcc7b6933a4c h1:fiH72XLcHof1XvVeBw/1NvaS+iIzoZqCTn7Mv0AAYss=
code.rocket9labs.com/tslocum/bei v0.0.0-20240107212214-bcc7b6933a4c/go.mod h1:tS60/VNAJphKvDBkSLQhKALa15msIAuWWfEKNc4oFZc=