Trevor Arjeski
Trevor Arjeski

Reputation: 2168

How can I make this stopping criteria effective?

I dont know why this stopping criteria keeps failing but I believe it has something to do with the logical or (||). I debugged and it doesnt seem to stop where I want it to, which is on when cp (current position) is on the perimeter of the array.

 if(cp == start || (cp.getColumn() > 0 || cp.getColumn() < maze[0].length-1 || cp.getRow() > 0 || cp.getRow() < maze.length+1)){`

This is a for a maze solving algorithm. Basically, the maze is a 2D array made up of X's for walls. Every cell with an X is a wall and the 'current position' can not access it. The mission of the 'current position' is to get to out of the maze which means it either has to be in the first or last column or first of last row. The algorithm works very well but just doesn't recognize when the current position is at the index of the exit.

I'm sorry for being so vague, I've been working on this for so long...

Upvotes: 1

Views: 130

Answers (5)

djna
djna

Reputation: 55907

I'm not sure you've copied your code correctly

 if(cp == start 
        || (cp.getColumn() > 0 || cp.getColumn() < maze[0].length-1 
        || cp.getRow() > 0 || cp.getRow() < maze.length+1)) {

For simplicity of understanding pretend that maze.length[0]-1 evaluates to, say 3, then consider this bit:

cp.getColumn() > 0 || cp.getColumn() < 3

That will be true for every value of the cp.getColumn(), try say 8, yep, bigger than 0, -99, yep it's less than 3. I'll bet you meant

cp.getColumn() > 0 && cp.getColumn() < 3

this will be true if the column is in the maze. If you want to test for out of the maze:

cp.getColumn() <= 0 || cp.getColumn() >= 3 

(Side note I use <= 3, not just == 3 deliberately, this deals with cases such as move of more than one step.)

If you want to test if it's in the maze you have

 if(cp == start 
        || (
             cp.getColumn() > 0 && cp.getColumn() < columnCount 
             && cp.getRow() > 0 && cp.getRow() < rowCount)) {

if you want to test if it's outside the maze

 if(cp != start 
        && (
             cp.getColumn() <= 0 || cp.getColumn() >= columnCount 
             || cp.getRow() <= 0 || cp.getRow() >= rowCount)) { 

Upvotes: 0

BillThor
BillThor

Reputation: 7576

It appears you are using the start conditions in your OR set. You may want to put an ! (Not) before the first bracket. As written, I think the else clause would be where your stop should occur.

Consider wrapping the processing code in this if statement.

Desk check your logic to see when you exit. Remember that OR stops processing on the first TRUE, and AND stop processing on the first FALSE.

Upvotes: 0

Carl Manaster
Carl Manaster

Reputation: 40336

So, none of the following things is true:

  • cp == start
  • cp.getColumn() > 0
  • cp.getColumn() < maze[0].length - 1
  • cp.getRow() > 0
  • cp.getRow() < maze.length + 1

But you think (at least) one of them should be true. Which one?

Upvotes: 1

FRotthowe
FRotthowe

Reputation: 3662

I con't really see what you're trying to do here, but guessing that 'maze' is a two dimensional array, it should probably be maze.length-1 in the last comparison. However, this would make the if statement evaluate to true when cp is NOT on the perimeter of the array.

Upvotes: 0

J V
J V

Reputation: 11936

Being or statements, that extra set of parentheses are redundant:

if(cp == start || cp.getColumn() > 0 || cp.getColumn() < maze[0].length-1 || cp.getRow() > 0 || cp.getRow() < maze.length+1){

I can't see exactly what you are doing, but I hope this helps at least...

Upvotes: 0

Related Questions