Results 1 to 13 of 13
Thread: need help explining please.
 03202010, 11:33 PM #1Member
 Join Date
 Mar 2009
 Posts
 23
 Rep Power
 0
need help explining please.
hello there i have an exercise given where i will need to re construct my code using binary search tree.
the problem with me is if someone explains the code to me it makes it easier for me to implement.
i have been given a source code to do so, but there is a little section of the code which i get quite confused with. all i need is explaning, if you don't want to write a whole description, could you comment out each line
thank you for your time and help.
Java Code:for (int y = 1; y < 8; y++) { for (int x = 1; x < 8; x++) { if (btns[x][y].getText().equalsIgnoreCase("R")) { if ((btns[x + 1][y + 1].getText() .equalsIgnoreCase("C"))  (btns[x][y + 1].getText() .equalsIgnoreCase("C"))  (btns[x + 1][y].getText() .equalsIgnoreCase("C"))  (btns[x  1][y  1].getText() .equalsIgnoreCase("C"))) { btns[x + 1][y + 1].setIcon(empty); r = new Random(); int location1 = r.nextInt(4); if (location == 0) { x = 2; y = 2; btns[x][y].setIcon(cat); } if (location == 1) { x = +2; y = 2; btns[x][y].setIcon(cat); } if (location == 2) { x = 2; y = +2 btns[x][y].setIcon(cat); } if (location == 3) { x = +2; y = +2; btns[x][y].setIcon(cat); } break;
Last edited by yasmin k; 03212010 at 12:00 AM.
 03202010, 11:36 PM #2Senior Member
 Join Date
 Mar 2010
 Posts
 953
 Rep Power
 5
How about editing your post and properly indenting the code. Just doing that much will probably help you understand it better, and it will certainly encourage people to want to help you more.
Gary
 03202010, 11:45 PM #3Member
 Join Date
 Mar 2009
 Posts
 23
 Rep Power
 0
done :D
sorry, i didn't know that would be a problem.
 03202010, 11:51 PM #4Senior Member
 Join Date
 Mar 2010
 Posts
 953
 Rep Power
 5
Done? It doesn't look done to me. It looks like everything is way over to the left. Do you understand what I mean by "properly indenting"?
Java Code:for (int y = 1; y < 8; y++) { for (int x = 1; x < 8; x++) { if (btns[x][y].getText().equalsIgnoreCase("R")) { ...

 03212010, 12:01 AM #6Member
 Join Date
 Mar 2009
 Posts
 23
 Rep Power
 0
haha! your very nice.
hope i'm done now :D
 03212010, 12:11 AM #7Senior Member
 Join Date
 Mar 2010
 Posts
 953
 Rep Power
 5
OK, that's better than it was, at least. I don't see anything that looks like a binary search tree here, or even anything that calls for a binary search tree.
It looks like you're looping through an 8 x 8 grid (of buttons) and when you find one with text "R", you want to check all the buttons around it for text "C"  except you're not doing that. As far as I can tell, you're checking four of the eight buttons you need to check. (I could be wrong, as I'm guessing about the requirements  you haven't told us what they are.)
You should be doing the checking with another pair of loops, and because it's going to get messy otherwise, you should do it in a separate method.
Your variable r (your Random object) should be a static class variable  you don't need one for each instance, and you certainly don't need a new one each time through the loop.
You're assigning the result of the r.nextInt() call to variable location1, but then you seem to be checking variable location.
So what is your question? Why do you think you need a binary search tree?
Gary
 03212010, 12:21 AM #8Senior Member
 Join Date
 Mar 2010
 Posts
 953
 Rep Power
 5
By the way, your source code is still not formatted properly, and I'd be willing to bet that it's even more confusing to you than it is to me. Style is important. Readability is important. Attention to detail is important. If you let the easy things defeat you, what will you do when you get to the difficult things?
Gary
 03212010, 12:21 AM #9Member
 Join Date
 Mar 2009
 Posts
 23
 Rep Power
 0
i have a game where its a 9 by 9 grid. There are
three main objects in the game, namely, the platform, cheetah and rabbit.
a platform consists of a two dimensional array of x and y positions, x for rows and y for columes. Each position is represented by a pair of indices (x; y), where x; y are integers representing the row index and colume index respectively.
The Cheetah is represented by a capital letter `C' and Rabbit by `R', which may move independently
one step at a time. A step by the Cheetah or Rabbit changes one position by moving from the current
position (x; y) to one of the 4 immediate neighbours (x0; y0) in one of the 4 directions (U, D, L, R),
i.e. (Up, Down, Left, Right) respectively.
anyways i need to design appropriate data structures and algorithms for 3 most important visual functions of the
game that i choose. and i chose binary search tree. i have used array as part of my data structure.
 03212010, 12:31 AM #10Member
 Join Date
 Mar 2009
 Posts
 23
 Rep Power
 0
right, but you have not seen the whole source code, this is just a section of it, i understand most of whats given, is just this section confuses me. and all i wanted was an explanation. and as for
If you let the easy things defeat you, what will you do when you get to the difficult things?
 03212010, 12:37 AM #11Senior Member
 Join Date
 Mar 2010
 Posts
 953
 Rep Power
 5
OK, study some more about binary search trees, but here's a hint: they will not help you in implementing this game.
Here's another hint: if you really want a 9 x 9 play grid, you should implement it on top of an 11 x 11 virtual grid. Here's why:
Java Code:R = rabbit * = grid squares to check *** *R* *** * = squares your code is checking now * R* **
Gary
 03212010, 12:45 AM #12Senior Member
 Join Date
 Mar 2010
 Posts
 953
 Rep Power
 5
I did not mean to criticize your inexperience, and I'm sorry if it seemed that way. I was talking about things like formatting and indentation, which it seemed to me you considered an unimportant chore (and I apologize if my impression was mistaken). I want to persuade you that such things are important, and you should pay attention to them now and develop good habits. Good formatting helps make the easy parts easy and the hard parts possible.
Gary
 03212010, 12:54 AM #13Member
 Join Date
 Mar 2009
 Posts
 23
 Rep Power
 0
my indentation is fine on my source code, is just a copying a section from my source code and pasting on to the forum does not paste properly as it is shown on my actual source code. i do indent my source code always, and i know its valuable.
as for theIf the rabbit happens to be on an edge row or column, then your method will have difficulty looking at squares beyond the edge. But if those squares exist, but are simply defined as out of bounds (initialized with a sentinel value), it's easier for the method.
i.e if (if (x < 0) {
x = 0;
}
if (y < 0) {
y = 0; etc, and it stops from going outside the box.
Bookmarks