Skip to content

Commit

Permalink
Add SAY_ENVIDO_SCORE action. Fix ui & bot small bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
marianogappa committed Jul 16, 2024
1 parent a2fd933 commit fc0f7d0
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 63 deletions.
8 changes: 4 additions & 4 deletions examplebot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func possibleActionsMap(gs truco.ClientGameState) map[string]truco.Action {
func filter(possibleActions map[string]truco.Action, candidateActions ...truco.Action) []truco.Action {
filteredActions := []truco.Action{}
for _, action := range candidateActions {
if _, ok := possibleActions[action.GetName()]; ok {
filteredActions = append(filteredActions, action)
if possibleAction, ok := possibleActions[action.GetName()]; ok {
filteredActions = append(filteredActions, possibleAction)
}
}
return filteredActions
Expand Down Expand Up @@ -95,7 +95,7 @@ func canAnyEnvido(actions map[string]truco.Action) bool {
truco.NewActionSayEnvido(1),
truco.NewActionSayRealEnvido(1),
truco.NewActionSayFaltaEnvido(1),
truco.NewActionSayEnvidoQuiero(0, 1),
truco.NewActionSayEnvidoQuiero(1),
truco.NewActionSayEnvidoNoQuiero(1),
)) > 0
}
Expand Down Expand Up @@ -511,7 +511,7 @@ func envidoNoQuiero() truco.Action {
return truco.NewActionSayEnvidoNoQuiero(1)
}
func envidoQuiero() truco.Action {
return truco.NewActionSayEnvidoQuiero(0, 1)
return truco.NewActionSayEnvidoQuiero(1)
}
func trucoQuiero() truco.Action {
return truco.NewActionSayTrucoQuiero(1)
Expand Down
2 changes: 1 addition & 1 deletion exampleclient/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func renderScores(rs renderState) {

youMano := ""
themMano := ""
if rs.gs.TurnPlayerID == rs.gs.YouPlayerID {
if rs.gs.RoundTurnPlayerID == rs.gs.YouPlayerID {
youMano = " (mano)"
} else {
themMano = " (mano)"
Expand Down
9 changes: 7 additions & 2 deletions exampleclient/ui_spanish.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ func getActionString(log truco.ActionLog, playerID int) string {
case truco.SAY_FALTA_ENVIDO:
what = fmt.Sprintf("%v falta envido!", said)
case truco.SAY_ENVIDO_QUIERO:
action := lastAction.(*truco.ActionSayEnvidoQuiero)
what = fmt.Sprintf("%v quiero con %d", said, action.Score)
what = fmt.Sprintf("%v quiero", said)
case truco.SAY_ENVIDO_SCORE:
action := lastAction.(*truco.ActionSayEnvidoScore)
what = fmt.Sprintf("%d", action.Score)
case truco.SAY_ENVIDO_NO_QUIERO:
what = fmt.Sprintf("%v no quiero", said)
case truco.SAY_TRUCO:
Expand Down Expand Up @@ -102,6 +104,9 @@ func spanishAction(action truco.Action) string {
return "quiero"
case truco.SAY_ENVIDO_NO_QUIERO:
return "no quiero"
case truco.SAY_ENVIDO_SCORE:
_action := action.(*truco.ActionSayEnvidoScore)
return fmt.Sprintf("%d", _action.Score)
case truco.SAY_TRUCO:
return "truco"
case truco.SAY_TRUCO_QUIERO:
Expand Down
28 changes: 27 additions & 1 deletion truco/action_any_quiero.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package truco

type ActionSayEnvidoNoQuiero struct{ act }
type ActionSayEnvidoQuiero struct {
type ActionSayEnvidoQuiero struct{ act }
type ActionSayEnvidoScore struct {
act
Score int `json:"score"`
}
Expand All @@ -28,6 +29,17 @@ func (a ActionSayEnvidoQuiero) IsPossible(g GameState) bool {
return g.EnvidoSequence.CanAddStep(a.GetName())
}

func (a ActionSayEnvidoScore) IsPossible(g GameState) bool {
if len(g.RoundsLog[g.RoundNumber].ActionsLog) == 0 {
return false
}
lastAction := _deserializeCurrentRoundLastAction(g)
if lastAction.GetName() != SAY_ENVIDO_QUIERO {
return false
}
return g.EnvidoSequence.CanAddStep(a.GetName())
}

func (a ActionSayTrucoQuiero) IsPossible(g GameState) bool {
if g.IsRoundFinished {
return false
Expand Down Expand Up @@ -84,6 +96,14 @@ func (a ActionSayEnvidoQuiero) Run(g *GameState) error {
return nil
}

func (a ActionSayEnvidoScore) Run(g *GameState) error {
if !a.IsPossible(*g) {
return errActionNotPossible
}
g.EnvidoSequence.AddStep(a.GetName())
return nil
}

func (a ActionSayTrucoQuiero) Run(g *GameState) error {
if !a.IsPossible(*g) {
return errActionNotPossible
Expand Down Expand Up @@ -120,3 +140,9 @@ func (a ActionSayEnvidoNoQuiero) YieldsTurn(g GameState) bool {
// In son_buenas/son_mejores/no_quiero, the turn should go to whoever started the sequence
return g.TurnPlayerID != g.EnvidoSequence.StartingPlayerID
}

func (a ActionSayEnvidoQuiero) YieldsTurn(g GameState) bool {
// In envido_quiero, the next turn should go to whoever has to reveal the score.
// This should always be the "mano" player.
return g.TurnPlayerID != g.RoundTurnPlayerID
}
8 changes: 6 additions & 2 deletions truco/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ func NewActionSayEnvidoNoQuiero(playerID int) Action {
return ActionSayEnvidoNoQuiero{act: act{Name: SAY_ENVIDO_NO_QUIERO, PlayerID: playerID}}
}

func NewActionSayEnvidoQuiero(score int, playerID int) Action {
return ActionSayEnvidoQuiero{act: act{Name: SAY_ENVIDO_QUIERO, PlayerID: playerID}, Score: score}
func NewActionSayEnvidoQuiero(playerID int) Action {
return ActionSayEnvidoQuiero{act: act{Name: SAY_ENVIDO_QUIERO, PlayerID: playerID}}
}

func NewActionSayEnvidoScore(score int, playerID int) Action {
return ActionSayEnvidoScore{act: act{Name: SAY_ENVIDO_SCORE, PlayerID: playerID}, Score: score}
}

func NewActionSayTrucoQuiero(playerID int) Action {
Expand Down
Loading

0 comments on commit fc0f7d0

Please sign in to comment.