Posted 15 May 2013 - 12:30 PM
Hey there, i'm working on a blackjack game for a casino on the server i'm playing on. The code is working really great and i can play it, except for the part where it is supposed to check if the drawn card has already been drawn.
The way i have been trying to get it to work is the following:
math.random generates a number between 1 and 52. Then comes a function that is supposed to go through the table restrictedNumber and check if the random number is present there. If it is it should loop back and generate a new number. If it's not present then it should insert the random number in the restrictedNumber tables and break the loop.
For simplicity sake i wrote a new test code to mess around with the table but i can't for the life of me figure out how i get it to actually restrict numbers.
As far as i can tell this code is supposed to loop forever untill it generates a number that is not restricted, however it does not do that. IsRestrictedNumber is somehow set to false, even though a restricted number is generated and the loop breaks and a number that is restricted is printed.
function generateNumber() – generates a number between 1 and 16. Then it checks if the number is restricted. If not then it breaks the loop, if it is it should generate a new number.
while true do
randomNum = math.random(1, 16)
checkRestrictedNumber()
if isRestrictedNumber == false then
break
end
end
end
function checkRestrictedNumber() – should go through restrictedNumber table and check if the generated number is listed there. if it is not it should insert it to the table and break the loop.
for k, v in ipairs(restrictedNumber) do
if restrictedNumber[k] == randomNum then
isRestrictedNumber = true
else
isRestrictedNumber = false
table.insert(restrictedNumber, randomNum)
break
end
end
end
function wait()
event = os.pullEvent()
end
– Code
term.clear()
term.setCursorPos(1,1)
for i = 1, 10 do
table.insert(restrictedNumber, i)
end
print("keys")
for k, v in ipairs(restrictedNumber) do
write(k.."\n")
end
wait()
term.clear()
term.setCursorPos(1,1)
print("values")
for k, v in ipairs(restrictedNumber) do
write(v.."\n")
end
wait()
term.clear()
term.setCursorPos(1,1)
print("Generated numbers:")
for i = 1,16 do
generateNumber()
print(randomNum)
isRestrictedNumber = true – this sets isRestrictedNumber to true so it can loop through the program again.
end
wait()
term.clear()
term.setCursorPos(1,1)
print("list restricted numbers")
for k, v in ipairs(restrictedNumber) do
print(v)
end
The way i have been trying to get it to work is the following:
math.random generates a number between 1 and 52. Then comes a function that is supposed to go through the table restrictedNumber and check if the random number is present there. If it is it should loop back and generate a new number. If it's not present then it should insert the random number in the restrictedNumber tables and break the loop.
For simplicity sake i wrote a new test code to mess around with the table but i can't for the life of me figure out how i get it to actually restrict numbers.
As far as i can tell this code is supposed to loop forever untill it generates a number that is not restricted, however it does not do that. IsRestrictedNumber is somehow set to false, even though a restricted number is generated and the loop breaks and a number that is restricted is printed.
Spoiler
restrictedNumber = {}function generateNumber() – generates a number between 1 and 16. Then it checks if the number is restricted. If not then it breaks the loop, if it is it should generate a new number.
while true do
randomNum = math.random(1, 16)
checkRestrictedNumber()
if isRestrictedNumber == false then
break
end
end
end
function checkRestrictedNumber() – should go through restrictedNumber table and check if the generated number is listed there. if it is not it should insert it to the table and break the loop.
for k, v in ipairs(restrictedNumber) do
if restrictedNumber[k] == randomNum then
isRestrictedNumber = true
else
isRestrictedNumber = false
table.insert(restrictedNumber, randomNum)
break
end
end
end
function wait()
event = os.pullEvent()
end
– Code
term.clear()
term.setCursorPos(1,1)
for i = 1, 10 do
table.insert(restrictedNumber, i)
end
print("keys")
for k, v in ipairs(restrictedNumber) do
write(k.."\n")
end
wait()
term.clear()
term.setCursorPos(1,1)
print("values")
for k, v in ipairs(restrictedNumber) do
write(v.."\n")
end
wait()
term.clear()
term.setCursorPos(1,1)
print("Generated numbers:")
for i = 1,16 do
generateNumber()
print(randomNum)
isRestrictedNumber = true – this sets isRestrictedNumber to true so it can loop through the program again.
end
wait()
term.clear()
term.setCursorPos(1,1)
print("list restricted numbers")
for k, v in ipairs(restrictedNumber) do
print(v)
end