Can anyone tell me why the code doesn't work as I had expected ? It keeps returning ftw even if it's not.
1 Answer
Let's break down this conditional
if (j > 0 && board[i][j] < board[i][j - 1] && board[d - 1][d - 1] == 0)
to see why it is "always" returning true. It cannot return true if j is 0, so when i is 0 and j is 1:
j > 0
? true
board[i][j] < board[i][j - 1]
=> board[0][1] < board[0][0]
=> 7 < 8
? true
board[d - 1][d - 1] == 0
? true
true && true && true evaluates to true, returns true, gives ftw!
The won function can be written without comparing two board locations. Think about init
and how each board location is assigned to a decreasing counter (with maybe some special handling for the swap). Now turn it inside out. To validate a winning board perhaps compare each board location to an increasing counter (with maybe some special handling for the 0 tile).