Grid of Parts Help (Warning : [Very Complicated and Advanced] )

Alright so I have a script that creates a grid with a specified size, I also have a table that holds 4 unique colors.

image

Okay so what I am trying to do is set every single tile to have an even number of each of the colors from that DefaultColors table, and I want it to depend on the size of the grid…

I know I may not get help but it’s a shot in the dark

If you can clarify your goal here we could help you out. Does a “tile” consist of more than one part? Depend on the size of the grid in what way?

2 Likes

As @A9X requested, please describe what your goal is in more detail, right now it is kind of hard to know what you mean with every tile to have an even number of the colours etc.

I’d also change the code slightly, right now you are repeating grid[x][y] a lot. Consider doing something like this instead:

local tile = Instance.new("Part")
tile.TopSurface = Enum.SurfaceType.Smooth
... -- Rest of configuration
grid[x][y] = tile
tile.Parent = workspace
1 Like

Okay so basically lets say I have a 4 x 4, okay so thats 16 tiles in total!

It’s like SUPER HARD to explain but basically THIS

@ifkpop
@A9X

What if the grid dimensions are not divisible by 4?

1 Like

Yeah this is exactly where I ran into a problem, Ive been testing out the numbers and it can only work if gridsizeX * gridsizeY is an even number, and if i dont want the colors to repeat ill need to have the same amount of colors as one of the grid sizes, and im not even gonna worry about different different X and Y sizes right now

6 x 6 Grid

6 Colors

Yeah I mean you won’t be able to achieve what you want, it’s not really possible. Maybe describe what the thing you’re working on is for because there could be a different solution.

In your image most of the colours are 2 & 2, do you want the colours to be randomly chosen? If the colours are randomly chosen the most likely outcome is for all colours to have approximately the same amount of tiles.

Otherwise you can calculate a “budget” for each colour before hand, and then randomly pick a square that has not been coloured yet and set it’s colour to the colour to be filled in.

It’d be helpful if you explained what exactly you’re trying to achieve with more detail, but based on what you’ve described so far, something like this might work.

math.randomseed(os.time()) -- randomize results

local function distributeColors(gridSize, colors) -- gridsize is a number, colors is an array of BrickColors
	if gridSize % #colors == 0 then -- make sure it's divisible
		local unrandomizedColors = {}
		local randomizedColors = {}

		for _, color in pairs(colors) do
			for i = 1, (gridSize*gridSize)/#colors do
				table.insert(unrandomizedColors, color) -- for every color, insert into a table every time that color can exist in the grid, e.g., a 4x4 grid with 2 colors has 8 of each color, we insert eight copies of each color in the table
			end
		end

		for i = 1, #unrandomizedColors do
			table.insert(randomizedColors, table.remove(unrandomizedColors, math.random(1, #unrandomizedColors))) -- for every color that can exist in the table, randomly pick one, insert it into a randomized table, then remove it from the unrandomized table
		end

		return randomizedColors
	else
		warn("gridSize was not evenly divisible by the number of colors provided, so an even distribution can not be made.")
	end
end

local result = distributeColors(9, {BrickColor.new("Really blue"), BrickColor.new("Really red"), BrickColor.new("Really black")}) -- example usage

This will return a table of BrickColors in a randomized order, with the same amount of each color based on the size of the grid. At the bottom I’ve included an example of an 9x9 grid with 81 tiles and 3 colors. The resultant array will have 27 of each color, but randomly dispersed.

1 Like

How would I implement this into my existing script?

I prefer not to hand-out exact code, but you could run this function before your tiles are generated, then while looping through each tile, use the index of a for loop to pick from the table.

E.g.,

local function distributeColors()
-- blah blah blah
end

local results = distributeColors(gridSize, colors)

for i = 1, gridSize*gridSize do
-- create tiles
    tile.BrickColor = results[i]
end
1 Like

Alright, this is working thanks alot for your help and explanation, it’s greatly appreciated

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.