Tag Info

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 ...
• 69k
Accepted

PSET 3: Game of fifteen. Everything in the board is equal to zero

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]; ...
• 1,139

A wierd error in won function (fifteen)

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 ...
• 18.7k
Accepted

Pset3 Fifteen Check50 not Working

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 ...
• 69k
Accepted

Pset3 Fifteen: Draw and/or Init Function Problem

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) { //...
• 549
Accepted

Am I going about Init() correctly?

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 ...
• 69k
Accepted

Error in dependency - pset3 fifteen

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 ...
• 69k

Pset3/Fifteen/Init Function - Problem with 1 and 2 swap

Actually, the swap shouldn't be inside any for loop. You only want to do the swap once. Putting it inside any loop does it multiple times. Since the swap only depends on the value of d, and nothing ...
• 69k
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 ...
• 3,050

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 ...
• 1,223

Fifteen: Error in a function I haven't touched yet

I couldn't find anything about this and I ended solving the problem a few minutes after posting the question: for some reason, the MakeFile file had been wiped out and was empty. I uploaded the ...
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, ...
• 21k
Accepted

Pset3 - Game of Fifteen - init function

You use local stack variable for the board, local variables will be deleted on leaving the function, should probably use a global variable. Local variables of same name shadow global ones (compiler ...
• 21k
Accepted

Game of Fifteen pset3 - won() function works, but why?

Think about what happens in various scenarios. First, the first if test checks that 0 is in the last tile. If it isn't, the code returns false, so from there on, 0 must be in the last tile. Keep this ...
• 69k
Accepted

Long numbers appearing when trying to retrieve element of 2D array - Game of Fifteen, pset3

Delete this: int board[d][d]; The reason is that board has already been declared globally int board[DIM_MAX][DIM_MAX] and filled with values by init(). Int board[d][d] declares new, local variable ...
• 1,139
Accepted

Won()- Pset 3- Game of Fifteen

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 ...
• 69k
1 vote
Accepted

CS50 pset3 game fifteen won funcition

It would be simpler to just compare against a simple int var and increment that var with each tile. However, your idea should still work. Your code appears to populate the reference array as you loop ...
• 69k
1 vote
Accepted

Pset3 Fifteen won function reorders correctly but finished with illegal move, what's wrong?

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 ...
• 69k
1 vote
Accepted

Pset3/Fifteen/Init Function - Problem with 1 and 2 swap

Just figured it out! My if statement was nested incorrectly. It should not be in the inner for loop!
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 ...
• 1,223
1 vote
Accepted

CS50 Game of fifteen, Move Function, checking for legal move

Right idea to check limits of t and i, but the wrong choices for limits. For example, with if (board[i][t+1] == 0 && t < d ), t+1 must be less than d, not t. If d is 3 and t is 2, then t+1=...
• 69k
1 vote

Check50 initialising error pset3

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 ...
• 69k
1 vote
Accepted

Pset 3 : Game of fifteen. Help understanding the error message

Try generating a 3x3 board and make the same moves check50 did, you should be able to spot the error. If this answers your question please mark as answered.
• 167
1 vote
Accepted

Game of fifteen

Check50 depends on the log.txt file being processed correctly. The code for that was already created and correct in the distro file. That code depends on the board[][] array being populated by the ...
• 69k
1 vote
Accepted

PSet 3 - Game of fifteen - draw tile gives weird output

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 ...
• 69k
1 vote
Accepted

problem while playing game of fifteen

There is a problem with your draw() function. Specifically in the code ` // prints blank tile if ( i == (dimen - 1) && j == (dimen - 1)) { printf("%2c",'_')...
• 122
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 &...
• 69k
1 vote
Accepted

expected an exit code of 0, not a prompt for input WON

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 ...
• 69k
1 vote
Accepted

Pset3 draw function outputs zeroes or random numbers

Without seeing the entirety of the code, it's very difficult to know what's happening. Having said that, I am concerned about some things here. First, in the original code distribution, board was a ...
• 69k
1 vote
Accepted

pset 3 - can't get to move anything

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, ...
• 69k

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