SHARE
TWEET

Untitled

a guest Mar 25th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.*;
  2. public class Assignment1pt2 {
  3.     public static final byte LENGTH = 7;
  4.     public static final byte WIDTH = 11;
  5.  
  6.     public static void main(String[] args) throws IOException, InterruptedException{
  7.         byte pointX,pointY;
  8.         boolean flag;
  9.         String anyString;
  10.         char grid[][]=new char[LENGTH][WIDTH];
  11.         anyString = commandCenter();
  12.  
  13.         while(!anyString.equals("stop")) {
  14.             grid=maze();
  15.             pointX=column();
  16.             pointY=row();
  17.             flag = findExit(grid, pointY-1, pointX-1);
  18.             anyString = checkFlag(flag, grid);
  19.  
  20.         }
  21.  
  22.     }
  23.  
  24.     public static String checkFlag(boolean flag, char[][]grid) throws IOException,InterruptedException  {
  25.         String input;
  26.         if(flag==true) {
  27.             System.out.println("Finding optimal route...");
  28.             Thread.sleep(1000);
  29.             outputMaze(grid);
  30.             System.out.println("The path to exit has been found, enter [yes] to continue, [stop] to end");
  31.             input= input();
  32.             return input;
  33.         }
  34.         else {
  35.             outputMaze(grid);
  36.             System.out.println("Prgram was unable to find exit. Would you like to try again? Enter [yes] to continue, [stop] to end");
  37.             input = input();
  38.             return input;
  39.         }
  40.     }
  41.  
  42.     public static String input() throws IOException{
  43.         BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
  44.         String input;
  45.         input=br.readLine();
  46.         return input;
  47.     }
  48.     public static String commandCenter() throws IOException, InterruptedException{
  49.         String word;
  50.         System.out.println("============================================================================================");
  51.         System.out.println("This program finds the exit of a maze, with the user giving the starting point");
  52.         System.out.println("In this maze '.' represent a path and 'B' represent a barrier");
  53.         System.out.println("If you wish to proceed type [yes]");
  54.         System.out.println("If you do not wish to run the program type [stop]");
  55.         System.out.println("============================================================================================");
  56.         while (true) {
  57.             word=input();
  58.             if (word.equalsIgnoreCase("stop")) {
  59.                 return word;
  60.             }
  61.             if (word.equalsIgnoreCase("yes")) {
  62.                 System.out.println("Lets Begin");
  63.                 System.out.println("Loading maze...");
  64.                 Thread.sleep(1000);
  65.                 return word;
  66.             }
  67.             else {
  68.                 System.out.println("Invalid, please try agian");
  69.             }
  70.         }
  71.  
  72.     }
  73.  
  74.     public static boolean findExit(char grid[][], int y, int x) {
  75.         boolean flag=false;
  76.         char right = grid[y][x+1];
  77.         char left = grid[y][x-1];
  78.         char up = grid[y+1][x+1];
  79.         char down = grid[y-1][y];
  80.  
  81.         System.out.println(grid[y][x]);
  82.         if (x>=0||x<WIDTH||y>=0||y<LENGTH) {
  83.             if (right == 'B' || left == 'B' || up == 'B' || down == 'B') {
  84.                 grid[y][x]='-';
  85.             }
  86.  
  87.             if (right=='.') {
  88.                 grid[y][x]='-';
  89.                 findExit(grid,y,x+1);
  90.             }
  91.             if (left=='.') {
  92.                 grid[y][x] = '-';
  93.                 findExit(grid,y,x-1);
  94.             }
  95.             if (up=='.') {
  96.                 grid[y][x]='-';
  97.                 findExit(grid,y+1,x);
  98.             }
  99.             if (down=='.') {
  100.                 grid[y][x]='-';
  101.                 findExit(grid,y-1,x);
  102.             }
  103.             if (right == 'X' || left == 'X' || up == 'X' || down == 'X') {
  104.                 flag=true;
  105.                 grid[y][x]='*';
  106.             }
  107.         }
  108.  
  109.         return flag;
  110.  
  111.     }
  112.  
  113.  
  114.     public static byte column() throws IOException{
  115.         byte x;
  116.         System.out.println("Please input the column number of the point you wish to start at");
  117.         while (true) {
  118.             x = selectPoint();
  119.             if (x<=WIDTH)
  120.                 return x;
  121.             else
  122.                 System.out.println("The column number is invalid, please try again");
  123.         }
  124.     }
  125.  
  126.     public static byte row() throws IOException{
  127.         byte y;
  128.         System.out.println("Please input the row number of the point you wish to start at");
  129.         while (true) {
  130.             y = selectPoint();
  131.             if (y<=LENGTH)
  132.                 return y;
  133.             else
  134.                 System.out.println("The row number is invalid, please try again");
  135.         }
  136.     }
  137.  
  138.     public static byte selectPoint() throws IOException{
  139.         String temp;
  140.         byte num;
  141.         while (true) {
  142.             try {
  143.                 temp = input();
  144.                 num = Byte.parseByte(temp);
  145.                 break;
  146.             }
  147.             catch (Exception e){
  148.                 System.out.println("Input not accpeted, please try again");
  149.             }
  150.         }
  151.         return num;
  152.     }
  153.  
  154.     public static char[][] maze() throws IOException{
  155.         BufferedReader input=new BufferedReader (new FileReader("D:\\Java\\Maze.txt"));
  156.         String line;
  157.  
  158.  
  159.         char grid[][]=new char[LENGTH][WIDTH];
  160.         for(byte row=0;row<LENGTH;row++) {
  161.             line=input.readLine();
  162.             for(byte col=0;col<WIDTH;col++) {
  163.                 grid[row][col]= line.charAt(col);
  164.             }
  165.         }
  166.         outputMaze(grid);
  167.         return grid;
  168.     }
  169.  
  170.     public static void outputMaze(char[][]grid) {
  171.         System.out.println(" 12345678911");
  172.         System.out.println("          01");
  173.         byte rows=1;
  174.  
  175.         for(byte row=0;row<LENGTH;row++){
  176.             System.out.print(rows);
  177.             rows++;
  178.             for(byte col=0;col<WIDTH;col++){
  179.                 System.out.print(grid[row][col]);
  180.             }
  181.             System.out.println();
  182.  
  183.         }
  184.     }
  185.  
  186. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top