Skip to content

Instantly share code, notes, and snippets.

@marioidival
Created May 26, 2015 21:52
Show Gist options
  • Select an option

  • Save marioidival/35d73b6881a3b6788b57 to your computer and use it in GitHub Desktop.

Select an option

Save marioidival/35d73b6881a3b6788b57 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"bytes"
"math/rand"
"time"
)
func shuffle(arr []string) {
t := time.Now()
rand.Seed(int64(t.Nanosecond()))
for i := len(arr) - 1; i > 0; i-- {
j := rand.Intn(i)
arr[i], arr[j] = arr[j], arr[i]
}
}
func deal(arr *[]string, numberCards int) []string {
newArr := *arr
card := newArr[len(newArr)-numberCards:]
*arr = newArr[:len(newArr)-numberCards]
return card
}
func main() {
values := []string{"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}
suits := []string{"Clubs", "Diamonds", "Hearts", "Spades"}
cards := []string{}
for suit := range suits {
for value := range values {
var buffer bytes.Buffer
buffer.WriteString(values[value] + " of " + suits[suit])
card := buffer.String()
cards = append(cards, card)
}
}
shuffledCards := cards
shuffle(shuffledCards)
dealing := deal(&shuffledCards, 2)
fmt.Println("Your hand: ", dealing[0], ",", dealing[1])
fmt.Println("How many players (2-8):")
n_players := 0
fmt.Scanf("%d", &n_players)
for i := 0; i < n_players; i++ {
hand := deal(&shuffledCards, 2)
fmt.Printf("CPU %d, %s, %s\n", i+1, hand[0], hand[1])
}
flop, turn, river := deal(&shuffledCards, 3), deal(&shuffledCards, 1), deal(&shuffledCards, 1)
fmt.Printf("Flop: %s, %s, %s\n", flop[0], flop[1], flop[2])
fmt.Printf("Turn: %s\n", turn[0])
fmt.Printf("River: %s\n", river[0])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment