Results 1 to 13 of 13
  1. #1
    yasmin k is offline Member
    Join Date
    Mar 2009
    Posts
    23
    Rep Power
    0

    Question 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; 03-20-2010 at 11:00 PM.

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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-

  3. #3
    yasmin k is offline Member
    Join Date
    Mar 2009
    Posts
    23
    Rep Power
    0

    Default

    done :D
    sorry, i didn't know that would be a problem.

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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")) {
    ...
    -Gary-

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by yasmin k View Post
    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.

  6. #6
    yasmin k is offline Member
    Join Date
    Mar 2009
    Posts
    23
    Rep Power
    0

    Default

    haha! your very nice.

    hope i'm done now :D

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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-

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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-

  9. #9
    yasmin k is offline Member
    Join Date
    Mar 2009
    Posts
    23
    Rep Power
    0

    Default

    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.

  10. #10
    yasmin k is offline Member
    Join Date
    Mar 2009
    Posts
    23
    Rep Power
    0

    Default

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

  11. #11
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

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

  12. #12
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by yasmin k View Post
    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-

  13. #13
    yasmin k is offline Member
    Join Date
    Mar 2009
    Posts
    23
    Rep Power
    0

    Default

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •