Page 1 of 1

Snek game bug

Posted: June 11th, 2020, 10:29 am
by Lividpayment
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;
}

Re: Snek game bug

Posted: June 11th, 2020, 5:30 pm
by albinopapa
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.