This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Ravneravn's profile picture

Blackjack game, how to prevent cards from being drawn two times.

Started by Ravneravn, 15 May 2013 - 10:30 AM
Ravneravn #1
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.

SpoilerrestrictedNumber = {}


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
Lyqyd #2
Posted 15 May 2013 - 05:09 PM
Split into new topic.

You are doing this very much the hard way. Instead of randomly generating a card to draw (which can take an excessively long time if you end up trying to draw the last card), you should shuffle the deck and then use the cards in order. You'll never have to worry about trying to generate cards. An alternative would be to keep the deck in order, then randomly draw a card from it and remove the card. The latter concept is ver simple:


function drawCard(deck)
  return table.remove(deck, math.random(1, #deck))
end
LordIkol #3
Posted 16 May 2013 - 03:00 AM
i would do the same like Liquid said. just create a new Deck with Cards in Order and then remove cards Randomly.
Like this for Example:



local mydecksize = 52

function newDeck(size)  -- creates a new deck with x cards 
local myDeck = {}
for i = 1,size do 
myDeck[i] = i
end
return myDeck
end

function drawCard(deck)
  return table.remove(deck, math.random(1, #deck))
end

local moep = newDeck(mydecksize)
local mycount = 0

for i = 1,#moep do
mycount = mycount+1
print (mycount.. ": " ..drawCard(moep))
end
Ravneravn #4
Posted 18 May 2013 - 02:44 PM
Thanks for the help, that made it a lot easier.

Here is a video of the program:

[media]http://youtu.be/9iw2PQpzFFM[/media]

and the pastebin for the downloader: http://pastebin.com/NBuWntUv

This game was inspired by ChiknNuggets blackjack game, but the code is all mine.