Last active
August 29, 2015 14:27
-
-
Save zats/0ad1dac35e75587da7e0 to your computer and use it in GitHub Desktop.
Fizz Buzz with Gameplay Kit in Swift
This file contains hidden or 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
import Cocoa | |
import GameplayKit | |
let fizzRule = GKRule(blockPredicate: { system in | |
(system.state["value"] as! Int) % 3 == 0 | |
}, action: { system in | |
system.state["output"] = "fizz" | |
}) | |
fizzRule.salience = 1; | |
let buzzRule = GKRule(blockPredicate: { system in | |
return (system.state["value"] as! Int) % 5 == 0 | |
}, action: { system in | |
if let out = system.state["output"] as? String { | |
system.state["output"] = "\(out) buzz" | |
} else { | |
system.state["output"] = "buzz" | |
} | |
}) | |
buzzRule.salience = 2; | |
let printRule = GKRule(blockPredicate: { system in | |
let value = system.state["value"] as! Int | |
return (value % 5 != 0) && (value % 3 != 0) | |
}, action: { system in | |
let value = system.state["value"] as! Int | |
system.state["output"] = "\(value)" | |
}) | |
printRule.salience = 0 | |
let ruleSystem = GKRuleSystem() | |
ruleSystem.addRule(fizzRule) | |
ruleSystem.addRule(buzzRule) | |
ruleSystem.addRule(printRule) | |
for i in 1...100 { | |
ruleSystem.state["value"] = Int(i) | |
ruleSystem.state["output"] = nil | |
ruleSystem.evaluate() | |
print(ruleSystem.state["output"]!) | |
ruleSystem.reset() | |
} |
This file contains hidden or 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
1 | |
2 | |
fizz | |
4 | |
buzz | |
fizz | |
7 | |
8 | |
fizz | |
buzz | |
11 | |
fizz | |
13 | |
14 | |
fizz buzz | |
16 | |
17 | |
fizz | |
19 | |
buzz | |
fizz | |
22 | |
23 | |
fizz | |
buzz | |
26 | |
fizz | |
28 | |
29 | |
fizz buzz | |
31 | |
32 | |
fizz | |
34 | |
buzz | |
fizz | |
37 | |
38 | |
fizz | |
buzz | |
41 | |
fizz | |
43 | |
44 | |
fizz buzz | |
46 | |
47 | |
fizz | |
49 | |
buzz | |
fizz | |
52 | |
53 | |
fizz | |
buzz | |
56 | |
fizz | |
58 | |
59 | |
fizz buzz | |
61 | |
62 | |
fizz | |
64 | |
buzz | |
fizz | |
67 | |
68 | |
fizz | |
buzz | |
71 | |
fizz | |
73 | |
74 | |
fizz buzz | |
76 | |
77 | |
fizz | |
79 | |
buzz | |
fizz | |
82 | |
83 | |
fizz | |
buzz | |
86 | |
fizz | |
88 | |
89 | |
fizz buzz | |
91 | |
92 | |
fizz | |
94 | |
buzz | |
fizz | |
97 | |
98 | |
fizz | |
buzz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment