Skip to content

Instantly share code, notes, and snippets.

@takaheraw
Created November 7, 2015 08:38
Show Gist options
  • Save takaheraw/0cfb5aaf4dcb1917b9ca to your computer and use it in GitHub Desktop.
Save takaheraw/0cfb5aaf4dcb1917b9ca to your computer and use it in GitHub Desktop.
class TreasureMap {
enum Treasures {
case GALLEON
case BURIED_GOLD
case SUNKEN_JEWELS
}
struct MapLocation {
let gridLetter: Character
let gridNumber: UInt
}
func findTreasure(type:Treasures) -> MapLocation {
switch type {
case .GALLEON:
return MapLocation(gridLetter: "D", gridNumber: 6)
case .BURIED_GOLD:
return MapLocation(gridLetter: "C", gridNumber: 2)
case .SUNKEN_JEWELS:
return MapLocation(gridLetter: "F", gridNumber: 12)
}
}
}
class PirateShip {
struct ShipLocation {
let NorthSouth: Int
let EastWest: Int
}
var currentPosition: ShipLocation
var movementQueue = dispatch_queue_create("shipQ", DISPATCH_QUEUE_SERIAL)
init() {
currentPosition = ShipLocation(NorthSouth: 5, EastWest: 5)
}
func moveToLocation(location:ShipLocation, callback:(ShipLocation) -> Void) {
dispatch_async(movementQueue, {() in
self.currentPosition = location
callback(self.currentPosition)
})
}
}
class PirateCrew {
let workQueue = dispatch_queue_create("crewWorkQ", DISPATCH_QUEUE_SERIAL)
enum Actions {
case ATTACK_SHIP
case DIG_FOR_GOLD
case DIVE_FOR_JEWELS
}
func performAction(action:Actions, callback:(Int) -> Void) {
dispatch_async(workQueue, {() in
var prizeValue = 0
switch (action) {
case .ATTACK_SHIP:
prizeValue = 10000
case .DIG_FOR_GOLD:
prizeValue = 5000
case .DIVE_FOR_JEWELS:
prizeValue = 1000
}
callback(prizeValue)
})
}
}
let map = TreasureMap()
let ship = PirateShip()
let crew = PirateCrew()
let treasureLocation = map.findTreasure(TreasureMap.Treasures.GALLEON)
let sequence:[Character] = ["A", "B", "C", "D", "E", "F", "G"]
let eastWestPos = sequence.indexOf(treasureLocation.gridLetter)
let shipTarget = PirateShip.ShipLocation(NorthSouth: Int(treasureLocation.gridNumber), EastWest: eastWestPos!)
ship.moveToLocation(shipTarget, callback: { location in
crew.performAction(PirateCrew.Actions.ATTACK_SHIP, callback: { prize in
print("Prize: \(prize) pieces of eight")
})
})
NSFileHandle.fileHandleWithStandardInput().availableData
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment