Question

In: Computer Science

Word Find A popular diversion in the United States, “word find” (or “word search”) puzzles ask...

  1. Word Find A popular diversion in the United States, “word find” (or “word search”) puzzles ask the player to find each of a given set of words in a square table filled with single letters. A word can read horizontally (left or right), vertically (up or down), or along a 45 degree diagonal (in any of the four directions) formed by consecutively adjacent cells of the table; it may wrap around the table’s boundaries, but it must read in the same direction with no zigzagging. The same cell of the table may be used in different words, but, in a given word, the same cell may be used no more than once. Write a computer program for solving this puzzle.

Solutions

Expert Solution

I have coded in java. Please compile "WordSearch.java" first. Then, compile and run "SolveWordSearch.java". The "words.txt" file should contain the data as given below. The first 2 lines of the "words.txt" file indicates the number of rows and columns of the puzzle(10x10). The next 10 lines is the puzzle. The next line indicates the number of words to be found and the last 4 lines indicates the words to be found on the given puzzle.

WordSearch.java

import java.util.Scanner;
import java.io.*;
public class WordSearch
{
   private int rows;//instance variable
   private int cols;//instance variable
   private int words;//instance variable
   private char[][] wordSearchchar;//instance variable
   private String[] wordSearchstring;//instance variable  
  
   public WordSearch()//an initialized constructor
   {
   }

   public int addX(int x, int ax)//rows
   {
       x = x + ax;
       if(x < 0)
       {
           x = rows - 1;
       }
       if(x >= rows)
       {
           x = 0;
       }
       return x;
   }

   public int addY(int y, int ay)//columns
   {
       y = y + ay;
       if(y < 0)
       {
           y = cols - 1;
       }
       if(y >= cols)
       {
           y = 0;
       }
       return y;
   }

   public boolean testdirect(int x, int y, int d,String word, int ax, int ay)//checking the directions
   {
       if(d == word.length())
       {
           return true;
       }
       if(wordSearchchar[x][y] == word.charAt(d))
       {
           return testdirect(addX(x, ax), addY(y, ay), d+1, word, ax, ay);
       }
       return false;
   }

   public void wordFind()//gets the matrix of words and checks if the words are there in the scrambled words
   {
       System.out.println();
       System.out.println("The Words Are Found At [Row Column] Respectively:");
       System.out.println();
       for(int d = 0; d < words; d++)
       {
           String word = wordSearchstring[d];
           boolean foundWord = false;
           for(int i = 0; i < rows; i++)
           {
               if(foundWord == true)
               {
                   break;
               }
               for(int j = 0; j < cols; j++)
               {
                   if(wordSearchchar[i][j] == word.charAt(0))
                   {
                       if(testdirect(i, j, 0, word, 1, 0) == true)
                       {
                           System.out.println(i + " " + j);
                           foundWord = true;
                       }
                       else if(testdirect(i, j, 0, word, -1, 0) == true)
                       {
                           System.out.println(i + " " + j);
                           foundWord = true;
                       }
                       else if(testdirect(i, j, 0, word, 0, 1) == true)
                       {
                           System.out.println(i + " " + j);
                           foundWord = true;
                       }
                       else if(testdirect(i, j, 0, word, 0, -1) == true)
                       {
                           System.out.println(i + " " + j);
                           foundWord = true;
                       }
                       else if(testdirect(i, j, 0, word, 1, 1) == true)
                       {
                           System.out.println(i + " " + j);
                           foundWord = true;
                       }
                       else if(testdirect(i, j, 0, word, -1, -1) == true)
                       {
                           System.out.println(i + " " + j);
                           foundWord = true;
                       }
                       else if(testdirect(i, j, 0, word, -1, 1) == true)
                       {
                           System.out.println(i + " " + j);
                           foundWord = true;
                       }
                       else if(testdirect(i, j, 0, word, 1, -1) == true)
                       {
                           System.out.println(i + " " + j);
                           foundWord = true;
                       }
                   }
               }
           }
           if(foundWord == false)
           {
               System.out.println("NOT FOUND");
           }
       }
   }

   public void readWordSearch()//reads in the WordSearch file
   {
       String inputLine;
       Scanner fileInput;
       File inFile = new File("words.txt");
       try
       {
           fileInput = new Scanner(inFile);
           //Pulls the data from the top to make the maze
           //Sets the start and end row for a win
           rows = fileInput.nextInt();
           cols = fileInput.nextInt();
           wordSearchchar = new char[rows][cols];
           fileInput.nextLine();
           for(int i = 0; i < rows; i++)
           {
               inputLine = fileInput.nextLine();
               for(int j = 0; j < cols; j++)
               {
                   wordSearchchar[i][j] = inputLine.charAt(j);
               }
           }
           words = fileInput.nextInt();
           wordSearchstring = new String[words];
           fileInput.nextLine();
           for(int i = 0; i < words; i++)
           {
               wordSearchstring[i] = fileInput.nextLine();
           }
           fileInput.close();
       }
       catch (FileNotFoundException e)
       {
           System.out.println(e);
           System.exit(1);//an IO error and exits the program
       }
   }
   public void printWordSearch()//displays the solved array of words
   {
       System.out.println();
       System.out.println("THE WORD PUZZLE");
       System.out.println("~~~~~~~~~~~~~~~");
       for(int i = 0; i < rows; i++)
       {
           for (int j = 0; j < cols; j++)
           {
               System.out.print(wordSearchchar[i][j]);
           }
           System.out.println();
       }
       System.out.println();
       System.out.println("The Words To Be Searched Are:");
       System.out.println();
       for(int i = 0;i < words; i++)
       {
           System.out.print(wordSearchstring[i]);
           System.out.println();
       }
   }
}

