Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@akrabat
Created September 13, 2017 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akrabat/5a646412638b1d11bab39bf5851931e7 to your computer and use it in GitHub Desktop.
Save akrabat/5a646412638b1d11bab39bf5851931e7 to your computer and use it in GitHub Desktop.
OpenWhisk Rock-Paper-Scissors game
// Rock Paper Scissors game
// To install:
// $ wsk action update rock-paper-scissors rock-paper-scissors.swift
//
// To play:
// $ wsk action invoke rock-paper-scissors -r -p shape rock
import Foundation
func main(args: [String:Any]) -> [String:Any] {
let shapes = ["rock", "paper", "scissors"]
// retrieve your shape
guard let yourShape = args["shape"] as? String,
let yourShapeIndex = shapes.index(of: yourShape) else {
return ["error": "Please provide a shape ('rock', 'paper' or 'scissors'"]
}
// determine my shape
srandom(UInt32(NSDate().timeIntervalSince1970))
let myShapeIndex = random() % shapes.count
let myShape = shapes[myShapeIndex]
// who won?
if yourShapeIndex == myShapeIndex {
return [ "winner" : "tie", "your_shape": yourShape, "my_shape": myShape ]
} else if ((yourShapeIndex + 1) % 3) == myShapeIndex {
return [ "winner" : "me", "your_shape": yourShape, "my_shape": myShape ]
}
return [ "winner" : "you", "your_shape": yourShape, "my_shape": myShape ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment