swift - Add a method to Card that creates a full deck of cards, with one card of each combination of rank and suit -


so have been doing experiments in apple swift book.

i have been able of them, except 1 far. below have tried, can't figure out how working.

add method card creates full deck of cards, 1 card of each combination of rank , suit.

// playground - noun: place people can play  enum rank: int {     case ace = 1     case two, three, four, five, six, seven, eight, nine, ten     case jack, queen, king      func simpledescription() -> string {         switch self {             case .ace:                 return "ace"             case .jack:                 return "jack"             case .queen:                 return "queen"             case .king:                 return "king"             default:                 return string(self.toraw())         }     } }  enum suit {     case spades, hearts, diamonds, clubs      func simpledescription() -> string {         switch self {             case .spades:                 return "spades"             case .hearts:                 return "hearts"             case .diamonds:                 return "diamonds"             case .clubs:                 return "clubs"         }     } }  struct card {     var rank: rank     var suit: suit      func simpledescription() -> string {         return "the \(rank.simpledescription()) of \(suit.simpledescription())"     }      func createfulldeck() -> array{         var fulldeck: array          fulldeck = card(rank: .ace, suit: .spades)         fulldeck = card(rank: .two, suit: .spades)          return fulldeck     } }  let threeofspades = card(rank: .three, suit: .spades) let threeofspadesdescription = threeofspades.simpledescription()  threeofspades.createfulldeck() 
  • i don't know supposed return method, array?
  • should use loop create this? or there proper/easier way enum
  • why create method inside of card, calling threeofspades.createfulldeck() seems incorrect.

here's way of doing it, time using techniques have learned point*

first define possible ranks , suits, using respective rank , suit enums defined previously.

next have function iterate on each rank within each suit, creating card each, , returning array of cards.

struct card {     var rank: rank     var suit: suit     func simpledescription() -> string {         return "the \(rank.simpledescription()) of \(suit.simpledescription())"     }      func createdeck() -> card[] {         let ranks = [rank.ace, rank.two, rank.three, rank.four, rank.five, rank.six, rank.seven, rank.eight, rank.nine, rank.ten, rank.jack, rank.queen, rank.king]         let suits = [suit.spades, suit.hearts, suit.diamonds, suit.clubs]         var deck = card[]()         suit in suits {             rank in ranks {                 deck.append(card(rank: rank, suit: suit))             }         }         return deck     } } 

(* notable exception tour hadn't explicitly explained how append arrays @ point)


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -