Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Snake program not working.  (Read 10889 times)

0 Members and 1 Guest are viewing this topic.

wizh

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Snake program not working.
« Reply #15 on: June 09, 2013, 06:46:25 pm »
Aaaaand it's working. Didn't realize that was happening. I just setup a timer to fix that problem :)

Thanks a bunch! Will report back if I get stuck ^^

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Snake program not working.
« Reply #16 on: June 09, 2013, 07:01:25 pm »
You should never try to do any SFML related functions before you create your SFML window, things tend to not work correctly.
I actually thought SFML would ensure a valid OpenGL context when it needs one. wizh, is the Game instance global?

Some suggestions concerning the STL (even if it was just a quick and dirty example, writing the correct code doesn't really take longer):

for(std::vector<int>::size_type i = 0; i != snakeSegments.size(); i++)
That's not the usual way to iterate. First, you can use std::size_t directly if you need indices. But generally, prefer iterators if you don't need random access, because they allow you to switch containers. Also, pre-increment ++i can be faster for complexer types (not integers).
for (std::vector<int>::iterator itr = snakeSegments.begin(); itr != snakeSegments.end(); ++itr)
With C++11, you can infer the type:
for (auto itr = snakeSegments.begin(); itr != snakeSegments.end(); ++itr)
Or directly use the range-based for loop:
for (sf::Sprite& sprite : snakeSegments)

Furthermore, std::vector::at() is almost always the wrong choice. It performs an index check and throws an exception. If your indices are correct, you don't need that (definitely not in an iteration loop). If they are not correct, the STL implementation will still check the error in debug mode with an assertion.

In short: Use std::vector::operator[] for random access. And use iterators to iterate.

Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: Snake program not working.
« Reply #17 on: June 09, 2013, 07:23:22 pm »
Furthermore, std::vector::at() is almost always the wrong choice. It performs an index check and throws an exception. If your indices are correct, you don't need that (definitely not in an iteration loop). If they are not correct, the STL implementation will still check the error in debug mode with an assertion.

In short: Use std::vector::operator[] for random access. And use iterators to iterate.

Like I said, I've found 3+ ways to do everything between all the tutorials and examples I've ever seen, and most of the time no one bothers to explain WHY you want to use one way over another.  Thank you for this knowledge.

I want to start using C++11 so bad but I'm stuck developing across multiple machines stuck with multiple dev environments from different points in time so I'm not sure if I can without a huge headache. Smart pointers look frickin awesome

wizh

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Snake program not working.
« Reply #18 on: June 09, 2013, 07:43:51 pm »
Thanks for the additional comments :)

If you mean global as being instantiated in another class, then yes. It's not globally available though.
« Last Edit: June 09, 2013, 07:50:44 pm by wizh »

wizh

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Snake program not working.
« Reply #19 on: June 09, 2013, 10:28:09 pm »
Hmm. I just can't seem to get it working... When I pickup another piece, I'm trying to get something that's out of bounds of my vector. I can't seem to get the logic down. Here is the code: http://pastebin.com/Fw9CgdUd.

I'm going to bed now. Thanks a bunch in advance! I don't want you to code it for me, just give me a pointer as wto what should be done differently :)
« Last Edit: June 09, 2013, 10:53:18 pm by wizh »

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: Snake program not working.
« Reply #20 on: June 10, 2013, 07:58:03 am »
I must go to bed as well.  Looking quickly over your code I can't see anything blaringly obvious.  One of the easiest and most basic debugging techniques I can share with you is make massive use of std::cout, or printf(), whichever you prefer.  Whenever something gives you grief, like an 'Out of Bounds' error, throw some COUTs where you think its happening to check where you iterators are pointing to.  Hell sometimes a simple COUT can show whethere you even get into a function or not.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Snake program not working.
« Reply #21 on: June 10, 2013, 11:17:52 am »
wizh, you should really use the debugger to solve such problems, because you will encounter them again and again. Out of bound errors are found in seconds with the debugger.

And generally, if you really can't solve an issue and want us to help you, consider the advice given in this post. Nobody likes to look at 250 lines of your custom code, even less if there is no meaningful error description.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

wizh

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Snake program not working.
« Reply #22 on: June 10, 2013, 12:40:36 pm »
I posted all the code since I had no idea where the error was occurring. I fixed the problem by using breakpoints - that was a really helpful method! :)

If someone was wondering, there were several problems in the moveSnake() method and the eatCircle() method. Here is the changes i made: http://pastebin.com/abdxsKMG

Have a great day everybody, and thanks for the help :D