// JP opened flex table

Click to See Complete Forum and Search --> : I need help with my project


-EquinoX-
March 24th, 2007, 02:23 PM
I am asked to implement this algorithm to the method findTheExit, but somehow I don't know where I made a mistake and need some suggestions here. The algorithm is:



public boolean findTheExit(int row, int column) {
boolean escaped = false;
if current row and column is a possibility (not blocked and not tried) {
set current location to TRIED;
if current location is on the border, the mover is out so
set escaped = true
else {
let escaped = (RP1) success of escaping using the row below

if still not escaped
let escaped = (RP2) success of escaping using the column to the right
if still not escaped
let escaped = (RP3) success of escaping using the row above
if still not escaped
let escaped = (RP4)success of escaping using the column to the left
}
if the mover was known to escape (escaped is true) during this current method
let current location = PART_OF_PATH;
}


and my code is:


public boolean findTheExit(int row, int col) {
char[][] array = this.getArray();
boolean escaped = false;
if (possible(row, col))
array[row][col] = '.';
if (checkBorder(row, col))
escaped = true;
else {
escaped = findTheExit(row+1, col);
if (escaped == false)
escaped = findTheExit(row, col+1);
else if (escaped == false)
escaped = findTheExit(row-1, col);
else if (escaped == false)
escaped = findTheExit(row, col-1);
}
if (escaped == true)
array[row][col] = 'O';

return escaped;
}


somehow it worked for
+++++++
+S + +
+ +
+ + + +
+++++ +

but when I tested it on other maze it doesn't work, as in the maze:

http://www.cs.arizona.edu/%7Emercer/BookCode/courseC

it won't go to row 1 column 5. this is really weird and I don't know where I was wrong this time

my original task/assignment is located at:
http://www.cs.arizona.edu/classes/cs127b/spring07/projects/8.html

Thanks for those who are willing to help

petes1234
March 24th, 2007, 11:03 PM
Can you show us the rest of your code? Also, why are you getting a new array with each recursion? Would it be better to continue to use one course array for the whole shebang?

-EquinoX-
March 25th, 2007, 01:01 PM
the whole code is:


import java.io.File;
import java.util.Scanner;

public class ObstacleCourse implements ObstacleCourseInterface {

private String file = "";

public ObstacleCourse(String filename) {
file = filename;
}

public boolean findTheExit(int row, int col) {
char[][] array = this.getArray();
boolean escaped = false;
System.out.println("row" + " " + row + " " + "col" + " " + col);
System.out.println(array[row][col]);
System.out.println("escaped" + " " + escaped);
if (possible(row, col)){
System.out.println("possible:" + " " + possible(row,col));
array[row][col] = '.';
if (checkBorder(row, col)){
//System.out.println("check border:" + " " + checkBorder(row,col));
escaped = true;
}
else {
//System.out.println();
escaped = findTheExit(row+1, col);
if (escaped == false)
escaped = findTheExit(row, col+1);
else if (escaped == false)
escaped = findTheExit(row-1, col);
else if (escaped == false)
escaped = findTheExit(row, col-1);
}
if (escaped == true)
array[row][col] = 'O';
}
return escaped;
}

public char[][] getArray() {
char[][] result = null;
try {
Scanner s = new Scanner(new File(file));
int row = 0;
int col = 0;

row = s.nextInt();
col = s.nextInt();

String x = "";
result = new char[row][col];
s.nextLine();

for (int i = 0; i < result.length; i++) {
x = s.nextLine();
for (int j = 0; j < result[i].length; j++) {
result[i][j] = x.charAt(j);
}
}
} catch (Exception e) {
}
return result;
}

public int getRow(){
int row = 0;
try{
Scanner s = new Scanner(new File(file));
row = s.nextInt();
} catch (Exception e){
}
return row;
}

public int getColumns(){
int column = 0;
try{
Scanner s = new Scanner(new File(file));
column = s.nextInt();
column = s.nextInt();
} catch (Exception e){
}
return column;
}

public int getStartColumn() {
char[][] result = this.getArray();
int columns = -1;
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[i].length; j++) {
if (result[i][j] == 'S')
columns = j;
}
}
return columns;
}

public int getStartRow() {
char[][] result = this.getArray();
int row = -1;
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[i].length; j++) {
if (result[i][j] == 'S')
row = i;
}
}
return row;
}

