0

Can someone please tell me if my "won" logic will work? Will loop through each tile making sure first tile is 1, second tile is 2, etc. If a tile is not the expected number, return false(not sure if this is right) and continue playing the game. If loop is able to finish, i'm not sure how to make a function that says if the loop makes it to the end without returning false, you win. So my thought here was to finish the loop, check the first tile(because it has to be 1 in order for you to get to that check point) for victory. Thanks in advance.

    /**
 * Returns true if game is won (i.e., board is in winning configuration), 
 * else false.
 */
bool won(void)
{
// TODO
return false;

int x = 1;
do
// go through each tile from 1 to d-1//
{
    for (int row = 0; row < d; row++)
        {
           for (int column = 0; column < d; column++)
                {
                    if(board[row][column] == x)
                        {
                        continue;
                        x++;
                        }
                    else{
                        return false;}
                }


        }


}

while (x < d+1);

//if loop is able to finish, check the first tile and declare victory//
if (board[0][0] == 1)
    {
        return true;
    }

}

1 Answer 1

0

You'd have to switch the continue and the x++ (or remove that continue, as it does not make a difference).

That do-while loop does not make any sense to me, the for loops already do all the work.

Also, depending on how you internally represent the hole, (I'm not sure if you are required to do so, but I think I used 0 for that), you'd have to ignore that last value, using something like if (board[row][column] == x || row + 1 == d && column + 1 == d) (&& has higher precedence than ||)

After the nested for loops, you could simply return true; without any if. The condition you put there is always true, as the function would have returned false earlier otherwise.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .