Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 52
Like Tree10Likes

Thread: Java Wordsearch puzzle method 2 - newbie

  1. #21
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: Java Wordsearch puzzle method 2 - newbie

    I can't seem to work it out right now - getting late here and my head is turning to mush ;p The 1st method I posted checks from left to right that there is free space in the grid for a word to be inserted horizontally and that there is enough BLANK_ELEMENT - returns true if it can false if not. This method works fine.

    The 2nd method that I posted needs to do the same but check vertically from top to bottom...

    I've got the searching bit and logic wrong i think here.

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,790
    Rep Power
    25

    Default Re: Java Wordsearch puzzle method 2 - newbie

    check vertically from top to bottom...
    The column index stays the same, the row index changes

  3. #23
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: Java Wordsearch puzzle method 2 - newbie

    i thought i was incrementing the row with for loop and i++ - am i changing the column index in my code instead of rows?

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,790
    Rep Power
    25

    Default Re: Java Wordsearch puzzle method 2 - newbie

    Write a test program that defines a 2D array and goes thru it by going down a column and see what the logic is.
    Or write down the index values for the elements in a column of a 2D array.

  5. #25
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: Java Wordsearch puzzle method 2 - newbie

    hey again,

    i'm refreshed and ready to go again ;p i will write this test today and see what happens.

    it was this
    Java Code:
    char[] columns = letterGrid[aRow];
    and this
    Java Code:
    if(columns[i] != BLANK_ELEMENT)
    that I don't really understand why it works. I know these lines work for the horizontal method -i guess i got lucky - but i don't understand very well the logic behind them.

  6. #26
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,790
    Rep Power
    25

    Default Re: Java Wordsearch puzzle method 2 - newbie

    Take a piece of paper and draw a small grid representing a 2D array. Label each square with its row, col indexes then look at how you must change the indexes to go down the squares in a column.

  7. #27
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: Java Wordsearch puzzle method 2 - newbie

    i tried this and I can populate the column in sequence easily enough

    Java Code:
    public void testVerticalLogic()
    {
    for (int i = 0; i < NUMBER_ROWS; i++)
    {
    letterGrid[i][0] = 'L';
    }
    }

  8. #28
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,790
    Rep Power
    25

    Default Re: Java Wordsearch puzzle method 2 - newbie

    Now apply that logic to the method that checks for space in a column
    lannie1980 likes this.

  9. #29
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: Java Wordsearch puzzle method 2 - newbie

    yes but how do you increase the columns and search down each one - i need to traverse the array searching for enough '*' elements to fit in a word. So that the word fits in vertically.

  10. #30
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: Java Wordsearch puzzle method 2 - newbie

    hi, got it eventually.

    Java Code:
     public boolean isVerticalSpaceFree(int aRow, int aCol, String word)
       {
          if(aRow + word.length() > NUMBER_ROWS)
          {
             return false;
          }
          char[] rows = letterGrid[aCol];
          for(int i = aRow; i < aRow + word.length(); i++)
          {
             if(letterGrid[i][aCol] != BLANK_ELEMENT)
             {
                return false;
             }
          }
          return true;
       }
    Seems to be working correctly. I just added in
    Java Code:
    (letterGrid[i][aCol] != BLANK_ELEMENT)
    and it worked! woot
    Thanks for the help by the way ;)

  11. #31
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,790
    Rep Power
    25

    Default Re: Java Wordsearch puzzle method 2 - newbie

    how do you increase the columns and search down each one
    If you change the column's index value you are searching on a row.
    To search in a column, you change the row index.

    What does the code on line 7 do?

  12. #32
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: Java Wordsearch puzzle method 2 - newbie

    I do not have a clue what the code on line 7 does - I asked for help in another forum and the reply included this line. I don't think it is necessary i.e irt does nothing that I can see that is useful.
    I'll take it out and test it now.
    Or have I got this completely and badly wrong lol?

  13. #33
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,790
    Rep Power
    25

    Default Re: Java Wordsearch puzzle method 2 - newbie

    I'll take it out and test it now.
    If you'd done that before posting, it might have saved your having to post.

  14. #34
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: Java Wordsearch puzzle method 2 - newbie

    and what do you know - that line of code does nothing after all - i've amended both methods now. Would I be right here?

  15. #35
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,790
    Rep Power
    25

    Default Re: Java Wordsearch puzzle method 2 - newbie

    If the code does what you want without that statement, then that statement must not be doing anything useful.

  16. #36
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: Java Wordsearch puzzle method 2 - newbie

    guess whats next Norm.... isDiagonalSpaceFree() - i'm thinking it involves aRow*1 to get corresponding column index

  17. #37
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,790
    Rep Power
    25

    Default Re: Java Wordsearch puzzle method 2 - newbie

    Take a piece of paper, draw a grid and mark the diagonal squares with their row,col values.
    Then find the formula for going diagonally up or down and left or up or down and right from any given row,col location.

  18. #38
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: Java Wordsearch puzzle method 2 - newbie

    my grid is 12 rows by 25 columns

    my code so far is

    Java Code:
    public void testDiagonalLogic()
    {
    for (int i = 0; i < letterGrid.length; i++)
    {
    letterGrid[i][i] = 'L';
    }
    }

  19. #39
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,790
    Rep Power
    25

    Default Re: Java Wordsearch puzzle method 2 - newbie

    From any square in the middle of the grid there are 4 different ways to go diagonally. Your code goes one way.

  20. #40
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: Java Wordsearch puzzle method 2 - newbie

    i only need it to go from top left to bottom right

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Friday night java puzzle
    By niu_niu in forum New To Java
    Replies: 12
    Last Post: 06-19-2010, 11:54 PM
  2. Replies: 0
    Last Post: 05-27-2009, 05:39 PM
  3. [SOLVED] [newbie] Is my method mutable?
    By jon80 in forum New To Java
    Replies: 4
    Last Post: 05-21-2009, 12:29 AM
  4. newbie. Reading a file, deprecated method
    By Roy Gardiner in forum New To Java
    Replies: 2
    Last Post: 10-24-2008, 09:08 PM
  5. Java Drawing PUZZLE
    By Cyorxamp in forum AWT / Swing
    Replies: 3
    Last Post: 06-09-2008, 11:35 AM

Tags for this Thread

Posting Permissions

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