What is going on here? 2048

I have made a function called game_over which returns a boolean based on whether a grid representing a 2048 game is in a game over situation. If it is it returns false if it is not it would return true.

The function works perfectly except in one situation where there is one zero left it would return true and I can not figure out why this is. It should if there is a zero return false as it goes into the first for loop which checks if there is a zero. What is going on here?
Last edited on
(On line 5) you are only checking the first sqrt(n) elements in the vector. Is this intended?
Last edited on

Thank you for your reply. I am using a function called twod_to_oned which allows me to used 2D indices on a vector as you can see above. Using this I am able to locate numbers in the vector using their row and coloumn index.
Why don't you just use a 2D vector?

std::vector<std::vector<int>> grid = {
    { 1, 0, 0, 1 },
    { 0, 1, 1, 0 },
    { 1, 1, 1, 1 },
    { 1, 0, 1, 1 }

And you are only checking a single "row" in your fake 2D matrix for zeroes. You should check up to v.size().
Last edited on
@dutch Than you for your reply.

The requirements for the task permits me from doing this, otherwise I would have. Regardless, these two functions should be working but it is very confusing why it is not.
If it is working with all other values except in the last part when there is only one zero left this could mean that there is some error with my other functions?
Did you fix the zero checking by making it go up to v.size()?
@dutch Thank you for your reply.

How could I not see that error. I have spent the last 7 hours trying to sort this issue out. Thank you.
That's what mbozzi meant with his post, too. :-)
Registered users can post here. Sign in or register to post.