Snek game bug

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Lividpayment
Posts: 10
Joined: April 13th, 2020, 8:47 pm

Snek game bug

Post by Lividpayment » June 11th, 2020, 10:29 am

Hi. I'm getting a weird bug that I can't seem to track down(image linked). Does everyone know what is possibly might be? I just finished re-writing the homework for Snek game. Code below;

Code: Select all

Snake::Snake(const Location& loc)
{
	constexpr int nBodyColors = 4;
	constexpr Color SnakeColor[nBodyColors] =
	{
		{10, 100, 32},
		{10, 130, 48},
		{18, 160, 48},
		{10, 130, 48}
	};


	for (int i = 0; i < nSegmentsMax; i++)
	{
		segements[i].InitBody(SnakeColor[i % nBodyColors]);
	}

	segements[0].InitHead(loc);
}

void Snake::MoveBy(const Location& delta_loc)
{
	for (int i = nSegments - 1; i > 0; --i)
	{
		segements[i].Follow(segements[i - 1]);
	}

	segements[0].MoveBy(delta_loc);
}

void Snake::Grow()
{
	if (nSegments < nSegmentsMax)
	{
		nSegments++;
	
	}
}

void Snake::Draw(Board& board) const
{
	for (int i = 0; i < nSegments; ++i)
	{
		segements[i].Draw(board);
	
	}

}

bool Snake::SnakeInTile(const Location& target)const
{
	for (int i = 0; i < nSegments - 1; ++i)
	{
		if (segements[i].GetLocation() == target)
		{
			return true;
		}
	}

	return false;
}

bool Snake::SnakeIsColliding(const Location& target)const
{

	for (int i = 0; i < nSegments - 1; ++i)
	{
		if (segements[i].GetLocation() == target)
		{
			return true;
		}
	}

	return false;
}
Location& Snake::GetHead(const Location& delta_loc) const
{
	Location l(segements[0].GetLocation());
	l.Add(delta_loc);

	return l;
}


void Snake::Segement::InitHead(const Location& in_loc)
{
	loc = in_loc;
	c = Snake::HeadColor;
}

void Snake::Segement::InitBody(Color c_in)
{
	c = c_in;

}

void Snake::Segement::Follow(const Segement& next)
{
	loc = next.loc;
}

void Snake::Segement::MoveBy(const Location& delta_loc)
{
	assert(abs(delta_loc.x) + abs(delta_loc.y) == 1);
	loc.Add(delta_loc);


}

void Snake::Segement::Draw(Board& board) const
{
	board.DrawCell(loc, c);
}

const Location& Snake::Segement::GetLocation()const
{
	return loc;
}
Attachments
bug.png
(37.46 KiB) Not downloaded yet

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

Re: Snek game bug

Post by albinopapa » June 11th, 2020, 5:30 pm

Not sure what the bug is. If something is happening that is unexpected, you could run through the debugger and check values of variables. When you have issues you can't solve on your own, it's best to upload a cleaned solution or a link to a github repository instead of posting code that might not even be the problem.

A cleaned solution means removing any Debug/Release/x64 folders as well as and especially the .vs folder in order to keep the RAR/ZIP file small.
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