0
\$\begingroup\$

I'm having trouble with the logic for this problem. I've got a grid of modules. Each module may take 1 or more cells on the grid, and each cell of a module may have 1 or more connectors. The connectors are represented by an enum (or int). Adjacent modules may only be placed if they have matching connectors.

So, for example:

-Module A in position (0, 1) has a down (3) connector. -Module B in position (0, 2) has an up (1) connector.

This is a valid configuration.

-Module A in position (0, 1) has a down (3) connector. -Module B in position (0, 2) has an right (2) connector.

This configuration is invalid.

I've attached an image below for clarity:

Valid module placement examples

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

The logic is quite simple:

goodToPlace = true
for every side
  if hasConnector(on that side) then
    if hasObject in direction of that side
      if not object.hasConnector(side - 2) then // side - 2 means to take opposite side
        goodToPlace = false
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .