added print_balance function, linting

This commit is contained in:
aconconi
2022-10-07 18:59:43 +02:00
parent 45df425348
commit 4dcde245f6

View File

@@ -40,10 +40,22 @@ local function input_number(prompt)
end end
--- Print custom balance message depending on balance value
local function print_balance(balance, under_msg, ahead_msg, even_msg)
if balance < 0 then
print(under_msg)
elseif balance > 0 then
print(ahead_msg)
else
print(even_msg)
end
end
--- Play a round and return winnings or losings. --- Play a round and return winnings or losings.
local function play_round() local function play_round()
-- Input the wager -- Input the wager
local wager = input_number("Input the amount of your wager: ") local wager = input_number("Input the amount of your wager: ")
-- Roll the die for the first time. -- Roll the die for the first time.
print("I will now throw the dice") print("I will now throw the dice")
@@ -69,13 +81,11 @@ local function play_round()
return -wager return -wager
end end
-- Any other number rolled becomes your "point." You continue to roll; -- Any other number rolled becomes the "point".
-- if you get your point you win. If you roll a 7, you lose and the dice -- Continue to roll until rolling a 7 or point.
-- change hands when this happens.
print(string.format("%d is the point. I will roll again", first_roll)) print(string.format("%d is the point. I will roll again", first_roll))
local second_roll while true do
repeat local second_roll = throw_dice()
second_roll = throw_dice()
if second_roll == first_roll then if second_roll == first_roll then
-- Player gets point and wins -- Player gets point and wins
print(string.format("%d - a winner.........congrats!!!!!!!!", first_roll)) print(string.format("%d - a winner.........congrats!!!!!!!!", first_roll))
@@ -83,14 +93,14 @@ local function play_round()
return 2 * wager return 2 * wager
end end
if second_roll == 7 then if second_roll == 7 then
-- Player gets 7 and loses -- Player rolls a 7 and loses
print(string.format("%d - craps. You lose.", second_roll)) print(string.format("%d - craps. You lose.", second_roll))
print(string.format("You lose $ %d", wager)) print(string.format("You lose $ %d", wager))
return -wager return -wager
end end
-- Continue to roll -- Continue to roll
print(string.format("%d - no point. I will roll again", second_roll)) print(string.format("%d - no point. I will roll again", second_roll))
until second_roll == first_roll or second_roll == 7 end
end end
@@ -98,17 +108,17 @@ end
local function craps_main() local function craps_main()
-- Print the introduction to the game -- Print the introduction to the game
print(string.rep(" ", 32) .. "Craps") print(string.rep(" ", 32) .. "Craps")
print(string.rep(" ", 14) .. "Creative Computing Morristown, New Jersey\n\n") print(string.rep(" ", 14) .. "Creative Computing Morristown, New Jersey\n\n")
print("2,3,12 are losers; 4,5,6,8,9,10 are points; 7,11 are natural winners.") print("2,3,12 are losers; 4,5,6,8,9,10 are points; 7,11 are natural winners.")
-- Initialize random number generator seeed -- Initialize random number generator seed
math.randomseed(os.time()) math.randomseed(os.time())
-- Initialize balance to track winnings and losings -- Initialize balance to track winnings and losings
local balance = 0 local balance = 0
-- Main game loop -- Main game loop
local keep_playing = true local keep_playing = true
while keep_playing do while keep_playing do
-- Play a round -- Play a round
balance = balance + play_round() balance = balance + play_round()
@@ -116,24 +126,22 @@ local function craps_main()
-- If player's answer is 5, then stop playing -- If player's answer is 5, then stop playing
keep_playing = (input_number("If you want to play again print 5 if not print 2: ") == 5) keep_playing = (input_number("If you want to play again print 5 if not print 2: ") == 5)
-- Print an update on money won -- Print an update on winnings or losings
if balance < 0 then print_balance(
print(string.format("You are now under $%d", -balance)) balance,
elseif balance > 0 then string.format("You are now under $%d", -balance),
print(string.format("You are now ahead $%d", balance)) string.format("You are now ahead $%d", balance),
else "You are now even at 0"
print("You are now even at 0") )
end
end end
-- Game over, print the goodbye message -- Game over, print the goodbye message
if balance < 0 then print_balance(
print("Too bad, you are in the hole. Come again.") balance,
elseif balance > 0 then "Too bad, you are in the hole. Come again.",
print("Congratulations---you came out a winner. Come again.") "Congratulations---you came out a winner. Come again.",
else "Congratulations---you came out even, not bad for an amateur"
print("Congratulations---you came out even, not bad for an amateur") )
end
end end