Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Algorithm [modified]

Algorithm [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmstutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cyclone_S
    wrote on last edited by
    #1

    Hi, I am coding an algorithm that searches for a box on the screen. So far I got the snake to find the food which works great except that for example. If the snake is moving right it will sometimes move left and move ontop of it's self. I've spent awhile trying to figure it out on my own with no success. I think it has to do with the two blocks of if statements but I'm not sure. Maybe they need to be combined somehow? This is the last thing I need to figure out and my program is finished. Thanks in advance. I tried.

    if(direction_comp!=1){direction_comp=-1;} else {direction_comp=2;} // If snake is not moving left then move it right else move it up to avoid it from moving on top of it's self.

    direction_comp of 1 = left;
    direction_comp of -1 = right;
    direction_comp of 2 = up;
    direction_comp of -2 = down;

    void comp_direction()
    {

    			if(segments\_comp\[head\_comp\]->panel->Left <= Food->Left)
    			{	
    				if(segments\_comp\[head\_comp\]->panel->Top >= Food->Top){direction\_comp =2;}
    				else{direction\_comp=-1;}
    			}
    			else if(segments\_comp\[head\_comp\]->panel->Left >= Food->Left)
    			{	
    				if(segments\_comp\[head\_comp\]->panel->Top >= Food->Top){direction\_comp =2;}
    				else{direction\_comp=1;}
    
    			}
    			if(segments\_comp\[head\_comp\]->panel->Top <= Food->Top)
    			{
    				if(segments\_comp\[head\_comp\]->panel->Left <= Food->Left){direction\_comp =-1;}
    				else{direction\_comp=-2;}
    				
    			}
    			else if(segments\_comp\[head\_comp\]->panel->Top >= Food->Top)
    			{
    				if(segments\_comp\[head\_comp\]->panel->Left >= Food->Left){direction\_comp =1;}
    				else{direction\_comp=2;}
    			}
    
    		}
    

    modified on Friday, April 15, 2011 7:47 PM

    H 1 Reply Last reply
    0
    • C Cyclone_S

      Hi, I am coding an algorithm that searches for a box on the screen. So far I got the snake to find the food which works great except that for example. If the snake is moving right it will sometimes move left and move ontop of it's self. I've spent awhile trying to figure it out on my own with no success. I think it has to do with the two blocks of if statements but I'm not sure. Maybe they need to be combined somehow? This is the last thing I need to figure out and my program is finished. Thanks in advance. I tried.

      if(direction_comp!=1){direction_comp=-1;} else {direction_comp=2;} // If snake is not moving left then move it right else move it up to avoid it from moving on top of it's self.

      direction_comp of 1 = left;
      direction_comp of -1 = right;
      direction_comp of 2 = up;
      direction_comp of -2 = down;

      void comp_direction()
      {

      			if(segments\_comp\[head\_comp\]->panel->Left <= Food->Left)
      			{	
      				if(segments\_comp\[head\_comp\]->panel->Top >= Food->Top){direction\_comp =2;}
      				else{direction\_comp=-1;}
      			}
      			else if(segments\_comp\[head\_comp\]->panel->Left >= Food->Left)
      			{	
      				if(segments\_comp\[head\_comp\]->panel->Top >= Food->Top){direction\_comp =2;}
      				else{direction\_comp=1;}
      
      			}
      			if(segments\_comp\[head\_comp\]->panel->Top <= Food->Top)
      			{
      				if(segments\_comp\[head\_comp\]->panel->Left <= Food->Left){direction\_comp =-1;}
      				else{direction\_comp=-2;}
      				
      			}
      			else if(segments\_comp\[head\_comp\]->panel->Top >= Food->Top)
      			{
      				if(segments\_comp\[head\_comp\]->panel->Left >= Food->Left){direction\_comp =1;}
      				else{direction\_comp=2;}
      			}
      
      		}
      

      modified on Friday, April 15, 2011 7:47 PM

      H Offline
      H Offline
      Hans Dietrich
      wrote on last edited by
      #2

      You may get a better response by posting here: http://www.codeproject.com/Forums/326859/Algorithms.aspx[^]

      Best wishes, Hans


      [Hans Dietrich Software]

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups