Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package maze;
- import java.io.*;
- import java.util.ArrayList;
- import maze.MazeCell.Type;
- /**
- *
- * @author David Shiverdecker
- */
- class MazeCrawler extends Maze
- {
- public MazeCrawler(int i, int i0)
- {
- super(i,i0);
- }
- public boolean isWin()
- {
- //the move methods watch for a win
- return false;
- }
- public boolean isNoMove()
- {
- MazeCell moveNorth;
- MazeCell moveSouth;
- MazeCell moveEast;
- MazeCell moveWest;
- try{
- moveNorth = getCell(getTrailHead().getRow()-1,
- getTrailHead().getColumn());
- }
- catch(ArrayIndexOutOfBoundsException e)
- {
- moveNorth=null;
- }
- try{
- moveSouth = getCell(getTrailHead().getRow()+1,
- getTrailHead().getColumn());
- }
- catch(ArrayIndexOutOfBoundsException e)
- {
- moveSouth=null;
- }
- try{
- moveWest = getCell(getTrailHead().getRow(),
- getTrailHead().getColumn()-1);
- }
- catch(ArrayIndexOutOfBoundsException e)
- {
- moveWest=null;
- }
- try{
- moveEast = getCell(getTrailHead().getRow(),
- getTrailHead().getColumn()+1);
- }
- catch(ArrayIndexOutOfBoundsException e)
- {
- moveEast=null;
- }
- if((moveNorth==null||moveNorth.getType()==Type.TRAIL||
- moveNorth.getType()==Type.WALL)&&(moveSouth==null||
- moveSouth.getType()==Type.TRAIL||moveSouth.getType()==Type.WALL)
- && (moveWest==null||moveWest.getType()==Type.TRAIL||
- moveWest.getType()==Type.WALL)&&(moveEast==null||
- moveEast.getType()==Type.TRAIL||moveEast.getType()==Type.WALL))
- {
- return true;
- }
- return false;
- }
- private void cleanMove(MazeCell moveTo)
- {
- MazeCell moveFrom = getCell(getTrailHead().getRow(),
- getTrailHead().getColumn());
- switch (moveTo.getType())
- {
- case TRAIL:
- case WALL: break;
- case EMPTY:
- moveFrom.setType(Type.TRAIL);
- moveTo.setType(Type.TRAIL_HEAD);
- setCell(moveFrom);
- setCell(moveTo); break;
- case EXIT: GUI.win(); break;
- case TRAIL_HEAD:
- default: System.err.println("Invaild Cell exiting maze.");
- System.exit(1);
- }
- super.paint(super.getGraphics());
- GUI.pause();
- }
- public void moveNorth()
- {
- try{
- MazeCell moveTo = getCell(getTrailHead().getRow()-1,
- getTrailHead().getColumn());
- cleanMove(moveTo);
- }catch(ArrayIndexOutOfBoundsException e){}
- }
- public void moveSouth()
- {
- try{
- MazeCell moveTo = getCell(getTrailHead().getRow()+1,
- getTrailHead().getColumn());
- cleanMove(moveTo);
- }catch(ArrayIndexOutOfBoundsException e){}
- }
- public void moveEast()
- {
- try{
- MazeCell moveTo = getCell(getTrailHead().getRow(),
- getTrailHead().getColumn()+1);
- cleanMove(moveTo);
- }catch(ArrayIndexOutOfBoundsException e){}
- }
- public void moveWest()
- {
- try{
- MazeCell moveTo = getCell(getTrailHead().getRow(),
- getTrailHead().getColumn()-1);
- cleanMove(moveTo);
- }catch(ArrayIndexOutOfBoundsException e){}
- }
- public void solveMaze()
- {
- System.out.println("Solving, Sometimes will not work not sure why");
- if(getPath(getTrailHead() .getRow() ,getTrailHead(). getColumn() ))
- {
- GUI.win();
- }else{
- //GUI.lose();
- }
- }
- private boolean getPath(int x,int y)
- {
- if(x>31||x<0||y>14||y<0)
- return false;
- if(getCell(x,y).getType()==Type.EXIT)
- return true;
- if(getCell(x,y).getType()==Type.WALL||getCell(x,y).getType()==Type.TRAIL)
- return false;
- //if(getCell(x,y).getType()==Type.TRAIL_HEAD)
- //return false;
- MazeCell currentCell = getCell(x,y);
- currentCell.setType(Type.TRAIL);
- setCell(currentCell);
- super.paint(super.getGraphics());
- GUI.pause();
- if(getPath(x-1,y)==true)
- return true;
- if(getPath(x,y+1)==true)
- return true;
- if(getPath(x+1,y)==true)
- return true;
- if(getPath(x,y-1)==true)
- return true;
- currentCell = getCell(x,y);
- currentCell.setType(Type.WALL);
- setCell(currentCell);
- super.paint(super.getGraphics());
- GUI.pause();
- return false;
- }
- /*
- private String getPath(MazeCell start)
- {
- int col=start.getColumn();
- int row=start.getRow();
- MazeCell westMove=null;
- MazeCell northMove=null;
- MazeCell southMove=null;
- MazeCell eastMove=null;
- try
- {
- westMove=getCell(row,col-1);
- northMove=getCell(row-1,col);
- southMove=getCell(row+1,col);
- eastMove=getCell(row,col+1);
- }
- catch(ArrayIndexOutOfBoundsException e)
- {
- }
- if(westMove.getType()==Type.EXIT)
- {
- return "w";
- }
- else if(northMove.getType()==Type.EXIT)
- {
- return "n";
- }
- else if(southMove.getType()==Type.EXIT)
- {
- return "s";
- }
- else if(eastMove.getType()==Type.EXIT)
- {
- return "e";
- }
- else if(westMove.getType()==Type.EMPTY)
- {
- getPath(westMove);
- }
- else if(northMove.getType()==Type.EMPTY)
- {
- getPath(northMove);
- }
- else if(southMove.getType()==Type.EMPTY)
- {
- getPath(southMove);
- }
- else if(eastMove.getType()==Type.EMPTY)
- {
- getPath(eastMove);
- }
- return null;
- }
- */
- public void saveToFile (String fileName)
- {
- ArrayList<String> saveFile = new ArrayList();
- PrintWriter outputFile;
- String outputLine;
- for (int row = 0; row < getRows(); row++)
- {
- outputLine="";
- for (int column = 0; column < getColumns(); column++)
- {
- Character c;
- c=getCell(row,column).getTypeChar();
- outputLine=outputLine.concat(c.toString());
- }
- saveFile.add(row,outputLine);
- }
- try
- {
- outputFile= new PrintWriter(fileName);
- for(String s:saveFile)
- {
- outputFile.println(s);
- }
- outputFile.close();
- }
- catch(IOException e)
- {
- System.err.println(e);
- }
- System.out.println("Maze Saved, now closeing game.");
- //System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement