Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7624623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:00:46+00:00 2026-05-31T05:00:46+00:00

I am currently writing a game that finds an inputted word in matrix in

  • 0

I am currently writing a game that finds an inputted word in matrix in snaky-ways. Here is a brief explanation for the game.

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 south, east and southeast. If it founds the word, displays the coordinates and show some message. An example matrix is below:

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

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.

Here is the search part of my code.

bool Search(tmatrix<char>& m, tmatrix<int>& c, const string& 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 < m.numrows()-1) && (m[i+1][j] == w[index+1]))
                return Search(m, c, w, i+1, j, index+1);
            else if((j < m.numcols()-1) && (i < m.numrows()-1) && (m[i+1][j+1] == w[index+1]))
                return Search(m, c, w, i+1, j+1, index+1);
            else if((j < m.numcols()-1) && (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;

}

int main()
{
   bool IsFound = false;                    //to check whether the word is found or not in the matrix
            tmatrix<int> 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 < m.numrows(); i++)
            {
                for(int j = 0; j < 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 << "The word "<< word << " is found!\n";
                        cout << "Indices in which this word is found in the matrix are:\n";
                        for(; index < word.length(); index++)
                        {
                            cout << word[index] << ":\t" << coord[index][0] << "," << coord[index][1] << endl;
                        }
                        break;      //whenever it finds a match in matrix, it finishes search in loops
                    }
                }
                if(IsFound)
                    break;
            }
}

It only goes to the direction that firstly appears on the if statement list. Changing else ifs to if didn’t work for me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:00:48+00:00Added an answer on May 31, 2026 at 5:00 am
    void Search(tmatrix<char>& m, tmatrix<int>& c, const string& w, int i, int j, int index) {
    if(m[i][j] == w[index])
    {
        c[index][0] = i;
        c[index][1] = j;
    
        if(index != w.length()-1)
        {
                if((i < m.numrows()-1) && (m[i+1][j] == w[index+1]))
                {
                    Search(m, c, w, i+1, j, index+1);
                }
                if((j < m.numcols()-1) && (i < m.numrows()-1) && (m[i+1][j+1] == w[index+1]))
                {
                    Search(m, c, w, i+1, j+1, index+1);
                }
                if((j < m.numcols()-1) && (m[i][j+1] == w[index+1]))
                {
                    Search(m, c, w, i, j+1, index+1);
                }
        }
        else
            IsFound = true;
    } }
    

    Changing the function into this solved all my problems. Thanks for your comment, they inspired me.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently writing a game of immense sophistication and cunning, that will fill you
I'm currently writing my own 2D Game Engine for a game that I would
I'm writing code for a game currently in java (newbie programmer here), but I'm
I'm currently writing a game that uses 2D OpenGL output under sdl, and I'm
I am currently working on writing small game that is written in javascript, alot
I'm currently writing a class for a game I'm working on that controls application
I am currently writing a game for android where there are enemies that fly
I'm currently writing an AI for a game that is written in c++. The
I am currently programming a game that requires reading and writing lines in a
I'm currently writing a 2-player chess game for the terminal and I'd like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.