Fix using dice roll when bearing off

This commit is contained in:
Trevor Slocum 2023-11-01 22:47:59 -07:00
parent b89776c323
commit 41fa0a24b0
3 changed files with 74 additions and 16 deletions

View file

@ -80,21 +80,18 @@ func CanBearOff(board []int, player int, local bool) bool {
homeStart, homeEnd := 1, 6
if !local {
homeStart, homeEnd = HomeRange(player)
homeStart, homeEnd = minInt(homeStart, homeEnd), maxInt(homeStart, homeEnd)
}
homeStart, homeEnd = minInt(homeStart, homeEnd), maxInt(homeStart, homeEnd)
ok := true
if PlayerCheckers(board[SpaceBarPlayer], player) > 0 || PlayerCheckers(board[SpaceBarOpponent], player) > 0 {
return false
}
for i := 1; i < 24; i++ {
if (i < homeStart || i > homeEnd) && PlayerCheckers(board[i], player) > 0 {
ok = false
break
return false
}
}
if ok && (PlayerCheckers(board[SpaceBarPlayer], player) > 0 || PlayerCheckers(board[SpaceBarOpponent], player) > 0) {
ok = false
}
return ok
return true
}
func ParseSpace(space string) int {

View file

@ -663,6 +663,12 @@ COMMANDS:
continue
}
opponent := clientGame.opponent(cmd.client)
if opponent == nil {
cmd.client.sendNotice("You may not double until your opponent rejoins the match.")
continue
}
clientGame.DoubleOffered = true
cmd.client.sendNotice(fmt.Sprintf("Double offered to opponent (%d points).", clientGame.DoubleValue*2))
@ -689,6 +695,12 @@ COMMANDS:
continue
}
opponent := clientGame.opponent(cmd.client)
if opponent == nil {
cmd.client.sendNotice("You may not resign until your opponent rejoins the match.")
continue
}
cmd.client.sendNotice("Declined double offer")
clientGame.opponent(cmd.client).sendNotice(fmt.Sprintf("%s declined double offer.", cmd.client.name))
@ -733,6 +745,14 @@ COMMANDS:
continue
}
opponent := clientGame.opponent(cmd.client)
if opponent == nil {
cmd.client.sendEvent(&bgammon.EventFailedRoll{
Reason: "You may not roll until your opponent rejoins the match.",
})
continue
}
if !clientGame.roll(cmd.client.playerNumber) {
cmd.client.sendEvent(&bgammon.EventFailedRoll{
Reason: "It is not your turn to roll.",
@ -776,6 +796,14 @@ COMMANDS:
continue
}
opponent := clientGame.opponent(cmd.client)
if opponent == nil {
cmd.client.sendEvent(&bgammon.EventFailedMove{
Reason: "You may not move until your opponent rejoins the match.",
})
continue
}
sendUsage := func() {
cmd.client.sendEvent(&bgammon.EventFailedMove{
Reason: "Specify one or more moves in the form FROM/TO. For example: 8/4 6/4",
@ -915,6 +943,12 @@ COMMANDS:
continue
}
opponent := clientGame.opponent(cmd.client)
if opponent == nil {
cmd.client.sendNotice("You must wait until your opponent rejoins the match before continuing the game.")
continue
}
if clientGame.DoubleOffered && clientGame.Turn != cmd.client.playerNumber {
opponent := clientGame.opponent(cmd.client)
if opponent == nil {
@ -1031,7 +1065,7 @@ COMMANDS:
clientGame.Turn = 1
clientGame.Roll1 = 1
clientGame.Roll2 = 2
clientGame.Board = []int{0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -4, 0, 0, 0}
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.eachClient(func(client *serverClient) {
clientGame.sendBoard(client)

39
game.go
View file

@ -357,7 +357,13 @@ func (g *Game) LegalMoves(local bool) [][]int {
needRoll = 25 - from
}
for i, roll := range rolls {
if roll >= needRoll {
if roll == needRoll {
rolls = append(rolls[:i], rolls[i+1:]...)
return
}
}
for i, roll := range rolls {
if roll > needRoll {
rolls = append(rolls[:i], rolls[i+1:]...)
return
}
@ -429,12 +435,33 @@ func (g *Game) LegalMoves(local bool) [][]int {
}
available := haveBearOffDiceRoll(SpaceDiff(space, homeSpace))
if available > 0 {
movable := playerCheckers
if movable > available {
movable = available
ok := true
if haveDiceRoll(space, homeSpace) == 0 {
_, homeEnd := HomeRange(g.Turn)
if g.Turn == 2 {
for homeSpace := space - 1; homeSpace >= homeEnd; homeSpace-- {
if PlayerCheckers(g.Board[homeSpace], g.Turn) != 0 {
ok = false
break
}
}
} else {
for homeSpace := space + 1; homeSpace <= homeEnd; homeSpace++ {
if PlayerCheckers(g.Board[homeSpace], g.Turn) != 0 {
ok = false
break
}
}
}
}
for i := 0; i < movable; i++ {
moves = append(moves, []int{space, homeSpace})
if ok {
movable := playerCheckers
if movable > available {
movable = available
}
for i := 0; i < movable; i++ {
moves = append(moves, []int{space, homeSpace})
}
}
}
}