public boolean visitedChar(int i, int j)
{
boolean[][] visited = {
{false,false,false,false},
{false,false,false,false},
{false,false,false,false},
{false,false,false,false}
};
visited[i][j] = true;
return visited[i][j];
}
Visited or not. Boggle
Page 1 of 113 Replies - 236 Views - Last Post: 12 July 2012 - 01:03 PM
#1
Visited or not. Boggle
Posted 12 July 2012 - 12:00 PM
So Im trying to make a Boolean 2d array that corresponds to my 4x4 boggle board. So that I can say if I have visited the specific coordinate. heres what I have tried. Is this wrong?
Replies To: Visited or not. Boggle
#2
Re: Visited or not. Boggle
Posted 12 July 2012 - 12:05 PM
No. The array is a local variable here, and will be garbage collected at the end of the method. Store it as an instance field instead. Also, to initialize it more cleanly, just do:
And in your method, you just need to update the element to true. I'm not sure why your method needs to return a value, though.
boolean[][] grid = new boolean[4][4];
And in your method, you just need to update the element to true. I'm not sure why your method needs to return a value, though.
#3
Re: Visited or not. Boggle
Posted 12 July 2012 - 12:05 PM
There is pretty much no purpose in that method at all. The boolean[][] is local to that method so changes to it are not going to be persistent with further calls and uses. At the moment all the method does is return true if i and j are in the bounds of the 2d array.
You should consider making visited a private field of a BoggleBoard class. Then your visitedChar should probably only return the current value of the index, and not change it. Either way you should rename your method as its a bit ambiguous.
Edit - ninja'd
You should consider making visited a private field of a BoggleBoard class. Then your visitedChar should probably only return the current value of the index, and not change it. Either way you should rename your method as its a bit ambiguous.
Edit - ninja'd
This post has been edited by Ryano121: 12 July 2012 - 12:06 PM
#4
Re: Visited or not. Boggle
Posted 12 July 2012 - 12:06 PM
Well, that surely looks like a 2D boolean array. Trouble is, because you initialize it each time the method is called, it's never going to have any useful information in it.
But that's not such a big trouble, because if you do this
you're always going to return exactly that you've set visited[i][j] to - so you could save a few lines, and simply do this:
And have the same effect.
EDIT: Yow! double-ninja'd.
But that's not such a big trouble, because if you do this
visited[i][j] = true; return visited[i][j];
you're always going to return exactly that you've set visited[i][j] to - so you could save a few lines, and simply do this:
public boolean visitedChar(int i, int j)
{
return true
}
And have the same effect.
EDIT: Yow! double-ninja'd.
This post has been edited by jon.kiparsky: 12 July 2012 - 12:07 PM
#5
Re: Visited or not. Boggle
Posted 12 July 2012 - 12:12 PM
Yeah idk what the hell I was thinking. I fixed it
#6
Re: Visited or not. Boggle
Posted 12 July 2012 - 12:22 PM
How would I go about resetting this?
private boolean[][] visited = {
{false,false,false,false},
{false,false,false,false},
{false,false,false,false},
{false,false,false,false}
};
public void setVisited(int i, int j)
{
visited[i][j] = true;
}
public boolean isVisited(int i, int j)
{
return visited[i][j];
}
public void resetVisited()
{
}
#7
Re: Visited or not. Boggle
Posted 12 July 2012 - 12:23 PM
Loop through all the elements and set each element to false.
#8
Re: Visited or not. Boggle
Posted 12 July 2012 - 12:24 PM
All-right. Thats what I thought. Thanks.
#9
Re: Visited or not. Boggle
Posted 12 July 2012 - 12:33 PM
Two ways to do it.
You could do it like mac says, and visit each element to set them to false. Or you could assign it to an array of false, the way you did in your original post. Either will work, as long as you refer to the visitted array that you set up at the top of the class.
The difference here is that here you're referring to a field, which is declared in the class and is persistent through the life of the class. Before, you were referring to a local variable, which exists just as long as the method runs and vanishes without a trace afterward. When you're referring to the field, you can reassign it to point to something else or overwrite its values, and the effect will last. When you declare a variable in a method, none of the changes you make to it are permanent.
You could do it like mac says, and visit each element to set them to false. Or you could assign it to an array of false, the way you did in your original post. Either will work, as long as you refer to the visitted array that you set up at the top of the class.
The difference here is that here you're referring to a field, which is declared in the class and is persistent through the life of the class. Before, you were referring to a local variable, which exists just as long as the method runs and vanishes without a trace afterward. When you're referring to the field, you can reassign it to point to something else or overwrite its values, and the effect will last. When you declare a variable in a method, none of the changes you make to it are permanent.
#10
Re: Visited or not. Boggle
Posted 12 July 2012 - 12:44 PM
Yeah its not sticking. If I just do this. have played around and cant seem to figure out why. I was trying to reassign like you mentioned previously. Why isnt this working? Im sure you explained and im just missing it.
public void resetVisited()
{
for(int i = 0; i<4;i++)
{
for(int j= 0; j<4;j++)
{
if(visited[i][j] = true)
{
visited[i][j] = false;
}
}
}
#11
Re: Visited or not. Boggle
Posted 12 July 2012 - 12:50 PM
MrOutsider, on 12 July 2012 - 02:44 PM, said:
Yeah its not sticking. If I just do this. have played around and cant seem to figure out why. I was trying to reassign like you mentioned previously. Why isnt this working? Im sure you explained and im just missing it.
if(visited[i][j] = true)
Well, for comparison, you want to use ==. = is assignment. However, this should return true in either case.
Why are you checking the value, though? Why not just set it to false? Don't worry - it won't break if it's already false when you set it to false.
#12
Re: Visited or not. Boggle
Posted 12 July 2012 - 12:56 PM
Yeah I know. I just threw that in there while I was trying to get it to work. Still dont know why. I tried adding a setter-method then using that to run through each element and set it false. Still no go. It seems like its treating my array as a local variable.
This post has been edited by MrOutsider: 12 July 2012 - 12:57 PM
#13
Re: Visited or not. Boggle
Posted 12 July 2012 - 12:57 PM
FYI, I just ran your code, and your reset works fine for me. What's the problem you're having?
#14
Re: Visited or not. Boggle
Posted 12 July 2012 - 01:03 PM
Nevermind. Its fixed now. It was a problem in my other class. Thanks!
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote







|