0

I used the cs50 sample 3x3.txt and 4x4.txt to check my code. I saw 'ftw' output for both cases so it works. I have checked my move() function manually and it works but when I use check50 to check for error I get two errors I can't really understand.

:( 3x3 board, from start try to move tile 2, 4, 5, 6, 7, 8 \ expected "5" character 1 of line 14, not "0":) 3x3 board, move blank left (tile 1) then up (tile 4), then try to move tiles 1, 2, 6, 8 :( 3x3 board, move blank up (tile 3), then try tile 2 \ expected "0" character 5 of line 10, not "2":) 3x3.txt wins the 3x3 board

Can some one tell me if there is a logical error in the code. Here is the code I wrote https://gist.github.com/Abel-2016/044186e6652125e2cb7fccc615d35a79

1

output "ftw" doesn't mean by itself that all is fine.

The first line of check50 error says that something is wrong with "5" tile. And indeed, it is.

The positions in the beginning are: "5" is at [1][0], "0" is at [2][2]. So if you'd try to move "5" you should fail. Let's check your code for move().

    if ( (i_tile == i_empty + 1 || i_tile == i_empty - 1) && (j_tile != j_empty + 1 && j_tile != j_empty - 1) ) {
    int tmp = board[i_empty][j_empty];
    board[i_empty][j_empty] = board[i_tile][j_tile];
    board[i_tile][j_tile] = tmp;
    return true;

i_empty == 2
j_empty == 2

i_tile == 1 == i_empty - 1
j_tile == 0 != j_empty - 1 != j_empty + 1

So, the condition is true and you swap "5" and "0".

| improve this answer | |
  • Thanks Bro. I have been stuck on it for quite a while. – Abel Jul 19 '17 at 10:24

You must log in to answer this question.

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