├── .classpath
├── .gitignore
├── .project
├── README.md
└── src
├── Dictionary.java
└── WordHue.java
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Mac OS
2 | .DS_Store
3 |
4 |
5 | *.class
6 |
7 | # Mobile Tools for Java (J2ME)
8 | .mtj.tmp/
9 |
10 | # Package Files #
11 | *.jar
12 | *.war
13 | *.ear
14 |
15 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
16 | hs_err_pid*
17 | /bin/
18 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | WordHue
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # WordHue
2 | ## Description
3 | This project is for a word game named "WordHue" on ios and I write a program to solve it.
4 |
5 | The solution could be improved by discarding the string that is not a prefix of a valid word. The computing complexity is too high for a board of 5x5 while the time of 4x4 is fine. In practice I did search on 4 board of 4x4 instead of a board of 5x5. It works and I got the highest score :) Have fun.
6 |
7 | ## Reference
8 | - [How to check if a string is an valid english word](http://stackoverflow.com/questions/11607270/how-to-check-whether-given-string-is-a-word) by Mr BullyWiiPlaza.
9 |
10 | - Note that the dictionary I borrowed is from [dwyl](https://github.com/dwyl/english-words/blob/master/words.txt).
11 |
--------------------------------------------------------------------------------
/src/Dictionary.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 |
5 | /**
6 | * @author dc
7 | *
8 | */
9 | import java.io.IOException;
10 | import java.nio.file.Files;
11 | import java.nio.file.Path;
12 | import java.nio.file.Paths;
13 | import java.util.Collections;
14 | import java.util.HashSet;
15 | import java.util.Set;
16 |
17 | public class Dictionary
18 | {
19 | private Set wordsSet;
20 |
21 | public Dictionary() throws IOException
22 | {
23 | Path path = Paths.get("words.txt");
24 | byte[] readBytes = Files.readAllBytes(path);
25 | String wordListContents = new String(readBytes, "UTF-8");
26 | String[] words = wordListContents.split("\n");
27 | wordsSet = new HashSet<>();
28 | Collections.addAll(wordsSet, words);
29 | }
30 |
31 | public boolean contains(String word)
32 | {
33 | return wordsSet.contains(word);
34 | }
35 | }
--------------------------------------------------------------------------------
/src/WordHue.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 |
5 | /**
6 | * @author dc
7 | *
8 | */
9 |
10 | import java.util.Scanner;
11 | /*
12 | import java.io.BufferedReader;
13 | import java.io.FileReader;
14 | import java.io.IOException;
15 | */
16 | import java.io.IOException;
17 | import java.nio.file.Files;
18 | import java.nio.file.Path;
19 | import java.nio.file.Paths;
20 | import java.util.Collections;
21 | import java.util.HashSet;
22 | import java.util.Set;
23 |
24 |
25 | public class WordHue {
26 | private static Set wordsSet;
27 |
28 | /**
29 | * @param args
30 | * check_for_word(String word) is copied from:
31 | * http://stackoverflow.com/questions/11607270/how-to-check-whether-given-string-is-a-word
32 | */
33 |
34 | /* public static boolean check_for_word(String word) {
35 | // System.out.println(word);
36 | try {
37 | BufferedReader in = new BufferedReader(new FileReader(
38 | "/usr/share/dict/web2"));
39 | String str;
40 | while ((str = in.readLine()) != null) {
41 | if ( str.contains(word) ) {
42 | return true;
43 | }
44 | else{
45 | return false;
46 | }
47 | }
48 | in.close();
49 | } catch (IOException e) {
50 | }
51 |
52 | return false;
53 | }*/
54 |
55 | public static void search_for_word(int length, int width,
56 | char[][] board, char[][] visited,
57 | int startX, int startY, String wordStr){
58 | //get a copy of the current visited
59 | char[][] visited_XY = new char[width][length];
60 | int i,j;
61 | for(i=0; i 4)
73 | System.out.println("A word has been found: " + wordStr_addXY);
74 | }
75 |
76 | //continue recursive search
77 | //left_above
78 | if(startX-1>=0 && startX-1=0 && startY-1=0 && startX-1=0 && startY=0 && startX-1=0 && startY+1=0 && startX=0 && startY-1=0 && startX=0 && startY+1=0 && startX+1=0 && startY-1=0 && startX+1=0 && startY=0 && startX+1=0 && startY+1();
132 | Collections.addAll(wordsSet, words);
133 | } catch(IOException e){
134 | System.out.println(e);
135 | }
136 |
137 | //get the size of the board
138 | System.out.println("Please give the length and width of the WordHue board");
139 | Scanner in = new Scanner(System.in);
140 | int length = in.nextInt();
141 | int width = in.nextInt();
142 |
143 | //read the WordHue board
144 | System.out.println("Please give the WordHue board");
145 | char[][] board = new char[width][length];
146 | int i=0, j=0;
147 | for(i=0; i