mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-25 12:25:10 -08:00
Nim version of Dice
This commit is contained in:
36
00_Alternate_Languages/33_Dice/nim/dice.nim
Normal file
36
00_Alternate_Languages/33_Dice/nim/dice.nim
Normal file
@@ -0,0 +1,36 @@
|
||||
import std/[random,strutils]
|
||||
|
||||
var
|
||||
a,b,r,x: int
|
||||
f: array[2..12, int]
|
||||
z: string
|
||||
retry: bool = true
|
||||
|
||||
echo(spaces(34), "DICE")
|
||||
echo(spaces(15), "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY")
|
||||
echo("\n")
|
||||
echo("THIS PROGRAM SIMULATES THE ROLLING OF A PAIR OF DICE.")
|
||||
echo("YOU ENTER THE NUMBER OF TIMES YOU WANT THE COMPUTER TO")
|
||||
echo("'ROLL' THE DICE. WATCH OUT, VERY LARGE NUMBERS TAKE")
|
||||
echo("A LONG TIME. IN PARTICULAR, NUMBERS OVER 5000.")
|
||||
|
||||
while(retry):
|
||||
echo("\n")
|
||||
echo("HOW MANY ROLLS")
|
||||
x = readLine(stdin).parseInt()
|
||||
for s in 1..x:
|
||||
a = rand(1..6) # Die 1
|
||||
b = rand(1..6) # Die 2
|
||||
r = a + b # Sum of dice
|
||||
f[r] += 1 # Increment array count of dice sum result
|
||||
echo()
|
||||
echo("TOTAL SPOTS: ", "NUMBER OF TIMES")
|
||||
for v in 2..12:
|
||||
echo(v, ": ", f[v]) # Print out counts for each possible result
|
||||
echo("\n")
|
||||
echo("TRY AGAIN?")
|
||||
z = readLine(stdin).normalize()
|
||||
if (z=="yes") or (z=="y"):
|
||||
retry = true
|
||||
else:
|
||||
retry = false
|
||||
Reference in New Issue
Block a user