Hot answers tagged

7 votes
Accepted

Pset3 won-function works only with 3x3 board. Where am I wrong?

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 ...
user avatar
  • 161
4 votes

Pset3 won-function works only with 3x3 board. Where am I wrong?

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,...
user avatar
  • 17.4k
2 votes

pset3 fifteen won

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 ...
user avatar
2 votes
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 ...
user avatar
  • 64.2k
2 votes
Accepted

Problems with won function on pset3

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) { ...
user avatar
  • 20.7k
2 votes
Accepted

game of fifteen won function (still!!) not working

You are definitely on the right track and are very close. Hmmm.... Let's look at the inside block of code: while(board[i][j] != board[d-1][d-1]) { if(board[...
user avatar
  • 64.2k
1 vote
Accepted

fifteen won function timing out

I see a couple of issues, although I don't immediately see why it is "timing out". The code depends on two if statements that are NOT interdependent. Let's walk through the code to understand what ...
user avatar
  • 64.2k
1 vote

Won() function not working and gives error

Hmmm... where have I seen this before? Oh yeah..... http://cs50.stackexchange.com/questions/2448/how-to-solve-control-may-reach-end-of-non-void-function-error http://cs50.stackexchange.com/...
user avatar
  • 64.2k
1 vote
Accepted

won returns illegal move when board reaches winning configuration

This won function won't work, imagine what would happen for i == d-1 and j == d-1 on a solved board. counter then would be d*d-1, and board[i][j]!=counter would be 0!=d*d-1, leading to the solved ...
user avatar
  • 20.7k
1 vote

pset3 fifteen won kind of give up

There are more elegant implementations, but most notably: if(board[i][j] != n) { return false; n++; } The n++ is never performed. You probably meant ...
user avatar
  • 20.7k
1 vote

pset3 fifteen won

Basically, you code will always return true when 0 and 1 in the right positions, no matter where the other tiles are. Mistakes: You loop not from 0 to d - 1, but from 0 to d - 2 (i < d - 1). Thus,...
user avatar
  • 1,139
1 vote

Won()- Pset 3- Game of Fifteen

A trick I like to use when debugging indexed ranges (for loops, for example) is to see what happens at the upper and lower bounds of the ranges. A lot of times, the code breaks down there.
user avatar
  • 1,415
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 ...
user avatar
  • 1,139
1 vote
Accepted

won function does not seem to return true (even though it should)

Why do you loop till j < d - 1 not j < d? For board 3*3, you miss board[0][2] so should return false for board[1][0]. However, I do not understand why you are stack in an infinite loop. You ...
user avatar
  • 1,139
1 vote

pset 3 won() function always return false

First, note that you change n only if something is wrong. Let's suppose that board [0][0] = 1 as it should be. Then, this condition else if (board[i][j] != n) { n = n+ (d*i)+j; ...
user avatar
  • 1,139
1 vote
Accepted

Pset3 Game of Fifteen Won Function

I suspect that a 4x4 can also generate false wins, given the right sequence of inputs. The problem is that the won() function, as written, can report a win when the tiles are in a certain order. If ...
user avatar
  • 64.2k
1 vote

Pset3 Game of Fifteen Won Function

A possible pseudocode for win would be: for row for column // in each movement, if the position of board [i] [j] coincides with // the expected value increases // the ...
user avatar
  • 5,172
1 vote
Accepted

won does not return exit code of 0

Well, the sandbox output says its working, but I'm thinking that it isn't the result of your program. Having said that, let's look at your won() code. In short, this code will always fail. It will ...
user avatar
  • 64.2k
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 ...
user avatar
  • 64.2k
1 vote
Accepted

pset3 check50 can't pass won for expected prompt input not exit code 0

Time for some tough love here. There are a lot of problems - almost every line of code. The error basically says that you're getting a premature false positive from win. Let's look at the if ...
user avatar
  • 64.2k
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 ...
user avatar
  • 64.2k
1 vote
Accepted

fifteen, won function does not seem to return if game one

You have a typo. Either that, or you made an error in your formula. if (board[i][j]!=1+j+d*j)
user avatar
  • 64.2k
1 vote
Accepted

Why is my pset3 fifteen won function not working?

What that message means is that your function is not returning true when it should. Before running check50, you should "play" the game with your program and make sure it works properly. You have the ....
user avatar
  • 1,677
1 vote

pset3 game of fifteen, won function

There are several issues. First, the coordinates for board[][] start at [0][0], not [1][1], so you are not processing the top row or the left column. Similarly, by checking <=d, you're looking at ...
user avatar
  • 64.2k
1 vote

Why my won routine does not return true even if the game is completed?

Consider what happens during the loop: i=0, j=0 if board[0][0] > board[0][1] i=0, j=1 if board[0][1] > board[0][2] i=0, j=2 if board[0][2] > board[0][3] // ??? // does board[0][3] exist? ...
user avatar
  • 18.4k
1 vote
Accepted

pset3 fifteen won function isn't working?

I didn't get the algorithm(logic) you used to tackle the problem. Lets take a look, this is the configuration depicting the already won game. To implement the won() function, we need check whether ...
user avatar
  • 3,326
1 vote
Accepted

won() Doesn't Work Properly

I'm not quite sure how that works with any size. First time through the loop: say d = 4 i = 0 j = 0 if 0 == 3 //no else if 0 > 0 //no else if board[0][0] < board[-1][3] //board[-1][3] ??...
user avatar
  • 18.4k

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