Grow and collision bug in Snek Game

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
sheriff40
Posts: 7
Joined: August 19th, 2017, 10:21 am

Grow and collision bug in Snek Game

Post by sheriff40 » September 20th, 2017, 8:17 am

Guys I have almost completed the snek game, but there seems to be a problem, kind of a bug.The snake collides one step ahead before actually colliding with the goal, making the goal respawn in some other location(Note:I had diasbled the Snek.Grow() function to test the collision). Also the grow is not working, whenever i eat the goal, the code crashes.(Enable Snek.Grow()). here's the repository. Thank you!!!!

https://github.com/Sheriff40/GitSnek.git

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

Re: Grow and collision bug in Snek Game

Post by chili » September 20th, 2017, 9:29 am

Are you running in debug config? You should be bro! If you do, you will see that you've got bigger problems than just grow not working.

Rule #1, always test your stuff in debug. That why we put all those assertions in there for m8.
Chili

sheriff40
Posts: 7
Joined: August 19th, 2017, 10:21 am

Re: Grow and collision bug in Snek Game

Post by sheriff40 » September 20th, 2017, 4:17 pm

Yes i tried to run it in debug mode. It does not run at all. Just a white screen. And then i have to stop the program.

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

Re: Grow and collision bug in Snek Game

Post by albinopapa » September 20th, 2017, 6:22 pm

First found problem:

Code: Select all

void Snake::Segment::InitHead(const Location & int_loc)
{
	Loc = int_loc;
	Color c = HeadColor;
}
Color c = HeadColor sets c to HeadColor, then gets destroyed at the }.

Second problem:

Code: Select all

void Snake::Segment::InitBody()
{
	Color c = BodyColor;
}
Same with this code, you aren't actually setting anything for the segments.

You are assigning Head and Body color to a temporary variable that gets destroyed when the function ends. The equals operator is an assignment operation, therefore, Color c = SomeColor means assign SomeColor to c.

You have a Color c in Snek::Segment, you should be assigning colors to that c instead of making a new temporary Color c in those functions.

Third problem:

Code: Select all

void Snake::Draw(Board& brd) const
{
	for (int i = 0; i < MaxSegments; i++)
	{
		segment[i].Draw(brd);
	}
}
You are using MaxSegments here to determine the end of the loop. You have a member variable nsegments, shouldn't that be used instead? You want to loop from the head segment to the number of segments that the snek has grown by, not the max size the snek can grow to.

Fourth problem

Code: Select all

	void Add(const Location& val)
	{
		x = val.x;
		y = val.y;
	}
In your Location::Add function you aren't adding, you are assigning the X and Y so there will never be any movement.
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

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

Re: Grow and collision bug in Snek Game

Post by chili » September 21st, 2017, 1:55 am

albinopapa wrote: Third problem:

Code: Select all

void Snake::Draw(Board& brd) const
{
	for (int i = 0; i < MaxSegments; i++)
	{
		segment[i].Draw(brd);
	}
}
You are using MaxSegments here to determine the end of the loop. You have a member variable nsegments, shouldn't that be used instead? You want to loop from the head segment to the number of segments that the snek has grown by, not the max size the snek can grow to.
This is the first one I noticed as well. I think it is what trips the assert when you run.
Chili

sheriff40
Posts: 7
Joined: August 19th, 2017, 10:21 am

Re: Grow and collision bug in Snek Game

Post by sheriff40 » September 21st, 2017, 6:31 am

Guys, these were the problems in my Master Branch. I have already created a WithoutObstacle branch a week ago without these code errors. The WithoutObstacle branch is the actual branch where I face those bugs. Could you guys please check the WihoutObstacle branch. The game runs in that Branch, but there are certain bugs and errors that i can't figure out. Thank you!!! ^_^ and i am sorry for not giving information about the WihoutObstacle Branch, i thought that it would automatically show the branch that i am currently working on.

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

Re: Grow and collision bug in Snek Game

Post by chili » September 21st, 2017, 6:54 am

+1 for not working in master branch (good habit)
-1 million for not mentioning the correct branch when asking for help
Chili

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Grow and collision bug in Snek Game

Post by Yumtard » September 21st, 2017, 6:58 am

Code: Select all

Location & Snake::GetNextLocation(const Location& delta_loc) const
{
	Snek.MoveBy(Delta_loc);
				if (goal.IsEating(Snek.GetNextLocation(Delta_loc)))
				{
					Snek.Grow();
					goal.respawn(rng, Snek, brd);
				};
}
Shouldn't MoveBy be called after checking next location? This might be the issue

sheriff40
Posts: 7
Joined: August 19th, 2017, 10:21 am

Re: Grow and collision bug in Snek Game

Post by sheriff40 » September 21st, 2017, 8:33 am

Sorry Chilli and albinopapa. I thought that the repo link would automatically adjust to the current connect branch. Also, in the current branch, the program runs and the game is playable in the release mode, but does not in the debug mode. Could you check it up!!! Thank you!

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

Re: Grow and collision bug in Snek Game

Post by albinopapa » September 21st, 2017, 2:39 pm

Code: Select all

bool Snake::IsInTile(const Location& target) const
{
	for (int i = 0; i < nsegment; i++)
	{
		if (segment[i].Getlocation() == target)
		{
			return true;
		}
	}
}
Do you see a problem with this function?

The way I found this was when the window was all white, I switched to Visual Studio and pressed the pause button instead of the stop button. This paused in this function:

Code: Select all

void Goal::respawn(std::mt19937 & rng, const Snake& snek, const Board& brd)
{
	std::uniform_int_distribution<int>xDist(2, brd.GetWidth() - 1);
	std::uniform_int_distribution<int>yDist(2, brd.GetHeight() -1 );
	Location loc;
	do
	{
		loc.x = xDist(rng);
		loc.y = yDist(rng);
	} while (snek.IsInTile(loc));
	Loc = loc;
}
Once paused, I simply stepped through the code line by line until coming across the previous function.
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

Post Reply