Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the most efficient way to link grid squares together?
    primarykey
    data
    text
    <p>Assume that I want to represent a 10*10 room which has 100 floors. I can start from any floor and walk through the room, but, there are walls between some floors. </p> <p>CS speaking, I am trying to make a 10*10 grid -room- where each square represent a floor. Each floor has certain characteristics and it could be represented as a node, the whole grid, however contains list of these nodes. </p> <p>Im trying to link each square to the one that relates to it. The relation could be described as follow: -Each square has another one on top, right, up, down to it except the ones on the edges of the grid. </p> <p>For example, the first floor which is at the top left of the grid has relation only to the floor on its right side and the one under it. </p> <p>-Additionally, some floors can't be linked together assuming that there's a block between them. Floor no. one in the previous example can't be linked with the one under it because there's a wall between them. </p> <p>I used a pointer to link each node -square or floor- to its related nodes:</p> <pre><code>public class Node{ private Node right; private Node left; private Node up; private Node down; //constructor other methods } </code></pre> <p>However, this solution may take many places in the memory, assuming we have 100 nodes and each node has 4 pointers!</p> <p>I've changed this solution by assigning ID to each node, then in each node there is an int[] array where I can store the number of related nodes. </p> <p>This solution introduced another problem in the grid class! Assume that after the change, a new method at Node class will be : </p> <pre><code>public void setNeighbors(int[] neighbors ) { this.neighbors = neighbors; } </code></pre> <p>At the grid class when I want to create each node to add it to the list, I'll have to write 100 line, one for each node! </p> <pre><code>int [] n1 ={2}; grid.getEntry(1).setNeighbors(n1); int [] n2 ={1,3}; grid.getEntry(2).setChars(n2 ); . . . And so on.. </code></pre> <p>My question is, how can I resolve the problem by making as efficient and clean code as possible. </p> <p>How can I represent the <strong>static</strong> relation between the squares without having to create an array at each step or without having to write 100 line.</p> <p>I found a mathematical relation between the squares but I wasn't able to use it because some squares can't be linked to the onse next to it because of the wall between them..</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload