Register    Login    Forum    Search    FAQ

Board index » Everything




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post Posted: September 20th, 2017, 8:17 am 
 

Joined: August 19th, 2017, 10:21 am
Posts: 7
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


Top 
 Profile  
Reply with quote  
 Post Posted: September 20th, 2017, 9:29 am 
Site Admin
User avatar

Joined: December 31st, 2011, 4:53 pm
Posts: 3470
Location: Japan
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


Top 
 Profile  
Reply with quote  
 Post Posted: September 20th, 2017, 4:17 pm 
 

Joined: August 19th, 2017, 10:21 am
Posts: 7
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.


Top 
 Profile  
Reply with quote  
 Post Posted: September 20th, 2017, 6:22 pm 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2828
Location: Oklahoma, United States
First found problem:
Code:
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:
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:
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:
   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


Top 
 Profile  
Reply with quote  
 Post Posted: September 21st, 2017, 1:55 am 
Site Admin
User avatar

Joined: December 31st, 2011, 4:53 pm
Posts: 3470
Location: Japan
albinopapa wrote:
Third problem:
Code:
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


Top 
 Profile  
Reply with quote  
 Post Posted: September 21st, 2017, 6:31 am 
 

Joined: August 19th, 2017, 10:21 am
Posts: 7
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.


Top 
 Profile  
Reply with quote  
 Post Posted: September 21st, 2017, 6:54 am 
Site Admin
User avatar

Joined: December 31st, 2011, 4:53 pm
Posts: 3470
Location: Japan
+1 for not working in master branch (good habit)
-1 million for not mentioning the correct branch when asking for help

_________________
Chili


Top 
 Profile  
Reply with quote  
 Post Posted: September 21st, 2017, 6:58 am 
User avatar

Joined: January 19th, 2017, 10:28 pm
Posts: 495
Location: Idiot from northern Europe
Code:
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

_________________
I just like to thumb my nose at millionaires.
If I was rolled I'd bust them heads up.


Top 
 Profile  
Reply with quote  
 Post Posted: September 21st, 2017, 8:33 am 
 

Joined: August 19th, 2017, 10:21 am
Posts: 7
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!


Top 
 Profile  
Reply with quote  
 Post Posted: September 21st, 2017, 2:39 pm 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2828
Location: Oklahoma, United States
Code:
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:
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


Top 
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
 
Post new topic Reply to topic  [ 10 posts ] 

Board index » Everything


 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for: