Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Word Search Program Help

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    28
    My Mood
    Stressed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Word Search Program Help

    I'm having trouble with a WordSearch project that I've been assigned.

    Essentially, what I'm supposed to do is create a Java program that reads a word search grid from a class that has been written for us (called WordBoard), and that identifies a word in the grid (either backwards, forwards, up, down, or diagonal) and prints it below the board. I believe I have a decent procedure for checking the directions on the grid (I'm sure there are much more efficient ways of doing so, but I'd ideally like to stick with what I have, or something very similar). I'm just not sure how to implement the methods my instructor has told me I need to use.

    EDIT: I've provided the code I have for the main method and the promptUserForSeed method. My problem is, I don't know how to implement the main method properly or the promptUserForSeed method properly, as I don't know how to pass the long variable "seed" from promptUserForSeed() to findWords(), and I don't know how to print the WordBoard that I receive from findWords() in the main method.

    Methods I have to write (with notes):
    public static void main(String[] args)
    - Your main method should create a new Scanner object to read from the keyboard, pass the scanner to promptUserForSeed(), and use the long that it returns as the seed that gets passed to findWords() (Set the dimensions of the board as 10 x 10). Print out the WordBoard that you receive from findWords(), and then use WordBoard's checkAnswers() method to see if your code works as expected.

    public static long promptUserForSeed(Scanner input) - This method should print a prompt to the user asking them to input a seed. Read in their input using input.nextLong() and return that value. You do not need to account for the user inputting invalid seeds.

    public static WordBoard findWords(int rows, int cols, long seed) - This method should contain the bulk of your code. Using the given seed and dimensions, create a new WordBoard object, fetch the 2D array that WordBoard has generated, and perform your word search on that 2D array. Whenever you find a word, use the highlightWord() method to tell the WordBoard that you've found it. Return the WordBoard object once you are finished finding all of the words.



    Methods I can use from the WordBoard class:
    public WordBoard(int rows, int columns)
    - Initializes a new WordBoard with random seed and the given dimensions.

    public WordBoard(int rows, int columns, long seed) - Initializes a new WordBoard with the given seed and dimensions.

    public char[][] getBoard() - Gets a copy of the 2D array that WordBoard generated. This is the array that you will be iterating over when you perform your word search.

    public String[] getDictionary() - Gets the list of words that may be hidden in the word board. Your code for finding a single word in the array should be surrounded by a loop that iterates over this array.

    public long getSeed() - Get the seed that was used to generate the board.

    public String toString() - Returns the contents of the board as a string. You can pass this straight to System.out.println() to print the entire board to the console.

    public void highlightWord(int i1, int j1, int i2, int j2) - This method should be called when you know where a word begins and ends. You'll be passing two orders pairs (i1, j1) and (i2, j2) to the method. The first ordered pair should be the location of the first character of the word, and the second one should be the location of the last character of the word. After you have called this method, using the toString() method will capitalize all of the words you've found.

    public void checkAnswers() - This method checks the words that you've found (according to highlightWord()) and checks to see if there are any missing words that you haven't found. It prints its finding to the console, as well as the seed for this board. Sample outputs from this method may look like:
    Correctly found word 'bug' from (9, 0) to (9, 2)
    Missing word 'float' from (3, 9) to (3, 5)
    Reversed word 'eurt' from (6, 3) to (3, 3)




    Here's the code I have so far:
    import java.util.Scanner;
    public class WordSearch
    {
    	public static void main(String[] args)
    	{
    		Scanner input = new Scanner(System.in);
    	}
     
    	public static long promptUserForSeed(Scanner input)
    	{
    		System.out.println("Enter seed:");
    		long seed = input.nextLong();
    		return seed;
    	}
     
    	public static WordBoard findWords(int rows, int cols, long seed)
    	{
    		WordBoard board = new WordBoard(rows, cols, WordSearch.promptUserForSeed(Scanner));
    		System.out.println(board);
    		System.out.println(board.getSeed());
     
    		boolean foundLetter = false;
    		boolean foundWord = false;
    		int i2 = 0;
    		int j2 = 0;
    		String tempWord = new String();
     
    		char[][] puzzle = board.getBoard();
    		for(String word : board.getDictionary())
    		{
    			char letter = word.charAt(0);
    			boolean up = false;
    			boolean down = false;
    			boolean left = false;
    			boolean right = false;
    			boolean upLeft = false;
    			boolean upRight = false;
    			boolean downLeft = false;
    			boolean downRight = false;
     
    			for(int i1 = 0; i1 < puzzle.length; i1++) //Loop through the puzzle rows
    			{
    				for(int j1 = 0; j1 < puzzle.length; j1++) //Loop through puzzle columns
    				{
    					if(puzzle[i1][j1] == letter) //When matched with a letter
    					{	
    						i2 = i1; //Copy location
    						j2 = j1;
     
    						foundLetter = true;
    						foundWord = false;
     
    						if(i1 >= word.length()) //Possible to go up
    						{
    							up = true;
    						}
    						if((i1 + word.length()) <= puzzle.length) //Possible to go down
    						{
    							down = true;
    						}
    						if(j1 >= word.length()) //Possible to go left
    						{
    							left = true;
    						}
    						if((j1 + word.length()) <= puzzle.length) //Possible to go right
    						{
    							right = true;
    						}
    						if(up == true && left == true) //Possible to go up and left
    						{
    							upLeft = true;
    						}
    						if(up == true && right == true) //Possible to go up and right
    						{
    							upRight = true;
    						}
    						if(down == true && left == true) //Possible to go down and left
    						{
    							downLeft = true;
    						}
    						if(down == true && right == true) //Possible to go down and right
    						{
    							downRight = true;
    						}
     
    						if(up == true && foundLetter == true && foundWord == false) //While at a matched letter, go up
    						{
    							tempWord = "";
    							for(int index = 0; index < word.length(); index++) //Make a string with the letters going up
    							{
    								tempWord += puzzle[i2][j2];
    								i2--;
    							}
     
    							if(word.equals(tempWord)) //If the current word matches the word going up
    							{	
    								board.highlightWord(i1, j1, i2, j2); //Highlight the current word
    							}
    						}
     
    						i2 = i1; //Reset index for current letter position
    						j2 = j1;
    						if(down == true && foundLetter == true && foundWord == false) //While at a matched letter, go down
    						{
    							tempWord = "";
    							for(int index = 0; index < word.length(); index++) //Make a string with the letters going down
    							{
    								tempWord += puzzle[i2][j2];
    								i2++;
    							}
     
    							if(word.equals(tempWord)) //If the current word matches the word going down
    							{
    								foundLetter = false;
    								foundWord = true;
     
    								board.highlightWord(i1, j1, i2, j2); //Highlight the current word
    							}
    						}
     
    						i2 = i1; //Reset index for current letter position
    						j2 = j1;
    						if(left == true && foundLetter == true && foundWord == false) //While at a matched letter, go left
    						{
    							tempWord = "";
    							for(int index = 0; index < word.length(); index++) //Make a string with the letters going left
    							{
    								tempWord += puzzle[i2][j2];
    								j2--;
    							}
     
    							if(word.equals(tempWord)) //If the current word matches the word going left
    							{
    								foundLetter = false;
    								foundWord = true;
     
    								board.highlightWord(i1, j1, i2, j2); //Highlight the current word
    							}
    						}
     
    						i2 = i1; //Reset the index for current letter position
    						j2 = j1;
    						if(right == true && foundLetter == true && foundWord == false) //While at a matched letter, go right
    						{
    							tempWord = "";
    							for(int index = 0; index < word.length(); index++) //Make a string with the letters going right
    							{
    								tempWord += puzzle[i2][j2];
    								j2++;
    							}
     
    							if(word.equals(tempWord)) //If the current word matches the word going right
    							{
    								foundLetter = false;
    								foundWord = true;
     
    								board.highlightWord(i1, j1, i2, j2); //Highlight the word
    							}
    						}
     
    						i2 = i1; //Reset index for current letter position
    						j2 = j1;
    						if(upLeft == true && foundLetter == true && foundWord == false) //While at a matched letter, go up and left
    						{
    							tempWord = "";
    							for(int index = 0; index < word.length(); index++) //Make a string with the letters going up and left
    							{
    								tempWord += puzzle[i2][j2];
    								j2--;
    								i2--;
    							}
     
    							if(word.equals(tempWord)) //If the current word matches the word going up and left
    							{
    								foundLetter = false;
    								foundWord = true;
     
    								board.highlightWord(i1, j1, i2, j2); //Highlight the word
    							}
    						}
     
    						i2 = i1; //Reset the index for current letter position
    						j2 = j1;
    						if(upRight == true && foundLetter == true && foundWord == false) //While at a matched letter, go up and right
    						{
    							tempWord = "";
    							for(int index = 0; index < word.length(); index++) //Make a string with the letters going up and right
    							{
    								tempWord += puzzle[i2][j2];
    								j2++;
    								i1--;
    							}
     
    							if(word.equals(tempWord)) //If the current word matches the word going up and right
    							{
    								foundLetter = false;
    								foundWord = true;
     
    								board.highlightWord(i1, j1, i2, j2); //Highlight the word
    							}
    						}
     
    						i2 = i1; //Reset index for current letter position
    						j2 = j1;
    						if(downLeft == true && foundLetter == true && foundWord == false) //While at a matched letter, go down and left
    						{
    							tempWord = "";
    							for(int index = 0; index < word.length(); index++) //Make a string with the letters going down and left
    							{
    								tempWord += puzzle[i2][j2];
    								j2--;
    								i2++;
    							}
     
    							if(word.equals(tempWord)) //If the current word matches the word going down and left
    							{
    								foundLetter = false;
    								foundWord = true;
     
    								board.highlightWord(i1, j1, i2, j2); //Highlight the word
    							}
    						}
     
    						i2 = i1; //Reset index for current letter position
    						j2 = j1;
    						if(downRight == true && foundLetter == true && foundWord == false) //While at a matched letter, go down and right
    						{
    							tempWord = "";
    							for(int index = 0; index < word.length(); index++) //Make a string with the letters going down and right
    							{
    								tempWord += puzzle[i2][j2];
    								j2++;
    								i2++;
    							}
     
    							if(word.equals(tempWord)) //If the current word matches the word going down and right
    							{
    								foundLetter = false;
    								foundWord = true;
     
    								board.highlightWord(i1, j1, i2, j2); //Highlight the word
    							}
    						}
    					}
    				}
    			}
    		}
    		return board;
    	}
    }
    Last edited by dataghost; April 30th, 2019 at 10:29 AM.

  2. #2
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    121
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Word Search Program Help

    To me it seems like you're mixing the use of a constructor.
    Maybe if you try to make the program for scratch then you can see what you're doing wrong.
    "Tick, tack"

  3. #3
    Junior Member
    Join Date
    Mar 2019
    Posts
    28
    My Mood
    Stressed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Word Search Program Help

    I think I've implemented the highlightWord() method correctly, I just need help now with passing the long variable "seed" from the promptUserForSeed() method into the findWords() method, and then printing the WordBoard object that I return in the findWords() method in the main() method. Do you know how I could go about this? I've updated the question and my code above.

Similar Threads

  1. Word Search Puzzle
    By Arklondonbridge in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 31st, 2014, 01:47 PM
  2. Method to solve a word search in Java using 2d arrays
    By Kimmi in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 17th, 2013, 10:13 PM
  3. Word Search
    By jamie1234 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: April 9th, 2013, 06:03 PM
  4. Word Search Help
    By Tanner in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 3rd, 2011, 01:39 AM
  5. Search Word puzzle game
    By lew1s in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 9th, 2011, 04:23 AM

Tags for this Thread