Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ok, I agree 100% that Enums is the way to go here. The only thing I would suggest, especially since this is logic being applied to a game and you likely will need to store the information that defines a room somewhere, is to use a system of binary ids. Using this sort of system, you can store a binary representation of what doors are available to a room. This system works well when you are dealing with items where you can only have a maximum of one. In your case, a room can only have 1 door for every direction. If you add up all the binary values for each door in a room, you can store that value and later turn that value back into the proper Enums.</p> <p>Example:</p> <pre><code>public enum Direction { NONE (0, 0), NORTH (1, 1), SOUTH (2, 2), EAST (3, 4), WEST (4, 8), NORTHEAST (5, 16), NORTHWEST (6, 32), SOUTHEAST (7, 64), SOUTHWEST (8, 128), UP (9, 256), DOWN (10, 512); private Integer id; private Integer binaryId; private Direction(Integer id, Integer binaryId) { this.id = id; this.binaryId = binaryId; } public Integer getId() { return id; } public Integer getBinaryId() { return binaryId; } public static List&lt;Direction&gt; getDirectionsByBinaryIdTotal(Integer binaryIdTotal) { List&lt;Direction&gt; directions = new ArrayList&lt;Direction&gt;(); if (binaryIdTotal &gt;= 512) { directions.add(Direction.DOWN); binaryIdTotal -= 512; } if (binaryIdTotal &gt;= 256) { directions.add(Direction.UP); binaryIdTotal -= 256; } if (binaryIdTotal &gt;= 128) { directions.add(Direction.SOUTHWEST); binaryIdTotal -= 128; } if (binaryIdTotal &gt;= 64) { directions.add(Direction.SOUTHEAST); binaryIdTotal -= 64; } if (binaryIdTotal &gt;= 32) { directions.add(Direction.NORTHWEST); binaryIdTotal -= 32; } if (binaryIdTotal &gt;= 16) { directions.add(Direction.NORTHEAST); binaryIdTotal -= 16; } if (binaryIdTotal &gt;= 8) { directions.add(Direction.WEST); binaryIdTotal -= 8; } if (binaryIdTotal &gt;= 4) { directions.add(Direction.EAST); binaryIdTotal -= 4; } if (binaryIdTotal &gt;= 2) { directions.add(Direction.SOUTH); binaryIdTotal -= 2; } if (binaryIdTotal &gt;= 1) { directions.add(Direction.NORTH); binaryIdTotal -= 1; } return directions; } } </code></pre>
 

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