0

Can anyone tell me why the code doesn't work as I had expected ? It keeps returning ftw even if it's not.

enter image description here

enter image description here

1 Answer 1

0

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).

You must log in to answer this question.

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