Skip to content

Instantly share code, notes, and snippets.

@fxn
Last active December 2, 2022 15:52
Show Gist options
  • Save fxn/bf89ba74bd5defdaae639a48c8cb502d to your computer and use it in GitHub Desktop.
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
Copy link

ghost commented Dec 2, 2022

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: Win

Same for other instances where that pattern is used.

@fxn
Copy link
Author

fxn commented Dec 2, 2022

@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.

@fxn
Copy link
Author

fxn commented Dec 2, 2022

@Yardanico I've removed the unnecessary returns from the gists, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment