HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Finding path in Maze

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
mazepathfinding

Problem

I recently gave an interview and was asked the following question. I posted this here
Finding path in Maze also, and someone suggested I should try Code Review. So here it goes.

A maze is a group of linked Places. Each Place has a North, South, East, and West Place adjacent to it. There are two special pre-defined Place's: Place Wall represents a wall - the mouse can't go there. Place Cheese is ... cheese! The connections between Places are symmetrical - if you start from any Place and go North and then South, you return to where you were.

To simplify things, the maze has no closed loops - that is, if you start in any Place and follow any path, you eventually either hit a wall or find cheese - you never come back to the Place you started unless you actually retrace your steps.

A mouse starts off in some Place and searches around for Cheese. When it finds Cheese, it returns a set of directions - a string of the letters NSEW that lead from where it started to the Cheese.

The following framework defines the classes and functions. Some other code not included here generates a maze by creating a bunch of Place's and linking them. It then calls mouse(), passing some starting Place from the maze:

interface Place {

// Return the adjacent Place in the given direction
public Place goNorth();
public Place goSouth();
public Place goEast();
public Place goWest();

// Returns true only for the special "Wall" place
public bool isWall();

// Returns true only for the special "Cheese" place
public bool isCheese();
};

class Mouse {
  public Mouse() {}

  // Return a string of the letters NSEW which, if used to traverse the
  // the maze from startingPoint, will reach a Place where isCheese() is
  // true.  Return null if you can't find such a path.
  public String findCheese(Place startingPoint) {
    ... fill this in ...
  }
}


Implement findCheese(). You can add any fields or helper methods you need to Mouse. Extra credit: Eliminate the "no closed loops" restriction

Solution

I'm sorry, but that code is really convoluted.

First off, does this actually work? The comparisons direction == "N"(etc.) should fail at compile time due to incomparable types (String and StringBuilder) and nDir.equals("") would always return false.

Generally you can't use use == to compare strings in Java. You have to use equals (for strings. StringBuilder doesn't have an implemention of equals).

You really are overdoing it with the StringBuilders. You should only need a single one (one new StringBuilder() call in the entire code) to which you append Strings.

To be honest in practice code like this, you don't need use a StringBuilder at all. Just use string concatenation. It's slow and a memory hog, so it's not something you should do in real life code, but it would make this code much more simpler and easier to read.

There is much more, but I believe that would be good for a first step in the right direction.

Context

StackExchange Code Review Q#44544, answer score: 6

Revisions (0)

No revisions yet.