0

So I'm trying to deal cards with my really small knowledge. I can randomize a card but I don't want to get the same card twice. I tried using "arrays" with "if" but I couldn't do it. Any idea how can I manage this?

  • 4
    [Shuffle](http://stackoverflow.com/questions/6127503/shuffle-array-in-c) then iterate over the array. – clcto Nov 19 '15 at 21:28
  • If you remove the drawn card from the deck, then picking another card at random from the deck could not possibly give you the same card. IMO no need to shuffle, just pick ANOTHER random card. – dtech Nov 19 '15 at 21:35
  • Who the h decided that dealing unique cards from a deck involves shuffling? That would be just wasted CPU cycles. – dtech Nov 19 '15 at 22:04

2 Answers2

-1

Start by making a full deck of cards as an array.

Shuffle the array using say, 1000 random swaps.

Have a counter for how much of the array you have dealt so far, starting at zero, and every time you 'deal' a card, increment that counter by one.

Dan
  • 660
  • 6
  • 15
  • 2
    The [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher–Yates_shuffle) will do a better job. See the comment by @clcto for more info and sample code. – user3386109 Nov 19 '15 at 21:36
  • Yeah, I don't claim at all that swapping is the best way to shuffle, but it is a extremely easy method to implement, and so I recommend it especially for novices. – Dan Nov 19 '15 at 21:54
  • 2
    The Fisher-Yates shuffle kind of does (N-1) random swaps but with one index given by a counter. I don't think it's more difficult to implement than just 1000 random swaps. – roeland Nov 19 '15 at 21:57
-1

Create a card object that contains a boolean of whether or not you have seen the card already. Then when you pick a new card, ask the card if it has been seen, if yes then pick again until you do not get one that has already been seen.

user3073234
  • 771
  • 5
  • 11
  • 24
  • 1
    I didn't know you could create objects in c. – Kubilay Doğru Nov 19 '15 at 21:38
  • @KubilayDoğru - if that is indeed the case, you need to make a step back, learn the language first, and then try to use it. – dtech Nov 19 '15 at 21:39
  • Thats what I'm trying to do. Look what did I write at my op. I'll be searching the ways this guys shows me but a little example with the explanation would be appreciated. – Kubilay Doğru Nov 19 '15 at 21:46
  • I get your frustration but stack overflow is to help people with their code, not to write the code itself. If you really need help go to codementor.io – user3073234 Nov 19 '15 at 21:53
  • 1
    @KubilayDoğru - if you don't know that you can create objects in C, I'd say you are getting ahead of yourself. Learn about `struct`, learn about functions and arrays, learn about memory, then you will know how to create card objects, how to create a deck of cards, how to draw from the deck, how to shuffle or split, how to sort the deck. It is very easy, but you need to know the language first. You can't learn it by trying to use it without knowing the basics. You can and will later learn from practice, but before that, you need the basic stuff. – dtech Nov 19 '15 at 21:54