Last active
June 14, 2023 11:47
-
-
Save mattt/b9d35a41c6599b7237a423715a2665c1 to your computer and use it in GitHub Desktop.
Example usage of @mxcl's swift-sh
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
#!/usr/bin/swift sh | |
import DeckOfPlayingCards // @NSHipster ~> 4.0.0 | |
import PlayingCard | |
import Cycle // @NSHipster == bb11e28 | |
class Player { | |
var name: String | |
var hand: [PlayingCard] = [] | |
init(name: String) { | |
self.name = name | |
} | |
} | |
extension Player: CustomStringConvertible { | |
var description: String { | |
var description = "\(name):" | |
let cardsBySuit = Dictionary(grouping: hand) { $0.suit } | |
for (suit, cards) in cardsBySuit.sorted(by: { $0.0 > $1.0 }) { | |
description += "\t\(suit) " | |
description += cards.sorted(by: >) | |
.map{ "\($0.rank)" } | |
.joined(separator: " ") | |
description += "\n" | |
} | |
return description | |
} | |
} | |
var north = Player(name: "North") | |
var west = Player(name: "West") | |
var east = Player(name: "East") | |
var south = Player(name: "South") | |
var deck = Deck.standard52CardDeck() | |
deck.shuffle() | |
let players = [north, west, east, south] | |
var round = players.cycled() | |
while let card = deck.deal(), | |
let player = round.next() | |
{ | |
player.hand.append(card) | |
} | |
for player in players { | |
print(player) | |
} |
Example output:
North: ♠︎ K 10 9 8 5 4
♡ K 3
♢ A 10
♣︎ A 4 2
West: ♠︎ J 3 2
♡ 9 6 5
♢ Q 9 8 3 2
♣︎ 6 5
East: ♠︎ Q 7
♡ A J 10 7 4
♢ K 5 4
♣︎ 10 7 3
South: ♠︎ A 6
♡ Q 8 2
♢ J 7 6
♣︎ K Q J 9 8
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run with @mxcl's swift-sh: