-5

I am making a card game, I started by placing all the cards in an array of tuples with a number 1-52 assigned. But by using a random number generator to pick the cards it becomes difficult to remove a card from the deck because I can't specifically remove that number from my random number generator. Any help is appreciated.

 func createDeck(){

    deck.append((name: "ace1", number: 1))
    deck.append((name: "ace2", number: 2))
    deck.append((name: "ace3", number: 3))
    deck.append((name: "ace4", number: 4))
    deck.append((name: "two1", number: 5))
    deck.append((name: "two2", number: 6))
    deck.append((name: "two3", number: 7))
    deck.append((name: "two4", number: 8))
    deck.append((name: "three1", number: 9))
    deck.append((name: "three2", number: 10))
    deck.append((name: "three3", number: 11))
    deck.append((name: "three4", number: 12))
    deck.append((name: "four1", number: 13))
    deck.append((name: "four2", number: 14))
    deck.append((name: "four3", number: 15))
    deck.append((name: "four4", number: 16))
    deck.append((name: "five1", number: 17))
    deck.append((name: "five2", number: 18))
    deck.append((name: "five3", number: 19))
    deck.append((name: "five4", number: 20))
    deck.append((name: "six1", number: 21))
    deck.append((name: "six2", number: 22))
    deck.append((name: "six3", number: 23))
    deck.append((name: "six4", number: 24))
    deck.append((name: "seven1", number: 25))
    deck.append((name: "seven2", number: 26))
    deck.append((name: "seven3", number: 27))
    deck.append((name: "seven4", number: 28))
    deck.append((name: "eight1", number: 29))
    deck.append((name: "eight2", number: 30))
    deck.append((name: "eight3", number: 31))
    deck.append((name: "eight4", number: 32))
    deck.append((name: "nine1", number: 33))
    deck.append((name: "nine2", number: 34))
    deck.append((name: "nine3", number: 35))
    deck.append((name: "nine4", number: 36))
    deck.append((name: "ten1", number: 37))
    deck.append((name: "ten2", number: 38))
    deck.append((name: "ten3", number: 39))
    deck.append((name: "ten4", number: 40))
    deck.append((name: "jack1", number: 41))
    deck.append((name: "jack2", number: 42))
    deck.append((name: "jack3", number: 43))
    deck.append((name: "jack4", number: 44))
    deck.append((name: "queen1", number: 45))
    deck.append((name: "queen2", number: 46))
    deck.append((name: "queen3", number: 47))
    deck.append((name: "queen4", number: 48))
    deck.append((name: "king1", number: 49))
    deck.append((name: "king2", number: 50))
    deck.append((name: "king3", number: 51))
    deck.append((name: "king4", number: 52))


}
func randomNumber(){
    random = arc4random_uniform(52) + 0;
    print(random)
}
func deal(){

    var aces = Set<Int>(arrayLiteral:1, 2, 3,4)
    var twos = Set<Int>(arrayLiteral:5, 6, 7,8)
    var threes = Set<Int>(arrayLiteral:9, 10, 11,12)
    var fours = Set<Int>(arrayLiteral:13, 14, 15,16)
    var fives = Set<Int>(arrayLiteral:17, 18,19,20)
    var sixes = Set<Int>(arrayLiteral:21, 22, 23,24)
    var sevens = Set<Int>(arrayLiteral:25, 26, 27,28)
    var eights = Set<Int>(arrayLiteral:29, 30, 31,32)
    var nines = Set<Int>(arrayLiteral:33, 34, 35,36)
    var tens = Set<Int>(arrayLiteral:37, 38, 39,40)
    var jacks = Set<Int>(arrayLiteral:41, 42, 43,44)
    var queens = Set<Int>(arrayLiteral:45, 46, 47,48)
    var kings = Set<Int>(arrayLiteral:49, 50, 51,52)



    if aces.contains(Int(random)){
        playern1 = Int(random)
        aces.removeFirst()
    }
    if twos.contains(Int(random)){
        twos.removeFirst()

...

Johnd
  • 584
  • 2
  • 6
  • 21
  • How does it "become difficult"? And what does _picking_ or _removing_ a card have to do with _making a deck_? Show your code and explain what you want and what's actually happening. – matt Jan 23 '17 at 01:18
  • ok there you go – Johnd Jan 23 '17 at 01:20
  • any suggestion would be helpful – Johnd Jan 23 '17 at 01:24
  • 2
    Are you kidding? Suggestion about _what_? You didn't do what I asked. You didn't explain what you want and what's actually happening. And believe me, looking at the code you provided, I have _no_ idea what you are trying to do. Clue me in. – matt Jan 23 '17 at 01:25
  • 2
    You create a `deck` and you never use it. You define `randomNumber` and you never call it. Your code doesn't _do_ anything! – matt Jan 23 '17 at 01:27
  • 2
    Regardless of everything else, you should learn about arrays, there is no reason to have fifty `append` calls. – Benjamin Lowry Jan 23 '17 at 01:32
  • ok I just asked a question. do you need to rip me about everything?? – Johnd Jan 23 '17 at 02:03
  • Other people didn't seem to have any problems helping below – Johnd Jan 23 '17 at 02:04
  • Possible duplicate of [How to enumerate an enum with String type?](http://stackoverflow.com/questions/24007461/how-to-enumerate-an-enum-with-string-type) – muescha Jan 23 '17 at 04:35
  • many solutions how to make a Deck you find here: http://stackoverflow.com/questions/24007461/how-to-enumerate-an-enum-with-string-type – muescha Jan 23 '17 at 04:36

2 Answers2

0

Maintain an array of cards that are still available and not already picked. For the purpose of this discussion, I will call this the "deal array" (this must be maintained separately to the deck array! It could start off as a mutable copy of the deck array.).

When you randomly pick/deal a card remove it from the deal array.

Your random number generator should be choosing a number between zero and the number of items left in the deal array - NOT between zero and fifty two.

(Assuming the deal array is not empty - check this before randomly choosing a card from the deal array.)

When selecting a card from the deal array use the random number as an index to the array (not as the card numbers you've created, which are not of any real use for picking cards from the deal array randomly).

(Note that I'm not including any example code here, because it's unclear to me what the purpose of some of your code is. If I tried to post code here, the chances are that I would mis-interpret your code and therefore post incorrect code).

Son of a Beach
  • 1,733
  • 1
  • 11
  • 29
0

You may want to redesign how you define a card:

enum Suit: String {
    case heart, diamond, club, spade
}
enum Rank: Int {
    case _1 = 1, _2 = 2, _3 = 3, _4 = 4, _5 = 5, _6 = 6, _7 = 7, _8 = 8, _9 = 9, _10 = 10
    case jack = 11, queen = 12, king = 13
}
struct Card {
    let suit: Suit
    let rank: Rank
}

(or an alternative could be enum Card {case card(suit: Suit, rank: Rank)})

Then defining your deck:

var deck = [Card(suit: .heart, rank: ._1),
            Card(suit: .heart, rank: ._2),
            Card(suit: .heart, rank: ._3),
            Card(suit: .heart, rank: ._4),
            Card(suit: .heart, rank: ._5),
            Card(suit: .heart, rank: ._6),
            // ...
]

Then picking a card from a deck:

func pickRandomCardFromDeck() -> Card {
    return deck.remove(at: Int(arc4random_uniform(UInt32(deck.count))))
}

[edit for comment]

To check for a pair:

extension Card {
    func isPair(with card: Card) -> Bool {
        return rank == card.rank
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267