Merge pull request #878 from unquietwiki/main

Two more Nim ports + minor cleanup
This commit is contained in:
Jeff Atwood
2023-08-05 07:49:43 -07:00
committed by GitHub
5 changed files with 199 additions and 90 deletions

View File

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

View File

@@ -10,6 +10,8 @@ var
prompt: string prompt: string
stillplaying: bool = true stillplaying: bool = true
randomize() # Seed the random number generator
# Seed 3 unique random numbers; indicate if they're all unique # Seed 3 unique random numbers; indicate if they're all unique
proc genSeed(): bool = proc genSeed(): bool =
for i in 1..3: for i in 1..3:
@@ -24,19 +26,19 @@ proc playGame() =
# We want 3 unique random numbers: loop until we get them! # We want 3 unique random numbers: loop until we get them!
while unique == false: while unique == false:
unique = genSeed() unique = genSeed()
echo("O.K. I HAVE A NUMBER IN MIND.") echo "O.K. I HAVE A NUMBER IN MIND."
for i in 1..20: for i in 1..20:
var c, d: int = 0 var c, d: int = 0
echo("GUESS #", i) echo "GUESS #", i
prompt = readLine(stdin).normalize() prompt = readLine(stdin).normalize()
if (prompt.len() != 3): if (prompt.len() != 3):
echo("TRY GUESSING A THREE-DIGIT NUMBER.") echo "TRY GUESSING A THREE-DIGIT NUMBER."
continue continue
for z in 1..3: for z in 1..3:
b[z] = prompt.substr(z-1, z-1).parseInt() # Convert string digits to array ints b[z] = prompt.substr(z-1, z-1).parseInt() # Convert string digits to array ints
if (b[1] == b[2]) or (b[2] == b[3]) or (b[3] == b[1]): if (b[1] == b[2]) or (b[2] == b[3]) or (b[3] == b[1]):
echo("OH, I FORGOT TO TELL YOU THAT THE NUMBER I HAVE IN MIND") echo "OH, I FORGOT TO TELL YOU THAT THE NUMBER I HAVE IN MIND"
echo("HAS NO TWO DIGITS THE SAME.") echo "HAS NO TWO DIGITS THE SAME."
# Figure out the PICOs # Figure out the PICOs
if (a[1] == b[2]): c += 1 if (a[1] == b[2]): c += 1
if (a[1] == b[3]): c += 1 if (a[1] == b[3]): c += 1
@@ -51,45 +53,45 @@ proc playGame() =
if (d != 3): if (d != 3):
if (c != 0): if (c != 0):
for j in 1..c: for j in 1..c:
echo("PICO") echo "PICO"
if (d != 0): if (d != 0):
for j in 1..d: for j in 1..d:
echo("FERMI") echo "FERMI"
if (c == 0) and (d == 0): if (c == 0) and (d == 0):
echo("BAGELS") echo "BAGELS"
# If we have 3 FERMIs, we win! # If we have 3 FERMIs, we win!
else: else:
echo("YOU GOT IT!!!") echo "YOU GOT IT!!!"
echo("") echo ""
wincount += 1 wincount += 1
youwin = true youwin = true
break break
# Only invoke if we've tried 20 guesses without winning # Only invoke if we've tried 20 guesses without winning
if (youwin == false): if not youwin:
echo("OH WELL.") echo "OH WELL."
echo("THAT'S TWENTY GUESSES. MY NUMBER WAS ", a[1], a[2], a[3]) echo "THAT'S TWENTY GUESSES. MY NUMBER WAS ", a[1], a[2], a[3]
# main program # main program
echo(spaces(33), "BAGELS") echo spaces(33), "BAGELS"
echo(spaces(15), "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") echo spaces(15), "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"
echo("\n\n") echo "\n\n"
echo("WOULD YOU LIKE THE RULES (YES OR NO)") echo "WOULD YOU LIKE THE RULES (YES OR NO)"
prompt = readLine(stdin).normalize() prompt = readLine(stdin).normalize()
if (prompt.substr(0, 0) == "y"): if prompt.substr(0, 0) == "y":
echo("I AM THINKING OF A THREE-DIGIT NUMBER. TRY TO GUESS") echo "I AM THINKING OF A THREE-DIGIT NUMBER. TRY TO GUESS"
echo("MY NUMBER AND I WILL GIVE YOU CLUES AS FOLLOWS:") echo "MY NUMBER AND I WILL GIVE YOU CLUES AS FOLLOWS:"
echo(" PICO - ONE DIGIT CORRECT BUT IN THE WRONG POSITION") echo " PICO - ONE DIGIT CORRECT BUT IN THE WRONG POSITION"
echo(" FERMI - ONE DIGIT CORRECT AND IN THE RIGHT POSITION") echo " FERMI - ONE DIGIT CORRECT AND IN THE RIGHT POSITION"
echo(" BAGELS - NO DIGITS CORRECT") echo " BAGELS - NO DIGITS CORRECT"
echo("") echo ""
while(stillplaying == true): while(stillplaying == true):
playGame() playGame()
echo("PLAY AGAIN (YES OR NO)") echo "PLAY AGAIN (YES OR NO)"
prompt = readLine(stdin).normalize() prompt = readLine(stdin).normalize()
if (prompt.substr(0, 0) != "y"): if prompt.substr(0, 0) != "y":
stillplaying = false stillplaying = false
if wincount > 0: if wincount > 0:
echo("") echo ""
echo("A ", wincount, " POINT BAGELS BUFF!!") echo "A ", wincount, " POINT BAGELS BUFF!!"
echo("") echo ""
echo("HOPE YOU HAD FUN. BYE.") echo "HOPE YOU HAD FUN. BYE."

