Maze problem 的OO Design - 写给煜小堆

Question: 

How do you design a Maze and what kind of data structures you use for Maze. In addition, write a method to print the shorted path from start to end point.

Example:

#############
eoo#o#ooo#oo#
##o#o##o##o##
#oooo#oo#ooox
###o##o##o###
#ooooooooooo#
#####o###o###
#ooooooooooo#
#############

思路:

A collection of classes to solve the problem

The first step in constructing an object-oriented program to solve a complex problem is to identitfy the class or classes that will play a role in the solution. For this problem, three classes immediately suggest themselves:

  • A Cell class to represent individual cells in the maze.
  • A Maze class to hold all of the cells that make up the maze.
  • A Path class to represent a path through the maze.
/** A class to represent cells in a maze. Cells can be blocked,
 * open, or visited. In addition, entrances and exits in a maze
 * are represented by a special cell type.
 */
public class Cell {
  /* Class variables */
  private static final char OPEN = 'o';
  private static final char VISITED = 'v';
  private static final char CLOSED = '#';
  private static final char ENTRANCE = 'e';
  private static final char EXIT = 'x';
  
  /* Member variables. */
  private int row;
  private int column;
  private char state;
  
  /* Constructor */
  public Cell(int row,int column,char code) {
    this.row = row;
    this.column = column;
    state = code;
  }
  
  /* Accessor methods */
  public boolean isOpen() { return state == OPEN || state == ENTRANCE || state == EXIT; }
  public boolean isEntrance() { return state == ENTRANCE; }
  public boolean isExit() { return state == EXIT; }
  public int getRow() { return row; }
  public int getColumn() { return column; }
  public char toChar() {
    return state;
   }
  public String toString() {
    return "(" + row + "," + column + ":" + toChar() + ")";
  }
  
  /* Mutator methods */
  public void markVisited() {
    if(state == OPEN) 
      state = VISITED;
  }
  public void clearVisited() {
    if(state == VISITED)
      state = OPEN;
  }

}

The Maze Class

Here now is an outline of the Maze class that our application will use to represent mazes. In this outline I have left out the code for the methods to make it easier to get an overview of the structure of the class.

public class Maze {
  /* Member variables */
  private Cell[][] grid;
  
  /* Construct a maze by reading it from a file. */
  public Maze(String fileName) throws Exception {}  
  
  /* Construct a maze by passing in a 2-dim array
  public Maze(Cell[][] grid) {
    this.grid = grid;
  }
  /* Accessor methods */
  
  // Find and return an entrance cell
  public Cell findEntrance() {}
  
  // Find a neighbor of the given cell that is open
  // and return a reference to that neighbor cell. If
  // you can find no such neighbor, return null.
  public Cell findOpenNeighbor(Cell fromCell) {}
  
  // Print the contents of the Maze to System.out
  public void print() {}
  
  /* Mutator methods */
  
  // Reset all visited cells to open
  public void clearVisited() {}

}


The Path class

The final class we will need to solve the maze search problem is a class that can represent a path through the maze. At the start of the maze search, the path will consist of just a single Cell, the entrance cell for the maze. As the search progresses we will systematically add Cells to the path. Occasionally, the path will run up into a blind alley. When that happens, we will not be able to find any unvisited neighbors for the cell at the end of the path and we will have to backtrack to a point where there are unvisited neighbors. To facilitate that, we have to be able to shed Cells from the end of the path.

Here is an outline of the Path class showing its methods and member variables.

public class Path {
  /* Member variables */
  private ArrayList<Cell> cells; // Holds the cells along the path
  
  /* Construct an initially empty path. */
  public Path() {}
  
  /* Accessor methods */
  
  // Return the last cell on the path, or null if the path is empty.
  public Cell lastCell() {}
  // Print every cell on the path
  public void print() {}
  
  /* Mutator methods */
  
  // Add the given cell to the end of the path
  public void addCell(Cell toAdd) {}
  // Remove the last cell from the path
  public void removeCell() {}
  
  // Test program to test the Path class
  public static void main(String args[]) {}
}


The Path class stores its Cells in an internal ArrayList. An ArrayList is the appropriate structure to use here, since we can start off with an empty ArrayList and use the add() and remove() methods as needed to add cells to the end of the path or remove them.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值