public boolean possible(int row, int col) {
boolean result = false;
char[][] array = this.getArray();
if (array[row][col] != '+' && array[row][col] != '.')
result = true;
return result;
}


public String toString() {
String result = "";
try {
Scanner s = new Scanner(new File(file));
s.nextLine();
while (s.hasNextLine())
result += s.nextLine() + "\n";
} catch (Exception e) {
}
return result;
}


private boolean checkBorder(int row, int col) {
char[][] array = this.getArray();
boolean result = false;
int checkRow = 0;
int checkColumns = 0;
try {
Scanner s = new Scanner(new File(file));
checkRow = s.nextInt();
checkColumns = s.nextInt();
} catch (Exception e) {
}
if ((row + 1 == checkRow || col + 1 == checkColumns) && array[row][col] != '+')
result = true;
return result;
}
}

petes1234
March 25th, 2007, 06:35 PM
I think that you may need to discuss this w/ your instructor. I have a feeling that you are supposed to read the input file once and store values internally including having a private internal array representation of the course that is supposed to be output on a call to getArray; likewise a private int to represent the start row and start column that are obtained from your class by your getter routines. I don't think that you need to keep scanning in from the input file to get these results.

-EquinoX-
March 25th, 2007, 06:55 PM
yes that is technically correct and I can do that. but now what I am asking is only the findTheExit() method. Where did I failed to get this right according to the algorithm??

petes1234
March 25th, 2007, 08:02 PM
It may matter. If the getArray is not correct and it returns the original array and not the updated one, you will never be able to know what was tried previously.

-EquinoX-
March 25th, 2007, 08:28 PM
what do you mean that the getArray is not correct? I already tested it and it returns the maze as it's supposed to be. So how would I fix the getArray to be correct? so do you think that my findTheExit() method is already correct?

petes1234
March 25th, 2007, 09:55 PM
Does your getArray routine return the original maze or the maze w/ "." dots placed in tried locations? If the latter, then it's working ok.

-EquinoX-
March 25th, 2007, 10:19 PM
yeah I consult this with my instructor and in fact my current code doesn't update the array everytime the '.' is added. how do I do this? should I modify the findTheExit() method only or what method should I change? Thanks though for pointing that out

petes1234
March 25th, 2007, 11:12 PM
The answer is there in your code, but really, you have to put more effort into this. You're taking the course, not us, and the more effort you put into this, the more you will get out of it in the long term.

-EquinoX-
March 25th, 2007, 11:14 PM
okay so first of all do I just have to create a new private instance variable for the array in my class?? should I only read the file once??

-EquinoX-
March 25th, 2007, 11:20 PM
I've been spending on almost 2 days working on this project. Sorry if you think that I may be a little stupid, I am a native speaker of english and it takes time for me to understand things. You mentioned earlier above that I should only read the file once. How do I do that?

petes1234
March 25th, 2007, 11:26 PM
No, I have never said nor meant to imply that you are stupid. Inexperienced, maybe, and there is nothing wrong with that. The best way for you to learn though is to recognize the problem (there's one in getting a proper array above) and hammering away at the problem yourself. The more effort you put into this class, the more you will get out of it (sorry to repeat myself). I think you are smart enough to figure this out, now please prove me right!

-EquinoX-
March 25th, 2007, 11:29 PM
I don't know where I should start. I know the problem is that I didn't update the array when ever I change it. So how??? Please just tell me this one.. I have no other idea to do this. Should I read the file only once?

dlorde
March 26th, 2007, 06:30 AM
Should I read the file only once?Consider what the alternatives are.

You can read the file over and over, reading the same thing each time, but what happens to your markers? and why keep reading the same thing over and over?

You can read the file over and over, writing it back out every time it changes - but do you want to change the original? What happens if you make a mistake? and is this the most efficient way?

You can read the file once, keeping the original unchanged. What's wrong with that? Once you've read the information from the file, why read it again?

You might also consider that you don't necessarily have to put the markers physically into the maze, you could keep markers and their positions in the maze separate from the maze itself... just a suggestion - it might not be the best solution.

The key to performance is elegance, not batallions of special cases...
J. Bently & D. McIlroy

//JP added flex table