From ccfdc429a50e75bf8b19ac4b111aa4f2911bf3f6 Mon Sep 17 00:00:00 2001 From: BrianWilkinsFL Date: Tue, 7 Jun 2022 18:47:35 -0400 Subject: [PATCH] Initial implementation of Rock, Paper, Scissors in Lua --- 74_Rock_Scissors_Paper/lua/rockscissors.lua | 91 +++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 74_Rock_Scissors_Paper/lua/rockscissors.lua diff --git a/74_Rock_Scissors_Paper/lua/rockscissors.lua b/74_Rock_Scissors_Paper/lua/rockscissors.lua new file mode 100644 index 00000000..d7ae7242 --- /dev/null +++ b/74_Rock_Scissors_Paper/lua/rockscissors.lua @@ -0,0 +1,91 @@ +function getInput(prompt) + io.write(prompt) + io.flush() + local input = io.read("l") + if not input then --- test for EOF + print("GOODBYE") + os.exit(0) + end + return input +end + +function has_key(table, key) + return table[key]~=nil +end + +function choice(prompt, table) + resp = getInput(prompt) + while not has_key(table, resp) do + print("INVALID.") + resp = getInput(prompt) + end + return resp +end + +function playGame(n) + local computerWins = 0 + local humanWins = 0 + itemChoices = { + ["1"] = "PAPER", + ["2"] = "SCISSORS", + ["3"] = "ROCK", + } + print("GAME NUMBER " .. n) + + math.randomseed(os.time()) + computerChoice = math.random(1,3) + + print(computerChoice) + humanChoice = choice("3=ROCK...2=SCISSORS...1=PAPER\n1...2...3...WHAT'S YOUR CHOICE? ", itemChoices) + humanChoice = tonumber(humanChoice) + print("THIS IS MY CHOICE...") + + print("... " .. itemChoices[tostring(computerChoice)]) + + if computerChoice == humanChoice then + print("TIE GAME. NO WINNER.") + elseif computerChoice > humanChoice then + if humanChoice ~=1 or computerChoice ~= 3 then + computerWins = 1 + print("WOW! I WIN!!!") + else + humanWins = 1 + print("YOU WIN!!!") + end + elseif computerChoice == 1 then + if humanChoice ~= 3 then + humanWins = 1 + print("YOU WIN!!!") + else + computerWins = 1 + print("WOW! I WIN!!!") + end + end + return computerWins, humanWins +end + +local games = 0 +while games == 0 or games >= 11 do + resp = getInput("HOW MANY GAMES? ") + + assert(tonumber(resp)) + games = tonumber(resp) + if games < 11 then break end + + print("SORRY, BUT WE AREN'T ALLOWED TO PLAY THAT MANY.") +end + +totalComputerWins = 0 +totalHumanWins = 0 + +for n=1, games, 1 do + computerWins, humanWins = playGame(n) + totalComputerWins = totalComputerWins + computerWins + totalHumanWins = totalHumanWins + humanWins +end + +print("HERE IS THE FINAL GAME SCORE:") +print("I HAVE WON " .. totalComputerWins .. " GAME(S).") +print("YOU HAVE WON " .. totalHumanWins .." GAME(S).") +print("AND " .. games-(totalComputerWins+totalHumanWins) .. " GAME(S) ENDED IN A TIE.") +print("THANKS FOR PLAYING!!") \ No newline at end of file