Created
May 17, 2023 19:58
-
-
Save StephanieSunshine/051baca288a27e1aad1bc27fcb24677d to your computer and use it in GitHub Desktop.
Basic OOPish structuring in Golang to build a deck of cards
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/rand" | |
) | |
var ( | |
HEARTS Suits = Suits{"HEARS", 3} | |
SPADES Suits = Suits{"SPADES", 2} | |
DIAMONDS Suits = Suits{"DIAMONDS", 1} | |
CLUBS Suits = Suits{"CLUBS", 0} | |
ACE Ranks = Ranks{"ACE", 1} | |
TWO Ranks = Ranks{"TWO", 2} | |
THREE Ranks = Ranks{"THREE", 3} | |
FOUR Ranks = Ranks{"FOUR", 4} | |
FIVE Ranks = Ranks{"FIVE", 5} | |
SIX Ranks = Ranks{"SIX", 6} | |
SEVEN Ranks = Ranks{"SEVEN", 7} | |
EIGHT Ranks = Ranks{"EIGHT", 8} | |
NINE Ranks = Ranks{"NINE", 9} | |
TEN Ranks = Ranks{"TEN", 10} | |
JACK Ranks = Ranks{"JACK", 11} | |
QUEEN Ranks = Ranks{"QUEEN", 12} | |
KING Ranks = Ranks{"KING", 13} | |
) | |
var ( | |
GetSuits []Suits = []Suits{HEARTS, SPADES, DIAMONDS, CLUBS} | |
GetRanks []Ranks = []Ranks{ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING} | |
) | |
type Suits struct { | |
Name string | |
Value int | |
} | |
type Ranks struct { | |
Name string | |
Value int | |
} | |
type Card struct { | |
Suit Suits | |
Rank Ranks | |
} | |
type Deck struct { | |
Cards [52]Card | |
} | |
func New() *Deck { | |
d := Deck{} | |
c := 0 | |
for _, r := range GetRanks { | |
for _, s := range GetSuits { | |
d.Cards[c] = Card{s, r} | |
c++ | |
// fmt.Println(r, s) | |
} | |
} | |
return &d | |
} | |
func (d *Deck) Shuffle() { | |
for _ = range [3]int{0, 1, 2} { | |
rand.Shuffle(len(d.Cards), func(i, j int) { | |
d.Cards[i], d.Cards[j] = d.Cards[j], d.Cards[i] | |
}) | |
} | |
} | |
func (d *Deck) Show() { | |
for _, c := range d.Cards { | |
fmt.Println(c.Rank.Name, c.Suit.Name) | |
} | |
} | |
func main() { | |
d := New() | |
d.Shuffle() | |
d.Show() | |
//fmt.Println("Hello World") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment