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

  • SEARCH
  • Home
  • 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 9062727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:48:36+00:00 2026-06-16T15:48:36+00:00

I realize this code might be a bit dense to read, what I´ve tried

  • 0

I realize this code might be a bit dense to read, what I´ve tried to do is to adapt a standard recursive maze-solving algorithm – where all directions are tried until a solution is found, to an algorithm for the game ‘boggle’ that checks if the target word can formed on the grid by going in either of the four directions. I realize this code got more than one bug, but I´d like some input on how to solve the most important like that the direction and way it is searching the grid is obviously not working.

Console output:

Enter word: 
aefn
First matching letter found at [row: 0] [col: 0]
findNext: Point location: Y[0] X[0]

Point location: Y[0] X[-1]
A //(This is the algorithm trying to find "targetWord" on grid "currentWord")
Point location: Y[1] X[0]
AA
Point location: Y[0] X[0]
AAE
Point location: Y[0] X[-1]
AAEA
Point location: Y[1] X[0]
AAEAA
Point location: Y[0] X[1]
AAEAAA
Point location: Y[-1] X[0]
AAEAAAA
Point location: Y[1] X[1]
AAEE
Point location: Y[1] X[0]
AAEEU
Point location: Y[2] X[1]
AAEEUU
Point location: Y[1] X[2]
AAEEUUU
Point location: Y[0] X[1]
AAEEUUUU
Point location: Y[0] X[2]
AAEEE
Point location: Y[-1] X[1]
AAEEEE
Point location: Y[0] X[1]
AAA
Point location: Y[1] X[-1]
AAAE
Point location: Y[2] X[0]
AAAEE
Point location: Y[1] X[1]
AAAEEE
Point location: Y[0] X[0]
AAAEEEE
Point location: Y[-1] X[0]
AAAA

Code:

enum Direction { NORTH,
                 EAST,
                 SOUTH,
                 WEST };






/**  Attempts to find a matching letter on the grid to the first letter in the 
  *  targetWord, if found it stores it location in grid in 'point' and calls
  *  findNext() with the location of matching letter stored in the 'point'.  
  */
bool findMatchingFirstLetter(Grid<char> &cubeGrid, string currentWord, string targetWord, int index)
{

    for (int row = 0; row < cubeGrid.numRows(); row++)
    {
        for (int col = 0; col < cubeGrid.numCols(); col++)
        {
            if (cubeGrid.get(row, col ) == targetWord[0])
            {
                cout <<  "First matching letter found at [row: " << row << "] [col: " << col << "]" << endl;
                GPoint point(col, row);
                cout << "findNext: " << findNextRecur(cubeGrid, currentWord, targetWord, index, point) << endl;

            }
        }
    }
    return false;
}



/**  Recursive function.
  *  Exhaustively searches the grid for all possible combinations, moving in all 
  *  compass directions. */
bool findNextRecur(Grid<char> &cubeGrid, string currentWord, string targetWord, int index, GPoint point)
{
    cout << "Point location: Y[" << point.getY() << "] X[" << point.getX() << "]" << endl;
    cout << currentWord << endl;
    if (currentWord == targetWord)
        return true;
    if (currentWord.length() > targetWord.length() || 
        point.getX() > cubeGrid.numCols() ||
        point.getX() < 0 ||
        point.getY() > cubeGrid.numRows() ||
        point.getY() < 0)
    { return false; }


    for (Direction dir = NORTH; dir <= WEST; dir++ )
    {

        if (findNextRecur(cubeGrid,
                          currentWord += cubeGrid.get(point.getY(), point.getX()),
                          targetWord,
                          index,    //index is currently not in use.
                          adjacentPoint(point, dir)))
        { return true; }
    }
}



GPoint adjacentPoint(GPoint point, Direction dir)
{
    switch  (dir) {
        case NORTH: return GPoint(point.getY()-1, point.getX());
        case EAST: return GPoint(point.getY(), point.getX()+1);
        case SOUTH: return GPoint(point.getY()+1, point.getX());
        case WEST: return GPoint(point.getY(), point.getX()-1);
    }
    return point;
}


/**  Wrapper 
  */
bool findWordOnGrid(Grid<char> &cubeGrid, string word)
{
    cout << findMatchingFirstLetter(cubeGrid, "", toUpperCase(word), 0) << endl;
    return true;
}
  • 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-06-16T15:48:37+00:00Added an answer on June 16, 2026 at 3:48 pm

    Not sure if this the only problem, but currentWord += ... should only be currentWord + .... The += is changing the local currentWord variable as each of the 4 directions are probed, rather than simply passing the new currentWord into the recursive function.

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

Sidebar

Related Questions

I realize that without code this might be hard to answer but the problem
I have pieces of code like this in a project and I realize it's
I realize this might be a long shot, but does anyone have an example
I realize this might be a duplicate of Using module 'subprocess' with timeout .
This might be impossible or a bit wrong headed, however in WinForms I've got
I realise this is a bit vague but hoping someone might be able to
I realize this might be stupid, and its probably caused by my lack of
EDIT: I realized that this code compiles and works: #include <iostream> template<class Something> class
I just realized that this piece of code works well in Firefox but not
I am using the BlockingQueue code posted in this question , but realized I

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.