SolveWordSearch.java

import java.lang.Math;
import java.util.Scanner;
import java.io.*;
public class SolveWordSearch
{
   public static void main(String [] args)
   {
       WordSearch WordSearching = new WordSearch();//object creation
       WordSearching.readWordSearch();//WordSearching object reads the word search
       WordSearching.printWordSearch();//WordSearching object prints the word search
       WordSearching.wordFind();//WordSearching object finds the word search
   }
}

words.txt

10
10
ECOMPUTERO
CEDCEVLEVR
MKUJONRPBH
HERPQYOLHF
RVTLCQTHNC
WCWECGIBCG
JHMOSMNNJG
POIUUTOERT
ZXCVBNMNMI
EDRFTGYHUJ
4
COMPUTER
CPU
MONITOR
TEST

Output:


Related Solutions

Word Find A popular diversion in the United States, “word find” (or “word search”) puzzles ask...
Word Find A popular diversion in the United States, “word find” (or “word search”) puzzles ask the player to find each of a given set of words in a square table filled with single letters. A word can read horizontally (left or right), vertically (up or down), or along a 45 degree diagonal (in any of the four directions) formed by consecutively adjacent cells of the table; it may wrap around the table’s boundaries, but it must read in the...
Are corporate taxes in the United States too high? Search the Internet for an economist’s or...
Are corporate taxes in the United States too high? Search the Internet for an economist’s or other expert’s opinion on the subject. Why would multinational corporations compare tax rates among countries before setting up business operations in a specific country?
Are corporate taxes in the United States too high? Search the Internet for an economist’s or...
Are corporate taxes in the United States too high? Search the Internet for an economist’s or other expert’s opinion on the subject. Why would multinational corporations compare tax rates among countries before setting up business operations in a specific country?
In at least a 250 word posting, respond to the following question. Is the United States...
In at least a 250 word posting, respond to the following question. Is the United States getting its money's worth in terms of health outcomes for the money spent on health care? After reading the postings of other students select the one that you found most interesting to respond to. Your response should be at least 75 words and include why you found this posting interesting.
The following questions have you search online for information that is reflective of the United States...
The following questions have you search online for information that is reflective of the United States as a whole. Be sure to explain your answer and briefly cite your source. 4. When can pregnancy and childbirth generally allow you to receive disability insurance? 5. How long does it take for Social Security benefits to start for a disability? What are the criteria for qualifying for these benefits? 6. What is a “rider” when it comes to disability insurance? 7. How...
For example, Dicks Burger is a popular fast food with multiple locations in the United States....
For example, Dicks Burger is a popular fast food with multiple locations in the United States. In order to achieve fast-growing, Dicks Burger would like to open stores in other countries. (Please response in 700-800 words) a) Identify the environmental factors (economic, social/cultural, environmental, etc.) of a new location that you think would be important to the future success of Dicks Burger. Provide detailed analysis for each environmental factor you have chosen. b) Which countries would you recommend to Dicks...
1.keying a word or phrase in search tool to find information on the internet is called?...
1.keying a word or phrase in search tool to find information on the internet is called? 2. proper positioning to avoid physical stress while keyboarding id called? 3. rules of common courtesy when working online is called? 4. key to hold down when copying files by dragging is called? 5. computer code or a program designed to harm or gain access to your computer is called? 6. key to hold down when selecting more than one file is called? 7....
in a 500 word post please discuss ‘why is MERCOSUR a trade diversion? and what are...
in a 500 word post please discuss ‘why is MERCOSUR a trade diversion? and what are the impacts of MERCOSUR on a firms operations?. In your discussion, please specify the impacts on firms from MERCOSUR member countries and firms outside MERCOSUR. inlcude works cited if used
Red Bull is the most popular energy drink in sales in the United States. Red Bull...
Red Bull is the most popular energy drink in sales in the United States. Red Bull GmbH (the parent company) has observed that daily sales are normally distributed with an average of 6,284,050 drinks sold with a standard deviation of 8,130.78. What is the probability that on a given day between 6,290,096 and 6,301,996 drinks are sold? Question 9 options: 1) We do not have enough information to calculate the value. 2) 0.0136 3) 0.2149 4) 0.0005 5) 0.7715 Question...
Write a 1,000-word answer about one of the health disparity in the United States. For example,...
Write a 1,000-word answer about one of the health disparity in the United States. For example, cardiovascular disease, cancer, diabetes, HIV/AIDS, infant mortality, asthma, or mental health. Give a synopsis of the article as well as your thoughts on how we as healthcare providers can improve the quality of care.  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT