=== MazeBoard.java === {{{~cpp import java.awt.Point; public class MazeBoard { public int boardMatrix[][]; public static int WALL = 0; public static int PATH = 1; public static int END = 2; public static int BLOCK = 3; public MazeBoard(int boardMatrix[][]) { int x = boardMatrix[0].length; int y = boardMatrix.length; this.boardMatrix = new int[y+2][x+2]; for(int i = 0 ; i < x ; ++i) for(int j = 0 ; j < y ; ++j) this.boardMatrix[j+1][i+1] = boardMatrix[j][i]; } public boolean isEndPoint(Point point) { return getBoardSymbol(point) == END; } public boolean isOpened(Point pt) { return getBoardSymbol(pt) != MazeBoard.WALL && getBoardSymbol(pt) != MazeBoard.BLOCK; } public int getBoardSymbol(Point pt) { return boardMatrix[pt.y][pt.x]; } } }}} === Player.java === {{{~cpp import java.awt.Point; import java.util.Vector; public class Player { public Vector tracedPath = new Vector(); public MazeBoard board; public static final int E = 0; public static final int S = 1; public static final int W = 2; public static final int N = 3; public static final Point directionTable[] = { new Point(1, 0), new Point(0, 1), new Point(-1, 0), new Point(0, -1)}; public Player(int x, int y, MazeBoard board) { tracedPath.add(new Point(0,0)); tracedPath.add(new Point(x + 1, y + 1)); this.board = board; } public void move() { for (int i = 0; i < 4; ++i) { Point curPoint = new Point( getLastPoint() ); if ( isValidMove(curPoint,i) ) { curPoint.x += directionTable[i].x; curPoint.y += directionTable[i].y; tracedPath.add( new Point(curPoint) ); return; } } backToPrev(); } public boolean isValidMove(Point curPoint, int nth) { Point nextPt = new Point( curPoint.x + directionTable[nth].x, curPoint.y + directionTable[nth].y ); Point prevPt = (Point) tracedPath.elementAt(tracedPath.size() - 2); return board.isOpened(nextPt) && getDirection(nextPt)!= getDirection(prevPt); } public int getDirection(Point point) { Point curPoint = new Point(getLastPoint()); if (point.x > curPoint.x) return E; if (point.x < curPoint.x) return W; if (point.y > curPoint.y) return S; return N; } public Point getLastPoint() { return (Point)tracedPath.get(tracedPath.size() - 1); } public void traverseMaze() { while( !board.isEndPoint( getLastPoint() ) ) move(); } public void backToPrev() { Point pt = new Point(getLastPoint()); board.boardMatrix[pt.y][pt.x] = MazeBoard.BLOCK; tracedPath.remove( tracedPath.size()-1 ); } } }}} === MazeTestCase === {{{~cpp import java.awt.Point; import java.util.Vector; import junit.framework.TestCase; public class MazeTestCase extends TestCase { final int X = 3; final int Y = 3; int mazeBoard[][] = { {1,1,0}, {0,1,0}, {0,1,2} }; MazeBoard mb = new MazeBoard(mazeBoard); Player player = new Player(0,0,mb); public MazeTestCase(String title) { super(title); } public void testCreation() { int expected[][] = { {0,0,0,0,0}, {0,1,1,0,0}, {0,0,1,0,0}, {0,0,1,2,0}, {0,0,0,0,0} }; for(int i = 0 ; i < X+2 ; ++i) for(int j = 0 ; j < Y+2 ; ++j) assertEquals(expected[j][i], mb.boardMatrix[j][i]); } public void testMovement() { Point expectedPoint[] = { new Point(2,1), new Point(2,2), new Point(2,3), new Point(3,3) }; for(int i = 0 ; i < 4 ; ++i) { player.move(); assertEquals( expectedPoint[i], player.getLastPoint() ); } } public void testMovement2() { Vector expected = new Vector(); Point pt[] = {new Point(0,0), new Point(1,1), new Point(2,1), new Point(2,2), new Point(2,3), new Point(3,3)}; for(int i = 0 ; i < pt.length ; ++i) expected.add(pt[i]); player.traverseMaze(); assertEquals( expected, player.tracedPath ); } public void testJudge() { Point point = new Point(2,1); assertEquals(Player.E, player.getDirection(point)); point.setLocation(1,2); assertEquals(Player.S, player.getDirection(point)); } public void testEnd() { player.traverseMaze(); Point point = player.getLastPoint(); assertEquals(2, mb.boardMatrix[point.y][point.x]); } } }}} === ComplexMazeTestCase === {{{~cpp import java.awt.Point; import junit.framework.TestCase; public class ComplexMazeTestCase extends TestCase { int complexMatrix[][] = {{1,1,1,0},{0,0,1,0},{1,1,1,1},{2,0,0,0}}; MazeBoard mb = new MazeBoard(complexMatrix); Player player = new Player(0,0,mb); public void testBlocked() { for(int i = 0 ; i < 5 ; ++i) player.move(); player.backToPrev(); assertEquals(MazeBoard.BLOCK, mb.getBoardSymbol(new Point(4,3))); assertEquals(new Point(3,3), player.getLastPoint()); } public void testAll() { Point expected[] = {new Point(0,0), new Point(1,1), new Point(2,1), new Point(3,1), new Point(3,2), new Point(3,3), new Point(2,3), new Point(1,3), new Point(1,4)}; player.traverseMaze(); for(int i = 0 ; i < expected.length ; i++) assertEquals(expected[i], (Point) player.tracedPath.get(i)); } public void testAcceptance1() { int matrix[][] = { {1,1,0,0,0,0}, {0,1,1,1,1,0}, {0,0,0,0,1,0}, {0,0,0,0,2,0} }; MazeBoard maze = new MazeBoard(matrix); Player p = new Player(0,0,maze); p.traverseMaze(); assertEquals(new Point(5,4), p.getLastPoint()); } public void testAcceptance2() { int matrix[][] = {{1,1,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0}, {0,1,1,1,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0}, {0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,1,1,1,0,0,0,0}, {0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,1,1,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,0,0}, {1,1,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0}, {1,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0}, {1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0}, {1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0}, {1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0}, {1,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0}, {1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0}, {0,1,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0}, {1,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0}, {0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,1,1,0,0,0}, {0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,1,1,1,2}}; MazeBoard maze = new MazeBoard(matrix); Player p = new Player(0,0,maze); p.traverseMaze(); assertEquals(new Point(30,20), p.getLastPoint()); } } }}} === MazeFrame.java === {{{~cpp import java.awt.*; import javax.swing.*; public class MazeFrame extends JApplet { JButton mazeUnit[][] = new JButton[20][30]; MazeBoard maze; Player player; public void init() { int matrix[][] = {{1,1,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0}, {0,1,1,1,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0}, {0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,1,1,1,0,0,0,0}, {0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,1,1,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,0,0}, {1,1,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0}, {1,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0}, {1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0}, {1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0}, {1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0}, {1,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0}, {1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0}, {0,1,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0}, {1,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0}, {0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,1,1,0,0,0}, {0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,1,1,1,2}}; maze = new MazeBoard(matrix); player = new Player(0,0,maze); player.traverseMaze(); getContentPane().setLayout(new GridLayout(20,30)); for(int i = 0 ; i < 20 ; ++i) { for(int j = 0 ; j < 30 ; ++j) { mazeUnit[i][j] = new JButton(); int boardSymbol = maze.getBoardSymbol(new Point(j+1,i+1)); if( boardSymbol == MazeBoard.WALL ) mazeUnit[i][j].setBackground(Color.BLACK); else if( boardSymbol == MazeBoard.PATH || boardSymbol == MazeBoard.BLOCK ) mazeUnit[i][j].setBackground(Color.WHITE); getContentPane().add(mazeUnit[i][j]); } } setSize(600,600); setVisible(true); traverse(); } public void traverse() { for(int i = 1 ; i < player.tracedPath.size() ; ++i) { Point pt = (Point)player.tracedPath.get(i); mazeUnit[pt.y-1][pt.x-1].setBackground(Color.RED); } } } }}}