13 Replies - 236 Views - Last Post: 12 July 2012 - 01:03 PM Rate Topic: -----

#1 MrOutsider  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 06-March 12

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?

	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];
		}
   


Is This A Good Question/Topic? 0
  • +

Replies To: Visited or not. Boggle

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9044
  • View blog
  • Posts: 33,555
  • Joined: 27-December 08

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:
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.
Was This Post Helpful? 1
  • +
  • -

#3 Ryano121  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1055
  • View blog
  • Posts: 2,235
  • Joined: 30-January 11

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

This post has been edited by Ryano121: 12 July 2012 - 12:06 PM

Was This Post Helpful? 2
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5440
  • View blog
  • Posts: 8,760
  • Joined: 19-March 11

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
	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

Was This Post Helpful? 2
  • +
  • -

#5 MrOutsider  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 06-March 12

Re: Visited or not. Boggle

Posted 12 July 2012 - 12:12 PM

Yeah idk what the hell I was thinking. I fixed it :)
Was This Post Helpful? 0
  • +
  • -

#6 MrOutsider  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 06-March 12

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()
		{
		
		}


Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9044
  • View blog
  • Posts: 33,555
  • Joined: 27-December 08

Re: Visited or not. Boggle

Posted 12 July 2012 - 12:23 PM

Loop through all the elements and set each element to false.
Was This Post Helpful? 1
  • +
  • -

#8 MrOutsider  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 06-March 12

Re: Visited or not. Boggle

Posted 12 July 2012 - 12:24 PM

All-right. Thats what I thought. Thanks.
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5440
  • View blog
  • Posts: 8,760
  • Joined: 19-March 11

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.
Was This Post Helpful? 0
  • +
  • -

#10 MrOutsider  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 06-March 12

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;
               }
            }
         }
      

Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5440
  • View blog
  • Posts: 8,760
  • Joined: 19-March 11

Re: Visited or not. Boggle

Posted 12 July 2012 - 12:50 PM

View PostMrOutsider, 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. :)
Was This Post Helpful? 1
  • +
  • -

#12 MrOutsider  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 06-March 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

Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5440
  • View blog
  • Posts: 8,760
  • Joined: 19-March 11

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?
Was This Post Helpful? 1
  • +
  • -

#14 MrOutsider  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 06-March 12

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!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1