#marble drop game, logic not working

23 messages · Page 1 of 1 (latest)

cyan sedgeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

glad fern
#

The problem is that you start at just below the +, and you can only ever go straight down, or diagonally - so you just have to start straight down, and diagonally down

main spindle
#

so change my int row = 1?

glad fern
main spindle
#

Yeah i changed the int row = 0, and it doesnt write to anything

#

but overwriting the + is how i get the marbles to print into the box

glad fern
main spindle
#

why a '+'?

#

If I start a row = 1, itll never equal '+'

#

and starting a row = 0 kills my program

#

You are comepletly right , sorry

#

Only thing is it replaced my '+' with a 'o' (marble) for some reason

glad fern
#

In that case, you can also do a for loop to start one to the left, straight down, and one to the right of (1, startingColumn); this is the alternative solution, which IMHO is uglier than starting at the +

main spindle
#

It works now with your logic

#

just that it replaced my '+' with a marble , which i assume is the last iteration

glad fern
#
bool dropMarble(vector<string> &box, int openingCol) 
{
    int row = 0; // Start at the opening
    int col = openingCol;

    if (box[row][col] != '.' && box[row][col] != '+') 
    {
        return false;  
    }
    while (row < box.size() - 1) 
    {  
        if (box[row + 1][col] == '.') 
        {
            row++;  // Fall straight down
        } 
        else if (col > 1 && box[row + 1][col - 1] == '.') 
        {
            row++; col--;  // Move left
        } 
        else if (col < box[0].size() - 2 && box[row + 1][col + 1] == '.') 
        {
            row++; col++;  // Move right
        } 
        else if (box[row][col] == '.') 
        {
            box[row][col] = 'o';
            return true; 
        }
        else
        {
            return false;
        }
    }

    return false;  
}

This is what I'm thinking should work, assuming the calling code works as I imagine

main spindle
#

Thank you so much 🙏

#

!solved

cyan sedgeBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

#

@main spindle

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.

#

@main spindle Has your question been resolved? If so, type !solved :)

main spindle
#

!solved