View File

@@ -0,0 +1,68 @@
import std/[random,strutils]
var
wager, winnings, rollResult: int
stillplaying: bool = true
randomize() # Seed the random number generator
proc tryAgain(): bool =
echo "WANT TO PLAY AGAIN? (YES OR NO)"
var answer = readLine(stdin).normalize()
result = (answer == "y") or (answer == "yes")
proc takePoint(point: int) =
var flag = true
while flag:
var pointRoll: int = (rand 1..6) + (rand 1..6) # roll dice, then add the sum
if pointRoll == 7:
echo pointRoll, "- CRAPS. YOU LOSE."
echo "YOU LOSE ", wager, " DOLLARS."
winnings -= wager
flag = false
if pointRoll == point:
echo point, "- A WINNER.........CONGRATS!!!!!!!!"
echo "AT 2 TO 1 ODDS PAYS YOU...LET ME SEE... ", 2*wager, " DOLLARS"
winnings += (2*wager)
flag = false
if flag:
echo pointRoll, " - NO POINT. I WILL ROLL AGAIN"
echo spaces(33), "CRAPS"
echo spaces(15), "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"
echo "\n"
echo "2,3,12 ARE LOSERS; 4,5,6,8,9,10 ARE POINTS; 7,11 ARE NATURAL WINNERS."
winnings = 0
# play the game
while stillplaying:
echo ""
echo "INPUT THE AMOUNT OF YOUR WAGER."
wager = readline(stdin).parseInt()
echo "I WILL NOW THROW THE DICE"
rollResult = (rand 1..6) + (rand 1..6) # roll dice, then add the sum
case rollResult:
of 7, 11:
echo rollResult, "- NATURAL....A WINNER!!!!"
echo rollResult, " PAYS EVEN MONEY, YOU WIN ", wager, " DOLLARS"
winnings += wager
of 2:
echo rollResult, "- SNAKE EYES....YOU LOSE."
echo "YOU LOSE ", wager, " DOLLARS."
winnings -= wager
of 3, 12:
echo rollResult, "- CRAPS...YOU LOSE."
echo "YOU LOSE ", wager, " DOLLARS."
winnings -= wager
else:
echo rollResult, " IS THE POINT. I WILL ROLL AGAIN"
takePoint(rollResult)
if winnings < 0: echo "YOU ARE NOW UNDER $", winnings
if winnings > 0: echo "YOU ARE NOW AHEAD $", winnings
if winnings == 0: echo "YOU ARE NOW EVEN AT 0"
stillplaying = tryAgain()
# done playing
if winnings < 0: echo "TOO BAD, YOU ARE IN THE HOLE. COME AGAIN."
if winnings > 0: echo "CONGRATULATIONS---YOU CAME OUT A WINNER. COME AGAIN!"
if winnings == 0: echo "CONGRATULATIONS---YOU CAME OUT EVEN, NOT BAD FOR AN AMATEUR"

