Page 3 of 3 FirstFirst 123
Results 41 to 52 of 52
Like Tree10Likes

Thread: Java Wordsearch puzzle method 2 - newbie

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

    Default Re: Java Wordsearch puzzle method 2 - newbie

    That will be easier.

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

    Default Re: Java Wordsearch puzzle method 2 - newbie

    So the method will basically do the same as the previous 2 methods (but this time check that there is enough '*' diagonally to fit a String word). It really is the traversing i'm having the most problems with - but now i know how to process horizontally and vertically.

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

    Default Re: Java Wordsearch puzzle method 2 - newbie

    I think this does the job...

    Java Code:
    if(aRow + word.length() > NUMBER_ROWS
          || aCol + word.length() > NUMBER_COLS)
          {
             return false;
          }
          for(int i = aRow; i < aRow + word.length(); i++)
          {
             if(letterGrid[i][i] != BLANK_ELEMENT)
             {
                return false;
             }
          }
       return true;
    }
    does it look right to you?
    This will be my last post hopefully Norm - thanks to your help and guidance I now know more about arrays than when I 1st posted - cheers alot and all the best ;)

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

    Default Re: Java Wordsearch puzzle method 2 - newbie

    Compile it and thoroughly test it. That'll tell you if it does what you want.

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

    Default Re: Java Wordsearch puzzle method 2 - newbie

    That was my 2nd last post

    Java Code:
    /**
        * Attempts up to 10 times to insert the word given by the String argument 
        * diagonally into the grid starting at a random row and column position. 
        * If insertion successful, returns true, false otherwise.
        */
       public boolean insertDiagonally(String word)
       {
          boolean result = false;
          for (int attempts = 0; attempts < 10; attempts++)
          {
             int targetRow = this.randomPosition(WordSearchMaker.NUMBER_ROWS - word.length() + 1);
             int targetCol = this.randomPosition(WordSearchMaker.NUMBER_COLS - word.length() + 1);
             if (this.isDiagonalSpaceFree(targetRow, targetCol, word))
             {
                for (int len = 0; len < word.length(); len++)
                {
                   letterGrid[targetRow + len][targetCol + len] = word.charAt(len);
                }
                result = true;
             }
             else
             {
                result = false;
             }
          }
          return result;
       }
    it's almost there - it inserts diagonally but sometimes an extra random letter appears at end of string for some reason.

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

    Default Re: Java Wordsearch puzzle method 2 - newbie

    Add some printlns for debug. Print out the row and column and the letter.

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

    Default Re: Java Wordsearch puzzle method 2 - newbie

    it's my isDiagonalSpaceFree() method that I posted earlier that is messing the insertDiagonally() method up...
    Last edited by lannie1980; 03-06-2012 at 01:13 PM.

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

    Default Re: Java Wordsearch puzzle method 2 - newbie

    Hi again, I've managed to get everything working so far - now finally...

    Write a public instance method called buildFrom() which takes a single argument called wordList that references an array of type String and returns no result. The method should iterate through the strings in wordList using a foreach loop, attempting on each iteration to insert the current string into letterGrid by sending addWordAllOrientations() to the receiver with the current string as argument.

    I have so far

    Java Code:
      public void buildFrom(String[] wordList)
       { 
          for(String word : wordList)
          {
             this.addWordAllOrientations(word);
          }
       }
    it adds the words but repeats words when all I want is each word inserted once. What am I doing wrong here?
    Regards

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

    Default Re: Java Wordsearch puzzle method 2 - newbie

    What does addWordAllOrientations do?
    I want is each word inserted once
    Where are the words inserted? What code decides if the word is to be inserted?
    Change the code so it doesn't insert if the word is already there.

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

    Default Re: Java Wordsearch puzzle method 2 - newbie

    Hi,

    Here is addWordAllOrientations() method that works correctly - I just wanted to know if my foreach loop above was working correctly and I have written it right - i'm not sure it is correct.

    Java Code:
    /**
        * Method attempts to insert word into letterGrid.
        * 
        * If an attempt at inserting word vertically is unsuccessful, 
        * an attempt is made at inserting it horizontally; if this attempt 
        * is unsuccessful, an attempt is made at inserting it diagonally.
        * 
        * The above sequence of attempts is carried out 20 times or until  
        * word has been successfully inserted into letterGrid. 
        */
       public void addWordAllOrientations(String word)
       {
          int orientation =  this.getLastWordOrientation() % 3;
          boolean placed = false;
          this.setLastWordOrientation(this.getLastWordOrientation() + 1);
          int index = 0;
          while ((placed == false) && (index < 20))
          {
             if (orientation == 0)
             {
                placed = this.insertVertically(word);
             }
             if (orientation == 1)
             {
                placed = this.insertHorizontally(word);
             }
             if (orientation == 2)
             {
                placed = this.insertDiagonally(word);
             }
             index++;
          }
       }
    }
    Thanks

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

    Default Re: Java Wordsearch puzzle method 2 - newbie

    I just wanted to know if my foreach loop above was working correctly and I have written it right
    To find out, write a small test program that loops through an array and prints out the results.

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

    Default Re: Java Wordsearch puzzle method 2 - newbie

    i'll try that

Page 3 of 3 FirstFirst 123

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
  •