Skip to content

Instantly share code, notes, and snippets.

@hjpotter92
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hjpotter92/a8c13c971cc3292c69b8 to your computer and use it in GitHub Desktop.
Save hjpotter92/a8c13c971cc3292c69b8 to your computer and use it in GitHub Desktop.
math.randomseed( os.time() )
local Random, Read, Write = math.random, io.read, io.write
Write "Number of players -> "
local tPlayers, iNumPlayers = {}, Read '*n'
Write "How many rounds? -> "
local iRounds = Read '*n'
io.input():flush()
for i = 1, iNumPlayers do
Write( "Player name for #", i, ": " )
local sPlayerName = Read '*l'
tPlayers[sPlayerName] = { iTotal = 0 }
tPlayers[i] = sPlayerName
end
for i = 1, iRounds do
Write( "\n\n\t\tROUND ", i, "\n\n" )
for j = 1, iNumPlayers do
local sPlayerName = tPlayers[j]
local tPlayer = tPlayers[sPlayerName]
Write( "It is ", sPlayerName, "'s chance... Please start:\n" )
local iNumber, iCount = Random( 1, 100 ), 0
repeat
Write "Your guess > "
local iGuess = Read '*n'
iCount = iCount + 1
if iGuess > iNumber then
print( "Think a smaller number..." )
elseif iGuess < iNumber then
print( "Try a larger number...." )
end
until iGuess == iNumber
print( ("Congratulations %s; you won in %02d chances!!!\n\n\n"):format(sPlayerName, iCount) )
table.insert( tPlayer, iCount )
tPlayer.iTotal = tPlayer.iTotal + iCount
end
end
Write "\n\nFinal scores are as follows:\n\n\n\tName\t\tScore\t\tRound breakup\n\t-----\t\t------\t\t--------------\n\t"
for i = 1, iNumPlayers do
local sName = tPlayers[i]
local tPlayer = tPlayers[sName]
Write( sName, '\t\t', tPlayer.iTotal, '\t\t', table.concat(tPlayer, ' '), '\n\t' )
end
local Write, Floor, Ceil = io.write, math.floor, math.ceil
local function Nearest( iInput )
local iRoot = math.sqrt( iInput )
local f, c = Floor( iRoot ), Ceil( iRoot )
local d1, d2 = iRoot - f, c - iRoot
return d1 > d2 and c or f
end
local function Read()
return io.read '*n'
end
Write "Think of a number and give an upper limit of your choice (Max. 127) > "
local iLimit, iResult = Read(), 0
if iLimit > 127 then iLimit = 127 end
local iNear = Nearest( iLimit / 2 )
for i = 0, 8 do
local iCounter, j = 0, 2 ^ i
if j > iLimit then break end
Write '\n'
for k = j, iLimit, 2 * j do
for m = 0, j - 1 do
if k + m <= iLimit then
Write( ("%-3d "):format(k + m) )
iCounter = iCounter + 1
if iCounter % iNear == 0 then Write '\r\n' end
end
end
end
Write "\n\nIs the number in above table? Press 1 if true; any key otherwise > "
local iChoice = Read()
if iChoice and iChoice == 1 then
iResult = iResult + j
end
end
print( "\n\nThe number you thought was ", iResult )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment