Fix calculating backgammon win

This commit is contained in:
Trevor Slocum 2023-11-10 17:15:49 -08:00
parent 7e7aed5776
commit 2bc5ed236b
3 changed files with 40 additions and 32 deletions

View file

@ -40,23 +40,19 @@ func (g *serverGame) roll(player int) bool {
}
if g.Turn == 0 {
var secondRoll bool
if player == 1 {
if g.Roll1 != 0 {
return false
} else {
secondRoll = true
}
g.Roll1 = randInt(6) + 1
} else {
if g.Roll2 != 0 {
return false
} else {
secondRoll = true
}
g.Roll2 = randInt(6) + 1
}
if secondRoll && g.Started.IsZero() {
if g.Started.IsZero() {
g.Started = time.Now()
}

View file

@ -348,7 +348,7 @@ COMMANDS:
cmd.client.Terminate("Invalid username: must contain at least one non-numeric character.")
return false
} else if s.clientByUsername(username) != nil || (!randomUsername && !s.nameAllowed(username)) {
cmd.client.Terminate("Username unavailable.")
cmd.client.Terminate("That username is already in use.")
return false
}
return true
@ -862,13 +862,25 @@ COMMANDS:
if clientGame.Winner != 0 {
opponent := 1
opponentHome := bgammon.SpaceHomePlayer
playerBar := bgammon.SpaceBarPlayer
if clientGame.Winner == 1 {
opponent = 2
opponentHome = bgammon.SpaceHomeOpponent
playerBar = bgammon.SpaceBarOpponent
}
backgammon := bgammon.PlayerCheckers(clientGame.Board[playerBar], opponent) != 0
if !backgammon {
homeStart, homeEnd := bgammon.HomeRange(clientGame.Winner)
bgammon.IterateSpaces(homeStart, homeEnd, func(space, spaceCount int) {
if bgammon.PlayerCheckers(clientGame.Board[space], opponent) != 0 {
backgammon = true
}
})
}
winPoints := 1
if !bgammon.CanBearOff(clientGame.Board, opponent, false) {
if backgammon {
winPoints = 3 // Award backgammon.
} else if clientGame.Board[opponentHome] == 0 {
winPoints = 2 // Award gammon.
@ -903,11 +915,11 @@ COMMANDS:
ev.Player = string(cmd.client.name)
client.sendEvent(ev)
clientGame.sendBoard(client)
if winEvent != nil {
client.sendEvent(winEvent)
}
clientGame.sendBoard(client)
})
case bgammon.CommandReset:
if clientGame == nil {
@ -1071,7 +1083,7 @@ COMMANDS:
clientGame.Turn = 1
clientGame.Roll1 = 1
clientGame.Roll2 = 2
clientGame.Board = []int{0, 4, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -4, 0, 0, 0}
clientGame.Board = []int{0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 0, -1}
clientGame.eachClient(func(client *serverClient) {
clientGame.sendBoard(client)

42
game.go
View file

@ -113,25 +113,6 @@ func (g *Game) opponentPlayer() Player {
}
}
func (g *Game) iterateSpaces(from int, to int, f func(space int, spaceCount int)) {
if from == to || from < 1 || from > 24 || to < 1 || to > 24 {
return
}
i := 1
if to > from {
for space := from; space <= to; space++ {
f(space, i)
i++
}
} else {
for space := from; space >= to; space-- {
f(space, i)
i++
}
}
}
func (g *Game) addMove(move []int) bool {
opponentCheckers := OpponentCheckers(g.Board[move[1]], g.Turn)
if opponentCheckers > 1 {
@ -403,7 +384,7 @@ func (g *Game) LegalMoves(local bool) [][]int {
}
if mustEnter { // Must enter from bar.
from, to := HomeRange(g.opponentPlayer().Number)
g.iterateSpaces(from, to, func(homeSpace int, spaceCount int) {
IterateSpaces(from, to, func(homeSpace int, spaceCount int) {
if movesFound[barSpace*100+homeSpace] {
return
}
@ -474,7 +455,7 @@ func (g *Game) LegalMoves(local bool) [][]int {
lastSpace = 24
}
g.iterateSpaces(space, lastSpace, func(to int, spaceCount int) {
IterateSpaces(space, lastSpace, func(to int, spaceCount int) {
if movesFound[space*100+to] {
return
}
@ -794,6 +775,25 @@ func SpaceDiff(from int, to int) int {
return diff
}
func IterateSpaces(from int, to int, f func(space int, spaceCount int)) {
if from == to || from < 1 || from > 24 || to < 1 || to > 24 {
return
}
i := 1
if to > from {
for space := from; space <= to; space++ {
f(space, i)
i++
}
} else {
for space := from; space >= to; space-- {
f(space, i)
i++
}
}
}
func PlayerCheckers(checkers int, player int) int {
if player == 1 {
if checkers > 0 {