|
Wow, that's messy :S needs mass refactoring. A tip for writing procedurally complex code like that is to employ comment-first coding which allows you to understand exactly what it is you need to do before you even write it. This code was obviously written "as you went along".
Start fresh, write a bunch of pseudocode in comments, then all you should have to do is fill in the logic under the comments.
|
On October 14 2009 12:48 prOxi.swAMi wrote: Wow, that's messy :S needs mass refactoring. A tip for writing procedurally complex code like that is to employ comment-first coding which allows you to understand exactly what it is you need to do before you even write it. This code was obviously written "as you went along".
Start fresh, write a bunch of pseudocode in comments, then all you should have to do is fill in the logic under the comments.
uhh were you looking at the most recent version? cuz its very simple now lol. and.. this is due in 10 mins so i dont really have time to do it. its logically correct, ive had 4 people look at it and agree, but for some reason its endlessly looping between 4 method calls and.. well idk. ill just submit it as is and hope for the best. oddly enough correctness is NOT a huge part of our project grades.
thanks for help everyone, especially yenta.
|
Did you move gridCopy[curr_x][curr_y] = 0; to before the recursive calls?
On October 14 2009 12:53 tossinYoSalad wrote: oddly enough correctness is NOT a huge part of our project grades. Yeah, I'd take clean code that doesn't quite work over that mess you started with any day. Adding anything to that would be a nightmare, but fixing the newer version should be easy.
|
On October 14 2009 12:56 SonuvBob wrote: Did you move gridCopy[curr_x][curr_y] = 0; to before the recursive calls?
yes i did
edit: haha well thats what i get when i wait till last day to do programming assignments. this is like the 10th class ive taken involving programming and this is the only assignment ive EVER had problems with.
|
On October 14 2009 12:35 yenta wrote:Show nested quote +On October 14 2009 11:34 tossinYoSalad wrote:On October 14 2009 11:29 yenta wrote:On October 14 2009 11:16 tossinYoSalad wrote:On October 14 2009 11:12 yenta wrote: What are the INPUTs and OUTPUTs of this function supposed to be? all it does is count the number of blobs and keep track of the recursion depth. numBlobs is incremented immediately after the while loop and recDepth is incremented at the end of the function. i know its a horrible implementation believe me. Rewrite it like this: RECURSIVE CALLER Initialize count Initialize array of array with flags (w/e you want) FOR( 0...x ) FOR( 0..y ) if point @ x,y is unchecked & x.y is a space, call recursive worker //FOR //FOR return w/e RECURSIVE WORKER ( root, current_x, current_y, pointer to 2D array, ref to count ) base case: return if out of bounds return if space you're looking has been marked 'looked at' recursive case: count adj spaces by calling self with: ( x-1,y ), ( x+1, y), (x, y+1), (x, y-1) + diagonals if they count. if adj spaces > 0 & root == true, increment counter //endif mark current space as read END thanks alot, what do you mean by root though? and because the 2d array is a class var i dont need to pass it though do i? look at this: http://pastebin.com/m7c4ab47d
quoting myself incase you missed the solution i posted :p
|
On October 14 2009 13:00 yenta wrote:Show nested quote +On October 14 2009 12:35 yenta wrote:On October 14 2009 11:34 tossinYoSalad wrote:On October 14 2009 11:29 yenta wrote:On October 14 2009 11:16 tossinYoSalad wrote:On October 14 2009 11:12 yenta wrote: What are the INPUTs and OUTPUTs of this function supposed to be? all it does is count the number of blobs and keep track of the recursion depth. numBlobs is incremented immediately after the while loop and recDepth is incremented at the end of the function. i know its a horrible implementation believe me. Rewrite it like this: RECURSIVE CALLER Initialize count Initialize array of array with flags (w/e you want) FOR( 0...x ) FOR( 0..y ) if point @ x,y is unchecked & x.y is a space, call recursive worker //FOR //FOR return w/e RECURSIVE WORKER ( root, current_x, current_y, pointer to 2D array, ref to count ) base case: return if out of bounds return if space you're looking has been marked 'looked at' recursive case: count adj spaces by calling self with: ( x-1,y ), ( x+1, y), (x, y+1), (x, y-1) + diagonals if they count. if adj spaces > 0 & root == true, increment counter //endif mark current space as read END thanks alot, what do you mean by root though? and because the 2d array is a class var i dont need to pass it though do i? look at this: http://pastebin.com/m7c4ab47d quoting myself incase you missed the solution i posted :p
holy shit i didnt see that before lol. thanks man but its too late now. you can see my final solution at http://pastebin.com/d3918f10e.
i modified your solution slightly to simply have every call of processWork mark off an entire blob and then every iteration of the loop in processGrid increment the number of blobs.
|
line 13: you have an assignment instead of condition
|
hey if anyone is curious.. i fixed it, 10 minutes after project was due. sonovbob found the broken code like 10 posts back i just missed it lol. it was the comparison == , and i was using =. fucking stupid c++ compiler should catch that lol.
anyway... thats what it does.
|
On October 14 2009 13:14 tossinYoSalad wrote: sonovbob found the broken code like 10 posts back i just missed it lol. ... did he now?
|
The compiler's not going to catch it because it's a legal operation, not an error. When you do an assignment, the assigned variable is the return value.
|
|
On October 14 2009 13:14 tossinYoSalad wrote:
hey if anyone is curious.. i fixed it, 10 minutes after project was due. sonovbob found the broken code like 10 posts back i just missed it lol. it was the comparison == , and i was using =. fucking stupid c++ compiler should catch that lol.
anyway... thats what it does. It's not a compile error so it shouldn't catch that. Doing assignment within a conditional is a pretty normal thing to do. If you want some help from the compiler I believe there are certain warning flags you can set that will give a warning if you do not parenthesize the assignment within the conditional.
if( something = newSomething) ... Could be set up to give you a compiler warning which would then go away if it was changed to if( (something = newSomething) ) ...
Can't remember the exact flag you need but I'm sure you can google it easily enough.
|
On October 14 2009 14:16 AeTheReal wrote:Show nested quote +On October 14 2009 13:14 tossinYoSalad wrote: sonovbob found the broken code like 10 posts back i just missed it lol. ... did he now?
yes.. yes.. he did.
as to the assignment within a conditional, yeah i know its a legal operation i was just mad that i didnt catch it lol. I normally don't mess that up so I don't think to check it.
also, when would you need to do an assignment within a conditional? i cant really find a use for that..
|
On October 14 2009 14:16 AeTheReal wrote:Show nested quote +On October 14 2009 13:14 tossinYoSalad wrote: sonovbob found the broken code like 10 posts back i just missed it lol. ... did he now? I must've used my admin powers to post it as someone else.
|
On October 14 2009 13:14 tossinYoSalad wrote:hey if anyone is curious.. i fixed it, 10 minutes after project was due. sonovbob found the broken code like 10 posts back i just missed it lol. it was the comparison == , and i was using =. fucking stupid c++ compiler should catch that lol. anyway... thats what it does.
Recursion depth 534 o.O Didn't read the code, though that seems like a lot of recursion. Prolly would've done it by recursively calling hasBlobProperty() and having the outer layer go through every array position that hasn't already been marked as having a part of a blob there, and the second layer would keep track of the # of blobs. But apparently your method works so it's all good =)
|
Jesus christ, you guys are so awesome. I wish I knew a programming language. Just looking at the way you talk makes my head hurt.
Is C++ hard to learn?
|
nah C++ is pretty easy, although nowadays I suggest you learn Java or C#
EDIT: Although, if I recall correctly C# can execute up to 40 times slower than C++, and the JVM means Java is slower as well (but you would learn the details yourself in a Java course). Of course, efficiency usually won't matter too much... though I think most games are not made with Java/C# (WoW for instance is written in C++), but you would use a lot of C# and Java in other programming jobs.
|
On October 14 2009 15:31 tossinYoSalad wrote:Show nested quote +On October 14 2009 14:16 AeTheReal wrote:On October 14 2009 13:14 tossinYoSalad wrote: sonovbob found the broken code like 10 posts back i just missed it lol. ... did he now? yes.. yes.. he did. as to the assignment within a conditional, yeah i know its a legal operation i was just mad that i didnt catch it lol. I normally don't mess that up so I don't think to check it. also, when would you need to do an assignment within a conditional? i cant really find a use for that..
I don't think it's ever a necessity (could be wrong), it's just a nice convenience for a lot of things.
|
Damn, this looks a lot harder than Java. Does C++ use the same operators?
|
The issue with c++ is that there are a lot of nuances to it that gives it a steeper learning curve. As a result, one is a lot more error-prone when writing c++ code.
Regarding the assignment inside a conditional, here's random example code where it might be used:
char[] str = "Hello TeamLiquid!" char c; for (int i = 0; (c=str[i]) != 0; i++) { printf("Letter: %c", c); }
From a stylistic point of view, I don't think it's usually a particularly good idea, but it would be inconsistent to treat the assignment differently just because it was placed inside a conditional statement.
|
|
|
|