J0hnnysmokes's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
Members
Active Posts:
16 (0.02 per day)
Joined:
30-November 12
Profile Views:
143
Last Active:
User is offline Dec 10 2012 08:51 AM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   J0hnnysmokes has not set their status

Posts I've Made

  1. In Topic: Implementing Doubly Linked list

    Posted 10 Dec 2012

    The try, catch isnt my code, it is the test data my professor used to test my code
    He used the try catch to see if any of the methods have failed his test data
    I am trying to figure out how I can fix the ones that failed his test data
  2. In Topic: Traversing a Maze

    Posted 2 Dec 2012

    Ok so I fixed what you said
    public boolean traverse(int row, int column) throws ArrayIndexOutOfBoundsException {
    		
    		boolean done = false;
    		
    		if (valid(row-1, column-1) == true && maze[row-1][column-1] == E) {
    			return true;
    			
    		}
    		else if (valid(row-1, column-1) == false){
    			return false;
    		}
    		else {
    			
    			maze[row-1][column-1] = '+';
    			if ( traverse(row-1, column)){
    				done = true;
    			} else if (traverse(row, column+1) ){
    				done = true;
    			} else if (traverse(row+1, column) ){
    				done = true;
    			} else if (traverse(row, column-1)){
    				return true;
    			} else {
    				maze[row-1][column-1] = ' ';
    				done = false;
    			}
    		}
    		return done;
    	
    	}    
     
    


    However, once again when I input the coordinated
    row: 2, col: 12
    it seems that my traverse method stops working because it is automatically printing that the maze doesnt have a solution
                1         
       1234567890123456789
      +-------------------+
     1|    ###   #      # |1
     2|###   ### # #   ###|2
     3|#####   #     ###  |3
     4|E  #### # ##   #  #|4
     5| # ## # # ####   ##|5
     6|        #  ##  ####|6
     7|## ###  ## ## ###  |7
     8|   #### ## ####### |8
     9| # ## #### ## # # #|9
    10| #         ## ## ##|10
      +-------------------+
                1         
       1234567890123456789
    
    Starting point? row: 2
    		col: 12
                1         
       1234567890123456789
      +-------------------+
     1|    ###   #      # |1
     2|###   ### #S#   ###|2
     3|#####   #     ###  |3
     4|E  #### # ##   #  #|4
     5| # ## # # ####   ##|5
     6|        #  ##  ####|6
     7|## ###  ## ## ###  |7
     8|   #### ## ####### |8
     9| # ## #### ## # # #|9
    10| #         ## ## ##|10
      +-------------------+
                1         
       1234567890123456789
    
    Help, I am trapped!
    
    

    but clearly it should have a solution

    oh i see i fixed the wrong code haha
    hmmm how can I fix this problem?
    would inserting a for loop into my valid method reading the array help?
    because the valid method was suppose to check that the locations are valid and if the moves are valid and within the array
  3. In Topic: Traversing a Maze

    Posted 2 Dec 2012

    Ok so I think i FINALLY got everything down
    but here comes the dreaded debugging part
    I have inputted a different maze text file that has 10*row and 19*columns and I think atleast 2 locations when there will be one empty space surrounded by walls

    Here are my issues
    1) whenever I try to make a starting position at a location that is surrounded by walls my program keeps looping ("illegal starting point try again") where it is suppose to end the traverse method by stating that the maze has no solution from that point therefore it should print ("help I am trapped!")

    2) BIG ISSUE
    for odd reason whenever I try to input the coordinated row:2, col: 12 it gives me a bunch of errors saying ArrayIndexOutOfBoundsException

    I believe it has something to do with my traverse method, the method for storing the user inputted data into my maze, and the validation method. However I am having trouble making printlns on every method to see what is going wrong

    Here is my updated code
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Scanner;
    import java.util.HashMap;
    
    public class Proj71773 {
    
        static int rowSize = 10;
        static int columnSize = 20;
    	
    	static char[][] mazeArray = new char[rowSize][columnSize];
    
    	public static void main(String[] args) throws FileNotFoundException,
    			IOException {
    
    		try {
    
    			File inputFile = new File (args [0]);
    			Scanner readFile = new Scanner(inputFile);
    
    			String lineLn = " ";
    			int row = 0;
    
    			while (readFile.hasNext()) {
    				lineLn = readFile.next();
    				mazeArray[row++] = lineLn.toCharArray();
    			}
    		} catch (FileNotFoundException e) {
    			 //problem with getting file
    			e.printStackTrace();
    		} catch (IOException e) {
    			// problem with the inputstream while reading
    			e.printStackTrace();
    		} finally {
    			// close all files & inpusStreams
    
    		}
    
    		// instantiate your maze class here
    		Maze maze = new Maze(mazeArray);
    		System.out.println(maze);
    		maze.location();
    		System.out.println(maze.toString());
    		
    		boolean solved = maze.traverse(maze.getStartRowNumber(), maze.getStartColumnNumber());
    		
    		if (solved == true) {
    			
    			//EXTRA CREDIT, I WISH TO RECEIVE MORE THAN 15%!!!!
    			maze.recoverStartPos(maze.getStartRowNumber(), maze.getStartColumnNumber());
    			System.out.println(maze.toString());
    			System.out.println("I am free!");
    		} else {
    			System.out.println("Help, I am trapped!");
    		}
    	}
    
    }
    
    class Maze {
    
    	
    	private int rowSize = 10;
    	private int columnSize = 20;
    	private char[][] maze = new char[rowSize][columnSize];
    	private final char E = 'E';
    	
    	private int startRowNumber = 0;
    	private int startColumnNumber = 0;
       
        
        
        
        
    	// constructor with argument char 2-d array from input file
    	public Maze(char[][] maze) {
    		super();
    		this.maze = maze;
    	}
    	
    	// getter/setter for starting row
    	public int getStartRowNumber() {
    		return this.startRowNumber;
    	}
    
    	public void setStartRowNumber(int row) {
    		this.startRowNumber = row;
    	}
    	
    	// getter/setter for starting column
    	public int getStartColumnNumber() {
    		return this.startColumnNumber;
    	}
    
    	public void setStartColumnNumber(int col) {
    		this.startColumnNumber = col;
    	}
    	
    	public void location() {
    
    		Scanner scan = new Scanner(System.in);
    		int i;
    		int j;
    		int maxTry = 10;
    		// set up max try to keep looping in case of invalid try
    		
    
    		System.out.print("Starting point? row: ");
    		i = scan.nextInt();
    
    		System.out.print("		col: ");
    		j = scan.nextInt();
    
    		// set up some loop until getting valid result
    		// if the maximum tried, then kick out
    		for (int tried = 0; tried < maxTry; tried++) {
    			boolean isInValidPoistion = isValidPosition(i, j);
    			if (isInValidPoistion == true) {
    				// if the char is in right position
    				// and value is '0', then it's right
    				if (maze[i-1][j-1] == '0') {
    					// add 'S' char here and return
    					maze[i-1][j-1] = 'S';
    
    					// record starting row & column number here
    					// to start traverse method
    					setStartRowNumber(i);
    					setStartColumnNumber(j);
    					// if it right, get out of looping and skip the rest
    					break;
    				} else if (maze[i-1][j-1] == 'E') {
    					// add filtering logic to catch 'E' char here
    					// because it's valid position
    					System.out
    							.println("Try again - you cannot start at the exit");
    					System.out.print("Starting point? row: ");
    					i = scan.nextInt();
    					System.out.print("		col: ");
    					j = scan.nextInt();
    				} else if (maze[i-1][j-1] == '1'){
    					// it's '1' and other invalid cases
    					// you cannot start if the value is '1'.
    					System.out
    					.println("Illegal starting position, try again");
    					System.out.print("Starting point? row: ");
    					i = scan.nextInt();
    					System.out.print("		col: ");
    					j = scan.nextInt();
    				}
    				
    			} else {
    				System.out
    				.println("Illegal starting position, try again");
    				System.out.print("Starting point? row: ");
    				i = scan.nextInt();
    				System.out.print("		col: ");
    				j = scan.nextInt();
    			}
    			
    		}
    		
    	  }
    	
    	
    	
        
    	// -----------------------------------------------------------------
    	// Attempts to recursively traverse the maze. Inserts special
    	// characters indicating locations that have been tried and that
    	// eventually become part of the solution.
    	// ----------------------------------------------------------------
    	public boolean traverse(int row, int column) throws ArrayIndexOutOfBoundsException {
    		
    		boolean done = false;
    		
    		if (valid(row, column) == true && maze[row-1][column-1] == E) {
    			return true;
    			
    		}
    		else if (valid(row, column) == false){
    			return false;
    		}
    		else {
    			
    			maze[row-1][column-1] = '+';
    			if ( traverse(row-1, column)){
    				done = true;
    			} else if (traverse(row, column+1) ){
    				done = true;
    			} else if (traverse(row+1, column) ){
    				done = true;
    			} else if (traverse(row, column-1)){
    				return true;
    			} else {
    				maze[row-1][column-1] = ' ';
    				done = false;
    			}
    		}
    		return done;
    	
    	}    
     
    	// -----------------------------------------------------------------
    	// Determines if a specific location is valid.
    	// -----------------------------------------------------------------
    	private boolean isValidPosition(int row, int column) {
    		boolean result = false;
    
    		if (row > 0 && row < rowSize && column > 0
    				&& column < columnSize) {
    			// just check here if it's in valid position
    			// location method will handle if it's a valid character
    			result = true;
    		}
    		return result;
    	}
    	
    	
    	
    
    	// -----------------------------------------------------------------
    	// Determines if a specific location is valid.
    	// -----------------------------------------------------------------
    	private boolean valid(int row, int column ) {
    		boolean result = false;
    		if (row > 0 && row <= rowSize && column > 0 && column <= columnSize){
    			
    		if (maze[row-1][column-1] == '0' || maze[row-1][column-1] == 'S' || maze[row-1][column-1] == 'E') {
    			result = true;
    
    			
    	}
    	}
    	   return result;
    	}
    	// -----------------------------------------------------------------
    	// Returns the maze as a string.
    	// -----------------------------------------------------------------
    	public String toString() {
    		StringBuffer titleBuf = new StringBuffer();
    		StringBuffer tagStr = new StringBuffer();
    		tagStr.append("\n");
    
    		int rowCount = 1;
    		int colCount = 0;
    
    		for (int row = 0; row < maze.length; row++) {
    
    			if (row == 0 || row == maze[row].length - 1) {
    				titleBuf.append(titlePadding(maze[row].length));
    				titleBuf.append(lineDraw(maze[row].length));
    			}
    
    			for (int column = 0; column < maze[row].length; column++) {
    
    				// padding row # & pipe symbol in left end
    				if (column == 0) {
    					// padding an empty space for single digit number
    					if (rowCount < 10) {
    						tagStr.append(" ");
    					}
    					// print row count & pipe symbol
    					tagStr.append(rowCount);
    					tagStr.append("|");
    				}
    
    				if (maze[row][column] == 'E') {
    					tagStr.append("E");
    				} else if (maze[row][column] == '1') {
    					tagStr.append("#");
    				} else if (maze[row][column] == 'S') {
    					tagStr.append("S");
    					// for extra credit
    				} else if (maze[row][column] == '+') {
    					tagStr.append("+");
    				} else {
    					tagStr.append(" ");
    				}
    
    				// testing
    				// tagStr.append(maze[row][column]);
    				// padding row # & pipe symbol in right end
    				if (column == maze[row].length - 1) {
    					tagStr.append("|");
    					tagStr.append(rowCount);
    					rowCount++;
    				}
    				colCount = maze[row].length;
    
    			}
    			tagStr.append("\n");
    		}
    
    		titleBuf.append(tagStr);
    		titleBuf.append(lineDraw(colCount));
    		titleBuf.append("\n");
    		titleBuf.append(titlePadding(colCount));
    		return titleBuf.toString();
    	}
    
    	// -----------------------------------------------------------------
    	// Returns the the number title format of string as stringbuffer.
    	// It read the number of column, divided by 10.
    	// If the number is less than 10, print " " string.
    	// if 10 is reached, print 1. To drop after 10, filter out by modular
    	// number.
    	// The second line is mod by 10 to keep 0-9 repeat
    	// -----------------------------------------------------------------
    	private StringBuffer titlePadding(int colSize) {
    		StringBuffer sbuf = new StringBuffer();
    		sbuf.append("   ");
    		for (int i = 1; i <= colSize; i++) {
    			// divide by 10
    			if (i / 10 > 0) {
    				// print only 10 mod number
    				if (i % 10 == 0) {
    					sbuf.append(i / 10);
    				} else {
    					sbuf.append(" ");
    				}
    			} else {
    				sbuf.append(" ");
    			}
    		}
    		sbuf.append("\n");
    		sbuf.append("   ");
    
    		for (int i = 1; i <= colSize; i++) {
    			sbuf.append(i % 10);
    		}
    
    		sbuf.append("\n");
    		return sbuf;
    	}
    
    	// -----------------------------------------------------------------
    	// Returns the line draw as stringbuffer.
    	// The first and last symbol is "+", and others "=".
    	// -----------------------------------------------------------------
    	private StringBuffer lineDraw(int colSize) {
    
    		StringBuffer tbuf = new StringBuffer();
    		int formatSize = colSize + 2;
    		// space padding
    		tbuf.append("  ");
    		for (int i = 0; i < formatSize; i++) {
    			if (i == 0 || i == formatSize - 1) {
    
    				tbuf.append("+");
    			} else {
    				tbuf.append("-");
    			}
    		}
    		return tbuf;
    	}
    	
    	public void recoverStartPos(int row, int col){
    		maze[row-1][col-1] = 'S';
    	}
    
    }
    
    


    here is the new maze input data
    0000111000100000010
    1110001110101000111
    1111100010000011100
    E001111010110001001
    0101101010111100011
    0000000010011001111
    1101110011011011100
    0001111011011111110
    0101101111011010101
    0100000000011011011


    And here is my output when I try certain input locations such as row:1, col:19
              1         
       1234567890123456789
      +-------------------+
     1|    ###   #      # |1
     2|###   ### # #   ###|2
     3|#####   #     ###  |3
     4|E  #### # ##   #  #|4
     5| # ## # # ####   ##|5
     6|        #  ##  ####|6
     7|## ###  ## ## ###  |7
     8|   #### ## ####### |8
     9| # ## #### ## # # #|9
    10| #         ## ## ##|10
      +-------------------+
                1         
       1234567890123456789
    
    Starting point? row: 1
    		col: 19
                1         
       1234567890123456789
      +-------------------+
     1|    ###   #      #S|1
     2|###   ### # #   ###|2
     3|#####   #     ###  |3
     4|E  #### # ##   #  #|4
     5| # ## # # ####   ##|5
     6|        #  ##  ####|6
     7|## ###  ## ## ###  |7
     8|   #### ## ####### |8
     9| # ## #### ## # # #|9
    10| #         ## ## ##|10
      +-------------------+
                1         
       1234567890123456789
    
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 19
    	at Maze.valid(Proj71773.java:237)
    	at Maze.traverse(Proj71773.java:185)
    	at Maze.traverse(Proj71773.java:197)
    	at Proj71773.main(Proj71773.java:54)
    
    
  4. In Topic: Traversing a Maze

    Posted 2 Dec 2012

    Here is an updated version of my traverse method
    public boolean traverse(int row, int column) {
    		done = false;
    		
    		
    		if (maze[row][column] == 'E'){
    			System.out.println("I am free!");
    			done = true;
    		}
    		else if (!valid(row, column)){
    			done = false;
    		}
    		else {
    			maze[row][column] = TRIED;
    		} while (!done){	
    			if (maze[row-1][column] == '0' || maze[row-1][column] == 'E'){
    				row = row-1;
    				column = column;
    				done = traverse(row-1, column);
    			}
    			if (maze[row][column+1] == '0' || maze[row][column+1] == 'E' ){
    				row = row;
    				column = column+1;
    				done = traverse(row, column+1);
    			}
    			if (maze[row+1][column] == '0' || maze[row+1][column] == 'E'){
    				row = row+1;
    				column = column;
    				done = traverse(row+1, column);
    			}
    			if (maze[row][column+1] == '0' || maze[row][column+1] == 'E'){
    				row = row;
    				column = column+1;
    				done = traverse(row, column+1);
    			}
    		}
    		
    	
    		return done;
    	
    	}
    
    


    and my valid method
    	private boolean valid(int row, int column ) {
    		boolean result;
    		if (row > 0 && row < maze.length && column > 0
    				&& column < maze[row].length)  // checks if it is within the maze bounds
    			
    		if (maze[row][column] == TRIED || maze[row][column] == '1') //checks if there is a wall or we have already went there
    			return false;
    		
    	   return true;
    	}
    
    



    However, it is giving me errors
                1         2
       12345678901234567890
      +--------------------+
     1|    ###   #      #  |1
     2|###   ### # #   ####|2
     3|#####   #     ###   |3
     4|E  #### # ##   #  # |4
     5| # ## # # ####   ## |5
     6|        #  ##  #### |6
     7|## ###  ## ## ###   |7
     8|   #### ## ####### #|8
     9| # ## #### ##      #|9
    10| #         ## ## ###|10
      +--------------------+
                1         2
       12345678901234567890
    
    Starting point? row: 1
    		col: 1
                1         2
       12345678901234567890
      +--------------------+
     1|S   ###   #      #  |1
     2|###   ### # #   ####|2
     3|#####   #     ###   |3
     4|E  #### # ##   #  # |4
     5| # ## # # ####   ## |5
     6|        #  ##  #### |6
     7|## ###  ## ## ###   |7
     8|   #### ## ####### #|8
     9| # ## #### ##      #|9
    10| #         ## ## ###|10
      +--------------------+
                1         2
       12345678901234567890
    
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    	at Maze.traverse(Proj71773.java:170)
    	at Maze.traverse(Proj71773.java:183)
    	at Proj71773.main(Proj71773.java:52)
    
    



    any ideas?
  5. In Topic: Traversing a Maze

    Posted 2 Dec 2012

    hmm definitely having trouble with the traverse method
    Yes I am trying to only use the traverse method using recursion but so far I still do not have a clue on how to accurately do it
    Here is what I have so far

    class Maze {
    
    	private char[][] maze;
    	private static final char E = 'E';
    	private int startRowNumber = 0;
    	private int startColumnNumber = 0;
    	private boolean done = false;
    	private static final char PATH = 'P';
    	private static final char TRIED = 'T';
        
        
        
        
        
    	// constructor with argument char 2-d array from input file
    	public Maze(char[][] maze) {
    		super();
    		this.maze = maze;
    	}
    	
    	// getter/setter for starting row
    	public int getStartRowNumber() {
    		return this.startRowNumber;
    	}
    
    	public void setStartRowNumber(int row) {
    		this.startRowNumber = row;
    	}
    	
    	// getter/setter for starting column
    	public int getStartColumnNumber() {
    		return this.startColumnNumber;
    	}
    
    	public void setStartColumnNumber(int column) {
    		this.startColumnNumber = column;
    	}
    	
    	public void location() {
    
    		Scanner scan = new Scanner(System.in);
    		int i;
    		int j;
    		int maxTry = 10;
    		// set up max try to keep looping in case of invalid try
    		
    		int numberAttempted = 0;
    
    		System.out.print("Starting point? row: ");
    		i = scan.nextInt();
    
    		System.out.print("		col: ");
    		j = scan.nextInt();
    
    		// set up some loop until getting valid result
    		// if the maximum tried, then kick out
    		for (int tried = 0; tried < maxTry; tried++) {
    			boolean isInValidPoistion = isValidPosition(i, j);
    			if (isInValidPoistion == true) {
    				// if the char is in right position
    				// and value is '0', then it's right
    				if (maze[i-1][j-1] == '0') {
    					// add 'S' char here and return
    					maze[i-1][j-1] = 'S';
    
    					// record starting row & column number here
    					// to start traverse method
    					setStartRowNumber(i);
    					setStartColumnNumber(j);
    					// if it right, get out of looping and skip the rest
    					break;
    				} else if (maze[i-1][j-1] == 'E') {
    					// add filtering logic to catch 'E' char here
    					// because it's valid position
    					System.out
    							.println("Try again - you cannot start at the exit");
    					System.out.print("Starting point? row: ");
    					i = scan.nextInt();
    					System.out.print("		col: ");
    					j = scan.nextInt();
    				} else if (maze[i-1][j-1] == '1'){
    					// it's '1' and other invalid cases
    					// you cannot start if the value is '1'.
    					System.out
    					.println("Illegal starting position, try again");
    					System.out.print("Starting point? row: ");
    					i = scan.nextInt();
    					System.out.print("		col: ");
    					j = scan.nextInt();
    				}
    			} else {
    				System.out.println("Illegal starting position, try again");
    				System.out.print("Starting point? row: ");
    				i = scan.nextInt();
    				System.out.print("		col: ");
    				j = scan.nextInt();
    			}
    			numberAttempted++;
    		}
    		
    	  }
    	
    
    	// -----------------------------------------------------------------
    	// Attempts to recursively traverse the maze. Inserts special
    	// characters indicating locations that have been tried and that
    	// eventually become part of the solution.
    	// ----------------------------------------------------------------
    	public boolean traverse(int row, int column) {
    		done = false;
    		
    		
    		if (valid(row, column)){
    			if ((row == E) && (column == E)){
    				System.out.println("I am free!");
    				System.out.println(maze + " " + row + "," + column);
    				done = true;
    			}
    			else
    			{
    				if (traverse(row+1, column) ||
    				    traverse(row, column+1) ||
    				    traverse(row-1, column) ||
    				    traverse(row, column-1)){
    					maze[row][column] = PATH;
    					return true;
    				}
    			
    			} 
    	    
    	    } else if (valid(row, column)){
    	    	System.out.println("Help, I am trapped!");
    	    	System.out.println(maze[row][column] + " " + row + "," + column);
    	    	done = true;
    	    }
    	    return done; 
    	}
    
    	// -----------------------------------------------------------------
    	// Determines if a specific location is valid.
    	// -----------------------------------------------------------------
    	private boolean isValidPosition(int row, int column) {
    		boolean result = false;
    
    		if (row >= 0 && row < maze.length && column >= 0
    				&& column < maze[row].length) {
    			// just check here if it's in valid position
    			// location method will handle if it's a valid character
    			result = true;
    		}
    		return result;
    	}
    	
    	
    	
    
    	// -----------------------------------------------------------------
    	// Determines if a specific location is valid.
    	// -----------------------------------------------------------------
    	private boolean valid(int row, int column ) {
    		boolean result;
    		if (row > 0 && row < maze.length && column > 0
    				&& column < maze[row].length)  // checks if it is within the maze bounds
    			
    		if (maze[row][column] == TRIED || maze[row][column] == '1') //checks if there is a wall or we have already went there
    			return false;
    		
    	   return true;
    	}
    
    

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Private

Friends

J0hnnysmokes hasn't added any friends yet.

Comments

J0hnnysmokes has no profile comments yet. Why not say hello?