I've been trying to write the won function for days, but it has never worked correctly. I can't find any flaws in my logic, so can somone help me figure out what I'm doing wrong?

ebool won(void)
int n = d * d - 1;
int x= 0;
int y= 0;

for (int i = 1; i <= n; i++)
    if (board[x][y]!= i)
        return false;


    if (x == d)
        x = 0;

return true;
  • return type of the function is "ebool"!, that should be "bool" imo.
    – sinister
    Aug 31, 2014 at 3:56
  • btw can you explain your logic?
    – sinister
    Aug 31, 2014 at 5:02

1 Answer 1


I didn't get the algorithm(logic) you used to tackle the problem.

Lets take a look, this is the configuration depicting the already won game.

enter image description here

To implement the won() function, we need check whether each tile is placed at its corresponding index. For this, we need to check the value of each and every board[rth_row][cth_column] by traversing(moving) from board[0][0] to board[d-1][d-1]. This can be done as

for (i = 0 ; i < d ; ++i)
      for (j = 0 ; j < d ; ++j)
           // do check for the value of board[i][j]

You can set another variable(say counter) to 1 before the loops, and increment it by one inside the inner loop so that you can check the value of board[i][j] with counter. If the values aren't equal, then you can return false, no else part should be there for now. At the end of the function, you can simply return true because execution will fall out of the loop only and only if each and every board[i][j] has the expected value.

But there is a problem, I have not mentioned a corner case. Find it yourself and also tackle it. Best of Luck.

  • I had a similar approach as your suggestion but I was stuck somewhere until i saw your clarification, it was very helpful to me. Thanks!
    – cashmisa
    Apr 21, 2017 at 12:26

You must log in to answer this question.

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