Skip to content

Commit

Permalink
fix: isFollowing not working correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
daithihearn committed Jan 23, 2024
1 parent edaac0c commit 989c966
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 3 deletions.
7 changes: 4 additions & 3 deletions pkg/game/game-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,13 @@ func canRenage(leadOut Card, myTrumps []Card) bool {
func isFollowing(myCard CardName, myCards []CardName, currentHand Hand, suit Suit) bool {
mySuit := myCard.Card().Suit
leadOut := currentHand.LeadOut.Card()
suitLead := leadOut.Suit == suit || leadOut.Suit == Wild
trumpLead := leadOut.Suit == suit || leadOut.Suit == Wild

if suitLead {
if trumpLead {
var myTrumps []Card
for _, card := range myCards {
if mySuit == suit || mySuit == Wild {
s := card.Card().Suit
if s == suit || s == Wild {
myTrumps = append(myTrumps, card.Card())
}
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/game/game-utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,22 @@ func TestGameUtils_isFollowing(t *testing.T) {
suit: Diamonds,
expectedResult: true,
},
{
name: "Following",
myCard: THREE_CLUBS,
myCards: []CardName{TWO_HEARTS, THREE_CLUBS, FOUR_DIAMONDS, FIVE_SPADES, SIX_HEARTS},
currentHand: Hand{LeadOut: ACE_CLUBS, PlayedCards: []PlayedCard{{PlayerID: "1", Card: ACE_CLUBS}}},
suit: Clubs,
expectedResult: true,
},
{
name: "Not following",
myCard: FOUR_DIAMONDS,
myCards: []CardName{TWO_HEARTS, THREE_CLUBS, FOUR_DIAMONDS, FIVE_SPADES, SIX_HEARTS},
currentHand: Hand{LeadOut: ACE_CLUBS, PlayedCards: []PlayedCard{{PlayerID: "1", Card: ACE_CLUBS}}},
suit: Clubs,
expectedResult: false,
},
}

for _, test := range tests {
Expand Down
74 changes: 74 additions & 0 deletions pkg/game/game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1201,3 +1201,77 @@ func TestGame_Buy(t *testing.T) {
})
}
}

func TestGame_Play(t *testing.T) {
tests := []struct {
name string
game Game
playerID string
card CardName
expectedStatus RoundStatus
expectedNextPlayer string
expectingError bool
}{
{
name: "Valid play - first card",
game: PlayingGame_RoundStart("1"),
playerID: "1",
card: TWO_HEARTS,
expectedStatus: Playing,
expectedNextPlayer: "2",
},
{
name: "Try to play a card that isn't in your hand",
game: PlayingGame_RoundStart("1"),
playerID: "1",
card: ACE_SPADES,
expectedStatus: Playing,
expectingError: true,
},
{
name: "Following suit",
game: PlayingGame_RoundStart_FirstCardPlayed(),
playerID: "2",
card: THREE_CLUBS,
expectedStatus: Playing,
expectedNextPlayer: "1",
},
{
name: "Not following suit",
game: PlayingGame_RoundStart_FirstCardPlayed(),
playerID: "2",
card: FOUR_DIAMONDS,
expectedStatus: Playing,
expectingError: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := test.game.Play(test.playerID, test.card)
if test.expectingError {
if err == nil {
t.Errorf("expected an error, got nil")
}
} else {
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if test.game.CurrentRound.Status != test.expectedStatus {
t.Errorf("expected round status to be %s, got %s", test.expectedStatus, test.game.CurrentRound.Status)
}
if test.game.CurrentRound.CurrentHand.CurrentPlayerID != test.expectedNextPlayer {
t.Errorf("expected next player to be %s, got %s", test.expectedNextPlayer, test.game.CurrentRound.CurrentHand.CurrentPlayerID)
}
// Check that he has all of retained the cards he selected
state, err := test.game.GetState(test.playerID)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if contains(state.Cards, test.card) {
t.Errorf("expected player to not have played card %s, got %v", test.card, state.Cards)
}
}
})
}
}
32 changes: 32 additions & 0 deletions pkg/game/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,38 @@ func PlayingGame_RoundStart(dealerId string) Game {
return game
}

func PlayingGame_RoundStart_FirstCardPlayed() Game {
p1 := Player1()
p1.Cards = []CardName{KING_CLUBS, QUEEN_HEARTS, JACK_SPADES, TEN_DIAMONDS}
p2 := Player2()
p2.Cards = []CardName{TWO_HEARTS, THREE_CLUBS, FOUR_DIAMONDS, FIVE_SPADES, SIX_HEARTS}

return Game{
ID: "1",
Name: "Test Game",
Status: Active,
Timestamp: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Players: []Player{p1, p2},
CurrentRound: Round{
DealerID: "1",
GoerID: "2",
Status: Playing,
CurrentHand: Hand{
CurrentPlayerID: "2",
LeadOut: ACE_CLUBS,
PlayedCards: []PlayedCard{{PlayerID: "1", Card: ACE_CLUBS}},
},
CompletedHands: []Hand{},
Suit: Clubs,
Number: 1,
},
Dummy: Dummy(),
AdminID: "1",
Deck: NewDeck(),
}

}

func PlayingGame_Hand1Complete(dealerId string) Game {
game := PlayingGame_RoundStart(dealerId)

Expand Down

0 comments on commit 989c966

Please sign in to comment.