Question

Write a program of wordSearch puzzle that use the following text file as an input. The...

Write a program of wordSearch puzzle that use the following text file as an input. The output should be like this: PIXEL found (left) at (0,9). ( Use JAVA Array ) .Please do not use arrylist and the likes!

Hints
• The puzzle can be represented as a right-sized two-dimensional array of characters (char).
• A String can be converted into a right-sized array of characters via the String method toCharArray.

. A word can occur in any of 8 directions starting from a square in the puzzle(but not enter by the users):
o to the right (normal text: MEMORY above)
o to the left (backwards: PIXEL)
o downwards (INTERNET)
o upwards (GOOGLE)
o diagonally downwards to the right (APPLICATION)
o diagonally downwards to the left (CHAT)
o diagonally upwards to the right (WIRELESS)
o diagonally upwards to the left (PROGRAMMER)
• Consider writing separate methods such as:
private boolean searchRight ( int x, int y, String word ) {
that look for a word starting from position (x,y) in the puzzle and proceeding
in the designated direction (right in this case). The method returns true if the
word is found in the designated direction starting from the indicated position. You
would have 8 such methods. Each of these methods will be quite similar.
• A word cannot occur starting from (x,y) if there isn’t enough room from
(x,y) to the edge of the puzzle in the designated direction. For example, if the
puzzle is 10×10, we are checking from position (3,7) to the right and the word
is more than 3 characters in length.
• Consider developing the solution in phases such as:
1. read and display puzzle
2. check for a single word in one direction only (e.g. right)
3. repeatedly add the ability to check for a single word in each of the other 7 directions
4. add the ability to search for multiple words.
• Don’t use the big puzzle for testing. Make special small puzzles to test each of the
8 search methods above such as the included puzzle right.txt that has only one word written to the right.

text file

18 18
T P I R C S A V A J L E X I P I G E
L I A M E M O R Y M M O U S E N I L
C R A B K S A T X I N U Y H S T F G
D N D I R E C T O R Y E T A O E O O
P O W E R S U P P L Y N I R F R L O
U C O A S A E V A S S C R E T N D G
K I R O P K T Y P S H R U W W E E L
C D D E C P R E E A H Y C A A T R M
A N R I M A L L T D R P E R R E A T
B O L E N M E I E K E T S E E P H H
R C K I P R A F C V R I I R S U L M
E E B E I A R R I A B O O T M B O R
N S T W R A P R G R T N W B I N G O
N O O S G N D L O O D I N T I O I S
A N G M A K A U L A R A O T E A N R
C A E A S P T L T A I P O N R N D U
S N F I R E W A L L W R E I K O O C
T F D P R D H T O O T E U L B Y T E
JAVASCRIPT
PIXEL
INTERNET
GIF
GOOGLE
LCD
EMAIL
MEMORY
MOUSE
SHAREWARE
TASKBAR
UNIX
SECURITY
SOFTWARE
FOLDER
ICON
DIRECTORY

Homework Answers

Answer #1
/**
 * @fileName WordSearch.java
 * @author 
 * @since 24/1/17
 */


package wordpuzzel;


import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Arrays;
import java.util.Scanner;

public class WordSearch {

    private static char[][] board;
    private static int row;
    private static int col;
    private static String[] words;

    /**
     * @param fileName
     * @throws FileNotFoundException
     */
    public static void readPuzzle(String fileName) throws FileNotFoundException {

        Scanner sc = new Scanner(new FileReader(fileName));
        row = sc.nextInt();
        col = sc.nextInt();
        board = new char[row][col];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                board[i][j] = sc.next().charAt(0);
            }
        }
        int count = 0;
        String str = "";
        while (sc.hasNext()) {
            str += sc.nextLine() + " ";
        }
        words = str.trim().split(" ");


    }

    /**
     * This function will display the puzzle
     */
    public static void displayPuzzle() {
        for (char[] word : board) {
            System.out.println(Arrays.toString(word));
        }
    }

    public static void searchRight(String word) {

        int i, j, k;
        for (i = 0; i < row; i++) {
            for (j = 0; j < col; j++) {
                int count = 0;
                for (k = j; k < col && count < word.length(); k++) {
                    if (word.charAt(count) != board[i][k]) {
                        break;
                    }
                    count++;
                }
                if (count == word.length()) {
                    System.out.println(i + " " + (k - 1));
                    System.out.println(word+"found(right) at ("+i + "," + (k -1)+")");
                }
            }
        }
    }


    public static void searchLeft(String word) {

        int i, j, k;
        for (i = row - 1; i >= 0; i--) {
            for (j = col - 1; j >= 0; j--) {
                int count = 0;
                for (k = j; k >= 0 && count < word.length(); k--) {
                    if (word.charAt(count) != board[i][k]) {
                        break;
                    }
                    count++;
                }
                if (count == word.length()) {
                    if (k == -1)
                        System.out.println(word+"found(left) at ("+i + "," + (k + 1)+")");
                    else
                        System.out.println(word+"found(left) at ("+i + "," +k +")");
                }
            }
        }
    }

    public static void searchDownwards(String word) {

        int i, j, k;
        for (i = 0; i < row; i++) {
            for (j = 0; j < col; j++) {
                int count = 0;
                for (k = j; k < col && count < word.length(); k++) {
                    if (word.charAt(count) != board[k][i]) {
                        break;
                    }
                    count++;
                }
                if (count == word.length()) {

                    System.out.println(word+"found(downwards) at ("+j + "," + i+")");
                }
            }
        }
    }

    public static void main(String[] args) throws FileNotFoundException {
        readPuzzle("input.txt");
        displayPuzzle();
        System.out.println(Arrays.toString(words));
        searchRight("MEMORY");
        searchLeft("JAVASCRIPT");
        searchLeft("PIXEL");
        searchDownwards("INTERNET");
    }

}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Read the following cases and decide the right supplement for each case. Highlight the supplement name...
Read the following cases and decide the right supplement for each case. Highlight the supplement name by finding the word from the given box. Follow the given example: Example: Naif has an infection. He needs an antibiotic Maryam cannot digest milk. She needs to have ___lactase______ supplements Majid is overweight. He decided to buy _________ supplements to help him lose weight. Muntaha’s nails always breaks so easily. She wants to get _________ supplements. Munther face looks older than his age....
A student would like to assess whether the mean amount of money spent on books during...
A student would like to assess whether the mean amount of money spent on books during the 2016-2017 school year is equal for all students in each of the four class ranks at California University (1: Freshmen, 2: Sophomores, 3: Juniors, 4: Seniors). To conduct this study, the student took a large random sample of students at California University and recorded for each student the total amount of money spent on books during the 2016-2017 school year and the class...
Write a C++ program to demonstrate thread synchronization. Your main function should first create a file...
Write a C++ program to demonstrate thread synchronization. Your main function should first create a file called synch.txt. Then it will create two separate threads, Thread-A and Thread-B. Both threads will open synch.txt and write to it. Thread-A will write the numbers 1 - 26 twenty times in nested for loops then exit. In other words, print 1 - 26 over and over again on separate lines for at least 20 times. Thread-B will write the letters A - Z...
Consider permutations of the 26-character lowercase alphabet Σ={a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}. In how many of these permutations do a,b,c...
Consider permutations of the 26-character lowercase alphabet Σ={a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}. In how many of these permutations do a,b,c occur consecutively and in that order? In how many of these permutations does a appear before b and b appear before c?
Part IV: All of the following arguments are VALID. Prove the validity of each using NATURAL...
Part IV: All of the following arguments are VALID. Prove the validity of each using NATURAL DEDUCTION. 1) 1. H É I 2. U v ~I 3. ~U 4. H v S                                               / S 2) 1. (I × E) É (F É Y) 2. Y É ~C 3. I × E                                                 / F É ~C 3) 1. L v O 2. (F v C) É B 3. L É F 4. O É C                                              / B 4) 1. K ×...
2. Let A = {p, q, r, s}, B = {k, l, m, n}, and C...
2. Let A = {p, q, r, s}, B = {k, l, m, n}, and C = {u, v, w}, Define f : A→B by f(p) = m, f(q) = k, f(r) = l, and f(s) = n, and define g : B→C by g(k) = v, g(l) = w, g(m) = u, and g(n) = w. Also define h : A→C by h = g ◦ f. (a) Write out the values of h. (b) Why is it that...
For each of the following bases, identify its conjugate acid.       -       A....
For each of the following bases, identify its conjugate acid.       -       A.       B.       C.       D.       E.       F.       G.       H.       I.       J.       K.       L.       M.       N.       O.    HSO3-       -       A.       B.       C.       D.       E.      ...
Match the statement to the correct answer. (Some reagents can be used more than once.) The...
Match the statement to the correct answer. (Some reagents can be used more than once.) The flowchart along with the report sheets in experiment 5 should be very helpful. A 6M Nitric Acid (HNO3) B Colbalt Nitrate (Co(NO3)2) C Disodium EDTA (Na2EDTA) D Aluminum Nitrate (Al(NO3)3) E Zinc Nitrate (Zn(NO3)2 F Barium Nitrate (Ba(NO3)2) G 6M Hydrochloric Acid (HCl) H OH-, CO32-, and PO43- I Lead chloride (PbCl2(s)) J 0.1M sodium carbonate (Na2CO3) K NH4+(aq) and OH-(aq) L Silver chloride...
You have data on birth weights and a number of child, mother, and family characteristics. In...
You have data on birth weights and a number of child, mother, and family characteristics. In particular, you are interested in the relationship between birth weight in ounces (bwght) and the sex of the child (male = 1 if baby is male and = 0 if baby is female), the number of years of mother’s education (motheduc), and family income in $1000s (faminc). An “l” prefix indicates the natural log of the variable. For example, lbwght is log(bwght). The general...
*******please don't copy and paste and don't use handwriting **** Q1: Using the below given ASCII...
*******please don't copy and paste and don't use handwriting **** Q1: Using the below given ASCII table (lowercase letters) convert the sentence “welcome to cci college” into binary values. a - 97 b - 98 c - 99 d - 100 e - 101 f - 102 g - 103 h - 104 i - 105 j - 106 k - 107 l - 108 m - 109 n - 110 o - 111 p - 112 q - 113...