How to match pieces

Let’s say I have 2 pieces of piece nr 1 and 4 pieces of piece nr 2.
The question I’d like to ask the system is “how many closed loops can I create with the bricks I have?”

How would I go about creating the square/rectangle? Or more complicated structures in the future…

Some sort of matching I suppose?

I’m completly new to Unity so any help regarding on where to begin is appreciated.

If you want the game to figure out what shapes can be made with given pieces, you’ll probably need to make an algorithm that makes attempts systematically for every possible option. I am not sure of the best way to do that, but one lead I can think of is taking one piece and connecting every other piece in all possible rotations. Then you put the combinations in a list and do a different combination of pieces. Once you’ve got all 2-piece combinations, you take those and try to mix them with all single pieces in every way they can attach together. Then when you’ve got all the 3-piece combos you continue until you have all 6-piece combinations. Then you check all the 6-piece combinations for loops and keep the best ones.

The pieces should be their own class/prefab that allows you to check where each connection is and either a grid-based system or a collision system to check for overlapping pieces when assembling.

In any case that’s probably not an algorithm a beginner would have a good time with, but perhaps some pieces of that code exist somewhere.