<p>I've been slowly making a card game when i have free time so some of you may see familiar code here.</p>
<p>Theres this annoying problem that happens when i create a function thats (supposed) to split the deck. I only want deckOne to hold 26 cards for the original deck in random order. I have a shuffle and delete function that works by itself then totally fucks up when i incorporate it into splitDeck.</p>
<p>So it gets 26 random cards then repeats the remaining 26 cards with the #2 suit cards which is infuriating. I dont know why.</p>
<pre><code> package main
import (
"fmt"
"math/rand"
"time"
)
type Card struct {
CardName string
Suit string
Value int
}
type Deck []Card
//create the deck
func deckSetup() []Card {
deck := Deck{
{CardName: "Ace", Suit: "Spades", Value: 14},
{CardName: "Ace", Suit: "Hearts", Value: 14},
{CardName: "Ace", Suit: "Diamonds", Value: 14},
{CardName: "Ace", Suit: "Cloves", Value: 14},
{CardName: "King", Suit: "Spades", Value: 13},
{CardName: "King", Suit: "Hearts", Value: 13},
{CardName: "King", Suit: "Diamonds", Value: 13},
{CardName: "King", Suit: "Cloves", Value: 13},
{CardName: "Queen", Suit: "Spades", Value: 12},
{CardName: "Queen", Suit: "Hearts", Value: 12},
{CardName: "Queen", Suit: "Diamonds", Value: 12},
{CardName: "Queen", Suit: "Cloves", Value: 12},
{CardName: "Jack", Suit: "Spades", Value: 11},
{CardName: "Jack", Suit: "Hearts", Value: 11},
{CardName: "Jack", Suit: "Diamonds", Value: 11},
{CardName: "Jack", Suit: "Cloves", Value: 11},
{CardName: "Ten", Suit: "Spades", Value: 10},
{CardName: "Ten", Suit: "Hearts", Value: 10},
{CardName: "Ten", Suit: "Diamonds", Value: 10},
{CardName: "Ten", Suit: "Cloves", Value: 10},
{CardName: "Nine", Suit: "Spades", Value: 9},
{CardName: "Nine", Suit: "Hearts", Value: 9},
{CardName: "Nine", Suit: "Diamonds", Value: 9},
{CardName: "Nine", Suit: "Cloves", Value: 9},
{CardName: "Eight", Suit: "Spades", Value: 8},
{CardName: "Eight", Suit: "Hearts", Value: 8},
{CardName: "Eight", Suit: "Diamonds", Value: 8},
{CardName: "Eight", Suit: "Cloves", Value: 8},
{CardName: "Seven", Suit: "Spades", Value: 7},
{CardName: "Seven", Suit: "Hearts", Value: 7},
{CardName: "Seven", Suit: "Diamonds", Value: 7},
{CardName: "Seven", Suit: "Cloves", Value: 7},
{CardName: "Six", Suit: "Spades", Value: 6},
{CardName: "Six", Suit: "Hearts", Value: 6},
{CardName: "Six", Suit: "Diamonds", Value: 6},
{CardName: "Six", Suit: "Cloves", Value: 6},
{CardName: "Five", Suit: "Spades", Value: 5},
{CardName: "Five", Suit: "Hearts", Value: 5},
{CardName: "Five", Suit: "Diamonds", Value: 5},
{CardName: "Five", Suit: "Cloves", Value: 5},
{CardName: "Four", Suit: "Spades", Value: 4},
{CardName: "Four", Suit: "Hearts", Value: 4},
{CardName: "Four", Suit: "Diamonds", Value: 4},
{CardName: "Four", Suit: "Cloves", Value: 4},
{CardName: "Three", Suit: "Spades", Value: 3},
{CardName: "Three", Suit: "Hearts", Value: 3},
{CardName: "Three", Suit: "Diamonds", Value: 3},
{CardName: "Three", Suit: "Cloves", Value: 3},
{CardName: "Two", Suit: "Spades", Value: 2},
{CardName: "Two", Suit: "Hearts", Value: 2},
{CardName: "Two", Suit: "Diamonds", Value: 2},
{CardName: "Two", Suit: "Cloves", Value: 2},
}
return deck
}
//shuffles and draws the first card. Should also remove the card via removeCard()
func shuffleAndDraws(setOfCards Deck) (Card, []Card) {
rand.Seed(time.Now().UnixNano())
i := rand.Intn(52)
newCard := setOfCards[i]
setOfCards = removeCard(setOfCards, i)
return newCard, setOfCards
}
//removes card and returns the entire deck without the card
func removeCard(setOfCards Deck, choiceElement int) []Card {
return append(setOfCards[0:choiceElement], setOfCards[choiceElement+1:]...)
}
//splits the deck into two seperate piles/decks. Suppose to mimick shuffling
//then creating two piles in real life
func deckSplit(setOfCards Deck) []Card {
var yo Card
var deckOne []Card
//var deckTwo []Card
//doesnt do what i want
for i := 0; i < 26; i++ {
yo, deckOne = shuffleAndDraws(setOfCards)
deckOne[i] = yo
}
return deckOne
}
func main() {
newDeck := deckSetup()
///var yo Card
//yo, newDeck = shuffleAndDraws(newDeck)
deckTwo := deckSplit(newDeck)
fmt.Println(deckTwo)
//fmt.Println(yo)
}
</code></pre>
<hr/>**评论:**<br/><br/>dbud: <pre><p>Here's a fixed and working version on <a href="https://play.golang.org/p/WjIg89BwO3" rel="nofollow">Play</a></p>
<pre><code>package main
import (
"fmt"
"math/rand"
"time"
)
func init() {
// dbud: only set seed once.
// dbud: In play, time is fixed, so this will always have same sort.
// dbud: on your machine it will be different
// dbud: to see different sorts on play, do +1 or whatever on the unix nano)
rand.Seed(time.Now().UnixNano())
}
type Card struct {
CardName string
Suit string
Value int
}
type Deck []Card
//create the deck
func deckSetup() Deck {
// dbud: simplify deck generation
var deck Deck
for _, suit := range []string{"Spades", "Hearts", "Diamons", "Cloves"} {
for idx, name := range []string{"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"} {
deck = append(deck, Card{name, suit, idx + 2})
}
}
return deck
}
func shuffle(d Deck) {
// dbud: simplify shuffling
for i := range d {
j := rand.Intn(i + 1)
d[i], d[j] = d[j], d[i]
}
}
// Draw from the top of the deck, if you want random order, call shuffle first
func draw(d Deck) (Card, Deck) {
return d[0], d[1:]
}
func main() {
newDeck := deckSetup()
shuffle(newDeck)
deck1, deck2 := newDeck[:26], newDeck[26:]
fmt.Println("deck1:", deck1)
fmt.Println("deck2:", deck2)
}
</code></pre>
<p>Your solution was a bit overly complicated for what amounts to a simple problem. </p>
<p>Your main issues were that you kept re-setting the random seed on each take of a card. The app executes quick enough that you were probably just setting it to the same number over and over (causing the next int to always be the same).</p>
<p>Secondly, you kept resetting deckOne to be the remaining set of cards then overwriting the index. I can't quite figure out what you logic was here other than to say it was just wrong.</p>
<pre><code>yo, deckOne = shuffleAndDraws(setOfCards)
deckOne[i] = yo
</code></pre>
<p>needed to be:</p>
<pre><code>yo, setOfCards = shuffleAndDraws(setOfCards)
deckOne[i] = yo
</code></pre>
<p>Thirdly, you were always picking a random number between 0 and 52 even though you were removing a card each time. You needed to pick between 0 and length of slice.</p></pre>Prerogativ: <pre><p>wow. You literally rewrote all my code XD. Clearly have a lot more to learn. Thanks for the tips.</p>
<p>I did end up seeing/solving the deckOne problem were it resets it. </p>
<p>Wow, thanks man</p></pre>Prerogativ: <pre><p>I guess my one question would be the code for the deck. Wouldnt your solution just have, one card for each category when the loop runs?</p>
<p>For instance: spades-two-2 then procedes to go to hearts-two-two rather than 4 types for each suit?</p>
<p>Another is the shuffling. Whats stopping it from, in the process of calling shuffle...say 20times, 5 ending up being the same int and thereby the same card?</p></pre>Sythe2o0: <pre><p>What do you mean four types for each suit? Do you want four of each card? The loop in this code will get you one of each number for each suit. </p>
<p>And, while it's possible your shuffle might shuffle a card into the same space it was in before, that also happens when you shuffle a deck in real life.</p></pre>Prerogativ: <pre><p>as in a traditional deck has a single two spades, two hearts, two diamonds and two cloves. Wouldnt your code just give me a single two spades then move on to a single three hearts?</p></pre>Sythe2o0: <pre><p>No.</p>
<pre><code>// Perform the following block for each of "Spades", "Hearts", etc.
for _, suit := range []string{"Spades", "Hearts", "Diamons", "Cloves"} {
// Perform the following block for each of "Two", "Three", etc.
for idx, name := range []string{"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"} {
// Will loop through in this order:
// "Spades" - "Two", "Spades" - "Three" . . . "Hearts" - "Two", "Hearts" - "Three" etc.
deck = append(deck, Card{name, suit, idx + 2})
}
}
</code></pre>
<p>also it's not my code</p>
<p>If you need further convincing, go to the play link that the code-writer gave you and run that. It prints out the deck for you, and you can check it isn't missing any cards.</p></pre>dbud: <pre><p>If you want the initial order to be 2 Spade, 2 Hearts, 2 Diamonds, etc
rather than 2 spade, 3 spade... 2 heart, 3 heart...</p>
<p>then just flip the two loops around. Do numbers on the outside and suits on the inside.</p>
<p>I wasn't thinking about the normal deck order since there's really no version of a game where you don't immediately shuffle the deck</p></pre>callcifer: <pre><blockquote>
<p>// dbud: only set seed once.</p>
</blockquote>
<p>What does "dbud" mean here?</p></pre>dbud: <pre><p>I originally was just fixing the code and wanted to separate my comments from the authors. Just so folks knew where to look. The final posted version probably didn't need it</p></pre>callcifer: <pre><p>Ah, sorry, didn't realize it was your username :)</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传