Skip to content

Instantly share code, notes, and snippets.

@mattt mattt/bridge.swift
Last active Mar 29, 2019

Embed
What would you like to do?
Example usage of @mxcl's swift-sh
#!/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)
}
@mattt

This comment has been minimized.

Copy link
Owner Author

commented Jan 14, 2019

Run with @mxcl's swift-sh:

$ brew install mxcl/made/swift-sh
$ swift sh ./bridge.swift
@mattt

This comment has been minimized.

Copy link
Owner Author

commented Jan 14, 2019

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
You can’t perform that action at this time.