#Need help creating a shuffling system for a Stack of cards.

101 messages · Page 1 of 1 (latest)

rotund trench
#

So are you trying to generate a table with all numbers from 1 to ModifiersMS.Card_Range in a random order ?

wintry wadi
#

ModifiersMS.Card_Range is 48 btw

rotund trench
#

you can initialize NewStack to have all numbers in order then shuffle it

wintry wadi
#

Well the Stack has the numbers in order. Is that what you're saying?

rotund trench
#

for example you can start with {1, 2, 3, 4} using a for loop then shuffle it to {4, 2, 1, 3}

wintry wadi
#

I don't really get what you're saying

#

This is what's printed when I print Stack. There's a number associated with the card, which is a table that stores information such as the Model its assigned to, whether or not its Face Up, its Owner, and its value.

rotund trench
#
local stack = {}

local function shuffle(tab)
  for i = 1, #stack do
    local j = math.random(1, #tab)
    stack[i], stack[j] = stack[j], stack[i]
  end
end

for i = 1, 48 do
  stack[i] = i
end

shuffle(stack)
print(stack)
wintry wadi
#

Eh?

rotund trench
#

the shuffle function will shuffle a given array

wintry wadi
#

I was thinking of just replacing the numbers created in NewStack for the Index of the Stack argument passed.

#

But, okay, I'll try your method.

rotund trench
wintry wadi
#

Let me try it

wintry wadi
ripe gulchBOT
#

studio** You are now Level 3! **studio

rotund trench
#

error ?

wintry wadi
#

No, it doesn't give an error. It prints this

#

Its not sorted

rotund trench
#

what did you print

wintry wadi
#

The stack

rotund trench
#

can you show your code now

wintry wadi
#

I changed the variables of the code a bit

#

but it should be the same

rotund trench
#

you are overwriting the stack

#

after it has been shuffled

wintry wadi
#

Oh

#

right

rotund trench
#

that's supposed to be before the other for loop

wintry wadi
#

huh?

rotund trench
#

else this will shuffle an empty stack

wintry wadi
#

Isn't the way you're doing this currently just.. not a good way of shuffling it, since the math.random function could pick the same number twice?

#

I'm just trying to randomly move the Card model positions in the stack

#

its like

#

I prefer doing it the way I was doing it beforehand, just tweaking a few things to actually make it work.

rotund trench
#

well what was the problem with the other way

rotund trench
wintry wadi
#

I needed to take the Numbers generated in the NewStack table, and replace them with the Index of the Cards in the Stack.

rotund trench
#

so you want cards inside newstack, not numbers

wintry wadi
#

The values from the first screenshot, should be replaced with the Index of the second screenshot.

#

That's what I want anyway

wintry wadi
rotund trench
#

the index is that part

wintry wadi
#

Yes.

#

I was thinking, could I have done it like this for example..

rotund trench
#

yeah that works

wintry wadi
#

Really?

rotund trench
#

I don't know what would Card[Index] be though

wintry wadi
#

Oh, I meant Stack[Index]

rotund trench
#

that will make a copy of Stack into NewStack

wintry wadi
#

Well if I want to replace the NewStack's Value with the Stack's Index, would I need to do

NewStack[Card] = Stack[Index]```
rotund trench
#

that will error as Card is a table

wintry wadi
#

Oh right.

rotund trench
#

do you even need "NewStack" or can you just shuffle Stack directly

wintry wadi
#

I could probably shuffle Stack directly

#

I thought it would be better to do it this way

rotund trench
#

seems like unecessary memory usage

wintry wadi
#

How would I rewrite the code to only use the Stack table though

rotund trench
#

this but tab is Stack

#

that algorithm is called Fisher–Yates shuffle if you want more info on it

#

and it won't have any duplicates as it only switches the indices

wintry wadi
#

Alright, neat

#

Never heard of it

#

but I'll do a bit of research

rotund trench
# rotund trench

i got a bit confused with tab and stack, it should all be the same name as the parameter

wintry wadi
#

Ok

wintry wadi
rotund trench
#

what do you mean top card descending

wintry wadi
ripe gulchBOT
#

studio** You are now Level 4! **studio

wintry wadi
#

yknow the top card on the stack

rotund trench
#

yeah

wintry wadi
#

previously I had it picking the cards from the top

#

then the one under that

rotund trench
#

yes

wintry wadi
#

then the one under that

#

but now its taking out random cards from the middle

#

and it looks like its floating

#

which is wacky

rotund trench
#

im not sure how the shuffle could have done that

wintry wadi
#
function StackManagement.Distribute() -- Distributes the Cards to all players in the game.
    for _, Player in pairs(PlayersService:GetPlayers()) do -- Get all the players in the game.
        local CardAmount = 1 -- Set the card amount as an integer value. 

        for Index = #StackManagement.Stack, 0, -1 do -- Goes through the Stack of cards in reversing order.
            local Card = StackManagement.Stack[Index]-- Defines the Card.

            if CardAmount <= 4 then -- This will run 4 times, moving 4 Cards to the player's Roster.
                if not Card.Owner then -- Checks if the Card doesn't have an Owner.
                    Card.Owner = Player.Name -- Sets the Card.Owner property to the Player's username.

                    local CardLocation = RosterFolder[Card.Owner][CardAmount] -- Defines the CardLocation variable.

                    Card.Model:PivotTo(CardLocation:GetPivot()) -- Pivots the Card model to the CardLocation specified.
                    Card.Model.Parent = RosterFolder[Player.Name].Cards -- Moves the Card model's parent to the Player's Roster.
                    Card.Model.Main.Front.Number.Text = Card.Value -- Sets the Text on the Card to equal the Card's Value.

                    FaceUpRM:FireClient(Player, Card, CardAmount) -- Fires to the Client to set the FaceUp properties of the first 2 Cards to true.

                    CardAmount += 1 -- Adds 1 to the Card Amount.
                end
            end
        end
    end
end```

This is the distribute script, which is what's actually taking out the cards.
rotund trench
#

so you shuffle the stack beforehand

wintry wadi
#

Yeah

#

I create the Stack

#

shuffle it

#

then distribute it

#
function StackManagement.StackCards(Stack) -- Stack the Cards in the Workspace.

    -- The Cards argument passed is the Stack table, which stores all 48 cards.

    local NextStackLocation = nil -- Defines the NextStackLocation, which is the previous Card's position + 0.02.

    for Index, Card in pairs(Stack) do -- Loop through the Cards.
        Card.Model:PivotTo(NextStackLocation or StackFolder.StackLocation.CFrame + Vector3.new(0, 0.02, 0)) -- Moves the CardModel to either the NextStackLocation, or the StackLocation.CFrame, which is the point where the Stack begins.
        NextStackLocation = Card.Model:GetPivot() + Vector3.new(0, 0.02, 0) -- Defines the NextStackLocation property as the Previous Card. 

        Card.Model.Name = "Card_" .. Index -- Sets the name of the Card Model in the Workspace to the Index.
        wait()
    end
    
    StackManagement.Shuffle(Stack)
    
end

This is what's calling the Shuffle function

rotund trench
#

you should be shuffling it before displaying the cards

#

as they will not move after you shuffled the stack

#

so

  1. Shuffle(Stack)
  2. StackCards(Stack)
  3. Distribute(Stack)
wintry wadi
#

Ok

#

Minor issue

#

The cards is being created in this function

function StackManagement.NewCard() -- Creates a new card with the assigned properties.
    for Index = 1, ModifiersMS.Value_Range do -- Creates 48 cards in total.
        for _ = 1, (ModifiersMS.Card_Range / ModifiersMS.Value_Range) do -- Assigns the values for the cards. 4 Cards are assigned to 1 value.
            local Card = { -- Stores the card information, including the Model, Value, Owner, and if the card is Face Up.
                Model = script.Card:Clone(),
                Owner = nil,
                Value = Index,
                FaceUp = false,
            } 
            Card.Model.Parent = StackFolder -- Sets the Model of the Card to the StackFolder in the Workspace.
            
            table.insert(StackManagement.Stack, Card) -- Inserts the Card into the Stack table.
        end
    end
    
    StackManagement.StackCards(StackManagement.Stack) -- Calls the StackCards function.
end

which then calls the StackCards function. If I call Shuffle first, it won't have anything inside of the Stack table

rotund trench
#

you can shuffle the stack here

wintry wadi
#

Ok