View File

@@ -6,17 +6,19 @@ var
z: string z: string
retry: bool = true retry: bool = true
echo(spaces(34), "DICE") randomize() # Seed the random number generator
echo(spaces(15), "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY")
echo("\n") echo spaces(34), "DICE"
echo("THIS PROGRAM SIMULATES THE ROLLING OF A PAIR OF DICE.") echo spaces(15), "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"
echo("YOU ENTER THE NUMBER OF TIMES YOU WANT THE COMPUTER TO") echo "\n"
echo("'ROLL' THE DICE. WATCH OUT, VERY LARGE NUMBERS TAKE") echo "THIS PROGRAM SIMULATES THE ROLLING OF A PAIR OF DICE."
echo("A LONG TIME. IN PARTICULAR, NUMBERS OVER 5000.") 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): while(retry):
echo("\n") echo "\n"
echo("HOW MANY ROLLS") echo "HOW MANY ROLLS"
x = readLine(stdin).parseInt() x = readLine(stdin).parseInt()
for v in 2..12: for v in 2..12:
f[v] = 0 # Initialize array to 0 f[v] = 0 # Initialize array to 0
@@ -25,14 +27,11 @@ while(retry):
b = rand(1..6) # Die 2 b = rand(1..6) # Die 2
r = a + b # Sum of dice r = a + b # Sum of dice
f[r] += 1 # Increment array count of dice sum result f[r] += 1 # Increment array count of dice sum result
echo() echo ""
echo("TOTAL SPOTS: ", "NUMBER OF TIMES") echo "TOTAL SPOTS: ", "NUMBER OF TIMES"
for v in 2..12: for v in 2..12:
echo(v, ": ", f[v]) # Print out counts for each possible result echo v, ": ", f[v] # Print out counts for each possible result
echo("\n") echo "\n"
echo("TRY AGAIN?") echo "TRY AGAIN?"
z = readLine(stdin).normalize() z = readLine(stdin).normalize()
if (z=="yes") or (z=="y"): retry = (z=="yes") or (z=="y")
retry = true
else:
retry = false

View File

@@ -0,0 +1,38 @@
import std/[random,strutils]
var
carSpeed, diff, err, guess, trainSpeed, carTime: int
stillplaying: bool = true
randomize() # Seed the random number generator
# Return a tuple that'll be carSpeed, diff, trainSpeed
proc randomNumbers(): (int,int,int) =
result = (rand(41..65), rand(6..20), rand(21..39))
# Do we want to play again?
proc tryAgain(): bool =
echo "ANOTHER PROBLEM (YES OR NO)"
var answer = readLine(stdin).normalize()
result = (answer == "y") or (answer == "yes")
echo spaces(33), "TRAIN"
echo spaces(15), "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"
echo "\n"
echo "TIME - SPEED DISTANCE EXERCISE"
while stillplaying:
echo ""
(carSpeed, diff, trainSpeed) = randomNumbers() # Get random numbers for prompt
echo "A CAR TRAVELING ", carSpeed, " MPH CAN MAKE A CERTAIN TRIP IN"
echo diff, " HOURS LESS THAN A TRAIN TRAVELING AT ", trainSpeed, " MPH."
echo "HOW LONG DOES THE TRIP TAKE BY CAR?"
guess = readLine(stdin).parseInt() # Get guess
carTime = (diff * trainSpeed / (carSpeed - trainSpeed)).toInt() # Calculate answer
err = (((carTime - guess) * 100) / guess).toInt().abs() # Calculate error to an absolute value
if err > 5: # Error within 5%?
echo "SORRY. YOU WERE OFF BY ", err, " PERCENT."
else:
echo "GOOD! ANSWER WITHIN ", err, " PERCENT."
echo "CORRECT ANSWER IS ", carTime, " HOURS."
stillplaying = tryAgain()