Courses‎ > ‎APCS - Term 1‎ > ‎Konstantinovich‎ > ‎

2019-02-27 Maze

posted Feb 27, 2019, 5:52 AM by Konstantinovich Samuel   [ updated Feb 28, 2019, 8:38 AM ]
Deadline: Monday March 4th 8am

Lab04 :  Git repo MKS22X-Maze

Maze.java

import java.util.*;
import java.io.*;
public class Maze{

    private char[][]maze;
    private boolean animate;//false by default

/*Constructor loads a maze text file, and sets animate to false by default.
When the file is not found then:
throw a FileNotFoundException

You may assume the file contains a rectangular ascii maze, made with the following 4 characters:
'#' - Walls - locations that cannot be moved onto
' ' - Empty Space - locations that can be moved onto
'E' - the location of the goal (exactly 1 per file)
'S' - the location of the start(exactly 1 per file)

You ma also assume the maze has a border of '#' around the edges.
So you don't have to check for out of bounds!
*/

    public Maze(String filename) throws FileNotFoundException{
        //COMPLETE CONSTRUCTOR
    }
    
    private void wait(int millis){
         try {
             Thread.sleep(millis);
         }
         catch (InterruptedException e) {
         }
     }

    public void setAnimate(boolean b){
        animate = b;
    }

    public void clearTerminal(){
        //erase terminal, go to top left of screen.
        System.out.println("\033[2J\033[1;1H");
    }





   /*Return the string that represents the maze.
     It should look like the text file with some characters replaced.
    */
    public String toString(){
            return "WRITE THIS METHOD";
    }


    
/*Wrapper Solve Function returns the helper function
      Note the helper function has the same name, but different parameters.
      Since the constructor exits when the file is not found or is missing an E or S, we can assume it exists.
    */
    public int solve(){
            //find the location of the S. 

            //erase the S

            //and start solving at the location of the s.
            //return solve(???,???);
    }

    /*
      Recursive Solve function:

      A solved maze has a path marked with '@' from S to E.

      Returns the number of @ symbols from S to E when the maze is solved,
      Returns -1 when the maze has no solution.

Postcondition:
        The S is replaced with '@' but the 'E' is not.
        All visited spots that were not part of the solution are changed to '.'
        All visited spots that are part of the solution are changed to '@'
    */
    private int solve(int row, int col){ //you can add more parameters since this is private

        //automatic animation! You are welcome.
        if(animate){
            clearTerminal();
System.out.println(this);
            wait(20);
        }

        //COMPLETE SOLVE
        return -1; //so it compiles
    }

}

Driver.java
(just a sample of how to use it, you can improve
this to include args with the file name)

public class Driver{
    public static void main(String[]args){   
      String filename = "data1.dat";
      try{
        Maze f;
        f = new Maze(filename);//true animates the maze.
        
        f.setAnimate(true);
        f.solve();
        System.out.println(f);
      }catch(FileNotFoundException e){
        System.out.println("Invalid filename: "+filename);
      }
    }
}

data1.dat
#######
#     #
# ### #
# #E  #
# ### #
#S    #
#######

data2.dat

#########################
#S          #   # #     #
# # ####### # # # ### ###
# # #   #     # #       #
# # # # ############# # #
# #   # #             # #
# ####### # ### ####### #
#   #     # # # # #     #
# # # ##### # # # #######
# # # # # # # # #       #
# ### # # # # ### ##### #
# #     # #     #     # #
# ### ### # ##### ### # #
#       #         #   #E#
#########################

data3.dat

#####################################
#S             #                    #
# ################################# #
# #                                 #
# ############### ############# ### #
# #                           #   # #
# # ##################### ### # ### #
# # #                 # #   # #   # #
# ######### ######### # # ### # ### #
# #                 # # # #   # # # #
# # ################# # ##### # # # #
# #                 # #       # # # #
#   ######### # ##### ####### ### # #
# # #     # # # #     #       #   # #
# ####### # # # # ################# #
# #     # # # # #         # #       #
# # ### # # # # # ######### ####### #
# # #   # # # # #         #         #
# # ##### # ##### ######### # ##### #
# #               #       # # #     #
# ############### # ####### # # ### #
#               # #         # #   # #
################# ########### ### # #
#                 #           #   # #
# ################# ###########   # #
# #   #   #   #   # # # #         # #
# # # # # # # # # # # # ### ####### #
#   #   #   #   #   #             #E#
#####################################
Comments