Enhancement from Nim Discord

This commit is contained in:
Michael Adams
2023-08-08 22:09:02 -07:00
parent dd7d3c4c26
commit aa00566244

View File

@@ -1,4 +1,4 @@
import std/[random,strutils] import std/[random,strformat,strutils]
type type
symbol = enum symbol = enum
@@ -20,24 +20,21 @@ proc outcome(p: symbol, c: symbol): string =
if p == c: if p == c:
ties += 1 ties += 1
result = "TIE GAME. NO WINNER." result = "TIE GAME. NO WINNER."
if p == SCISSORS and c == PAPER: else:
playerWins += 1 const
result = "SCISSORS CUTS PAPER. YOU WIN." winTable = [
if p == PAPER and c == SCISSORS: PAPER: (ROCK, "COVERS"),
cpuWins += 1 SCISSORS: (PAPER, "CUTS"),
result = "SCISSORS CUTS PAPER. I WIN." ROCK: (SCISSORS, "CRUSHES")
if p == PAPER and c == ROCK: ]
playerWins += 1 let (winCond, winVerb) = winTable[p]
result = "PAPER COVERS ROCK. YOU WIN." if winCond == c:
if p == ROCK and c == PAPER: playerWins += 1
cpuWins += 1 result = fmt"{p} {winVerb} {c}. YOU WIN."
result = "PAPER COVERS ROCK. I WIN." else:
if p == ROCK and c == SCISSORS: let (_, winVerb) = winTable[c]
playerWins += 1 cpuWins += 1
result = "ROCK CRUSHES SCISSORS. YOU WIN." result = fmt"{c} {winVerb} {p}. I WIN."
if p == SCISSORS and c == ROCK:
cpuWins += 1
result = "ROCK CRUSHES SCISSORS. I WIN."
# Start the game # Start the game
echo spaces(21), "GAME OF ROCK, SCISSORS, PAPER" echo spaces(21), "GAME OF ROCK, SCISSORS, PAPER"