Skip to content

Instantly share code, notes, and snippets.

@cdanyl
Created February 14, 2023 09:45
Show Gist options
  • Save cdanyl/7c858ead6f144e2d46a2ed7e2db54934 to your computer and use it in GitHub Desktop.
Save cdanyl/7c858ead6f144e2d46a2ed7e2db54934 to your computer and use it in GitHub Desktop.
Here is an example algorithm in GoLang for shuffling and dealing poker cards from a deck of 52 cards
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano()) // initialisation du générateur de nombres aléatoires
// création du jeu de 52 cartes
cards := make([]string, 52)
cardIndex := 0
for _, suit := range []string{"Spades", "Hearts", "Diamonds", "Clubs"} {
for _, rank := range []string{"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"} {
cards[cardIndex] = rank + " of " + suit
cardIndex++
}
}
// mélange des cartes
for i := range cards {
j := rand.Intn(i + 1)
cards[i], cards[j] = cards[j], cards[i]
}
// distribution des cartes
playerHands := make([][]string, 4)
for i := range playerHands {
playerHands[i] = make([]string, 0)
}
currentHand := 0
for _, card := range cards {
playerHands[currentHand] = append(playerHands[currentHand], card)
currentHand = (currentHand + 1) % 4
}
// affichage des mains de joueurs
for i, hand := range playerHands {
fmt.Printf("Hand %d:\n", i+1)
for _, card := range hand {
fmt.Println(card)
}
fmt.Println()
}
}
@cdanyl
Copy link
Author

cdanyl commented Feb 14, 2023

This algorithm creates a 52-card deck, shuffles it randomly, and then deals the cards to the four players. Each hand is stored in a string slice, which is then displayed on the screen. Note that this example uses a simple shuffling method called "Fisher-Yates shuffle".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment