Hot answers tagged

7

As correctly pointed out by Kareem it is quite confusing when you start to debug the long 'if' condition and it may also at times give you wrong answer. I would suggest an alternative approach to such problems which require comparisons. You might want to initiate a counter that increments every time an array element matches the winning array element of a ...


4

Following your code, there are some issues. First, it's not a good practice to have such looong line of code. It's recommended by the CS50 staff that your line shouldn't exceed 80 characters. Second, the purpose of won() is to check whether the tiles are in ascending order followed by the blank tile. If you've tried to follow your code, or probably use gdb ...


2

You are checking if everything is alright. And if the first part is alright you are returning true right away. This piece of code is exiting if the second box is correct without checking the rest of the board. if (board[d - 1][d - 1] == 0) { if (board[0][0] == 1 && board [i][j - 2] < board[i][j - 1]) { return true; } } ...


2

What happens at i == j == (d-1)? That's the last tile and it should equal 0, but your test will fail because of that. If that answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)


2

You return true too early. You only check whether all the tiles are in the correct order, but you don't check the position of the empty tile. A quick fix would be to replace if (min == d*d) { return true; } else { return false; } with return (min == d*d) && (board[d-1][d-1] == 0); which additionally ensures that the last tile is the empty ...


2

You are definitely on the right track and are very close. Hmmm.... Let's look at the inside block of code: while(board[i][j] != board[d-1][d-1]) { if(board[i][j] == counter) { counter++; } else { return false; ...


1

I see a couple of issues, although I don't immediately see why it is "timing out". The code depends on two if statements that are NOT interdependent. Let's walk through the code to understand what happens. Say that the code is testing the first square, which should contain 1. Assume that it does. The first if statement evaluates as true, so count is ...


1

Hmmm... where have I seen this before? Oh yeah..... http://cs50.stackexchange.com/questions/2448/how-to-solve-control-may-reach-end-of-non-void-function-error http://cs50.stackexchange.com/questions/1647/how-to-fix-control-may-reach-end-of-non-void-function-and-no-such-file-or-dir Check these links. If this answers your question, please click on the ...


1

This won function won't work, imagine what would happen for i == d-1 and j == d-1 on a solved board. counter then would be d*d-1, and board[i][j]!=counter would be 0!=d*d-1, leading to the solved board to be rejected. I used something like if (board[i][j] != counter % (d*d)) { return false; } counter++; with the remainder operator %, which evaluates ...


1

There are more elegant implementations, but most notably: if(board[i][j] != n) { return false; n++; } The n++ is never performed. You probably meant something like if(board[i][j] != n) { return false; } else { n++; } Same for the second ...


1

Basically, you code will always return true when 0 and 1 in the right positions, no matter where the other tiles are. Mistakes: You loop not from 0 to d - 1, but from 0 to d - 2 (i < d - 1). Thus, for d = 3 you check just 2*2 not 3*3 Even if you correct #1, the problem is still here, as you your use j - 2 and j - 1. So for i = 0, j = 0, you compare ...


1

A trick I like to use when debugging indexed ranges (for loops, for example) is to see what happens at the upper and lower bounds of the ranges. A lot of times, the code breaks down there.


1

Thanks to @Cliff B for editing. It was impossible to read before. To @Genevie. Move(). if (board[i][j] == 0) { blank = board[i][j]; } What it is for? blank is already equals 0. This part of the code makes no harm, however. It is just useless. Then next two conditions (for up and down tiles) are absolutely correct. And in the following two (for ...


1

Why do you loop till j < d - 1 not j < d? For board 3*3, you miss board[0][2] so should return false for board[1][0]. However, I do not understand why you are stack in an infinite loop. You should return false. I suspect that something else might be wrong with your code. Also, please note that by most programmers goto is considered as bad style.


1

First, note that you change n only if something is wrong. Let's suppose that board [0][0] = 1 as it should be. Then, this condition else if (board[i][j] != n) { n = n+ (d*i)+j; return false; } doesn't work so you still have n == 1, and continue to check board[0][1]. Suppose it is 2 as it should be. You check else ...


1

I suspect that a 4x4 can also generate false wins, given the right sequence of inputs. The problem is that the won() function, as written, can report a win when the tiles are in a certain order. If the blank tile is out of place but the remaining tiles are in sequential order, it will result in a win being reported. A simple fix would be to check that ...


1

A possible pseudocode for win would be: for row for column // in each movement, if the position of board [i] [j] coincides with // the expected value increases // the counter in 1 if some value differs from the expected we left won if board[i][j] == expected value count++ else return false ...


1

Well, the sandbox output says its working, but I'm thinking that it isn't the result of your program. Having said that, let's look at your won() code. In short, this code will always fail. It will start by setting x=1 and then trying to compare every tile to x before incrementing x. So, if the board is right, it will compare x to the first tile, see that it ...


1

The following line has serious issues: if(board[i][j] != board[i][j-1] || board[i][j] != board[i+1][j-2]) If j == 0, then it's going to try to check board[i][-1], an invalid tile. This will produce unpredictable results. Same logic applies when i == d-1 and the second clause. If i == d-1, then board[d][j-2] is invalid. Same again for j-2. Next, no ...


1

Time for some tough love here. There are a lot of problems - almost every line of code. The error basically says that you're getting a premature false positive from win. Let's look at the if statement. It checks for 1 in the first or upper left tile, which is correct. Next, it looks for (d*d)-1 in the lower right. If all the tiles contained a number in ...


1

HINT: What is the value of the lower right hand corner tile supposed to be when the game is won? What is your code looking for? If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)


1

You have a typo. Either that, or you made an error in your formula. if (board[i][j]!=1+j+d*j)


1

What that message means is that your function is not returning true when it should. Before running check50, you should "play" the game with your program and make sure it works properly. You have the .txt files that you can pipe to make sure it works. I'm not 100% sure, but what I'm thinking the problem may be is this: Your counter variable is initialized ...


1

There are several issues. First, the coordinates for board[][] start at [0][0], not [1][1], so you are not processing the top row or the left column. Similarly, by checking <=d, you're looking at invalid array locations off the right side of the board or below the bottom row when line or column actually equals d. Remember, an array with n elements in a ...


1

Consider what happens during the loop: i=0, j=0 if board[0][0] > board[0][1] i=0, j=1 if board[0][1] > board[0][2] i=0, j=2 if board[0][2] > board[0][3] // ??? // does board[0][3] exist? // what if the value in it is zero because it's not part of the board? // won't you always return false? Might a simpler test be to see if board[0][0] != 1 ...


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


1

I'm not quite sure how that works with any size. First time through the loop: say d = 4 i = 0 j = 0 if 0 == 3 //no else if 0 > 0 //no else if board[0][0] < board[-1][3] //board[-1][3] ?? is off the grid Consider the winning position: 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 board[0][0] is 1 (i is 0, j is 0) board[0][1] is 2 (i is 0, j ...


Only top voted, non community-wiki answers of a minimum length are eligible