Romhacking.net

Romhacking => Programming => Topic started by: InfamousKnight on May 08, 2013, 08:52:45 am

Title: labyrinth loader im having trouble with
Post by: InfamousKnight on May 08, 2013, 08:52:45 am
So I have the labyrinth maker working just fine I just can't figure out how to read from it.
Here's the code on it:
http://pastebin.com/Z90DtvzV
I know what I'm doing wrong I just can't figure out new lines. I know what getline does but idk how I'm supposed to use a 2D array out of it.

I don't care that my algorithm is a bit messy its just my way of coding. In fact, I believe thats the best way of doing it using those basic factors.
Title: Re: labyrinth loader im having trouble with
Post by: furrykef on May 09, 2013, 01:15:20 am
I don't care that my algorithm is a bit messy its just my way of coding.
Your way of coding sucks.
Title: Re: labyrinth loader im having trouble with
Post by: KaioShin on May 09, 2013, 05:36:38 am
You can find out what's wrong with 2 minutes in a debugger looking at what your code actually does.

But I guess debugging is one of those hipster things that no real(tm) programmer needs anyway, right?
Title: Re: labyrinth loader im having trouble with
Post by: henke37 on May 09, 2013, 08:46:08 am
I smell a lot of duplicated code in that switch statement. Have you considered refactoring the code to avoid the duplication? You are also inconsistent in how you address cells in the array.
Title: Re: labyrinth loader im having trouble with
Post by: Rhys on May 09, 2013, 09:28:51 am
Your way of coding sucks.

zing!
Title: Re: labyrinth loader im having trouble with
Post by: InfamousKnight on May 09, 2013, 11:49:46 am
You can find out what's wrong with 2 minutes in a debugger looking at what your code actually does.

But I guess debugging is one of those hipster things that no real(tm) programmer needs anyway, right?
No debugger is going to help. The thing that's wrong with the code is that it reads byte by byte in one line and I have a 2d array which means I have multiple lines in the file. A debugger isn't going to tell me how it's done.

@henke know of any better way of doing this? I suppose I can write a function but then it would still be repeating. Also, I COULD have used else if but in some compilers it's either elseif or its else if and I didn't feel like switching it if I did it wrong.

All I'm asking is for a program to read 400 bytes to a 2d array line by line.
Title: Re: labyrinth loader im having trouble with
Post by: henke37 on May 09, 2013, 12:26:33 pm
A high level analysis of the code spots that the code for the four cases do the exact same thing, just with a minor behavior difference. Specifically, the difference is in how the future position is computed. The remaining code is identical. Isolate the different part and factor out the common part.

As for "no debugger is going to help", that's not true. The debugger allows you to follow the code as it executes and will let you spot the implementation error here. It might take a few hits on the step button, but the error here is clearly debugable.
Title: Re: labyrinth loader im having trouble with
Post by: LostTemplar on May 09, 2013, 02:53:10 pm
Also, I COULD have used else if but in some compilers it's either elseif or its else if and I didn't feel like switching it if I did it wrong.

What are you talking about? If it's C/C++ it's always "else (if)". There's no keyword elseif in the language. And even if you were unsure it's just a matter of trying to compile it, the compiler will tell you if it's wrong. Considering the scale of your programs that's a matter of ten seconds, if that. Also: Syntax highlighting.

Also, about your problem regarding the input of 2-D data, I don't think you're doing it wrong per se. What exactly is the problem? If you want to read line by line from a text file, you can use std::getline to read the next line into an std::string and iterate over its characters.
Title: Re: labyrinth loader im having trouble with
Post by: InfamousKnight on May 09, 2013, 03:05:12 pm
What are you talking about? If it's C/C++ it's always "else (if)". There's no keyword elseif in the language. And even if you were unsure it's just a matter of trying to compile it, the compiler will tell you if it's wrong. Considering the scale of your programs that's a matter of ten seconds, if that. Also: Syntax highlighting.

Also, about your problem regarding the input of 2-D data, I don't think you're doing it wrong per se. What exactly is the problem? If you want to read line by line from a text file, you can use std::getline to read the next line into an std::string and iterate over its characters.

OOPS, I was thinking about my map maker. Sorry. And about the 2-D data thing, what would I use to read a 2D array for getline?

Would this work:
for(int x = 0; x < 20; x++) {
   for(int y = 0; y < 20; y++)
   map
}
My programming teacher told me about that mapfile.getline thing. I never saw that in my life.
Title: Re: labyrinth loader im having trouble with
Post by: LostTemplar on May 09, 2013, 04:14:31 pm
That won't work at all. You need to think about a) what arguments getline(...) takes and b) what it returns (hint: it's not what you want).
Title: Re: labyrinth loader im having trouble with
Post by: KaioShin on May 09, 2013, 04:47:41 pm

My programming teacher...

How is it possible this person still has a job?