• 03-20-2010, 11:33 PM
yasmin k
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.

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;

• 03-20-2010, 11:36 PM
gcalvin

-Gary-
• 03-20-2010, 11:45 PM
yasmin k
done :D
sorry, i didn't know that would be a problem.
• 03-20-2010, 11:51 PM
gcalvin
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"?

Code:

for (int y = 1; y < 8; y++) {
for (int x = 1; x < 8; x++) {
if (btns[x][y].getText().equalsIgnoreCase("R")) {
...

-Gary-
• 03-21-2010, 12:00 AM
Fubarable
Quote:

Originally Posted by yasmin k
done :D
sorry, i didn't know that would be a problem.

It's not a problem, unless you want others to read your code. Then the easier you make it for others to read it and understand it, the more who will be willing to do so.

Best of luck.
• 03-21-2010, 12:01 AM
yasmin k

hope i'm done now :D
• 03-21-2010, 12:11 AM
gcalvin
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-
• 03-21-2010, 12:21 AM
gcalvin
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-
• 03-21-2010, 12:21 AM
yasmin k
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.
• 03-21-2010, 12:31 AM
yasmin k
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
Quote:

If you let the easy things defeat you, what will you do when you get to the difficult things?
practise makes perfect, if i can combat the easy stuff then i can move on to the hard codes. i am still a novice programmer.
• 03-21-2010, 12:37 AM
gcalvin
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:

Code:

R = rabbit
* = grid squares to check

***
*R*
***

* = squares your code is checking now

R*
**

You will want a method that can loop and check all the squares around the rabbit, am I right? If 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.

-Gary-
• 03-21-2010, 12:45 AM
gcalvin
Quote:

Originally Posted by yasmin k
practise makes perfect, if i can combat the easy stuff then i can move on to the hard codes. i am still a novice programmer.

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-
• 03-21-2010, 12:54 AM
yasmin k
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 the
Quote:

If 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 have created an if statement to stop that from happening.

i.e if (if (x < 0) {
x = 0;
}
if (y < 0) {
y = 0; etc, and it stops from going outside the box.