Write a program of wordSearch puzzle that use the following
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
Solution
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.lang.*;
public class WordSearchPuzzle {
private static final String file_name = \"text_file.txt\";
static char[][] array1;
static int row=1;
static int column=1;
public static void main(String[] args)
{
WordSearchPuzzle wsp = new WordSearchPuzzle();
wsp.readPuzzle();
int choice=-1;
do
{
System.out.println(\"Please Choose Any Option: \");
System.out.println(\"*************************************\");
System.out.println(\"1. Searching in Right Direction\");
System.out.println(\"2. Searching in Left Direction\");
System.out.println(\"3. Searching in Upward Direction\");
System.out.println(\"4. Searching in Downward Direction\");
System.out.println(\"5. Searching in Diagonal Downward Right Direction\");
System.out.println(\"6. Searching in Diagonal Downward Left Direction\");
System.out.println(\"7. Searching in Diagonal Upward Right Direction\");
System.out.println(\"8. Searching in Diagonal Upward Left Direction\");
System.out.println(\"9. Display Complete Puzzle\");
System.out.println(\"10. Exit\");
System.out.println(\"*************************************\");
Scanner in = new Scanner(System.in);
Scanner n1 = new Scanner(System.in);
choice = in.nextInt();
int x=0;
int y=0;
String word=null;
switch(choice)
{
case 1:
System.out.println(\"Enter x and y values\");
x = in.nextInt();
y = in.nextInt();
word = n1.nextLine();
System.out.println(\"Word Exists: \"+wsp.searchRight(x,y,word));
break;
case 2:
System.out.println(\"Enter x and y values\");
x = in.nextInt();
y = in.nextInt();
word = n1.nextLine();
System.out.println(\"\"+wsp.searchLeft (x,y,word));
break;
case 3:
System.out.println(\"Enter x and y values\");
x = in.nextInt();
y = in.nextInt();
word = n1.nextLine();
System.out.println(\"\"+wsp.searchUpward (x,y,word));
break;
case 4:
System.out.println(\"Enter x and y values\");
x = in.nextInt();
y = in.nextInt();
word = n1.nextLine();
System.out.println(\"\"+wsp.searchDownward (x,y,word));
break;
case 5:
System.out.println(\"Enter x and y values\");
x = in.nextInt();
y = in.nextInt();
word = n1.nextLine();
System.out.println(\"\"+wsp.searchDiagDownwardToRight(x,y,word));
break;
case 6:
System.out.println(\"Enter x and y values\");
x = in.nextInt();
y = in.nextInt();
word = n1.nextLine();
System.out.println(\"\"+wsp.searchDiagDownwardToLeft(x,y,word));
break;
case 7:
System.out.println(\"Enter x and y values\");
x = in.nextInt();
y = in.nextInt();
word = n1.nextLine();
System.out.println(\"\"+wsp.searchDiagUpwardToRight(x,y,word));
break;
case 8:
System.out.println(\"Enter x and y values\");
x = in.nextInt();
y = in.nextInt();
word = n1.nextLine();
System.out.println(\"\"+wsp.searchDiagUpwardToLeft(x,y,word));
break;
case 9:
wsp.displayPuzzle();
break;
case 10:
System.exit(1);
break;
}
}while(choice!=0);
}
private void readPuzzle()
{
BufferedReader br = null;
FileReader fr = null;
try
{
fr = new FileReader(file_name);
br = new BufferedReader(fr);
String sCurrentLine;
br = new BufferedReader(new FileReader(file_name));
if ((sCurrentLine = br.readLine()) != null)
{
try
{
int a=Integer.parseInt(sCurrentLine.replaceAll(\"[\\\\D]\", \"\"));
row=a/100;
column=a%100;
}
catch(Exception E)
{
}
}
array1=new char[row][column];
int i=0;
while ((sCurrentLine = br.readLine()) != null)
{
String[] str1=sCurrentLine.split(\" \");
char arr1[]=new char[column];
for (int j=0;j<18;j++)
{
array1[i][j]=str1[j].charAt(0);
}
i++;
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (br != null)
br.close();
if (fr != null)
fr.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
private void displayPuzzle()
{
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
System.out.print(array1[i][j]+\" \");
}
System.out.println();
}
}
private boolean searchRight ( int x, int y, String word ) {
if(x>18 | y>18 | x<1 | y<1)
return false;
if(x+(word.length())>column+1)
return false;
else
{
String main_str=null;
char main_char[]=new char[column-x+1];
int j=0;
for (int i=x-1;i<column;i++)
{
main_char[j]=array1[y-1][i];
j++;
}
main_str=String.copyValueOf(main_char);
System.out.println(\"Search String: \"+main_str);
return main_str.contains(word);
}
}
private boolean searchLeft ( int x, int y, String word ) {
if(x>18 | y>18 | x<1 | y<1)
return false;
if(x-(word.length())<0)
return false;
else
{
String main_str=null;
char main_char[]=new char[x];
int j=0;
for (int i=x-1;i>=0;i--)
{
main_char[j]=array1[y-1][i];
j++;
}
main_str=String.copyValueOf(main_char);
System.out.println(\"Search String: \"+main_str);
return main_str.contains(word);
}
}
private boolean searchDownward ( int x, int y, String word ) {
if(x>18 | y>18 | x<1 | y<1)
return false;
if(y+(word.length())>row+1)
return false;
else
{
String main_str=null;
char main_char[]=new char[column-y+1];
int j=0;
for (int i=y-1;i<column;i++)
{
main_char[j]=array1[i][x-1];
j++;
}
main_str=String.copyValueOf(main_char);
System.out.println(\"Search String: \"+main_str);
return main_str.contains(word);
}
}
private boolean searchUpward ( int x, int y, String word ) {
if(x>18 | y>18 | x<1 | y<1)
return false;
if(y-(word.length())<0)
return false;
else
{
String main_str=null;
char main_char[]=new char[y];
int j=0;
for (int i=y-1;i>=0;i--)
{
main_char[j]=array1[i][x-1];
j++;
}
main_str=String.copyValueOf(main_char);
System.out.println(\"Search String: \"+main_str);
return main_str.contains(word);
}
}
private boolean searchDiagDownwardToRight ( int x, int y, String word ) {
if(x>18 | y>18 | x<1 | y<1)
return false;
if(y+(word.length())>row+1 || x+(word.length())>column+1)
return false;
else
{
String main_str=null;
char main_char[]=new char[Math.min(row-x+1,column-y+1)];
int k=0;
int i,j;
System.out.println(Math.min(row-x+1,column-y+1));
for (i=y-1,j=x-1;i<row && j<column;i++,j++)
{
main_char[k]=array1[i][j];
k++;
}
main_str=String.copyValueOf(main_char);
System.out.println(\"Search String: \"+main_str);
return main_str.contains(word);
}
}
private boolean searchDiagDownwardToLeft ( int x, int y, String word ) {
if(x>18 | y>18 | x<1 | y<1)
return false;
if(x<word.length() || y+(word.length())>column+1)
return false;
else
{
String main_str=null;
char main_char[]=new char[Math.min(row-y+1,x)];
int k=0;
int i,j;
System.out.println(Math.min(row-y+1,x));
for (i=y-1,j=x-1;k<Math.min(row-y+1,x);i++,j--)
{
main_char[k]=array1[i][j];
System.out.println(\"\"+main_char[k]);
k++;
}
main_str=String.copyValueOf(main_char);
System.out.println(\"Search String: \"+main_str);
return main_str.contains(word);
}
}
private boolean searchDiagUpwardToRight ( int x, int y, String word ) {
if(x>18 | y>18 | x<1 | y<1)
return false;
if(y<word.length() || x+(word.length())>column+1)
return false;
else
{
String main_str=null;
char main_char[]=new char[Math.min(y,column-x+1)];
int k=0;
int i,j;
System.out.println(Math.min(y,column-x+1));
for (i=y-1,j=x-1;k<Math.min(y,column-x+1);i--,j++)
{
main_char[k]=array1[i][j];
System.out.println(\"\"+main_char[k]);
k++;
}
main_str=String.copyValueOf(main_char);
System.out.println(\"Search String: \"+main_str);
return main_str.contains(word);
}
}
private boolean searchDiagUpwardToLeft ( int x, int y, String word ) {
if(x>18 | y>18 | x<1 | y<1)
return false;
if(x<(word.length())|| y<word.length())
return false;
else
{
String main_str=null;
char main_char[]=new char[Math.min(x,y)];
int k=0;
int i,j;
System.out.println(Math.min(x,y));
for (i=y-1,j=x-1;k<Math.min(x,y);i--,j--)
{
main_char[k]=array1[i][j];
k++;
}
main_str=String.copyValueOf(main_char);
System.out.println(\"Search String: \"+main_str);
return main_str.contains(word);
}
}
}
Output:-
--------------------Configuration: <Default>--------------------
Please Choose Any Option:
*************************************
1. Searching in Right Direction
2. Searching in Left Direction
3. Searching in Upward Direction
4. Searching in Downward Direction
5. Searching in Diagonal Downward Right Direction
6. Searching in Diagonal Downward Left Direction
7. Searching in Diagonal Upward Right Direction
8. Searching in Diagonal Upward Left Direction
9. Display Complete Puzzle
10. Exit
*************************************
1
Enter x and y values
1
2
MEMORY
Search String: LIAMEMORYMMOUSENIL
Word Exists: true
Please Choose Any Option:
*************************************
1. Searching in Right Direction
2. Searching in Left Direction
3. Searching in Upward Direction
4. Searching in Downward Direction
5. Searching in Diagonal Downward Right Direction
6. Searching in Diagonal Downward Left Direction
7. Searching in Diagonal Upward Right Direction
8. Searching in Diagonal Upward Left Direction
9. Display Complete Puzzle
10. Exit
*************************************







