Page 1 of 3 123 LastLast
Results 1 to 20 of 52
Like Tree10Likes

Thread: Java Wordsearch puzzle method 2 - newbie

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

    Default Java Wordsearch puzzle method 2 - newbie

    I'm looking for more help with my wordsearch program if possible. It is part of a homework assignment. This might seem a little long winded and I'll probably get flamed for this noobieness so I apologise in advance

    The method needs to insert a string horizontally into a 2d array (letterGrid) at randomly generated row and column positions.

    Within a loop that loops 10 times or until the method's argument has been entered into letterGrid.The method 1st sends two randomPosition() messages to the receiver to get values for targetRow and targetCol.

    Then it sends isHorizontalSpaceFree(targetRow,targetCol,word) to check for space. If there isn't space then back to top of loop and try new random positions - if there is space then iterate over word and assign each character to the appropriate component in letterGrid and return true.

    If after 10 attempts suitable starting positions can't be found then return false.

    Here is my code so far...

    Java Code:
     int targetRow = this.randomPosition(NUMBER_ROWS);
      int targetCol = this.randomPosition(WordSearchMaker.NUMBER_COLS - word.length() + 1);
      boolean result = true;
      for (int i = 0; i < word.length(); i++)
      {
      if (this.isHorizontalSpaceFree(targetRow, targetCol, word))
         {
            letterGrid[targetRow][targetCol] = word.charAt(i);
         }
      }
      return result;
    }
    My problems are with the loops and how to insert each character of the word. Any pointers are very much appreciated folks

    Thanks in advance.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,418
    Rep Power
    25

    Default Re: Java Wordsearch puzzle method 2 - newbie

    Your code creates two random numbers
    then loops for the length of the word
    calls a method with the random numbers and the word ???? what does this method do?
    if the method returns true => sets the value of the randomly chosen slot to the ith letter in the word

    when the loop exits, the method returns true. Always returns true???

    Questions:
    The random numbers never change in the loop so the same slot could have its contents changed to all the characters in the word depending on when isHorizontalSpaceFree() returns true???
    Why always return true?
    lannie1980 likes this.

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

    Default Re: Java Wordsearch puzzle method 2 - newbie

    Hey Norm,

    Thanks for the reply. Yes it always returns true - it's not supposed to lol.

    This is my rejigged code which does better - but now I need to put in the boolean conditions.

    Java Code:
    {
          boolean result = true;
          for (int i = 0; i < 10; i++)
          {
             int targetRow = this.randomPosition(NUMBER_ROWS);
             int targetCol = this.randomPosition(WordSearchMaker.NUMBER_COLS - word.length() + 1);
             if (this.isHorizontalSpaceFree(targetRow, targetCol, word))
             {
                for (int len = 0; len < word.length(); len++)
                {
                   letterGrid[targetRow][targetCol + len] = word.charAt(len);
                }
             }
          }
          return result;
       }
    I'm going to keep going and try to finish this off now but any more suggestions/pointers are appreciated greatly.

    Thanks in advance

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,418
    Rep Power
    25

    Default Re: Java Wordsearch puzzle method 2 - newbie

    Does your code work now?

    it always returns true - it's not supposed to
    What determines what it returns? Why true or false?
    lannie1980 likes this.

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

    Default Re: Java Wordsearch puzzle method 2 - newbie

    Hey,

    The code now puts the full word into the grid horizontally so it works better than my 1st code lol.

    If after 10 attempts suitable starting positions can't be found (i.e !=isHorizontalSpaceFree) then the method should return false. This is the bit I need to code now to finish.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,418
    Rep Power
    25

    Default Re: Java Wordsearch puzzle method 2 - newbie

    So you mean to return true if the word is stored in the array?
    lannie1980 likes this.

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

    Default Re: Java Wordsearch puzzle method 2 - newbie

    yes, if it assigns the characters of word to the appropriate components - it should return true - otherwise after 10 unsuccessful attempts to insert word it returns false.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,418
    Rep Power
    25

    Default Re: Java Wordsearch puzzle method 2 - newbie

    Where in your code are those conditions met?
    That is where you need to return the correct results.
    lannie1980 likes this.

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

    Default Re: Java Wordsearch puzzle method 2 - newbie

    Been working on booleans for an hour or two now and this is all I have so far ;(
    Java Code:
    {
          boolean result = false;
          for (int i = 0; i < 10; i++)
          {
             int targetRow = this.randomPosition(NUMBER_ROWS);
             int targetCol = this.randomPosition(WordSearchMaker.NUMBER_COLS - word.length() + 1);
             if (this.isHorizontalSpaceFree(targetRow, targetCol, word))
             {
                for (int len = 0; len < word.length(); len++)
                {
                   letterGrid[targetRow][targetCol + len] = word.charAt(len);
                   result = true;
                }
             }
             if (this.isHorizontalSpaceFree(targetRow, targetCol, word) != true)
             {
                result = false;
             }
          }
          return result;
       }
    I'm struggling to get it to return false after 10 loops. Any hints appreciated as always.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,418
    Rep Power
    25

    Default Re: Java Wordsearch puzzle method 2 - newbie

    You should stop the looping etc as soon as you know the result is true.
    Either return or break as soon as you know.
    lannie1980 likes this.

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

    Default Re: Java Wordsearch puzzle method 2 - newbie

    I think this may be right now.

    Java Code:
    {
          boolean result = false;
          for (int attempts = 0; attempts < 10; attempts++)
          {
             int targetRow = this.randomPosition(NUMBER_ROWS);
             int targetCol = this.randomPosition(WordSearchMaker.NUMBER_COLS - word.length() + 1);
             if (this.isHorizontalSpaceFree(targetRow, targetCol, word))
             {
                for (int len = 0; len < word.length(); len++)
                {
                   letterGrid[targetRow][targetCol + len] = word.charAt(len);
                }
                result = true;
             }
             else
             {
                result = false;
             }
          }
          return result;
       }
    Is it at all?

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,418
    Rep Power
    25

    Default Re: Java Wordsearch puzzle method 2 - newbie

    I think this may be right now.
    To be sure, make a program to test it.
    Create several different test cases with different arrays and words and call the method and see if it does what you want.
    lannie1980 likes this.

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

    Default Re: Java Wordsearch puzzle method 2 - newbie

    ok will do and cheers for the advice - this forum is great ;)

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,418
    Rep Power
    25

    Default Re: Java Wordsearch puzzle method 2 - newbie

    The Arrays class's toString method is useful for formatting arrays for printing.

    Remember a 2D array is a 1D array of arrays.
    lannie1980 likes this.

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

    Default Re: Java Wordsearch puzzle method 2 - newbie

    Yup, I've read a little on that concept in my learning - i'm gonna just keep going over everything till it sticks.

    There is no such thing as a 2d array - it is an array of arrays - same for multidimensional arrays I think.

    Is the toString method under java.util.arrays? Cheers

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,418
    Rep Power
    25

    Default Re: Java Wordsearch puzzle method 2 - newbie

    You can find the Arrays class in the API doc:
    Java Platform SE 6
    DarrylBurke and lannie1980 like this.

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

    Default Re: Java Wordsearch puzzle method 2 - newbie

    thats excellent - thanks a lot ;)

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

    Default Re: Java Wordsearch puzzle method 2 - newbie

    Just one last question then if your still awake lol

    This is my method for isHorizintalSpaceFree()

    Java Code:
    {
          if(aCol + word.length() > NUMBER_COLS)
          {
            return false;
          }
          char[] columns = letterGrid[aRow];
          for(int i = aCol; i < aCol + word.length(); i++)
          {
             if(columns[i] != BLANK_ELEMENT)
             {
                return false;
             }
          }
          return true;
       }
    and this is my method for isVerticalSpaceFree()

    Java Code:
    {
          if(aRow + word.length() > NUMBER_ROWS)
          {
            return false;
          }
          char[] rows = letterGrid[aCol];
          for(int i = aRow; i < aRow + word.length(); i++)
          {
             if(rows[i] != BLANK_ELEMENT)
             {
                return false;
             }
          }
          return true;
       }
    Why does the second method not work just like the first?

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,418
    Rep Power
    25

    Default Re: Java Wordsearch puzzle method 2 - newbie

    Can you test them to see what they do?

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

    Default Re: Java Wordsearch puzzle method 2 - newbie

    i'll try some println statements

Page 1 of 3 123 LastLast

Similar Threads

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