Note that there are some explanatory texts on larger screens.

plurals
  1. POWord Hunting Game algorithm in C++
    primarykey
    data
    text
    <p>I am currently writing a game that finds an inputted word in matrix in snaky-ways. Here is a brief explanation for the game. </p> <p>User will first asked to enter a filename that contains lines to create a matrix. After the creation, user will asked to input a word to be searched in that matrix. The search is only towards <em>south, east and southeast</em>. If it founds the word, displays the coordinates and show some message. An example matrix is below: </p> <pre><code>m e r e t z e x i t a v p p w a b i y u u b l l a l l l a l z k v e l o </code></pre> <p>I have managed to search words like "exit" in this matrix but my problem is words like "mere" or "table". In my algorithm, I can only search for words that doesn't have the same letter in two directions. I couldn't find a proper way to do it. </p> <p>Here is the search part of my code.</p> <pre><code>bool Search(tmatrix&lt;char&gt;&amp; m, tmatrix&lt;int&gt;&amp; c, const string&amp; w, int i, int j, int index) // m is the matrix to search in // w is the word // i and j are coordinates of matrix { if(m[i][j] == w[index]) { c[index][0] = i; // c matrix is to keep coordinates of words c[index][1] = j; if(index != w.length()-1) { if((i &lt; m.numrows()-1) &amp;&amp; (m[i+1][j] == w[index+1])) return Search(m, c, w, i+1, j, index+1); else if((j &lt; m.numcols()-1) &amp;&amp; (i &lt; m.numrows()-1) &amp;&amp; (m[i+1][j+1] == w[index+1])) return Search(m, c, w, i+1, j+1, index+1); else if((j &lt; m.numcols()-1) &amp;&amp; (m[i][j+1] == w[index+1])) return Search(m, c, w, i, j+1, index+1); else return false; } else return true; } return false; </code></pre> <p>}</p> <pre><code>int main() { bool IsFound = false; //to check whether the word is found or not in the matrix tmatrix&lt;int&gt; coord(word.length(), 2); //another matrix to keep coordinates of found word's coordinates. //it works with the index of words and the row index of matrix. for(int i = 0; i &lt; m.numrows(); i++) { for(int j = 0; j &lt; m.numcols(); j++) { int index = 0; //another variable to keep index number IsFound = Search(m, coord, word, i, j, index); //searches matrix for word and if found, makes IsFound's return value true if(IsFound) { cout &lt;&lt; "The word "&lt;&lt; word &lt;&lt; " is found!\n"; cout &lt;&lt; "Indices in which this word is found in the matrix are:\n"; for(; index &lt; word.length(); index++) { cout &lt;&lt; word[index] &lt;&lt; ":\t" &lt;&lt; coord[index][0] &lt;&lt; "," &lt;&lt; coord[index][1] &lt;&lt; endl; } break; //whenever it finds a match in matrix, it finishes search in loops } } if(IsFound) break; } } </code></pre> <p>It only goes to the direction that firstly appears on the if statement list. Changing <code>else if</code>s to <code>if</code> didn't work for me. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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