#Newbie needs help writing terminal game

138 messages · Page 1 of 1 (latest)

left robinBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

supple pumice
#

You seem to be using Linux. First, compile with -g -fsanitize=undefined,address, second run it under gdb. IDEs such as VSCode and Qt Creator will run it in a debugger automatically. It allows you to see where it crashed, how it got there and what the variables currently are when the crash happens.

#

Close enough, should still work.

#

I think it's xcode as the IDE, but the rest should be the same.

#

-g is for adding debug symbols, it allows gdb to figure out what the program is doing.

#

-fsanitize=address,undefined activates sanitizers that will attempt to detect errors and report them. So ideally instead of "segmentation fault" you get "Invalid read access on address xxx in main.cpp:xxx allocated in main.cpp:xxx and freed in main.cpp:xxx". Sometimes it helps, sometimes it doesn't, but it's essentially free.

#

Apparently you're supposed to use lldb instead of gdb on macos.

uncut sail
#

yeah step 1 is going to be to figure that out, a debugger is the #1 most important tool

supple pumice
#

Yes, VSC is based on extensions. It should recommend you the most popular one.

#

You press the little 🪲 symbol or usually F5.

#

And then the program runs as normal.

#

And if there is a problem like a segmentation fault the debugger stops the program and shows you the line where it crashed with a call stack to see how it got there and variable contents.

#

No clue. Ideally don't have one and it uses reasonable defaults.

#

Whelp, welcome to C++ toolchain pain.

#

Sometimes it takes days to fix, other times you just give up.

#

To just get something going, you can try lldb yourprogram and then probably r to run and see what it says.

#

It's a huge inconvenience compared to a working IDE, but at least you have a debugger.

#

Alternatively install Qt Creator where everything just works out of the box.

#

Because that's the bad way to do it.

#

It means you don't have IDE integration.

#

You really want your IDE to handle commands to lldb.

#

That looks like you forgot the -g or something.

#

It's supposed to show you C++ code, not assembler code, but the debug symbols are needed to figure out which assembler code corresponds to which C++ code.

#

🤷

#

Looks like something is borked in your setup.

#

Just find the magical lines in launch.json and everything works. It's my least favorite feature in VSC.

#

No, I didn't look at it.

#

main.cpp:63:9: warning: control reaches end of non-void function [-Wreturn-type]

#

The currently logic is "randomly crash, probably". Surely you can do better. An assert(false); would already be an improvement.

uncut sail
#

Wtf? You should be using one from 1 one

#

Programming without one is like driving a car blind

supple pumice
#

Just don't write bugs, then you don't need a debugger wesmart

supple pumice
#

Never use JavaScript when you can use TypeScript instead.

#

Because then you're not programming blind.

burnt ocean
#

Yes

supple pumice
#

Debatable. Unfortunately almost all tutorials and videos about C++ are utter garbage and harm more than help.

burnt ocean
#

Some sort of teaching material is recommended

#

At least

supple pumice
hollow kindle
supple pumice
#

You should remove all the class keywords when using a class name. It's just crate, not class crate.

burnt ocean
#

Most people would recommend a book

supple pumice
#

I don't know. You probably changed something. But this was also a general remark. You seem to add class before every occurrence of crate.

tardy bough
#

omg i love terminal games

#

this looks AMAZING

burnt ocean
#

I made this gif a while back

supple pumice
#

It'd be funnier without the animation.

#

"std::move doesn't move"

#

Combine it with storm chair or something.

burnt ocean
#

Storm chair?

supple pumice
tardy bough
#

haha

left robinBOT
#

@thin oasis Has your question been resolved? If so, run !solved :)

honest quarry
#

Do you have it in some repo? Looks cool

#

Will look into it later this evening

obsidian junco
#

what library is that

tardy bough
#

STARRED

left robinBOT
#

@thin oasis Has your question been resolved? If so, run !solved :)

supple pumice
#

Do you have a debugger yet?

#

See if Qt Creator works.

#

It's a different IDE.

#

The difference is that you don't need a launch.json and it integrates compilers and debuggers properly on installation.

#

