Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- public class Assignment1pt2 {
- public static final byte LENGTH = 7;
- public static final byte WIDTH = 11;
- public static void main(String[] args) throws IOException, InterruptedException{
- byte pointX,pointY;
- boolean flag;
- String anyString;
- char grid[][]=new char[LENGTH][WIDTH];
- anyString = commandCenter();
- while(!anyString.equals("stop")) {
- grid=maze();
- pointX=column();
- pointY=row();
- flag = findExit(grid, pointY-1, pointX-1);
- anyString = checkFlag(flag, grid);
- }
- }
- public static String checkFlag(boolean flag, char[][]grid) throws IOException,InterruptedException {
- String input;
- if(flag==true) {
- System.out.println("Finding optimal route...");
- Thread.sleep(1000);
- outputMaze(grid);
- System.out.println("The path to exit has been found, enter [yes] to continue, [stop] to end");
- input= input();
- return input;
- }
- else {
- outputMaze(grid);
- System.out.println("Prgram was unable to find exit. Would you like to try again? Enter [yes] to continue, [stop] to end");
- input = input();
- return input;
- }
- }
- public static String input() throws IOException{
- BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
- String input;
- input=br.readLine();
- return input;
- }
- public static String commandCenter() throws IOException, InterruptedException{
- String word;
- System.out.println("============================================================================================");
- System.out.println("This program finds the exit of a maze, with the user giving the starting point");
- System.out.println("In this maze '.' represent a path and 'B' represent a barrier");
- System.out.println("If you wish to proceed type [yes]");
- System.out.println("If you do not wish to run the program type [stop]");
- System.out.println("============================================================================================");
- while (true) {
- word=input();
- if (word.equalsIgnoreCase("stop")) {
- return word;
- }
- if (word.equalsIgnoreCase("yes")) {
- System.out.println("Lets Begin");
- System.out.println("Loading maze...");
- Thread.sleep(1000);
- return word;
- }
- else {
- System.out.println("Invalid, please try agian");
- }
- }
- }
- public static boolean findExit(char grid[][], int y, int x) {
- boolean flag=false;
- char right = grid[y][x+1];
- char left = grid[y][x-1];
- char up = grid[y+1][x+1];
- char down = grid[y-1][y];
- System.out.println(grid[y][x]);
- if (x>=0||x<WIDTH||y>=0||y<LENGTH) {
- if (right == 'B' || left == 'B' || up == 'B' || down == 'B') {
- grid[y][x]='-';
- }
- if (right=='.') {
- grid[y][x]='-';
- findExit(grid,y,x+1);
- }
- if (left=='.') {
- grid[y][x] = '-';
- findExit(grid,y,x-1);
- }
- if (up=='.') {
- grid[y][x]='-';
- findExit(grid,y+1,x);
- }
- if (down=='.') {
- grid[y][x]='-';
- findExit(grid,y-1,x);
- }
- if (right == 'X' || left == 'X' || up == 'X' || down == 'X') {
- flag=true;
- grid[y][x]='*';
- }
- }
- return flag;
- }
- public static byte column() throws IOException{
- byte x;
- System.out.println("Please input the column number of the point you wish to start at");
- while (true) {
- x = selectPoint();
- if (x<=WIDTH)
- return x;
- else
- System.out.println("The column number is invalid, please try again");
- }
- }
- public static byte row() throws IOException{
- byte y;
- System.out.println("Please input the row number of the point you wish to start at");
- while (true) {
- y = selectPoint();
- if (y<=LENGTH)
- return y;
- else
- System.out.println("The row number is invalid, please try again");
- }
- }
- public static byte selectPoint() throws IOException{
- String temp;
- byte num;
- while (true) {
- try {
- temp = input();
- num = Byte.parseByte(temp);
- break;
- }
- catch (Exception e){
- System.out.println("Input not accpeted, please try again");
- }
- }
- return num;
- }
- public static char[][] maze() throws IOException{
- BufferedReader input=new BufferedReader (new FileReader("D:\\Java\\Maze.txt"));
- String line;
- char grid[][]=new char[LENGTH][WIDTH];
- for(byte row=0;row<LENGTH;row++) {
- line=input.readLine();
- for(byte col=0;col<WIDTH;col++) {
- grid[row][col]= line.charAt(col);
- }
- }
- outputMaze(grid);
- return grid;
- }
- public static void outputMaze(char[][]grid) {
- System.out.println(" 12345678911");
- System.out.println(" 01");
- byte rows=1;
- for(byte row=0;row<LENGTH;row++){
- System.out.print(rows);
- rows++;
- for(byte col=0;col<WIDTH;col++){
- System.out.print(grid[row][col]);
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement