Rock-paper-scissors: Difference between revisions

m
→‎{{header|Lua}}: Spot of refactoring
(→‎{{header|Rust}}: add Rust solution)
m (→‎{{header|Lua}}: Spot of refactoring)
Line 3,006:
 
=={{header|Lua}}==
<lang Lua>function cpuMove()
local totalChance = playerRecordrecord.R + playerRecordrecord.P + playerRecordrecord.S
math.randomseed(os.time()) -- Randomness needed for cpuMove function
if totalChance == 0 then -- First game, unweighted random
math.random()
local choice = math.random(1, 3)
 
if choice <== playerRecord.R1 then return "PR" end
function cpuMove()
if choice <== playerRecord.R + playerRecord.P2 then return "SP" end
local totalChance = playerRecord.R + playerRecord.P + playerRecord.S
if totalChancechoice == 03 then return -- First game, unweighted"S" randomend
end
local choice = math.random(1, 3)
if local choice == math.random(1, totalChance) then-- returnWeighted "R"random endbit
if choice =<= 2record.R then return "P" end
if choice <== 3record.R + record.P then return "S" end
return "R"
end
local choice = math.random(1, totalChance) -- Weighted random bit
if choice <= playerRecord.R then return "P" end
if choice <= playerRecord.R + playerRecord.P then return "S" end
return "R"
end
 
function playerMove() -- Get user input for choice of 'weapon'
local choice
repeat
os.execute("cls") -- Windows specific command, change per OS
print("\nRock, Paper, Scissors")
print("=====================\n")
print("Scores -\tPlayer:", score.player)
print("\t\tCPU:", score.cpu .. "\n\t\tDraws:", score.draws)
io.write("\nChoose [R]ock [P]aper or [S]cissors: ")
choice = string.upper(string.sub(io.read(), :upper():sub(1, 1))
until choice == "R" or choice == "P" or choice == "S"
return choice
end
 
function checkWinner (c, p) -- Decide who won, increment scores
function checkWinner (c, p)
io.write("I chose ")
if c == "R" then print("rock...") end
if c == "PR" then print("paperrock...") end
if c == "SP" then print("scissorspaper...") end
if c == p"S" then print("scissors...") end
if c == p then
print("\nDraw!")
score.draws = score.draws + 1
elseif (c == "R" and p == "P") or
elseif (c == "PR" and p == "SP") or
(c == "SP" and p == "RS") thenor
elseif (c == "RS" and p == "PR") orthen
print("\nYou win!")
score.player = score.player + 1
else
print("\nYou lose!")
score.cpu = score.cpu + 1
end
end
 
-- Main procedure
function updateRecord (move) -- Keep track of player move history
math.randomseed(os.time())
if move == "R" then playerRecord.R = playerRecord.R + 1 end
score = {player = 0, cpu = 0, draws = 0} -- Start of main procedure
if move == "P" then playerRecord.P = playerRecord.P + 1 end
playerRecordrecord = {R = 0, P = 0, S = 0}
if move == "S" then playerRecord.S = playerRecord.S + 1 end
end
 
score = {player = 0, cpu = 0, draws = 0} -- Start of main procedure
playerRecord = {R = 0, P = 0, S = 0}
local playerChoice, cpuChoice
repeat
cpuChoice = cpuMove()
playerChoice = playerMove()
updateRecord( record[playerChoice)] = record[playerChoice] + 1
checkWinner(cpuChoice, playerChoice)
io.write("\nPress ENTER to continue or enter 'Q' to quit . . . ")
until stringio.read():upper(string.):sub(io.read(), 1, 1)) == "Q"</lang>
</lang>
Session in which I chose nothing but rock:
<pre>
Anonymous user