/*
 * Snake.h by Jocke "Firetech" Andersson (With help from "Bloodyduck")
 *
 * Licence: Free (as in freedom) to use, abuse and/or modify (and redistribute).
 */

const char shvers[] = "1.0.3.1";

class snake
{
public:
  int x, y;
  snake * next, * prev;
  snake( snake *, char dir, int px = 0, int py = 0 );
};

//function prototypes
snake * movesnake( snake * head, char dir, int & tailx, int & taily );

snake::snake( snake * psnake, char dir, int px, int py )
{
  if ( psnake )
  {
    psnake->next = this;
    px = psnake->x;
    py = psnake->y;
    switch ( dir )
    {
      case 'r':
        this->x = px + 1;
        this->y = py;
      break;
      case 'l':
        this->x = px - 1;
        this->y = py;
      break;
      case 'u':
        this->x = px;
        this->y = py - 1;
      break;
      case 'd':
        this->x = px;
        this->y = py + 1;
      break;
    }
  }
  else
  {
    this->x = px;
    this->y = py;
  }

  this->next = NULL;
  this->prev = psnake;
}

snake * movesnake( snake * head, char dir, int & tailx, int & taily )
{
  snake * tail;
  tail = head;
  while ( tail->prev )
  {
    tail = tail->prev;
  }

  tailx = tail->x;
  taily = tail->y;

  switch ( dir )
  {
    case 'r':
      tail->x = head->x + 1;
      tail->y = head->y;
    break;
    case 'l':
      tail->x = head->x - 1;
      tail->y = head->y;
    break;
    case 'u':
      tail->x = head->x;
      tail->y = head->y - 1;
    break;
    case 'd':
      tail->x = head->x;
      tail->y = head->y + 1;
    break;
  }

  tail->prev = head;
  head->next = tail;

  if ( tail->next )
  {
    tail->next->prev = NULL;
    tail->next = NULL;
  }

  return tail;
}