Also if you're wondering if it's just you who is too dumb to get the C++ extension to work properly in VSC, it's not. I gave up. Lots of others did too.

#

No, that doesn't help at all. That's for developing Qt applications which is not what you want.

#

Yes

#

🤔

#

When you do int a[3]; you get an array with 3 elements, the valid indexes are 0, 1 and 2.

#

That is not allowed in C++.

#

And no, it would have 0 elements and 0 size and not make sense overall.

#

Qt Creator should be packaged. I forgot what Macos uses as a package manager, but it should be in there. Alternatively do it the Windows way and download it from https://www.qt.io/download

#

Yup, top[6] does not exist.

#

That's good. Liking pain is a requirement for using C++.

uncut sail
supple pumice
#

For the first one you have to decide what happens if someone tries to pop a crate from an empty inventory. Typical ways is to decide that this should not happen and means you did a programming error elsewhere. In that case you use assert(!isEmpty(x));.
Alternatively you say it's a recoverable error, in which you throw std::runtime_error{"Invalid inventory"};.
Alternatively you say nothing should happen, you just grab nothing and you have a special crate value that expresses empty space.

uncut sail
#

I personally prefer the first 2 approaches

#

generally returning something empty like this is a bit of an anti pattern in my opinion, sometimes it's desirable but here it often denotes a bug in the program imho

supple pumice
#

The color error is easier, just give it a value. Instead of class crate newcrate; you do crate newcrate{}; which value-initializes all the members (basically means everything is set to 0).

uncut sail
#

struct crate newCrate is C syntax

#

but in C++ typing struct / class before stuff isn't needed anymore

#

hence the typedef trick often used in C

supple pumice
uncut sail
#

though it might be intresting to perhaps return an optional? 🤔

#

what code is throwing that?

#

or is it the same as online?

#

🤔 that should be just fine

supple pumice
#

Can you post the whole thing again?

uncut sail
#

omiting them here should be fine but it's weird it's complaining about it

supple pumice
#

Well, omitting them caused an error with uninitialized variables.

#

Something is very broken there.

#

One thing you should do is set a standard.

#

Add -std=c++20 to your g++ flags.

uncut sail
supple pumice
#

Yes, and crate doesn't have an explicit one and the implicit one does nothing.

uncut sail
#

I was questioning everything for a second

#

but yeah since it doesn't set default values it will indeed break

supple pumice
#

What is the output of g++ -v?

#

Also try using clang++ instead of g++.

supple pumice
#

That should just work then.

#

I don't know.

#

Blame 🍎

#

Yes, that's why we're using {} and not ().

supple pumice
#

I don't know what to tell you. The standard says crate newcrate{}; is fine and it works everywhere else. Either that or I've been tripping the last 10 years.

#

crate newcrate = {}; is an overcomplicated thing that just happens to also work.

#

I thought we already went over that. () is wrong, {} is right.

honest quarry
#

Ah yes the most vexing parse

supple pumice
#

crate newcrate(); is a function declaration, just like int f();.

#

By now compilers are smart enough to figure out that you didn't mean to use a function declaration there.

supple pumice
#

Weird. Blame Apple.

#

They really messed with clang.

#

It isn't. It clearly says it's "Apple clang".

#

Which is actually a thing.

uncut sail
supple pumice
#

Depends on what inventory.push returns.

burnt ocean
#

At a time quick glance it looks like a off by one error

#

No, i just looked at the animation

supple pumice
#

The first pickup at position 2 top actually picks up position 3 bottom. And after that everything breaks.

#

The index error should be easy to fix. That it goes bottom to top requires a little more thinking. I feel like that it grabbed that box is correct, but the display function draws it in the wrong order. The reason being that it leave a hole in the bottom.

#

If you try to grab from position 7 you should get an index error, at least with the sanitizers, so the location should be easy to find.

uncut sail
#

no

#

that seems like your code is broken :D

#

like your index is at -1 by default or something

#

or a 1 off error

#

hmmmmm well an index of -1 wouldn't be a valid index

#

so a crate being spawned should propably have an index of 0? since there is no 1 crate?

tardy bough
#

amazing

tardy bough
#

heyyy

left robinBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity