From d7c513d287b0d6dc4e7337dc91b80b64f375e6a2 Mon Sep 17 00:00:00 2001 From: linyi31 Date: Tue, 19 Apr 2016 14:56:31 +0800 Subject: [PATCH] add Package and Sources --- Package.swift | 5 +++++ README.md | 1 + Sources/O2PlayingCard.swift | 28 ++++++++++++++++++++++++++ Sources/O2Rank.swift | 40 +++++++++++++++++++++++++++++++++++++ Sources/O2Suit.swift | 35 ++++++++++++++++++++++++++++++++ 5 files changed, 109 insertions(+) create mode 100644 Package.swift create mode 100644 Sources/O2PlayingCard.swift create mode 100644 Sources/O2Rank.swift create mode 100644 Sources/O2Suit.swift diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..958bd2b --- /dev/null +++ b/Package.swift @@ -0,0 +1,5 @@ +import PackageDescription + +let package = Package( + name: "O2PlayingCard" +) diff --git a/README.md b/README.md index e69de29..2252b7a 100644 --- a/README.md +++ b/README.md @@ -0,0 +1 @@ +# O2PlayingCard \ No newline at end of file diff --git a/Sources/O2PlayingCard.swift b/Sources/O2PlayingCard.swift new file mode 100644 index 0000000..bc2c182 --- /dev/null +++ b/Sources/O2PlayingCard.swift @@ -0,0 +1,28 @@ +public struct O2PlayingCard { + let rank: O2Rank + let suit: O2Suit +} + + +// MARK: - Equatable + +extension O2PlayingCard: Equatable {} + +public func ==(lhs:O2PlayingCard, rhs: O2PlayingCard) -> Bool { + return lhs.rank == rhs.rank && lhs.suit == rhs.suit +} + +// MARK: - Comparable +extension O2PlayingCard: Comparable {} + +public func <(lhs: O2PlayingCard, rhs: O2PlayingCard) -> Bool { + return lhs.suit < rhs.suit || (lhs.suit == rhs.suit && lhs.rank < rhs.rank) +} + +// MARK: - CustomStringConvertible + +extension O2PlayingCard: CustomStringConvertible { + public var description: String { + return "\(suit)\(rank)" + } +} \ No newline at end of file diff --git a/Sources/O2Rank.swift b/Sources/O2Rank.swift new file mode 100644 index 0000000..6425b10 --- /dev/null +++ b/Sources/O2Rank.swift @@ -0,0 +1,40 @@ +public enum O2Rank: Int { + case Ace = 1 + case Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten + case Jack, Queen, King +} + +// MARK: - Comparable + +extension O2Rank: Comparable {} + +public func <(lhs: O2Rank, rhs: O2Rank) -> Bool { + switch (lhs,rhs) { + case (_, _) where lhs == rhs: + return false + case (.Ace, _): + return false + default: + return lhs.rawValue < rhs.rawValue + } +} + +// MARK: - CustomStringConvertible + +extension O2Rank: CustomStringConvertible { + public var description: String { + switch self { + case .Ace: + return "A" + case .Jack: + return "J" + case .Queen: + return "Q" + case .King: + return "K" + default: + return "\(rawValue)" + } + } +} + diff --git a/Sources/O2Suit.swift b/Sources/O2Suit.swift new file mode 100644 index 0000000..34a7277 --- /dev/null +++ b/Sources/O2Suit.swift @@ -0,0 +1,35 @@ +public enum O2Suit: String { + case Spades, Hearts, Diamonds, Clubs +} + +// MARK: - Comparable + +extension O2Suit: Comparable {} + +public func<(lhs: O2Suit, rhs: O2Suit) -> Bool { + switch (lhs, rhs) { + case (_, _) where lhs == rhs: + return false + case (.Spades, _), (.Hearts, .Diamonds), (.Hearts, .Clubs), (.Diamonds, .Clubs): + return false + default: + return true + } +} + + +// MARK: - CustomStringConvertible +extension O2Suit: CustomStringConvertible { + public var description: String { + switch self { + case .Spades: + return "♠︎" + case .Hearts: + return "♡" + case .Diamonds: + return "♢" + case .Clubs: + return "♣︎" + } + } +} \ No newline at end of file