Hot answers tagged

3

You are using a comparison symbol that does not exist. Change !== with != and that should do the work for the while statement. About the else I can't really help without some more information... Did you open close your braces correctly? Please post the code section if possible. Good Luck! Edit: ok this is clear now. The while statement has no else branch. ...


3

The int variable named d is a global variable. Global variables are variables that are declared outside of any functions. They can be accessed from anywhere in the source code file and that's why we don't need the function init to accept an int as an argument — because we can access d directly inside it. Usually we use global variables when we need to ...


3

You have two serious problems here. First, your move() function defines int blank = board[d][d]; The array element board[d][d] doesn't exist because both indexes run from 0 to d-1. Anything with d as an index is out of range. Even if it had d-1 as the two indexes, it's still broken because the blank cell is only known to be in the lower right when the game ...


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


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

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

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

"Give a man a fish and you feed him for a day. Teach him to fish and you feed him for a lifetime." You are currently printing out the tile values with %i as the placeholder for the tile in your statement printf("%i", board[j][i]); Instead, you need to change that placeholder to include formatting. You can actually specify how many columns to use to print ...


2

The reason that it only draws the initialized board is that move() is not swapping any tiles. The code is setting several int vars equal to values in the board[][] array, effectively making copies of the originals, and then swapping those copies. This does not swap the actual values in the board[][] array, so everything in the board array remains unchanged. ...


2

It probably is executing, but not the way you think. You're lucky it isn't seg faulting. (Well, actually, a seg fault would be better. ) Think about the array indexes that you're using. Remember that the array is a 2-d array with d elements in each dimension. More importantly, remember that the dimension indexes start at 0, not 1, so the largest valid ...


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

Simply put, there is a missing closing curly brace, }, in the draw() function. It looks to be missing at the first if statement code block. Because of the mispairing/missing closing curly brace, it appears to the compiler that the code is trying to declare a new function within another function which has not closed. This will cure the errors you see, but ...


2

Remember that you already got d from command line input, so there is no need for d=GetInt() in init() function. If on the other hand you are not providing d at command line, that would account for the invalid input error you are getting.


2

I think it's ok to search for both tile and empty tile, though you could maintain global variable(s) to store the position of the empty tile, some even keep track of the positions of all tiles, usually using one index, calculated as d*row+col. You have a line return false; after your for-loop, that will leave the function without moving the tile. You have ...


2

Resolved! I was watching week 4s lecture and realised that my swap didn't make sense, I was expecting the program to switch two values at the same time. So I used a temp variable to store the value of blank tile, then moved the tile value to the blank tile and finally stored the value of the temp variable in the then original tile value. Hope that makes ...


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

check50 removes your draw function as that is not tested (you can personalize it however you'd like). Because of this, if you update any global variables in that function, they won't be updated when check50 runs the program. Your draw should only print the board. You'll need to move your empty_i = i; and empty_j = j; lines to another function. (makes ...


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


1

In your init function you need to assign values to each position in the board array. In the draw function, you use the values from your board array to print to the screen. Note that the integer array board[MAX][MAX] has already been declared as a global variable in the distributed code.


1

Hmmm, two consecutive questions with roughly the same issue. After running check50 on almost every problem from pset1 to pset5, it only happened on a handful of programs. Also, it has stopped happening on some of them, and only appears to be on one or two still. I'm thinking there was some kind of system update in progress. My suggestion is to wait a ...


1

check50 has a lot of dependencies in order to work. One of them is that the blank tile is stored as 0 in the board[][] array. Your code appears to change that to -1, which takes up an extra character in the log file output. If you change your code to simply use 0 as the blank, it is likely to work. You might also find it useful to carefully review the ...


1

It looks like the original code that you had was more correct than after the changes you made, whatever they were. The original problem lies here: board[d - 1][d - 1] = '_'; Since board[][] is an int array, you actually set the value to 95, the ASCII value of an underscore. Since check50 is expecting 0 but got 95, it got an extra digit, thus the error. ...


1

The last element in your board does not need to be initialized by you. Remember you should organize your elements differently depending on the board size. The way you initialize your elements doesn't feel right, try writing by hand the expected values generated when making a small board, say the smallest value for d as possible and increase it by 1 or 2 if ...


1

The problem is with your if statements. In certain cases, you are trying to access array values that don't exist. Let's say the value at board[0][0] is 5. Will your first if statement be true? if (board[i - 1][j] == 0) If so, you need to find a way to make sure that i - 1 will be a value that is in your array.


1

8 7 6 5 3 1 0 -3 -6 It is what makes init () when d3, just have to take pen and paper and do some calculations. for(int i = 0; i < d; i++) { int k = i + 1; it leads to a wrong result. We start with a control variable before for loop, that contains the desired value when the loop (i = 0, j = 0) starts this value can be k = d*d-1 Like ...


1

It looks like the code that creates and populates the log.txt file was removed from the source code. That code is absolutely critical for check50 to test your program. What have you removed from the original source code file? You should only be modifying the sections with the //TODO comments. Do not remove any of the other code unless specifically permitted ...


1

The board[][] array is a global variable. In both functions, the code is creating a shadow array called board[][]. int board[DIM_MAX][DIM_MAX]; By redeclaring the board array in each function, it is creating a local array that takes precedence, hiding the global array. This is not the same array as the global array. Once the function ends, the local ...


1

This code will only change a tile adjacent to the target tile to 0. It won't actually swap the tiles. Once the code above finds the target tile in the board[][] array, it will choose one of the 4 adjacent array elements and change it to 0, based on which if condition is met first. After the adjacent tile is set to 0, it merely manipulates the values in ...


1

your init function is incorrect as noted @Matt Obert. if (d%2 != 0) { for .... } if d = 4, for example, your function init will not doing nothing at all, since board [d-1] [d-2]; It not even initialized to a valid value. int z = (d*d) - 1; Nor is housed in the right place, at each iteration of i (suppose the loop has three iterations) you do z = 8, in ...


1

This code is half-right. It does work, but it allows for diagonal swaps. You need to check for the condition where posx = blankx AND posy=blanky. If you are getting illegal move for valid moves, then you need to check elsewhere. Did you alter the call to move that is in main? Is it possible that you are running an earlier or different build? Maybe the ...


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