Last active
August 29, 2015 14:04
-
-
Save kuroyam/4e17ca389cfe69754206 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Playground - noun: a place where people can play | |
import Cocoa | |
enum Suit : Int { | |
case Spade, Club, Heart, Diamond | |
func toString() -> String { | |
switch self { | |
case .Spade: | |
return "\u2660" | |
case .Club: | |
return "\u2663" | |
case .Heart: | |
return "\u2665" | |
case .Diamond: | |
return "\u2666" | |
} | |
} | |
} | |
enum Rank : Int { | |
case Ace = 1 | |
case Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten | |
case Jack, Queen, King | |
func toString() -> String { | |
switch self { | |
case .Ace: | |
return "A" | |
case .Jack: | |
return "J" | |
case .Queen: | |
return "Q" | |
case .King: | |
return "K" | |
default: | |
return String(self.toRaw()) | |
} | |
} | |
} | |
class Card { | |
let suit : Suit | |
let rank : Rank | |
init(suit : Suit, rank : Rank) { | |
self.suit = suit | |
self.rank = rank | |
} | |
func toString() -> String { | |
return self.suit.toString() + self.rank.toString() | |
} | |
} | |
class Deck { | |
var cards : Array<Card> | |
init() { | |
cards = [] | |
for i in 0...3 { | |
for j in 1...13 { | |
cards += Card(suit: Suit.fromRaw(i)!, rank: Rank.fromRaw(j)!) | |
} | |
} | |
} | |
func shuffle() { | |
for var i = cards.count - 1; i > 0; i-- { | |
var j = Int(arc4random_uniform(UInt32(i))) | |
var tmp = cards[i] | |
cards[i] = cards[j] | |
cards[j] = tmp | |
} | |
} | |
func printAll() { | |
for card in cards { | |
print(card.toString()+"\n") | |
} | |
} | |
} | |
class Hand { | |
let maxCount = 5 | |
var cards : Array<Card?> | |
init() { | |
cards = Array<Card?>(count: maxCount, repeatedValue: nil) | |
} | |
func append(cards: Card?...) { | |
if self.cards.count + cards.count > 5 { | |
// TODO: assertion | |
return | |
} | |
self.cards += cards | |
} | |
func remove(card: Card?) { | |
} | |
func remove(cards: Card?...) { | |
if cards.count > 5 { | |
// TODO: assertion | |
return | |
} | |
} | |
} | |
var deck = Deck() | |
deck.shuffle() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment