From 45df4253487f9c3f735006d8fc6a96473307401f Mon Sep 17 00:00:00 2001 From: aconconi Date: Thu, 6 Oct 2022 16:10:27 +0200 Subject: [PATCH 1/4] Lua port and readme for 29_Craps added --- 29_Craps/lua/README.md | 16 ++++- 29_Craps/lua/craps.lua | 141 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 29_Craps/lua/craps.lua diff --git a/29_Craps/lua/README.md b/29_Craps/lua/README.md index c063f42f..091ddb4c 100644 --- a/29_Craps/lua/README.md +++ b/29_Craps/lua/README.md @@ -1,3 +1,17 @@ Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html) -Conversion to [Lua](https://www.lua.org/) +Conversion to [Lua](https://www.lua.org/) by Alex Conconi + +--- + +#### Lua porting notes + +- The `craps_main` function contains the main game loop, which iteratively +plays craps rounds by calling `play_round` and tracks winnings and losings. +- Replaced the original routine that tries to scramble the random number +generator with a proper seed initializer in Lua: `math.randomseed(os.time())` +(as advised in the general porting notes). += Added basic input validation to accept only positive integers for the +wager and the answer to the "If you want to play again print 5" question. +- "If you want to play again print 5 if not print 2" reads a bit odd but +we decided to leave it as is and stay true to the BASIC original version. \ No newline at end of file diff --git a/29_Craps/lua/craps.lua b/29_Craps/lua/craps.lua new file mode 100644 index 00000000..cdb9c6e3 --- /dev/null +++ b/29_Craps/lua/craps.lua @@ -0,0 +1,141 @@ +--[[ +Craps + +From: BASIC Computer Games (1978) +Edited by David H. Ahl + + This game simulates the games of craps played according to standard + Nevada craps table rules. That is: + 1. A 7 or 11 on the first roll wins + 2. A 2, 3, or 12 on the first roll loses + 3. 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. + + This version of craps was modified by Steve North of Creative Computing. + It is based on an original which appeared one day one a computer at DEC. + + +Lua port by Alex Conconi, 2022 +--]] + + +--- Throw two dice and return their sum. +local function throw_dice() + return math.random(1, 6) + math.random(1, 6) +end + + +--- Print prompt and read a number > 0 from stdin. +local function input_number(prompt) + while true do + io.write(prompt) + local number = tonumber(io.stdin:read("*l")) + if number and number > 0 then + return number + else + print("Please enter a number greater than zero.") + end + 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: ") + + -- Roll the die for the first time. + print("I will now throw the dice") + local first_roll = throw_dice() + + -- A 7 or 11 on the first roll wins. + if first_roll == 7 or first_roll == 11 then + print(string.format("%d - natural.... a winner!!!!", first_roll)) + print(string.format("%d pays even money, you win %d dollars", first_roll, wager)) + return wager + end + + -- A 2, 3, or 12 on the first roll loses. + if first_roll == 2 or first_roll == 3 or first_roll == 12 then + if first_roll == 2 then + -- Special 'you lose' message for 'snake eyes' + print(string.format("%d - snake eyes.... you lose.", first_roll)) + else + -- Default 'you lose' message + print(string.format("%d - craps.... you lose.", first_roll)) + end + print(string.format("You lose %d dollars", wager)) + 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. + print(string.format("%d is the point. I will roll again", first_roll)) + local second_roll + repeat + second_roll = throw_dice() + if second_roll == first_roll then + -- Player gets point and wins + print(string.format("%d - a winner.........congrats!!!!!!!!", first_roll)) + print(string.format("%d at 2 to 1 odds pays you...let me see... %d dollars", first_roll, 2 * wager)) + return 2 * wager + end + if second_roll == 7 then + -- Player gets 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 + + +--- Main game function. +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("2,3,12 are losers; 4,5,6,8,9,10 are points; 7,11 are natural winners.") + + -- Initialize random number generator seeed + math.randomseed(os.time()) + + -- Initialize balance to track winnings and losings + local balance = 0 + + -- Main game loop + local keep_playing = true + while keep_playing do + -- Play a round + balance = balance + play_round() + + -- 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 + 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 +end + + +--- Run the game. +craps_main() From 4dcde245f6b6391a2eed92b9f5dcfea8e8da049d Mon Sep 17 00:00:00 2001 From: aconconi Date: Fri, 7 Oct 2022 18:59:43 +0200 Subject: [PATCH 2/4] added print_balance function, linting --- 29_Craps/lua/craps.lua | 66 +++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 29 deletions(-) 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 From 3bf29b6f123a3d38047000ad1855be7ec5d91869 Mon Sep 17 00:00:00 2001 From: aconconi Date: Sat, 8 Oct 2022 13:08:17 +0200 Subject: [PATCH 3/4] minor edits --- 29_Craps/lua/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/29_Craps/lua/README.md b/29_Craps/lua/README.md index 091ddb4c..aac11dd0 100644 --- a/29_Craps/lua/README.md +++ b/29_Craps/lua/README.md @@ -4,14 +4,14 @@ Conversion to [Lua](https://www.lua.org/) by Alex Conconi --- -#### Lua porting notes +### Lua porting notes - The `craps_main` function contains the main game loop, which iteratively plays craps rounds by calling `play_round` and tracks winnings and losings. - Replaced the original routine that tries to scramble the random number generator with a proper seed initializer in Lua: `math.randomseed(os.time())` (as advised in the general porting notes). -= Added basic input validation to accept only positive integers for the +- Added basic input validation to accept only positive integers for the wager and the answer to the "If you want to play again print 5" question. - "If you want to play again print 5 if not print 2" reads a bit odd but we decided to leave it as is and stay true to the BASIC original version. \ No newline at end of file From 97b28ee5b44ca2d97c3fdca845e9a82998ea63b0 Mon Sep 17 00:00:00 2001 From: aconconi Date: Sat, 8 Oct 2022 20:52:11 +0200 Subject: [PATCH 4/4] converted indentation to spaces --- 29_Craps/lua/craps.lua | 178 ++++++++++++++++++++--------------------- 1 file changed, 89 insertions(+), 89 deletions(-) diff --git a/29_Craps/lua/craps.lua b/29_Craps/lua/craps.lua index 063adad8..ddd1ce30 100644 --- a/29_Craps/lua/craps.lua +++ b/29_Craps/lua/craps.lua @@ -22,126 +22,126 @@ Lua port by Alex Conconi, 2022 --- Throw two dice and return their sum. local function throw_dice() - return math.random(1, 6) + math.random(1, 6) + return math.random(1, 6) + math.random(1, 6) end --- Print prompt and read a number > 0 from stdin. local function input_number(prompt) - while true do - io.write(prompt) - local number = tonumber(io.stdin:read("*l")) - if number and number > 0 then - return number - else - print("Please enter a number greater than zero.") - end - end + while true do + io.write(prompt) + local number = tonumber(io.stdin:read("*l")) + if number and number > 0 then + return number + else + print("Please enter a number greater than zero.") + end + 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 + 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") - local first_roll = throw_dice() + -- Roll the die for the first time. + print("I will now throw the dice") + local first_roll = throw_dice() - -- A 7 or 11 on the first roll wins. - if first_roll == 7 or first_roll == 11 then - print(string.format("%d - natural.... a winner!!!!", first_roll)) - print(string.format("%d pays even money, you win %d dollars", first_roll, wager)) - return wager - end + -- A 7 or 11 on the first roll wins. + if first_roll == 7 or first_roll == 11 then + print(string.format("%d - natural.... a winner!!!!", first_roll)) + print(string.format("%d pays even money, you win %d dollars", first_roll, wager)) + return wager + end - -- A 2, 3, or 12 on the first roll loses. - if first_roll == 2 or first_roll == 3 or first_roll == 12 then - if first_roll == 2 then - -- Special 'you lose' message for 'snake eyes' - print(string.format("%d - snake eyes.... you lose.", first_roll)) - else - -- Default 'you lose' message - print(string.format("%d - craps.... you lose.", first_roll)) - end - print(string.format("You lose %d dollars", wager)) - return -wager - end + -- A 2, 3, or 12 on the first roll loses. + if first_roll == 2 or first_roll == 3 or first_roll == 12 then + if first_roll == 2 then + -- Special 'you lose' message for 'snake eyes' + print(string.format("%d - snake eyes.... you lose.", first_roll)) + else + -- Default 'you lose' message + print(string.format("%d - craps.... you lose.", first_roll)) + end + print(string.format("You lose %d dollars", wager)) + return -wager + end - -- Any other number rolled becomes the "point". + -- 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)) - 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)) - print(string.format("%d at 2 to 1 odds pays you...let me see... %d dollars", first_roll, 2 * wager)) - return 2 * wager - end - if second_roll == 7 then - -- 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)) - end + print(string.format("%d is the point. I will roll again", first_roll)) + 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)) + print(string.format("%d at 2 to 1 odds pays you...let me see... %d dollars", first_roll, 2 * wager)) + return 2 * wager + end + if second_roll == 7 then + -- 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)) + end end --- Main game function. 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("2,3,12 are losers; 4,5,6,8,9,10 are points; 7,11 are natural winners.") + -- Print the introduction to the game + print(string.rep(" ", 32) .. "Craps") + 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 seed - math.randomseed(os.time()) + -- Initialize random number generator seed + math.randomseed(os.time()) - -- Initialize balance to track winnings and losings - local balance = 0 + -- Initialize balance to track winnings and losings + local balance = 0 - -- Main game loop - local keep_playing = true - while keep_playing do - -- Play a round - balance = balance + play_round() + -- Main game loop + local keep_playing = true + while keep_playing do + -- Play a round + balance = balance + play_round() - -- 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) + -- 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 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 + -- 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 - 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" - ) + -- Game over, print the goodbye message + 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