Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
88
Task/Playing-cards/Swift/playing-cards-1.swift
Normal file
88
Task/Playing-cards/Swift/playing-cards-1.swift
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
struct Card: CustomStringConvertible
|
||||
{
|
||||
enum Suit: String, CaseIterable, CustomStringConvertible
|
||||
{
|
||||
case clubs = "♣️"
|
||||
case diamonds = "♦️"
|
||||
case hearts = "♥️"
|
||||
case spades = "♠️"
|
||||
|
||||
var description: String { rawValue }
|
||||
}
|
||||
|
||||
let suit: Suit
|
||||
let value: Int
|
||||
|
||||
var description: String
|
||||
{
|
||||
let valueAsString: String
|
||||
switch value
|
||||
{
|
||||
case 1:
|
||||
valueAsString = "A"
|
||||
case 11:
|
||||
valueAsString = "J"
|
||||
case 12:
|
||||
valueAsString = "Q"
|
||||
case 13:
|
||||
valueAsString = "K"
|
||||
default:
|
||||
valueAsString = "\(value)"
|
||||
}
|
||||
return valueAsString + suit.description
|
||||
}
|
||||
}
|
||||
|
||||
struct Deck: CustomStringConvertible
|
||||
{
|
||||
var cards: [Card] = []
|
||||
|
||||
init()
|
||||
{
|
||||
for suit in Card.Suit.allCases
|
||||
{
|
||||
for faceValue in 1 ... 13
|
||||
{
|
||||
cards.append(Card(suit: suit, value: faceValue))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var description: String
|
||||
{
|
||||
String(cards.map{ $0.description }.joined(separator: ", "))
|
||||
}
|
||||
|
||||
mutating func shuffle()
|
||||
{
|
||||
cards.shuffle()
|
||||
}
|
||||
|
||||
mutating func dealCard() -> Card?
|
||||
{
|
||||
guard !cards.isEmpty else { return nil }
|
||||
return cards.removeLast()
|
||||
}
|
||||
}
|
||||
|
||||
var deck = Deck()
|
||||
print("New deck:")
|
||||
print(deck)
|
||||
deck.shuffle()
|
||||
print("Shuffled deck:")
|
||||
print(deck)
|
||||
|
||||
var hands: [[Card]] = [[], [], [], []]
|
||||
|
||||
var handIndex = 0
|
||||
|
||||
while let card = deck.dealCard()
|
||||
{
|
||||
hands[handIndex].append(card)
|
||||
handIndex = (handIndex + 1) % hands.count
|
||||
}
|
||||
|
||||
print ("Hands:")
|
||||
print(hands.map({ $0.description }).joined(separator: "\n"))
|
||||
print("Remaining deck (should be empty):")
|
||||
print(deck)
|
||||
182
Task/Playing-cards/Swift/playing-cards-2.swift
Normal file
182
Task/Playing-cards/Swift/playing-cards-2.swift
Normal file
|
|
@ -0,0 +1,182 @@
|
|||
import Foundation
|
||||
|
||||
// extend any Indexed collection to be able to shuffle (see http://stackoverflow.com/questions/24026510/how-do-i-shuffle-an-array-in-swift)
|
||||
extension CollectionType where Index == Int {
|
||||
/// Return a copy of `self` with its elements shuffled
|
||||
func shuffle() -> [Generator.Element] {
|
||||
var list = Array(self)
|
||||
list.shuffleInPlace()
|
||||
return list
|
||||
}
|
||||
}
|
||||
|
||||
extension MutableCollectionType where Index == Int {
|
||||
/// Shuffle the elements of `self` in-place.
|
||||
mutating func shuffleInPlace() {
|
||||
// empty and single-element collections don't shuffle
|
||||
if count < 2 { return }
|
||||
|
||||
for i in 0..<count - 1 {
|
||||
let j = Int(arc4random_uniform(UInt32(count - i))) + i
|
||||
guard i != j else { continue }
|
||||
swap(&self[i], &self[j])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// now the model structs
|
||||
enum CardColor : Int {
|
||||
case Red
|
||||
case Black
|
||||
}
|
||||
extension CardColor : CustomStringConvertible {
|
||||
var description : String {
|
||||
switch self {
|
||||
case .Red:
|
||||
return "Red"
|
||||
case .Black:
|
||||
return "Black"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum Suit : Int {
|
||||
case Hearts = 1
|
||||
case Diamonds
|
||||
case Spades
|
||||
case Clubs
|
||||
|
||||
var color : CardColor {
|
||||
switch self {
|
||||
case .Hearts, .Diamonds:
|
||||
return .Red
|
||||
case .Spades, .Clubs:
|
||||
return .Black
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum Pip : Int {
|
||||
case Ace = 1
|
||||
case Two = 2
|
||||
case Three = 3
|
||||
case Four = 4
|
||||
case Five = 5
|
||||
case Six = 6
|
||||
case Seven = 7
|
||||
case Eight = 8
|
||||
case Nine = 9
|
||||
case Ten = 10
|
||||
case Jack = 11
|
||||
case Queen = 12
|
||||
case King = 13
|
||||
}
|
||||
|
||||
struct Card {
|
||||
let pip : Pip
|
||||
let suit : Suit
|
||||
|
||||
var isFaceCard : Bool {
|
||||
return pip.rawValue > 10
|
||||
}
|
||||
|
||||
var color : CardColor {
|
||||
return suit.color
|
||||
}
|
||||
}
|
||||
extension Card : Equatable {}
|
||||
func == (l:Card, r:Card) -> Bool {
|
||||
return l.pip == r.pip &&
|
||||
l.suit == r.suit
|
||||
}
|
||||
extension Card : CustomStringConvertible {
|
||||
var description : String {
|
||||
return "\(pip) of \(suit)"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct Deck {
|
||||
var cards : [Card]
|
||||
|
||||
var count : Int {
|
||||
return cards.count
|
||||
}
|
||||
|
||||
init(shuffling:Bool=true) {
|
||||
var startcards = [Card]()
|
||||
for suit in (Suit.Hearts.rawValue...Suit.Clubs.rawValue) {
|
||||
for pip in (Pip.Ace.rawValue...Pip.King.rawValue) {
|
||||
startcards.append(Card(pip: Pip(rawValue: pip)!, suit: Suit(rawValue: suit)!))
|
||||
}
|
||||
}
|
||||
cards = startcards
|
||||
|
||||
if shuffling {
|
||||
shuffle()
|
||||
}
|
||||
}
|
||||
|
||||
mutating func shuffle() {
|
||||
cards.shuffleInPlace()
|
||||
}
|
||||
|
||||
mutating func deal() -> Card {
|
||||
let out = cards.removeFirst()
|
||||
return out
|
||||
}
|
||||
|
||||
}
|
||||
extension Deck : CustomStringConvertible {
|
||||
var description : String {
|
||||
return "\(count) cards: \(cards.description)"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// test some cards
|
||||
let kh = Card(pip: .King, suit: .Hearts)
|
||||
let ad = Card(pip: .Ace, suit: .Diamonds)
|
||||
let tc = Card(pip: .Two, suit: .Clubs)
|
||||
let fc = Card(pip: Pip(rawValue:4)!, suit: .Spades)
|
||||
|
||||
|
||||
// create an unshuffled deck
|
||||
var efg = Deck(shuffling: false)
|
||||
|
||||
|
||||
// create a shuffled deck and print its contents
|
||||
var d = Deck()
|
||||
print(d)
|
||||
|
||||
// deal three cards
|
||||
d.deal()
|
||||
d.deal()
|
||||
d.deal()
|
||||
d
|
||||
|
||||
// deal a couple more cards and check their color
|
||||
let c = d.deal()
|
||||
c.color
|
||||
|
||||
let cc = d.deal()
|
||||
cc.color
|
||||
|
||||
// deal out the rest of the deck, leaving just one card
|
||||
while d.count > 1 {
|
||||
d.deal()
|
||||
}
|
||||
d
|
||||
|
||||
// test equality of a couple cards
|
||||
if kh == Card(pip: Pip.King, suit: Suit.Clubs) {
|
||||
let a = true
|
||||
}
|
||||
else {
|
||||
let a = false
|
||||
}
|
||||
|
||||
kh != Card(pip: Pip.King, suit: Suit.Clubs)
|
||||
kh.isFaceCard
|
||||
fc.isFaceCard
|
||||
Loading…
Add table
Add a link
Reference in a new issue