Skip to content

Commit

Permalink
Add requiresReminder to truco quiero/no quiero.
Browse files Browse the repository at this point in the history
  • Loading branch information
marianogappa committed Jul 24, 2024
1 parent b1df69a commit 7a85a56
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
43 changes: 40 additions & 3 deletions truco/action_any_quiero.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package truco

import "fmt"
import (
"fmt"
"slices"
)

type ActionSayEnvidoNoQuiero struct{ act }
type ActionSayEnvidoQuiero struct{ act }
Expand All @@ -12,8 +15,18 @@ type ActionRevealEnvidoScore struct {
act
Score int `json:"score"`
}
type ActionSayTrucoQuiero struct{ act }
type ActionSayTrucoNoQuiero struct{ act }
type ActionSayTrucoQuiero struct {
act
// RequiresReminder is true if a player ran say_truco and the other player
// initiated an envido sequence. This action might seem out of context.
RequiresReminder bool `json:"requires_reminder"`
}
type ActionSayTrucoNoQuiero struct {
act
// RequiresReminder is true if a player ran say_truco and the other player
// initiated an envido sequence. This action might seem out of context.
RequiresReminder bool `json:"requires_reminder"`
}

func (a ActionSayEnvidoNoQuiero) IsPossible(g GameState) bool {
if g.IsRoundFinished {
Expand Down Expand Up @@ -237,3 +250,27 @@ func (a ActionRevealEnvidoScore) YieldsTurn(g GameState) bool {
// and the current player must confirm round finished right after this action
return false
}

func (a *ActionSayTrucoQuiero) withRequiresReminder(g GameState) Action {
if len(g.RoundsLog[g.RoundNumber].ActionsLog) == 0 {
a.RequiresReminder = false
return a
}
lastAction := _deserializeCurrentRoundLastAction(g)
// If the last action wasn't a truco action, then an envido sequence
// got in the middle of the truco sequence. A reminder is needed.
a.RequiresReminder = !slices.Contains[[]string]([]string{SAY_TRUCO, SAY_QUIERO_RETRUCO, SAY_QUIERO_VALE_CUATRO}, lastAction.GetName())
return a
}

func (a *ActionSayTrucoNoQuiero) withRequiresReminder(g GameState) Action {
if len(g.RoundsLog[g.RoundNumber].ActionsLog) == 0 {
a.RequiresReminder = false
return a
}
lastAction := _deserializeCurrentRoundLastAction(g)
// If the last action wasn't a truco action, then an envido sequence
// got in the middle of the truco sequence. A reminder is needed.
a.RequiresReminder = !slices.Contains[[]string]([]string{SAY_TRUCO, SAY_QUIERO_RETRUCO, SAY_QUIERO_VALE_CUATRO}, lastAction.GetName())
return a
}
4 changes: 2 additions & 2 deletions truco/truco.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,8 @@ func (g GameState) CalculatePossibleActions() []Action {
NewActionSayEnvidoScore(envidoScore, g.TurnPlayerID),
NewActionSayEnvidoNoQuiero(g.TurnPlayerID),
NewActionSayTruco(g.TurnPlayerID),
NewActionSayTrucoQuiero(g.TurnPlayerID),
NewActionSayTrucoNoQuiero(g.TurnPlayerID),
NewActionSayTrucoQuiero(g.TurnPlayerID).(*ActionSayTrucoQuiero).withRequiresReminder(g),
NewActionSayTrucoNoQuiero(g.TurnPlayerID).(*ActionSayTrucoNoQuiero).withRequiresReminder(g),
NewActionSayQuieroRetruco(g.TurnPlayerID),
NewActionSayQuieroValeCuatro(g.TurnPlayerID),
NewActionSaySonBuenas(g.TurnPlayerID),
Expand Down
6 changes: 0 additions & 6 deletions truco/truco_sequence.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package truco

import (
"errors"
"fmt"
"strings"
)
Expand Down Expand Up @@ -119,8 +118,3 @@ func (ts TrucoSequence) WasAccepted() bool {
}
return false
}

var (
errInvalidTrucoSequence = errors.New("invalid truco sequence")
errUnfinishedTrucoSequence = errors.New("unfinished truco sequence")
)

0 comments on commit 7a85a56

Please sign in to comment.