Hot answers tagged

11

The move() function basically does 3 main things: receives the number on the tile to be move. validates the move. if the move is valid, it makes it and returns true. Otherwise, it returns false. First, receiving the number on the tile to be moved involves some linear search to get the row and the column of that tile in the board. Second, validating the ...


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

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

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


2

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


2

It looks like you have some repeated code. If you try to separate your move function into conceptually different parts, your issues will probably become much more clear. For example: Part 1: Find where tile is Part 2: Check if tile is close to the 0 Part 3: Swap them if valid and return This way you can use printfs to determine which of the parts contains ...


2

The ball gets stuck in the paddle when the paddle hits the side of the ball. I'm not particularly sure how exactly you tried to implement if the ball is moving down move the ball up but my guess is that you probably did something like if (ball collides with paddle AND ball is moving down) { move the ball up } else if (object is GRect) { ...


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

To check if a tile is adjacent to the tile the user requested to move to with indices you simply use an if statement with the condition you wish to check for, which is then followed by something like board[i-1][j], if I remember correctly this looks up the tile to the left of board[i][j]


2

@M.Kleiven's tips are interesting, but I just looked at my solution, and I did not need to use pointers to check if a tile is outside the board. Here's what I did, in pseudo-code: // initialize board using 0 for blank tile (as recommended in the problem spec) bool move(int tile) { // find blank tile (0) and remember its location (blankrow, blankcol) ...


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


1

The move() code seems to be working fine. In looking at your output (the raw form, not the version that Stack reformatted before I edited it to show as output), it looks to me that maybe you have a linefeed, \n printing out after your underscore for the blank tile. It wouldn't show up in the initial board layout, but will show after the first tile is moved. ...


1

Look at the following: if ( board[row - 1][column] == 0 && row - 1 > 0 ) Why does row-1 have to be greater than 0? 0 is still valid. If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)


1

For your code to compile, move has to return a value in every case. Your code will currently only return something if the else statement executes. This is because the function is "non-void", or not void, or in other words, it will return something.


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

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

Right idea, wrong test. Look at the following: } else if (board[x][y-1] == 0 && y-1 <= d-1) { // left of tile ... } else if (board[x-1][y] == 0 && x-1 <= d-1) { // up of tile What is y-1 when y=0? or x-1 when x=0? If this answers your question, please click on the check mark to accept. Let's ...


1

There are a few problems here. First, the test conditions all have a problem. Look at the first one, for example: if(tile == board[blankRow+1][blankCol] && tile != board[d+1][d]) As indexes for board[][], anything greater than d-1 is invlaid and will reference memory outside of the array or in another part of the array, depending on what the ...


1

I believe that your problem lies in how you are assigning tileloc. When you say tileloc = board[i][j], what you are really doing is taking whatever value is in that index of the array and assigning that to tileloc; that is to say, you are making tileloc equal to the value being searched for. I recommend creating two variables that represent the row and ...


1

The code is accessing memory outside of the board array. Note that the array board[x][y] is based on d. Both indexes x and y run from 0 to d-1. In the code, i and j are initialized as equal to d, so both indexes controlled by i and j are starting outside of the index limits. Unfortunately, this doesn't always result in an error, but the computer will access ...


1

The code never tests 0,0. In the search for tile, the first array element that is checked is board[0][1], continuing through the rest of the array. That means that the if (board[row][column] == tile) statement always evaluates as false and the code drops down to the retun false statement. The reason that it never tests 0,0 is that column is initialized to 0,...


1

Your code is a slight variation of the same problem. Look at the following block from your code: else if(board[i+1][j] == 0 && board[i+1][j] < d) { hold = board[i][j]; board[i][j] = board[i+1][j]; board[i+1][j] = hold; printf("\nmoved blank: %i with tile: %i\...


1

It looks to me that your code is swapping tiles off the edge of the board. The code doesn't check whether an array index is out of range. Look at the following: if(!moved && board[zeroRow + 1][zeroCollumn] == tile) { board[zeroRow][zeroCollumn] = tile; board[zeroRow + 1][zeroCollumn] = 0; Let's say d=3 and tile is at 2,1....


1

It looks like your problem is in your decision to put everything inside the same nested for loops. Your swap depends on setting blanktile_X and blanktile_Y, the location of the blank tile. If the blank tile is in a position that follows the position of tile, then one or both of the coordinates of the blank tile (X or Y) will not yet have been set and will ...


1

What do you mean by "gdb cannot run on function declaration"? Do you mean you don't know how to parse command line input to gdb? Try running your program like this with gdb: >gdb ./fifteen hi youre running gdb here is thirty lines of useless garbage before the command prompt and here is the command prompt >break main >run 5 (where 5 is your ...


1

Without completely giving it away: Why are you assigning 42 to the variable 'tile'? You want to store 42 in the board array, right? Not in the tile variable, right? So, you should be assigning 42 to the board[?][?]. BTW, you don't need the 'temp' variable at all. In the last 4 if() statements you are assigning 'tile' to 'temp' and then straight away 'temp' ...


1

You are storing your tile numbers in the board array, right? So you need to change the values in the board array. When you write: hold = tile; tile = symbol; symbol = hold; Does this change any values in the board array?... That's right, it don't. You should be doing something like: // board location that has the tile value // becomes the empty value (...


1

underscore = board[i][j]; You're assigning the value that is stored in that board position to an integer called underscore. At this point, underscore equals zero. This is the same issue you struck the other day with your init() swapping. I think you're trying to somehow capture the position of the tile that has a value of zero? If that's the case, then ...


1

If board[][] is an array that stores the board's current configuration and board[tracki2][trackj2] contains the tile, and you need to swap it with board[tracki1][trackj1], then you can do it in this way. int x = board[tracki1][trackj1]; board[tracki1][trackj1] = board[tracki2][trackj2]; board[tracki2][trackj2] = x; x is declared int because we know that ...


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