This article describes the Java implementation of the word search Maze game. Share to everyone for your reference. The specific analysis is as follows:
We often see words in magazines, in a two-dimensional form, there are all kinds of letters, we can look for words in eight directions. This is very convenient to use computer processing, but the quality of the algorithm is very important, because if the brute force algorithm to achieve, then the time-consuming time is unimaginable.
This is the data structure and problem solving in the Java language description of the book to achieve the idea
The complete code is as follows, the comment is very clear.
Import Java.io.BufferedReader;
Import Java.io.FileReader;
Import Java.io.InputStreamReader;
Import java.io.IOException;
Import java.util.ArrayList;
Import Java.util.Arrays;
Import java.util.List; /** * Word Search Maze * */public class Wordsearch {/** * Constructs two input streams in the constructor, the input stream of words, and the input stream of the form */public wordsearch () throws
IOException {puzzlestream = OpenFile ("Enter table file path:");
Wordstream = OpenFile ("Enter Word file path:");
SYSTEM.OUT.PRINTLN ("file read ...");
Readpuzzle ();
Readwords (); /** * @return matches total number of Word matches * to search in eight directions per position * Rd represents an increment on the line, Eg:rd=-1, indicating that the up row * CD represents the increment eg:cd=-1 on the column. To the left one step * So Rd=1,cd=0 said that the south * Rd=-1,cd=0 said North, * Rd=-1,cd=1, said the northeast * * public int solvepuzzle () {int matches
= 0; for (int r = 0; r < a r++) for (int c = 0; c < columns C + +) for (int rd =-1; rd <= 1; rd++ for (int cd =-1; CD <= 1; cd++) if (rd!= 0 | | | CD!= 0) matches + = Solvedirecti On (R, C, Rd, CD);
return matches; /** * In the specified coordinates, search in the given direction, return the number of matching words * @return of matches/private int solvedirection (int baserow, I
NT Basecol, int rowdelta, int coldelta) {String charsequence = "";
int nummatches = 0;
int SearchResult;
Charsequence + = theboard[baserow [Basecol];
for (int i = Baserow + Rowdelta, j = basecol + Coldelta;
I >= 0 && J >= 0 && i < rows && J < columns;
i + = Rowdelta, j + + Coldelta) {charsequence + = theboard[I [j];
SearchResult = Prefixsearch (Thewords, charsequence);
/** * below if (SearchResult = = thewords.length) * Must be judged, otherwise there will be a danger of crossing the line, and when the last word matches the prefix, the return index-1 * * * * * *
if (SearchResult = = thewords.length) break; /** * If there is no response prefix, skip the search, even if you continue to search, do not work * * */if (!thewords[searchresult].startswith (charsequence
)) break; if (thewords[SearchResult].equals ( charsequence)) {nummatches++;
System.out.println ("found" + charsequence + "in" + baserow+1 + "Row" + Basecol + "column" +
i + "" + j);
} return nummatches; /** * First explains Arrays.binarysearch (object[], object) * uses a binary search algorithm to search for the specified array to get the specified object.
Before making this call, * must sort the group in ascending order based on the natural sequence of the array elements (through the sort (object[) method above). * If no array is sorted, the result is ambiguous. (if the array contains elements that cannot be compared to each other (for example, strings and integers), * cannot sort the array according to the natural order of the element, so the result is ambiguous.
* If the array contains more than one element equal to the specified object, you cannot guarantee which one is found.
* * private static int Prefixsearch (string [] A, string x) {int idx = Arrays.binarysearch (A, x);
if (IDX < 0) return-idx-1;
else return idx;
/** * Read the contents of the file, get input stream/private BufferedReader OpenFile (String message) {string fileName = "";
FileReader thefile;
BufferedReader Filein = null;
Do {System.out.println (message + ":");
try {fileName = In.readline (); if (fIlename = = null) system.exit (0);
Thefile = new FileReader (fileName);
Filein = new BufferedReader (thefile); catch (IOException e) {System.err.println ("Cannot open" + FileName);}
while (Filein = = null);
System.out.println ("opened" + FileName);
return Filein;
/** * Read FORM * * */private void Readpuzzle () throws IOException {String oneline;
list<string> puzzlelines = new arraylist<string> ();
if ((Oneline = Puzzlestream.readline ()) = = null) throw new IOException ("No lines in Puzzle file");
columns = Oneline.length ();
Puzzlelines.add (oneline); while (Oneline = Puzzlestream.readline ())!= null) {if (Oneline.length ()!= columns) System.err. println ("puzzle is not rectangular;
Skipping row ");
else Puzzlelines.add (oneline);
rows = Puzzlelines.size ();
Theboard = new char[rows [columns]; Int R = 0;
for (String theline:puzzlelines) theboard[r++] = Theline.tochararray (); /** * Read the list of words that have been sorted by dictionary/private void Readwords () throws IOException {list<string> words = new A
Rraylist<string> ();
String Lastword = null;
String Thisword; while ((Thisword = Wordstream.readline ())!= null) {if (Lastword!= null && thisword.compareto (las
Tword) < 0) {System.err.println ("not sorted in dictionary order, skipped");
Continue
} words.add (Thisword.trim ());
Lastword = Thisword;
} thewords = new string[words.size ()];
Thewords = Words.toarray (thewords);
}//Cheap main public static void main (String [] args) {Wordsearch p = null;
try {p = new wordsearch ();
catch (IOException e) {System.out.println ("IO Error:");
E.printstacktrace ();
Return
} System.out.println ("Searching ...");
P.solvepuzzle (); private int rows;
private int columns;
Private Char [] theboard;
Private String [] thewords;
Private BufferedReader Puzzlestream;
Private BufferedReader Wordstream;
Private BufferedReader in = new BufferedReader (new InputStreamReader (system.in));
}
I hope this article will help you with your Java programming.