26

This problem comes from a misunderstanding of the flow of control in your algorithm. Here's a representation of that control flow in pseudocode: if (outer-condition-1) if (inner-condition-1) return 1 else if (outer-condition-2) if (inner-condition-2) return -1 else return 0 If you translate this into a flow chart, you will see ...


8

First, run update50 to make sure you are up to date. Run appliance50 to verify that you have version 19-2. Then, run ls ~cs50/pset3 You should see the files there. As a last resort, download the files directly and place where you want them: http://d2o9nyf4hwsci4.cloudfront.net/2009/fall/psets/3/tests/3x3.txt http://d2o9nyf4hwsci4.cloudfront.net/2009/...


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


5

Recall that arrays are zero based. So if you have an array of d elements, the index goes from 0 to d - 1.


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


4

The problem is the semicolon on line 138: for (int column = 0; column < d - 1; column++); In C, a semicolon is used to terminate a statement. The body of the for loop (the part in curly braces) is part of the loop statement. If you add a semicolon after the declaration, but before the curly braces, the compiler considers the loop statement to be ...


4

A very common problem. Look at the following: else if (i < d && board[i + 1][j] == 0) This code attempts to swap with a tile that is off the edge of the board. If i = d-1, then what is i+1? Is it still within the valid range of board[][]? Same applies to j. If this answers your question, please click on the check mark to accept. Let's keep ...


3

If you keep track of the location of the blank tile, you can check based on its location and the coordinates of the number to be moved if they're swappable, at which point you just have to set the value at the blank tile to the number being moved, and the coordinates of that numbers orginal spot get saved as blank tile. This one is very easy to over think.


3

You have a multidimensional array of size [x][y] (I know that in this case x = y... it's just to make it more clear), so, you need to look for the tile you want to swap simply going through the array: for (row = 0 to row < x) { for (column = 0 to column < y) { if board[row][column] is the one I'm looking for... .... } }


3

You may use an if/else statement like if the current value is 0 // do something else // do something else


3

Welcome to SE, first of all. Now, for your problem, think that when you had made the draw function for the GoF, there you were printing the values of the tile using printf, so using that very printf's and one added if condition, you can do that, see the pseudo code below for printing the _(underscore) or a number as required If(board[i][j] == 0) { ...


3

Your function is declared as having a return type, but the structure of your function is not guaranteed to return anything. For example, if you have: bool myfunction(int x) { if(x > 10) { return true; } } That function has specified that it will return something, but if x <= 10, it will reach the end of the function without ...


3

Well, I don't know how to tell you this, but I plugged your move function into my code and it worked perfectly. I suspect that you have a problem elsewhere in your code. Have you altered something in main that you shouldn't have? Is it possible that you are reinitializing the board after every move so that it just appears that you aren't making the move? ...


3

So, it was a subtle problem and an easy mistake to make. Look at the limits on your 4 conditions: > 0 and < d. >0 works because it allows for swapping to array elements with 0 as an index. However, For example, say that d=3. Given if (board[boardy][boardx + 1] == 0) that means that in this case, if boardx = 2, then boardx+1=3, so you're trying to ...


3

You only need to delete this and the code will work: int board[d][d]; The reason is that the global variable board is declared above and can be used by any function int board[DIM_MAX][DIM_MAX]; While your board[][] - is a local variable, It appears when you call init() and disappears when init() is done. So draw() knows nothing about your board[][].


3

the hint is in 1-2-3|4-5-6|0-7-8 ie 1 2 3 4 5 6 0 7 8 Your won function will say that's a winning position, which it isn't. That same sort of scenario doesn't occur during the 4x4 gameplay. To see why this returns true when it shouldn't, add this as a line just before checking if the board value is x: printf("board[%i][%i] = %i and x = %i\n", i, j, ...


2

The board was already declared for you as a global array, so you should be setting the initial values in your init() and then drawing that array in your draw(). The draw() function is also called after every move. Your move() function will be updating the values in the array. Notice here in the fifteen.c code we were given, near the top: // board, whereby ...


2

Don't you want to swap the two tiles? You are swapping 2 new integers that happen to hold the tile values. Get rid of those 2 extra integers and simply use the tiles themselves in your swap code. Let's say I wanted to swap 2 values: int hold = firstValue; firstValue = secondValue; secondValue = hold; In your case firstValue is board[d-1][d-3] so simply ...


2

Declare hori and verti as global variables and initialize them to d - 1 just after init().


2

if (!move(tile)) is a shortcut for bool didItMove = move(tile); if (didItMove == false) { // do this } In other words, it first calls the move function (that you are writing), passing in the tile (the number that the user entered). If that function returns false (i.e., it was an invalid move), then do whatever is in the statement following.


2

The thing is: The program has declared a global variable d already. //board dimension int d; In your program, there's another declaration of d, in your a b c d initialisation. Thus, your for loop will stop right after its 1st time of running, b/c: for (int i = 0; i < d; i++) equals for (int i = 0; i < 0; i++), leaves every single a b c d = 0 ...


2

It looks like you didn't account for the corner cases where the blank is on the edge of the board. In other words, when you try to swap, i or j could go out of range, essentially swapping with something undefined and off the edge of the board. For instance, if the blank is in the bottom right corner and it tries to swap with i+1 or j+1, it will swap with ...


2

I just tested your code and got it working with a couple minor tweaks. My advice is to go look back at your global variables. Something you have in both those functions has been declared earlier in order to be accessible across all the fifteen.c functions. It's part of the distribution code. Also, on an aesthetic level, and this is only a suggestion, it ...


2

When you declare an array, such as board, you want to use the actual size of the array, without subtracting 1.


2

Simply put, if tile is in board[i][j], you need to check the positions directly above, below, and on either side for 0. In other words, board[i+1][j], board[i-1][j], board[i][j+1], board[i][j-1]. But since you're saying you are getting seg faults, I'm betting you already do this. There's another part to this. Along with checking these array elements, you ...


2

It fails because 3 out of 4 of your if conditions are wrong.


2

Wow, this is something I've never seen before. Congratulations! ;-) Your code is both right and wrong at the same time! :-D Yes, your code displays correctly and looks perfectly correct. It also appears to be working perfectly (although I didn't test thoroughly.) The problem is an interaction between your code and the check50 code. The best explanation I ...


2

You cannot check if number is even with if (d/2 == 0) use % sign (remainder sign). Number is even if you divide the number with 2 and remainder becomes 0. for example if (d%2 == 0) { //BODY } Edit: For draw: Use %2d so that each time you get space for two letters and \t tab for equal spacing. printf("%2d\t", board[i][j]); and remove the ...


2

It looks good (not a guarantee) right up until the end. What do you think will happen when you try to assign a string to an int? Instead, leave the 0 in place for the blank tile here, and use the same if statement block to print the underscores in draw(). If this answers your question, please click on the check mark to accept. Let's keep up on forum ...


2

Assuming it does compile, the move function has a couple of serious flaws. First, given the math used, the tests should be comparing to d, not d-1. This error is preventing anything on the right side and bottom from being moved. Next, two of the moves allow swapping off the edge of the board. Instead of comparing to d-1, they should be comparing to 0. ...


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