diff --git a/29_Craps/lua/craps.lua b/29_Craps/lua/craps.lua index cdb9c6e3..063adad8 100644 --- a/29_Craps/lua/craps.lua +++ b/29_Craps/lua/craps.lua @@ -40,10 +40,22 @@ local function input_number(prompt) 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. local function play_round() - -- Input the wager - local wager = input_number("Input the amount of your wager: ") + -- Input the wager + local wager = input_number("Input the amount of your wager: ") -- Roll the die for the first time. print("I will now throw the dice") @@ -69,13 +81,11 @@ local function play_round() return -wager end - -- Any other number rolled becomes your "point." You continue to roll; - -- if you get your point you win. If you roll a 7, you lose and the dice - -- change hands when this happens. + -- Any other number rolled becomes the "point". + -- Continue to roll until rolling a 7 or point. print(string.format("%d is the point. I will roll again", first_roll)) - local second_roll - repeat - second_roll = throw_dice() + while true do + local second_roll = throw_dice() if second_roll == first_roll then -- Player gets point and wins print(string.format("%d - a winner.........congrats!!!!!!!!", first_roll)) @@ -83,14 +93,14 @@ local function play_round() return 2 * wager end 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("You lose $ %d", wager)) return -wager end -- Continue to roll print(string.format("%d - no point. I will roll again", second_roll)) - until second_roll == first_roll or second_roll == 7 + end end @@ -98,17 +108,17 @@ end local function craps_main() -- Print the introduction to the game 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.") - -- Initialize random number generator seeed + -- Initialize random number generator seed math.randomseed(os.time()) - -- Initialize balance to track winnings and losings + -- Initialize balance to track winnings and losings local balance = 0 -- Main game loop - local keep_playing = true + local keep_playing = true while keep_playing do -- Play a round balance = balance + play_round() @@ -116,24 +126,22 @@ local function craps_main() -- 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) - -- Print an update on money won - if balance < 0 then - print(string.format("You are now under $%d", -balance)) - elseif balance > 0 then - print(string.format("You are now ahead $%d", balance)) - else - print("You are now even at 0") - end + -- Print an update on winnings or losings + print_balance( + balance, + string.format("You are now under $%d", -balance), + string.format("You are now ahead $%d", balance), + "You are now even at 0" + ) end -- Game over, print the goodbye message - if balance < 0 then - print("Too bad, you are in the hole. Come again.") - elseif balance > 0 then - print("Congratulations---you came out a winner. Come again.") - else - print("Congratulations---you came out even, not bad for an amateur") - end + print_balance( + balance, + "Too bad, you are in the hole. Come again.", + "Congratulations---you came out a winner. Come again.", + "Congratulations---you came out even, not bad for an amateur" + ) end