War Card game! Annoying repetition problem

blov · · 530 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;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 ( &#34;fmt&#34; &#34;math/rand&#34; &#34;time&#34; ) type Card struct { CardName string Suit string Value int } type Deck []Card //create the deck func deckSetup() []Card { deck := Deck{ {CardName: &#34;Ace&#34;, Suit: &#34;Spades&#34;, Value: 14}, {CardName: &#34;Ace&#34;, Suit: &#34;Hearts&#34;, Value: 14}, {CardName: &#34;Ace&#34;, Suit: &#34;Diamonds&#34;, Value: 14}, {CardName: &#34;Ace&#34;, Suit: &#34;Cloves&#34;, Value: 14}, {CardName: &#34;King&#34;, Suit: &#34;Spades&#34;, Value: 13}, {CardName: &#34;King&#34;, Suit: &#34;Hearts&#34;, Value: 13}, {CardName: &#34;King&#34;, Suit: &#34;Diamonds&#34;, Value: 13}, {CardName: &#34;King&#34;, Suit: &#34;Cloves&#34;, Value: 13}, {CardName: &#34;Queen&#34;, Suit: &#34;Spades&#34;, Value: 12}, {CardName: &#34;Queen&#34;, Suit: &#34;Hearts&#34;, Value: 12}, {CardName: &#34;Queen&#34;, Suit: &#34;Diamonds&#34;, Value: 12}, {CardName: &#34;Queen&#34;, Suit: &#34;Cloves&#34;, Value: 12}, {CardName: &#34;Jack&#34;, Suit: &#34;Spades&#34;, Value: 11}, {CardName: &#34;Jack&#34;, Suit: &#34;Hearts&#34;, Value: 11}, {CardName: &#34;Jack&#34;, Suit: &#34;Diamonds&#34;, Value: 11}, {CardName: &#34;Jack&#34;, Suit: &#34;Cloves&#34;, Value: 11}, {CardName: &#34;Ten&#34;, Suit: &#34;Spades&#34;, Value: 10}, {CardName: &#34;Ten&#34;, Suit: &#34;Hearts&#34;, Value: 10}, {CardName: &#34;Ten&#34;, Suit: &#34;Diamonds&#34;, Value: 10}, {CardName: &#34;Ten&#34;, Suit: &#34;Cloves&#34;, Value: 10}, {CardName: &#34;Nine&#34;, Suit: &#34;Spades&#34;, Value: 9}, {CardName: &#34;Nine&#34;, Suit: &#34;Hearts&#34;, Value: 9}, {CardName: &#34;Nine&#34;, Suit: &#34;Diamonds&#34;, Value: 9}, {CardName: &#34;Nine&#34;, Suit: &#34;Cloves&#34;, Value: 9}, {CardName: &#34;Eight&#34;, Suit: &#34;Spades&#34;, Value: 8}, {CardName: &#34;Eight&#34;, Suit: &#34;Hearts&#34;, Value: 8}, {CardName: &#34;Eight&#34;, Suit: &#34;Diamonds&#34;, Value: 8}, {CardName: &#34;Eight&#34;, Suit: &#34;Cloves&#34;, Value: 8}, {CardName: &#34;Seven&#34;, Suit: &#34;Spades&#34;, Value: 7}, {CardName: &#34;Seven&#34;, Suit: &#34;Hearts&#34;, Value: 7}, {CardName: &#34;Seven&#34;, Suit: &#34;Diamonds&#34;, Value: 7}, {CardName: &#34;Seven&#34;, Suit: &#34;Cloves&#34;, Value: 7}, {CardName: &#34;Six&#34;, Suit: &#34;Spades&#34;, Value: 6}, {CardName: &#34;Six&#34;, Suit: &#34;Hearts&#34;, Value: 6}, {CardName: &#34;Six&#34;, Suit: &#34;Diamonds&#34;, Value: 6}, {CardName: &#34;Six&#34;, Suit: &#34;Cloves&#34;, Value: 6}, {CardName: &#34;Five&#34;, Suit: &#34;Spades&#34;, Value: 5}, {CardName: &#34;Five&#34;, Suit: &#34;Hearts&#34;, Value: 5}, {CardName: &#34;Five&#34;, Suit: &#34;Diamonds&#34;, Value: 5}, {CardName: &#34;Five&#34;, Suit: &#34;Cloves&#34;, Value: 5}, {CardName: &#34;Four&#34;, Suit: &#34;Spades&#34;, Value: 4}, {CardName: &#34;Four&#34;, Suit: &#34;Hearts&#34;, Value: 4}, {CardName: &#34;Four&#34;, Suit: &#34;Diamonds&#34;, Value: 4}, {CardName: &#34;Four&#34;, Suit: &#34;Cloves&#34;, Value: 4}, {CardName: &#34;Three&#34;, Suit: &#34;Spades&#34;, Value: 3}, {CardName: &#34;Three&#34;, Suit: &#34;Hearts&#34;, Value: 3}, {CardName: &#34;Three&#34;, Suit: &#34;Diamonds&#34;, Value: 3}, {CardName: &#34;Three&#34;, Suit: &#34;Cloves&#34;, Value: 3}, {CardName: &#34;Two&#34;, Suit: &#34;Spades&#34;, Value: 2}, {CardName: &#34;Two&#34;, Suit: &#34;Hearts&#34;, Value: 2}, {CardName: &#34;Two&#34;, Suit: &#34;Diamonds&#34;, Value: 2}, {CardName: &#34;Two&#34;, Suit: &#34;Cloves&#34;, 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 &lt; 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&#39;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 ( &#34;fmt&#34; &#34;math/rand&#34; &#34;time&#34; ) 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{&#34;Spades&#34;, &#34;Hearts&#34;, &#34;Diamons&#34;, &#34;Cloves&#34;} { for idx, name := range []string{&#34;Two&#34;, &#34;Three&#34;, &#34;Four&#34;, &#34;Five&#34;, &#34;Six&#34;, &#34;Seven&#34;, &#34;Eight&#34;, &#34;Nine&#34;, &#34;Ten&#34;, &#34;Jack&#34;, &#34;Queen&#34;, &#34;King&#34;, &#34;Ace&#34;} { 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(&#34;deck1:&#34;, deck1) fmt.Println(&#34;deck2:&#34;, 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&#39;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&#39;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 &#34;Spades&#34;, &#34;Hearts&#34;, etc. for _, suit := range []string{&#34;Spades&#34;, &#34;Hearts&#34;, &#34;Diamons&#34;, &#34;Cloves&#34;} { // Perform the following block for each of &#34;Two&#34;, &#34;Three&#34;, etc. for idx, name := range []string{&#34;Two&#34;, &#34;Three&#34;, &#34;Four&#34;, &#34;Five&#34;, &#34;Six&#34;, &#34;Seven&#34;, &#34;Eight&#34;, &#34;Nine&#34;, &#34;Ten&#34;, &#34;Jack&#34;, &#34;Queen&#34;, &#34;King&#34;, &#34;Ace&#34;} { // Will loop through in this order: // &#34;Spades&#34; - &#34;Two&#34;, &#34;Spades&#34; - &#34;Three&#34; . . . &#34;Hearts&#34; - &#34;Two&#34;, &#34;Hearts&#34; - &#34;Three&#34; etc. deck = append(deck, Card{name, suit, idx + 2}) } } </code></pre> <p>also it&#39;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&#39;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&#39;t thinking about the normal deck order since there&#39;s really no version of a game where you don&#39;t immediately shuffle the deck</p></pre>callcifer: <pre><blockquote> <p>// dbud: only set seed once.</p> </blockquote> <p>What does &#34;dbud&#34; 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&#39;t need it</p></pre>callcifer: <pre><p>Ah, sorry, didn&#39;t realize it was your username :)</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

530 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传