Skip to content

Commit

Permalink
Implement multiple important game fixes.
Browse files Browse the repository at this point in the history
- Fixed crash with awarding envido points.
- Added panics where unreachable code is reached.
- Fixed missing cases for chanceOfWinningTruco.
- Made bot say me_voy_al_mazo when it already lost.
  • Loading branch information
marianogappa committed Jul 21, 2024
1 parent 9034e13 commit a08556e
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 90 deletions.
227 changes: 151 additions & 76 deletions examplebot/bot.go

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions examplebot/bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,22 @@ func TestChanceOfWinningTruco(t *testing.T) {
},
expected: 0.4,
},
{
name: "test",
gs: truco.ClientGameState{
YourRevealedCards: []truco.Card{
{Suit: truco.ESPADA, Number: 10},
{Suit: truco.ORO, Number: 1},
},
YourUnrevealedCards: []truco.Card{
{Suit: truco.BASTO, Number: 1},
},
TheirRevealedCards: []truco.Card{
{Suit: truco.COPA, Number: 4},
},
},
expected: 0.4,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions truco/action_any_quiero.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ func (a ActionRevealEnvidoScore) Run(g *GameState) error {
}
// replace hand with our satisfactory candidate hand
g.Players[a.PlayerID].Hand = &candidateHand
if !g.tryAwardEnvidoPoints() {
return fmt.Errorf("couldn't award envido score after running reveal envido score action due to a bug, this code should be unreachable")
if !g.tryAwardEnvidoPoints(a.PlayerID) {
panic("couldn't award envido score after running reveal envido score action due to a bug, this code should be unreachable")
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion truco/action_reveal_card.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (a *ActionRevealCard) Run(g *GameState) error {
g.IsEnvidoFinished = true
}
// Revealing a card may cause the envido score to be revealed
if g.tryAwardEnvidoPoints() {
if g.tryAwardEnvidoPoints(a.PlayerID) {
a.EnMesa = true
a.Score = g.Players[a.PlayerID].Hand.EnvidoScore() // it must be the action's player
}
Expand Down
2 changes: 1 addition & 1 deletion truco/action_son_buenas.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (a ActionSaySonBuenas) Run(g *GameState) error {
g.RoundsLog[g.RoundNumber].EnvidoPoints = cost
g.RoundsLog[g.RoundNumber].EnvidoWinnerPlayerID = g.TurnOpponentPlayerID
g.IsEnvidoFinished = true
g.tryAwardEnvidoPoints()
g.tryAwardEnvidoPoints(a.PlayerID)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion truco/action_son_mejores.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (a ActionSaySonMejores) Run(g *GameState) error {
g.RoundsLog[g.RoundNumber].EnvidoPoints = cost
g.RoundsLog[g.RoundNumber].EnvidoWinnerPlayerID = g.TurnPlayerID
g.IsEnvidoFinished = true
g.tryAwardEnvidoPoints()
g.tryAwardEnvidoPoints(a.PlayerID)
return nil
}

Expand Down
28 changes: 26 additions & 2 deletions truco/actions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package truco

import "fmt"
import (
"fmt"
"strings"
)

type act struct {
Name string `json:"name"`
Expand All @@ -18,7 +21,8 @@ func (a act) GetPlayerID() int {
}

func (a act) String() string {
return fmt.Sprintf("%v runs %v", a.PlayerID, a.Name)
name := strings.ReplaceAll(strings.TrimPrefix(a.Name, "say_"), "_", " ")
return fmt.Sprintf("Player %v says %v", a.PlayerID, name)
}

func (a act) YieldsTurn(g GameState) bool {
Expand Down Expand Up @@ -92,3 +96,23 @@ func NewActionConfirmRoundFinished(playerID int) Action {
func NewActionRevealEnvidoScore(playerID int, score int) Action {
return &ActionRevealEnvidoScore{act: act{Name: REVEAL_ENVIDO_SCORE, PlayerID: playerID}, Score: score}
}

func (a ActionSaySonMejores) String() string {
return fmt.Sprintf("Player %v says %v son mejores", a.PlayerID, a.Score)
}

func (a ActionRevealEnvidoScore) String() string {
return fmt.Sprintf("Player %v says %v en mesa", a.PlayerID, a.Score)
}

func (a ActionRevealCard) String() string {
text := fmt.Sprintf("Player %v reveals %v of %v", a.PlayerID, a.Card.Number, a.Card.Suit)
if a.EnMesa {
text = fmt.Sprintf("%v (%v en mesa)", text, a.Score)
}
return text
}

func (a ActionConfirmRoundFinished) String() string {
return fmt.Sprintf("Player %v confirms round finished", a.PlayerID)
}
9 changes: 4 additions & 5 deletions truco/actions_any_truco.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ func (g GameState) AnyTrucoActionIsPossible(a Action) bool {
if !g.EnvidoSequence.IsEmpty() && !g.IsEnvidoFinished {
return false
}
// Only the player who said "quiero" last can raise the stakes, unless quiero hasn't been said yet,
// which can only happen if the last action is "truco"
if !g.IsLastActionOfName(SAY_TRUCO) &&
(a.GetName() == SAY_QUIERO_RETRUCO || a.GetName() == SAY_QUIERO_VALE_CUATRO) &&
g.TrucoSequence.QuieroOwnerPlayerID != g.TurnPlayerID {
// Only the player who said "quiero" last can raise the stakes, unless quiero hasn't been said yet
if (a.GetName() == SAY_QUIERO_RETRUCO || a.GetName() == SAY_QUIERO_VALE_CUATRO) &&
g.TrucoSequence.QuieroOwnerPlayerID != a.GetPlayerID() &&
g.TrucoSequence.QuieroOwnerPlayerID != -1 {
return false
}
return g.TrucoSequence.CanAddStep(a.GetName())
Expand Down
4 changes: 2 additions & 2 deletions truco/truco.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ func (g *GameState) canAwardEnvidoPoints(revealedHand Hand) bool {
return true
}

func (g *GameState) tryAwardEnvidoPoints() bool {
if !g.canAwardEnvidoPoints(Hand{Revealed: g.Players[g.TurnPlayerID].Hand.Revealed}) {
func (g *GameState) tryAwardEnvidoPoints(playerID int) bool {
if !g.canAwardEnvidoPoints(Hand{Revealed: g.Players[playerID].Hand.Revealed}) {
return false
}
wonBy := g.RoundsLog[g.RoundNumber].EnvidoWinnerPlayerID
Expand Down

0 comments on commit a08556e

Please sign in to comment.