🖥

#Go 言語でシャッフルした52枚のトランプを1枚ずつ配るサンプル ( Go lang deal shuffle playing cards

2020/03/16に公開
package main

import (
"fmt"
"math/rand"
"time"
)

func main() {
cards := generateCards()

shuffleCards(cards)

yourCards := []string{}

for _, card := range cards {
yourCards = append(yourCards, card)
fmt.Println(yourCards)
}
}

func generateCards() []string {
cardSeeds := []string{"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}
cards := []string{}

for i := 0; i < 4; i++ {
cards = append(cards, cardSeeds...)
}
return cards
}

func shuffleCards(cards []string) {
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(cards), func(i, j int) { cards[i], cards[j] = cards[j], cards[i] })
}

result example

[5]
[5 3]
[5 3 8]
[5 3 8 J]
[5 3 8 J Q]
[5 3 8 J Q 9]
[5 3 8 J Q 9 9]
[5 3 8 J Q 9 9 6]
[5 3 8 J Q 9 9 6 5]
[5 3 8 J Q 9 9 6 5 4]
[5 3 8 J Q 9 9 6 5 4 4]
[5 3 8 J Q 9 9 6 5 4 4 8]
[5 3 8 J Q 9 9 6 5 4 4 8 3]
[5 3 8 J Q 9 9 6 5 4 4 8 3 7]
[5 3 8 J Q 9 9 6 5 4 4 8 3 7 Q]
[5 3 8 J Q 9 9 6 5 4 4 8 3 7 Q A]
[5 3 8 J Q 9 9 6 5 4 4 8 3 7 Q A 4]
[5 3 8 J Q 9 9 6 5 4 4 8 3 7 Q A 4 9]
[5 3 8 J Q 9 9 6 5 4 4 8 3 7 Q A 4 9 9]
[5 3 8 J Q 9 9 6 5 4 4 8 3 7 Q A 4 9 9 7]
[5 3 8 J Q 9 9 6 5 4 4 8 3 7 Q A 4 9 9 7 2]
[5 3 8 J Q 9 9 6 5 4 4 8 3 7 Q A 4 9 9 7 2 A]
[5 3 8 J Q 9 9 6 5 4 4 8 3 7 Q A 4 9 9 7 2 A A]
[5 3 8 J Q 9 9 6 5 4 4 8 3 7 Q A 4 9 9 7 2 A A 10]
[5 3 8 J Q 9 9 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