Merge pull request #870 from unquietwiki/nimdev

Acey Ducey and Dice ported to Nim
This commit is contained in:
Jeff Atwood
2023-08-01 08:16:52 -07:00
committed by GitHub
2 changed files with 124 additions and 0 deletions

View File

@@ -0,0 +1,86 @@
import std/[random,strutils]
var
bet, cardA, cardB, cardC, stash: int
retry: bool = true
proc printGreeting() =
echo(spaces(26),"ACEY DUCEY CARD GAME")
echo(spaces(15),"CREATIVE COMPUTING MORRISTOWN, NEW JERSEY")
echo("\n")
echo("ACEY-DUCEY IS PLAYED IN THE FOLLOWING MANNER ")
echo("THE DEALER (COMPUTER) DEALS TWO CARDS FACE UP")
echo("YOU HAVE AN OPTION TO BET OR NOT BET DEPENDING")
echo("ON WHETHER OR NOT YOU FEEL THE CARD WILL HAVE")
echo("A VALUE BETWEEN THE FIRST TWO.")
echo("IF YOU DO NOT WANT TO BET, INPUT A 0")
echo("")
proc printBalance() =
echo("YOU NOW HAVE ", stash," DOLLARS.")
echo("")
proc printCard(aCard: int) =
case aCard:
of 11: echo("=== JACK ===")
of 12: echo("=== QUEEN ===")
of 13: echo("=== KING ===")
of 14: echo("=== ACE ===")
else: echo("=== ", aCard, " ===")
proc drawDealerCards() =
echo("HERE ARE YOUR NEXT TWO CARDS: ")
cardA = rand(2..14)
cardB = cardA # Copy cardA, so we can test cardB to be different
while cardB == cardA:
cardB = rand(2..14)
if cardA > cardB: # Make sure cardA is the smaller card
swap(cardA, cardB)
echo("")
printCard(cardA)
echo("")
printCard(cardB)
echo("")
proc drawPlayerCard() =
cardC = rand(2..14)
printCard(cardC)
echo("")
proc getBet(): int =
result = stash + 1 #ensure we enter the loop
while (result < 0) or (result > stash):
echo("WHAT IS YOUR BET: ")
result = readLine(stdin).parseInt()
if result > stash:
echo("SORRY, MY FRIEND, BUT YOU BET TOO MUCH.")
echo("YOU HAVE ONLY ", stash," DOLLARS TO BET.")
if bet == 0:
echo("CHICKEN!!")
proc tryAgain(): bool =
echo("TRY AGAIN (YES OR NO)")
var answer = readLine(stdin).normalize()
case answer:
of "yes", "y": result = true
else: result = false
printGreeting()
while retry:
stash = 100
while stash > 0:
printBalance()
drawDealerCards()
bet = getBet()
echo("")
drawPlayerCard()
if (cardC >= cardA) and (cardC <= cardB):
echo("YOU WIN!!!")
stash += bet
else:
echo("SORRY, YOU LOSE")
stash -= bet
echo("SORRY, FRIEND, BUT YOU BLEW YOUR WAD.");
echo("")
retry = tryAgain()
echo("O.K., HOPE YOU HAD FUN!");

View File

@@ -0,0 +1,38 @@
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 v in 2..12:
f[v] = 0 # Initialize array to 0
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