Note that there are some explanatory texts on larger screens.

plurals
  1. POTreating a bidimensional text (Ruzzle solver)
    text
    copied!<p>I am trying to program a Ruzzle solver Java application for learning purpose. I have a little problem in th ematter of "finding words" in a Ruzzle-type map.</p> <p>Example of a Ruzzle map (it is composed of 4 rows and 4 columns of 1 letter in each cell) :</p> <pre><code>Z O O H E Y L H I E L I H O F M </code></pre> <p><a href="http://www.maclife.com/files/imagecache/futureus_imagegallery_fullsize/gallery/ruzzle1.jpg" rel="nofollow">http://www.maclife.com/files/imagecache/futureus_imagegallery_fullsize/gallery/ruzzle1.jpg</a></p> <p>I would like to obtain a list of all possible words you can find in such a map. </p> <p>The difficulty : you can find a word by appending letters vertically, horizontally and diagonally (example : "HELLO").</p> <p>So far, I created 3 classes :</p> <ul> <li><p><b>Ruzzlesolver.java</b></p></li> <li><p><b>Letter.java</b></p></li> <li><p><b>Map.java</b></p></li> </ul> <h1>The Letter class</h1> <p>Describes a single Letter of the map, its fields are the X and Y positions and the character of the cell.</p> <h1>The Ruzzlesolver class</h1> <p>This is the main class.</p> <ul> <li>it reads the <i>Ruzzle map</i> (an line by line input in the console)</li> <li>it reads the <i>dictionnary.txt</i> file </li> <li>it compares the map with the dictionnary file</li> <li>it writes into the <i>results.txt</i> file</li> </ul> <p>Each line is stored in a char array. Then I create a new Map object from the 4 obtained arrays.</p> <h1>The Map class</h1> <p>This is the constructor of the Map objects :</p> <pre><code>public Map(final char[] pTab1, final char[] pTab2, final char[] pTab3, final char[] pTab4) { this.aLettres = new ArrayList&lt;Letter&gt;(); for (int i = 0 ; i &lt; 4 ; i++) { this.aLettres.add(new Letter(1, i+1, pTab1[i]));} for (int i = 0 ; i &lt; 4 ; i++) { this.aLettres.add(new Letter(2, i+1, pTab2[i]));} for (int i = 0 ; i &lt; 4 ; i++) { this.aLettres.add(new Letter(3, i+1, pTab3[i]));} for (int i = 0 ; i &lt; 4 ; i++) { this.aLettres.add(new Letter(4, i+1, pTab4[i]));} } </code></pre> <p>this.aLettres is the ArrayList that contains each of the 16 letters of the map.</p> <p>Each Letter knows its column (X position : "i+1"), its row (Y position : "1, 2, 3 and 4") and its character ("pTab[i]").</p> <p>Now that we know the map and the place of each letter, we can begin to find the words.</p> <h2>The contains() method</h2> <p>This is my problem : I am stuck using the following method :</p> <h3>How it is called</h3> <ol> <li>I pick a word from the dictionnary in the Ruzzlesolver class.</li> <li><p>I call the contains() method on my Map object, with this word as a parameter :</p> <pre><code>if (this.aMap.contains(vMot)) {/*print vMot in the result.txt file*/} </code></pre></li> </ol> <h3>How does the contains() method work</h3> <ol> <li><p>Variables :</p> <pre><code> char[] vChars = new char[pMot.length()]; ArrayList&lt;Letter&gt; vFoundCharS1 = new ArrayList&lt;Letter&gt;(); </code></pre></li> <li><p>Stocking each characters of pMot in an ArrayList :</p> <pre><code> for (int i = 0 ; i &lt; pMot.length() ; i++) { vChars[i] = pMot.charAt(i); } </code></pre></li> <li><p>Searching for the first character of pMot :</p> <pre><code> for (Letter vL : this.aLettres) { if (vL.getChar() == vChars[0]) { vFoundCharS1.add(vL); return true; } } </code></pre></li> <li>I am stuck.</li> </ol> <p><br/></p> <p>If I continue this method, I will have to create longer and longer blocks as I progress. Besides, I would need to write 16 blocks to consider every length possibility.</p> <p>I am sure this is a wrong method. How would you implement such a treatment ?</p> <p>Thanks a lot in advance for your help.</p> <p>PS : I apologize for grammar/English mistakes, English is not my natal language.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload