Skip to content

Commit

Permalink
chore: Update shuffleDrawStack to shuffle top card as well
Browse files Browse the repository at this point in the history
This commit modifies the `shuffleDrawStack` function in the `GameManager` to include an additional parameter `shuffleTopCardToo`. When `shuffleTopCardToo` is set to `true`, the function shuffles the top card along with the rest of the cards in the draw stack. This change ensures that the top card is also randomized when shuffling the draw stack.
  • Loading branch information
oriventi committed Jun 5, 2024
1 parent fb53eb2 commit 5738ea4
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions duo_backend/api/game_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (gm *GameManager) CreateGame(lobby *Lobby, lobbyId int32) (int, error) {
gm.GameStreams[int(dbGame.ID)] = NewGame(duoCards) //TODO make dynamic
gm.Mu.Unlock()

shuffleErr := gm.shuffleDrawStack(int(dbGame.ID))
shuffleErr := gm.shuffleDrawStack(int(dbGame.ID), true)
if shuffleErr != nil {
log.Printf("error shuffling place stack: %v", shuffleErr)
return 0, shuffleErr
Expand Down Expand Up @@ -856,16 +856,23 @@ func (gm *GameManager) DrawCardsFromStack(gameId int, amount int32) ([]string, e

}

func (gm *GameManager) shuffleDrawStack(gameId int) error {
func (gm *GameManager) shuffleDrawStack(gameId int, shuffleTopCardToo bool) error {
game, exists := gm.GetGame(gameId)
if !exists {
log.Printf("game does not exist")
return fmt.Errorf("game does not exist")
}

var length int
if shuffleTopCardToo {
length = len(game.CardsOnPlaceStack)
} else {
length = len(game.CardsOnPlaceStack) - 1
}

game.Mu.Lock()
for i := 0; i < len(game.CardsOnDrawStack)-1; i++ {
j := rand.Intn(len(game.CardsOnDrawStack) - 1)
for i := 0; i < length; i++ {
j := rand.Intn(length)
game.CardsOnDrawStack[i], game.CardsOnDrawStack[j] = game.CardsOnDrawStack[j], game.CardsOnDrawStack[i]
}
game.Mu.Unlock()
Expand Down

0 comments on commit 5738ea4

Please sign in to comment.