Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.58 KB | None | 0 0
  1.  
  2. /*
  3.  * To change this template, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. package maze;
  8.  
  9.  
  10. import java.io.*;
  11. import java.util.ArrayList;
  12. import maze.MazeCell.Type;
  13.  
  14. /**
  15.  *
  16.  * @author David Shiverdecker
  17.  */
  18. class MazeCrawler extends Maze
  19. {
  20.  
  21.  
  22.     public MazeCrawler(int i, int i0)
  23.     {
  24.         super(i,i0);
  25.     }
  26.    
  27.     public boolean isWin()
  28.     {
  29.         //the move methods watch for a win
  30.         return false;
  31.     }
  32.    
  33.     public boolean isNoMove()
  34.     {
  35.         MazeCell moveNorth;
  36.         MazeCell moveSouth;
  37.         MazeCell moveEast;
  38.         MazeCell moveWest;
  39.  
  40.         try{
  41.             moveNorth = getCell(getTrailHead().getRow()-1,
  42.                 getTrailHead().getColumn());
  43.         }
  44.         catch(ArrayIndexOutOfBoundsException e)
  45.         {
  46.             moveNorth=null;
  47.         }
  48.  
  49.         try{
  50.             moveSouth = getCell(getTrailHead().getRow()+1,
  51.                 getTrailHead().getColumn());
  52.         }
  53.         catch(ArrayIndexOutOfBoundsException e)
  54.         {
  55.             moveSouth=null;
  56.         }
  57.  
  58.         try{
  59.             moveWest = getCell(getTrailHead().getRow(),
  60.                 getTrailHead().getColumn()-1);
  61.         }
  62.         catch(ArrayIndexOutOfBoundsException e)
  63.         {
  64.             moveWest=null;
  65.         }
  66.  
  67.         try{
  68.             moveEast = getCell(getTrailHead().getRow(),
  69.                 getTrailHead().getColumn()+1);
  70.         }
  71.         catch(ArrayIndexOutOfBoundsException e)
  72.         {
  73.             moveEast=null;
  74.         }
  75.  
  76.         if((moveNorth==null||moveNorth.getType()==Type.TRAIL||
  77.                 moveNorth.getType()==Type.WALL)&&(moveSouth==null||
  78.                 moveSouth.getType()==Type.TRAIL||moveSouth.getType()==Type.WALL)
  79.                 && (moveWest==null||moveWest.getType()==Type.TRAIL||
  80.                 moveWest.getType()==Type.WALL)&&(moveEast==null||
  81.                 moveEast.getType()==Type.TRAIL||moveEast.getType()==Type.WALL))
  82.                 {
  83.                     return true;
  84.                 }
  85.                  
  86.         return false;
  87.     }
  88.    
  89.     private void cleanMove(MazeCell moveTo)
  90.     {
  91.         MazeCell moveFrom = getCell(getTrailHead().getRow(),
  92.                 getTrailHead().getColumn());
  93.  
  94.        switch (moveTo.getType())
  95.            {
  96.  
  97.                 case TRAIL:
  98.                 case WALL: break;
  99.  
  100.                 case EMPTY:
  101.                     moveFrom.setType(Type.TRAIL);
  102.                     moveTo.setType(Type.TRAIL_HEAD);
  103.                     setCell(moveFrom);
  104.                     setCell(moveTo); break;
  105.  
  106.                 case EXIT: GUI.win(); break;
  107.  
  108.  
  109.                 case TRAIL_HEAD:
  110.                 default: System.err.println("Invaild Cell exiting maze.");
  111.                          System.exit(1);
  112.            }
  113.  
  114.  
  115.        super.paint(super.getGraphics());
  116.        
  117.        GUI.pause();
  118.     }
  119.  
  120.     public void moveNorth()
  121.     {
  122.        try{
  123.         MazeCell moveTo = getCell(getTrailHead().getRow()-1,
  124.                 getTrailHead().getColumn());
  125.      
  126.         cleanMove(moveTo);
  127.        }catch(ArrayIndexOutOfBoundsException e){}
  128.     }
  129.  
  130.     public void moveSouth()
  131.     {
  132.        try{
  133.            MazeCell moveTo = getCell(getTrailHead().getRow()+1,
  134.                 getTrailHead().getColumn());
  135.        
  136.            cleanMove(moveTo);
  137.        }catch(ArrayIndexOutOfBoundsException e){}
  138.     }
  139.  
  140.     public void moveEast()
  141.     {
  142.        try{
  143.         MazeCell moveTo = getCell(getTrailHead().getRow(),
  144.                 getTrailHead().getColumn()+1);
  145.  
  146.        cleanMove(moveTo);
  147.        }catch(ArrayIndexOutOfBoundsException e){}
  148.     }
  149.  
  150.     public void moveWest()
  151.     {
  152.         try{
  153.         MazeCell moveTo = getCell(getTrailHead().getRow(),
  154.                 getTrailHead().getColumn()-1);
  155.  
  156.         cleanMove(moveTo);
  157.         }catch(ArrayIndexOutOfBoundsException e){}
  158.     }
  159.  
  160.     public void solveMaze()
  161.     {
  162.  
  163.         System.out.println("Solving, Sometimes will not work not sure why");
  164.         if(getPath(getTrailHead() .getRow() ,getTrailHead(). getColumn() ))
  165.         {
  166.             GUI.win();
  167.         }else{
  168.             //GUI.lose();
  169.         }
  170.     }
  171.  
  172.     private boolean getPath(int x,int y)
  173.     {
  174.        
  175.  
  176.         if(x>31||x<0||y>14||y<0)
  177.             return false;
  178.  
  179.         if(getCell(x,y).getType()==Type.EXIT)
  180.             return true;
  181.  
  182.         if(getCell(x,y).getType()==Type.WALL||getCell(x,y).getType()==Type.TRAIL)
  183.             return false;
  184.        
  185.        //if(getCell(x,y).getType()==Type.TRAIL_HEAD)
  186.             //return false;
  187.  
  188.         MazeCell currentCell = getCell(x,y);
  189.         currentCell.setType(Type.TRAIL);
  190.         setCell(currentCell);
  191.         super.paint(super.getGraphics());
  192.  
  193.        GUI.pause();
  194.  
  195.         if(getPath(x-1,y)==true)
  196.             return true;
  197.  
  198.         if(getPath(x,y+1)==true)
  199.             return true;
  200.  
  201.         if(getPath(x+1,y)==true)
  202.             return true;
  203.  
  204.         if(getPath(x,y-1)==true)
  205.             return true;
  206.  
  207.         currentCell = getCell(x,y);
  208.         currentCell.setType(Type.WALL);
  209.         setCell(currentCell);
  210.         super.paint(super.getGraphics());
  211.  
  212.        GUI.pause();
  213.  
  214.         return false;
  215.  
  216.     }
  217. /*
  218.     private String getPath(MazeCell start)
  219.     {
  220.         int col=start.getColumn();
  221.         int row=start.getRow();
  222.        
  223.         MazeCell westMove=null;
  224.         MazeCell northMove=null;
  225.         MazeCell southMove=null;
  226.         MazeCell eastMove=null;
  227.        
  228.         try
  229.         {
  230.             westMove=getCell(row,col-1);
  231.             northMove=getCell(row-1,col);
  232.             southMove=getCell(row+1,col);
  233.             eastMove=getCell(row,col+1);
  234.         }
  235.         catch(ArrayIndexOutOfBoundsException e)
  236.         {
  237.            
  238.         }        
  239.        
  240.        
  241.        
  242.         if(westMove.getType()==Type.EXIT)
  243.         {
  244.             return "w";
  245.         }
  246.         else if(northMove.getType()==Type.EXIT)
  247.         {
  248.             return "n";
  249.         }
  250.         else if(southMove.getType()==Type.EXIT)
  251.         {
  252.             return "s";
  253.         }
  254.         else if(eastMove.getType()==Type.EXIT)
  255.         {
  256.             return "e";
  257.         }
  258.  
  259.         else if(westMove.getType()==Type.EMPTY)
  260.         {
  261.             getPath(westMove);
  262.         }
  263.         else if(northMove.getType()==Type.EMPTY)
  264.         {
  265.             getPath(northMove);
  266.         }
  267.         else if(southMove.getType()==Type.EMPTY)
  268.         {
  269.             getPath(southMove);
  270.         }
  271.         else if(eastMove.getType()==Type.EMPTY)
  272.         {
  273.             getPath(eastMove);
  274.         }
  275.  
  276.         return null;
  277.     }
  278. */
  279.     public void saveToFile (String fileName)
  280.     {
  281.        ArrayList<String> saveFile = new ArrayList();
  282.        PrintWriter outputFile;
  283.        String outputLine;
  284.         for (int row = 0; row < getRows(); row++)
  285.         {
  286.             outputLine="";
  287.             for (int column = 0; column < getColumns(); column++)
  288.             {
  289.                 Character c;
  290.                 c=getCell(row,column).getTypeChar();
  291.                 outputLine=outputLine.concat(c.toString());
  292.             }
  293.             saveFile.add(row,outputLine);
  294.         }
  295.  
  296.         try
  297.         {
  298.             outputFile= new PrintWriter(fileName);
  299.             for(String s:saveFile)
  300.             {
  301.                  
  302.                outputFile.println(s);
  303.  
  304.             }
  305.  
  306.             outputFile.close();
  307.         }
  308.         catch(IOException e)
  309.         {
  310.             System.err.println(e);
  311.         }
  312.              
  313.  
  314.        
  315.         System.out.println("Maze Saved, now closeing game.");
  316.         //System.exit(0);
  317.        
  318.     }
  319.  
  320.        
  321.    
  322.  
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement