Skip to main content
4 votes
Accepted

Fifteen move not working in last row only

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 ...
Cliff B's user avatar
  • 69k
2 votes
Accepted

PSET3 Game of Fifteen move() Problem

@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 ...
hotwebmatter's user avatar
  • 3,050
2 votes

PSET3 Game of Fifteen move() Problem

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 ...
kluvin's user avatar
  • 1,223
2 votes

pset3 fifteen what am I doing wrong with my move function

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 ...
Sofia's user avatar
  • 65
2 votes
Accepted

pset3: Help with Fifteen Move function

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, ...
Blauelf's user avatar
  • 21k
1 vote
Accepted

Game Of Fifteen Moving

You'd have to protect your code against non-existent coordinates. So instead of if (board[m -1][n] == 0) do if (m > 0 && board[m -1][n] == 0) because if m>0, then m-1 is still in ...
Blauelf's user avatar
  • 21k
1 vote

PSET3- Game of fifteen, function move();

This is a common problem. The code is going off the edge of the board. Look at the following: if (board[row][column+1] == blank){ Let's assume d=3 and column=2. What is at board[row][column+1]? ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

PSET3 'Illegal Move" coming up for legal moves as well! Almost done!

The initial position of blank tile is board[d-1][d-1], not board[d][d]. Other issues: When checking tiles around the blank one, you go outside of the board borders, if the blank tile touches the ...
obesman's user avatar
  • 1,139
1 vote
Accepted

Fifteen - help please, I can't find the problem in my code for move and won

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 ...
obesman's user avatar
  • 1,139
1 vote

The tile doesnt swap or move when i use this code!

Why is this line, board[r][c]=t-1; in the move() function? It will reassign values to every tile on the board. It looks like a remnant of a cut and paste from init(). The code needs to find the ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

"Expected Expression" Error on Move Function

These are all syntax errors. In the case of the first two, it's an incorrect initialization, more or less. I'll explain. in order to use the {...} technique, you have to do this at the same time as ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

Game of Fifteen Not Swapping Tiles and Allowing Illegal Movies

In move () you are reading out of bounds. For example On a 3x3 board if j is 0 you will read j-1. This goes for the whole perimeter of the board. You need to account for these bounds. You also need ...
Ryan McCauley's user avatar
1 vote
Accepted

Pset3 (Game of Fifteen): The tiles won't swap... ("Illegal move")

You use 0-based indexing (the C standard) in some places (move, won), but 1-based indexing in others (init, draw). You use a strange variable blank. This variable is initially of undefined value (...
Blauelf's user avatar
  • 21k
1 vote
Accepted

Game of Fifteen - Move Function - getting wrong coordinates?

(new reply as I misread!) You would expect when tile==3, that x,y is 1,2 : 0,0 0,1 0,2 1,0 1,1 1,2 2,0 2,1 2,2 Remember, it's row,col so what you are seeing is correct. (Your init is doing the ...
curiouskiwi's user avatar
  • 18.7k
1 vote

cs50 pset3 move

It's the else ifs. Let's take a 3x3 board, first move, tile 1. It gets here if (tile == board[i][j]) when i = 2 and j = 1. if (i > 0) evaluates to true, so it goes into the code block. It doesn't ...
DinoCoderSaurus's user avatar
1 vote

Move function on pset3

There's a comment above move: /** * If tile borders empty space, moves tile and returns true, else * returns false. */ So swap only if tile and 0 are next to each other, and return true after ...
Blauelf's user avatar
  • 21k
1 vote
Accepted

What is wrong with move function?

There are several problems. First, it allows for an invalid tile: if(tile > d*d If the tile is d*d, it's invalid. Second, it looks for the blank tile off the edge of the board with i+1, j-1, etc. ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

pset3, move function !! Please Help?

You might want to get rid of the first set of nested loops: for(int i=0; i<d; i++) { for(int j=0; j<d; j++) { if(board[i][j] == 0) { blank = board[i][j]; ...
Ricardo David Antonetti's user avatar
1 vote
Accepted

My program returns false even when I input a correct value for tile

When you use the OR operator, it only takes one of the conditions to be true for the block to be executed. Since there is only one tile and four adjacent spaces, 3 of those conditions will always ...
Yuri Laguardia's user avatar
1 vote

pset3 move: won't move tiles in the same row

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 ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

Fifteen.C Move Function Fails One Check50 Test

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 ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

Pset3, move function doesn't compile

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 ...
kluvin's user avatar
  • 1,223
1 vote
Accepted

pset3: fifteen - problem with "move" function

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 (...
Ben's user avatar
  • 44
1 vote

move in pset3 fifteen not working properly

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 ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

pset3 - Game of Fifteen can move in basic directions, but not in combinations of moves

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 &...
Cliff B's user avatar
  • 69k
1 vote

Pset 3, Move, What am I doing wrong?

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 ...
Cliff B's user avatar
  • 69k
1 vote

PSET 3 - Fifteen - Move Function Not Working - Brain melted and not sure how to proceed. Please help

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 ...
Steven C. Eisemann's user avatar
1 vote
Accepted

Pset 3 Fifteen Move

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 ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

Pset3 game of 15 move function

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) ...
Cliff B's user avatar
  • 69k
1 vote
Accepted

move function doesn't move blank tile sideways

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) { ...
Cliff B's user avatar
  • 69k

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