Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

This is probably the most useful enum there is. I don't know how many versions of it I have. Also note that you can add dx and dy values to this enum you can add dx and dy values to this enum (UP for example is dx = 0 and dy = -1). This tends to reduce a lot of code duplication.

This is probably the most useful enum there is. I don't know how many versions of it I have. Also note that you can add dx and dy values to this enum (UP for example is dx = 0 and dy = -1). This tends to reduce a lot of code duplication.

This is probably the most useful enum there is. I don't know how many versions of it I have. Also note that you can add dx and dy values to this enum (UP for example is dx = 0 and dy = -1). This tends to reduce a lot of code duplication.

added 92 characters in body
Source Link
Simon Forsberg
  • 59.7k
  • 9
  • 157
  • 311

This is probably the most useful enum there is. I don't know how many versions of it I have. Also note that you can add dx and dy values to this enumyou can add dx and dy values to this enum (UP for example is dx = 0 and dy = -1). This tends to reduce a lot of code duplication.

This is probably the most useful enum there is. I don't know how many versions of it I have. Also note that you can add dx and dy values to this enum (UP for example is dx = 0 and dy = -1). This tends to reduce a lot of code duplication.

This is probably the most useful enum there is. I don't know how many versions of it I have. Also note that you can add dx and dy values to this enum (UP for example is dx = 0 and dy = -1). This tends to reduce a lot of code duplication.

Source Link
Simon Forsberg
  • 59.7k
  • 9
  • 157
  • 311

First of all, using i and j as variable names when dealing with 2D-arrays is just crazy in my personal opinion. Using x and y makes it so much clearer to know which is which.

Then, use an enum:

public enum Direction {
 LEFT, UP, RIGHT, BOTTOM;
 public Direction opposite() {
 switch (this) {
 case LEFT: return RIGHT;
 case RIGHT: return LEFT;
 case UP: return BOTTOM;
 case BOTTOM: return UP;
 default: throw new IllegalStateException();
 }
 }
}

This is probably the most useful enum there is. I don't know how many versions of it I have. Also note that you can add dx and dy values to this enum (UP for example is dx = 0 and dy = -1). This tends to reduce a lot of code duplication.

Now that we have an enum we can return that instead of those int things.

Also, we can use Java's EnumSet class to handle a collection of enums. This class is optimized to use a long (BitSet object for longer enums) and bitmasks, sort of like you seem to already be doing.

Once we have a nice EnumSet to use, then we can either create an array of it and grab a random item from the array, or we can use the iterator of it to iterate over random.nextInt(size) items (not the cleanest and fastest solution perhaps, but it should be faster than creating an array).

Random random = new Random();
private Direction nextDirection(final int y, final int x, final int rowLimit, final int columnLimit, final Direction previous) {
 EnumSet<Direction> directions = EnumSet.allOf(Direction.class);
 if (x == 0) {
 directions.remove(Direction.LEFT);
 }
 if (x == columnLimit) {
 directions.remove(Direction.RIGHT);
 }
 if (y == 0) {
 directions.remove(Direction.UP);
 }
 if (y == rowLimit) {
 directions.remove(Direction.BOTTOM);
 }
 directions.remove(previous.opposite());
 Iterator<Direction> it = directions.iterator();
 for (int i = 0; i < random.nextInt(directions.size()); i++) {
 it.next();
 }
 return it.next();
}

Or, if you would like the array approach:

Direction[] options = directions.toArray(new Direction[directions.size()]);
return options[random.nextInt(options.length)];
lang-java

AltStyle によって変換されたページ (->オリジナル) /