Page 1 of 1

Grow and collision bug in Snek Game

Posted: September 20th, 2017, 8:17 am
by sheriff40
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

Re: Grow and collision bug in Snek Game

Posted: September 20th, 2017, 9:29 am
by chili
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.

Re: Grow and collision bug in Snek Game

Posted: September 20th, 2017, 4:17 pm
by sheriff40
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.

Re: Grow and collision bug in Snek Game

Posted: September 20th, 2017, 6:22 pm
by albinopapa
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.

Re: Grow and collision bug in Snek Game

Posted: September 21st, 2017, 1:55 am
by chili
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.

Re: Grow and collision bug in Snek Game

Posted: September 21st, 2017, 6:31 am
by sheriff40
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.

Re: Grow and collision bug in Snek Game

Posted: September 21st, 2017, 6:54 am
by chili
+1 for not working in master branch (good habit)
-1 million for not mentioning the correct branch when asking for help

Re: Grow and collision bug in Snek Game

Posted: September 21st, 2017, 6:58 am
by Yumtard

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

Re: Grow and collision bug in Snek Game

Posted: September 21st, 2017, 8:33 am
by sheriff40
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!

Re: Grow and collision bug in Snek Game

Posted: September 21st, 2017, 2:39 pm
by albinopapa

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.