-
-
Save fxn/bf89ba74bd5defdaae639a48c8cb502d to your computer and use it in GitHub Desktop.
| import std/os | |
| type | |
| Shape = enum Rock, Paper, Scissors | |
| GameResult = range[-1..1] | |
| func toShape(shapeChr: char): Shape = | |
| case shapeChr: | |
| of 'A', 'X': Rock | |
| of 'B', 'Y': Paper | |
| of 'C', 'Z': Scissors | |
| else: raise | |
| func gameResult(a: Shape, b: Shape): GameResult = | |
| if a == b: | |
| return 0 | |
| case a: | |
| of Rock: | |
| if b == Scissors: -1 else: 1 | |
| of Paper: | |
| if b == Rock: -1 else: 1 | |
| of Scissors: | |
| if b == Paper: -1 else: 1 | |
| func shapeScore(shape: Shape): int = | |
| 1 + ord(shape) | |
| func myGameScore(a: Shape, b: Shape): int = | |
| [0, 3, 6][1 + gameResult(a, b)] | |
| func myScore(a: Shape, b: Shape): int = | |
| shapeScore(b) + myGameScore(a, b) | |
| var score = 0 | |
| for line in paramStr(1).lines: | |
| let a = line[0].toShape | |
| let b = line[2].toShape | |
| inc score, myScore(a, b) | |
| echo score |
| import std/os | |
| type | |
| Shape = enum Rock, Paper, Scissors | |
| GameResult = range[-1..1] | |
| func toShape(shapeChr: char): Shape = | |
| case shapeChr: | |
| of 'A', 'X': Rock | |
| of 'B', 'Y': Paper | |
| of 'C', 'Z': Scissors | |
| else: raise | |
| func gameResult(a: Shape, b: Shape): GameResult = | |
| if a == b: | |
| return 0 | |
| case a: | |
| of Rock: | |
| if b == Scissors: -1 else: 1 | |
| of Paper: | |
| if b == Rock: -1 else: 1 | |
| of Scissors: | |
| if b == Paper: -1 else: 1 | |
| func shapeScore(shape: Shape): int = | |
| 1 + ord(shape) | |
| func myGameScore(a: Shape, b: Shape): int = | |
| [0, 3, 6][1 + gameResult(a, b)] | |
| func myScore(a: Shape, b: Shape): int = | |
| shapeScore(b) + myGameScore(a, b) | |
| func myShapeFor(a: Shape, r: char): Shape = | |
| if r == 'Y': | |
| return a | |
| case a: | |
| of Rock: | |
| if r == 'X': Scissors else: Paper | |
| of Paper: | |
| if r == 'X': Rock else: Scissors | |
| of Scissors: | |
| if r == 'X': Paper else: Rock | |
| var score = 0 | |
| for line in lines(paramStr(1)): | |
| let a = line[0].toShape | |
| let b = myShapeFor(a, line[2]) | |
| inc score, myScore(a, b) | |
| echo score |
Ah! Also thought about that, but GameResult has to be asymmetric for my taste, because Win... who wins?
Also a small tip, in your case: return you can remove all return and just leave the values, so the whole case expression will give a value and it'll be returned as it's the last expression in the func. Like
func gameResult(a: Shape, b: Shape): GameResult =
if a == b:
return Draw
case a:
of Rock:
if b == Scissors: Loss else: Win
of Paper:
if b == Rock: Loss else: Win
of Scissors:
if b == Paper: Loss else: WinSame for other instances where that pattern is used.
@Yardanico Ah! Awesome! Was not aware you could do that in Nim (reading docs like crazy these days :). That is how you do it in Ruby, but I was into a Pythonic-mindset here I guess. Don't know yet what is an expression in Nim just yet too.
So that is way better for my taste, prefer the conciseness of the expression without explicit returns.
@Yardanico I've removed the unnecessary returns from the gists, thanks!
Well in that case you can drop the number and do
[Loss: 0, Draw: 3, Win: 6][gameResult(a, b)].