From aa0056624481b56e8b5dce40ad5a2f4ed0b0b8ed Mon Sep 17 00:00:00 2001 From: Michael Adams Date: Tue, 8 Aug 2023 22:09:02 -0700 Subject: [PATCH] Enhancement from Nim Discord --- .../nim/rockscissors.nim | 35 +++++++++---------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/00_Alternate_Languages/74_Rock_Scissors_Paper/nim/rockscissors.nim b/00_Alternate_Languages/74_Rock_Scissors_Paper/nim/rockscissors.nim index af2d1b1a..3c0cda0c 100644 --- a/00_Alternate_Languages/74_Rock_Scissors_Paper/nim/rockscissors.nim +++ b/00_Alternate_Languages/74_Rock_Scissors_Paper/nim/rockscissors.nim @@ -1,4 +1,4 @@ -import std/[random,strutils] +import std/[random,strformat,strutils] type symbol = enum @@ -20,24 +20,21 @@ proc outcome(p: symbol, c: symbol): string = if p == c: ties += 1 result = "TIE GAME. NO WINNER." - if p == SCISSORS and c == PAPER: - playerWins += 1 - result = "SCISSORS CUTS PAPER. YOU WIN." - if p == PAPER and c == SCISSORS: - cpuWins += 1 - result = "SCISSORS CUTS PAPER. I WIN." - if p == PAPER and c == ROCK: - playerWins += 1 - result = "PAPER COVERS ROCK. YOU WIN." - if p == ROCK and c == PAPER: - cpuWins += 1 - result = "PAPER COVERS ROCK. I WIN." - if p == ROCK and c == SCISSORS: - playerWins += 1 - result = "ROCK CRUSHES SCISSORS. YOU WIN." - if p == SCISSORS and c == ROCK: - cpuWins += 1 - result = "ROCK CRUSHES SCISSORS. I WIN." + else: + const + winTable = [ + PAPER: (ROCK, "COVERS"), + SCISSORS: (PAPER, "CUTS"), + ROCK: (SCISSORS, "CRUSHES") + ] + let (winCond, winVerb) = winTable[p] + if winCond == c: + playerWins += 1 + result = fmt"{p} {winVerb} {c}. YOU WIN." + else: + let (_, winVerb) = winTable[c] + cpuWins += 1 + result = fmt"{c} {winVerb} {p}. I WIN." # Start the game echo spaces(21), "GAME OF ROCK, SCISSORS, PAPER"