Skip to content

Instantly share code, notes, and snippets.

@roktas
Forked from ecylmz/card.go
Last active June 7, 2017 18:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roktas/5191259 to your computer and use it in GitHub Desktop.
Save roktas/5191259 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"sort"
"math/rand"
"time"
"strings"
)
// Kart
type Card struct {
Suite string
Rank string
}
// Kartı sembol olarak ver
func (c *Card) Symbol() string {
return c.Suite[:1] + c.Rank
}
func (c *Card) RankValue() int {
return sort.SearchStrings(RANKS, c.Rank)
}
func (c *Card) SuiteValue() int {
return sort.SearchStrings(SUITES, c.Suite)
}
// Kartı sembolle göster
func (c Card) String() string {
return GLYPH[c.Symbol()]
}
// Deste
type Deck struct {
Cards []Card
}
// Desteden el çıkar
func (d *Deck) Hand() Hand {
// pop
hand_cards := d.Cards[0:DEFAULT_HAND_SIZE]
// pop sonrası kalan deste
d.Cards = d.Cards[DEFAULT_HAND_SIZE:]
return Hand{hand_cards}
}
// Desteyi karıştır
func (d *Deck) Shuffle() *Deck {
src := d.Cards
rand.Seed(time.Now().UnixNano())
perm := rand.Perm(len(d.Cards))
for i, v := range perm {
d.Cards[v] = src[i]
}
return d
}
// El
type Hand struct {
Cards []Card
}
func (h *Hand) Deal() Card {
// pop işlemi
card := h.Cards[len(h.Cards)-1:]
h.Cards = h.Cards[0:len(h.Cards)-1]
return card[0]
}
// Eli sembollerle göster
func (h Hand) String() string {
var cardsSymbol []string
for _, card := range h.Cards {
cardsSymbol = append(cardsSymbol, card.String())
}
return strings.Join(cardsSymbol, " ")
}
var (
SUITES []string = []string{"club", "diamondi", "heart", "spade"}
RANKS []string = []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "j", "q", "k"}
DEFAULT_HAND_SIZE = 4 // Öntanımlı el kaç kart
GLYPH map[string]string = map[string]string {
"s1": "\U0001f0a1", "h1": "\U0001f0b1", "d1": "\U0001f0c1", "c1": "\U0001f0d1",
"s2": "\U0001f0a2", "h2": "\U0001f0b2", "d2": "\U0001f0c2", "c2": "\U0001f0d2",
"s3": "\U0001f0a3", "h3": "\U0001f0b3", "d3": "\U0001f0c3", "c3": "\U0001f0d3",
"s4": "\U0001f0a4", "h4": "\U0001f0b4", "d4": "\U0001f0c4", "c4": "\U0001f0d4",
"s5": "\U0001f0a5", "h5": "\U0001f0b5", "d5": "\U0001f0c5", "c5": "\U0001f0d5",
"s6": "\U0001f0a6", "h6": "\U0001f0b6", "d6": "\U0001f0c6", "c6": "\U0001f0d6",
"s7": "\U0001f0a7", "h7": "\U0001f0b7", "d7": "\U0001f0c7", "c7": "\U0001f0d7",
"s8": "\U0001f0a8", "h8": "\U0001f0b8", "d8": "\U0001f0c8", "c8": "\U0001f0d8",
"s9": "\U0001f0a9", "h9": "\U0001f0b9", "d9": "\U0001f0c9", "c9": "\U0001f0d9",
"s10": "\U0001f0aa", "h10": "\U0001f0ba", "d10": "\U0001f0ca", "c10": "\U0001f0da",
"sj": "\U0001f0ab", "hj": "\U0001f0bb", "dj": "\U0001f0cb", "cj": "\U0001f0db",
"sq": "\U0001f0ad", "hq": "\U0001f0bd", "dq": "\U0001f0cd", "cq": "\U0001f0dd",
"sk": "\U0001f0ae", "hk": "\U0001f0be", "dk": "\U0001f0ce", "ck": "\U0001f0de",
}
)
func main() {
// Kartları şimdi üret
var CARDS []Card
for _, suite := range SUITES {
for _, rank := range RANKS {
card := Card{suite, rank}
CARDS = append(CARDS, card)
}
}
// Yeni deste
d := Deck{CARDS}
// Desteyi karıştır
d.Shuffle()
for i := 0; i < (52 / DEFAULT_HAND_SIZE); i++ {
h := d.Hand() // Desteden eller
fmt.Println(h)
for j := 0; j < DEFAULT_HAND_SIZE; j++ {
card := h.Deal() // Ellerden tek tek kart
fmt.Println(card)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment