Stuck trying to make snek grow by > 1 for each goal eaten.

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
FatteusHornblow
Posts: 1
Joined: August 31st, 2017, 11:17 am

Stuck trying to make snek grow by > 1 for each goal eaten.

Post by FatteusHornblow » August 31st, 2017, 12:17 pm

This is my first post here, so i'd like to start off by saying how much I love this community. I've learned so much going through these beginner tutorials and the forum seems incredibly supportive of newbies like myself.
That said, I've been stuck for countless hours trying to implement a feature that at first seemed simple enough. My goal was to make the snake grow more than a single segment at a time when eating. I got it working by naively just changing the Grow() function to increase nSegments by 3. This worked on the surface, except for the fact that while new segments are being added, a single segment is also drawn slightly outside of the top left corner of the board. I think the problem lies in the way Snake::MoveBy() is implemented and the fact that the new segments don't get assigned valid locs before they are drawn, but i can't for the life of me come up with a working solution. I've been banging my head against this for 3 days now trying all sorts of wacky things and bumbling around with the debugger but I'm still completely stumped. Any guidance would be greatly appreciated.

Also as a sort of bonus bug, if anyone has the time, in Board::DrawBorder() i needed to add 1 to
width in this line:

Code: Select all

const int right = left + (borderPadding + borderWidth) * 2 + (width + 1) * dimension;
This was in order to keep the snake from being able to move over the right border by 1 block. Adding 1 to width fixes the issue but it's not satisfying. I just can't seem to see why it was an issue in the first place; height doesn't need the adjustment...?

Anyways, sorry this got a bit long winded, and thank you in advance to any kind souls who might offer up some of their time and knowledge. And to chili, your tutorials are honestly the best learning tool I've found and your presentation style makes them even better. Please keep em comin!

My git repo for snek.
https://github.com/FatteusHornblow/Snek ... HomeWork17

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Stuck trying to make snek grow by > 1 for each goal eate

Post by albinopapa » August 31st, 2017, 6:30 pm

Code: Select all

bool Board::IsInsideBoard(const Location& loc) const
{
	return loc.x >= 0 && loc.x <= width && 
		   loc.y >= 0 && loc.y < height;
}
See anything wrong with this function? This could be the reason why you have to do (width + 1) in the Board::DrawBorder call.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Stuck trying to make snek grow by > 1 for each goal eate

Post by albinopapa » August 31st, 2017, 6:39 pm

Yes, you are correct. When you grow by three, the three segments you allow are not initialized to a valid location. In the MoveBy function, the last allowed segment gets it's location from the previous one which is also not initialized, and same for the next previous one. It's not until you get the the first of the three that a valid location is found from the fourth one up the chain of segments.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

jamesmfox
Posts: 15
Joined: September 1st, 2017, 10:46 pm
Location: Mississippi, USA

Re: Stuck trying to make snek grow by > 1 for each goal eate

Post by jamesmfox » September 2nd, 2017, 5:25 am

I got the grow 3 sections working by adding a counter and then decremented each time the snake moved.
and added 2 after the snake grew after eating

Code: Select all

if (moveBy3 > 0)
{
	--moveBy3;
	snek.Grow();
}
bool isEating = next == food.GetLocation();
if (isEating)
{
	snek.Grow();
	++foodCounter;
	moveBy3 += 2;
}
snek.MoveBy(delta_loc);

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Stuck trying to make snek grow by > 1 for each goal eate

Post by chili » September 2nd, 2017, 8:09 am

Basically what i was thinking, have a counter, add X whenever you eat, whenever you move, if counter is greater than 1, grow. That's the basic idea anyways.
Chili

Post Reply