Okay I feel really stupid... I've been trying to create a Snake clone. Right now I'm just doing it in the console with a number grid - though it'll be easily be turned to graphics with SFML. Just trying to get the basics done before graphics.
Anyway...okay I was trying to do the snake with a linked list(Which I've never used before). Moves around just fine. Except I must be creating it incorrectly or something. Because I cannot seem to grab the end of the snake's coordinates. It continually thinks it's the same - and I want them in order to make it empty when the end moves.
I know I'm sure I'm doing plenty of unnecessary things. My brains just been fried and I think I know what it is, but just can't solve it. Ready for a brain replacement.
Code:
snake.h
#include <iostream>
#include "grid.h"
class Snake
{
public:
Snake();
enum Direction
{
UP,
DOWN,
LEFT,
RIGHT,
};
void createSnake(Grid &grid);
void move( Direction direct );
void grow();
int getPosX();
int getPosY();
...
private:
int mPosX;
int mPosY;
int mDirection;
int mEndX;
int mEndY;
int getEndX();
int getEndY();
void moveNode( Snake *link, int x, int y );
Snake *next;
Snake *head;
Snake *link;
...
};
snake.cpp
void Snake::createSnake(Grid &grid)
{
mGrid = &grid;
head = new Snake;
head->next = 0;
head->mPosX = 10;
head->mPosY = 10;
mGrid->setCell(grid.SNAKE, head->mPosX, head->mPosY);
for( int i = 0; i < 2; i++ )
{
this->link = head;
if( this->link != 0 )
{
this->link->next = new Snake;
this->link = this->link->next;
this->link->next = 0;
this->link->mPosX = 11 + i;
this->link->mPosY = 10;
mGrid->setCell(mGrid->SNAKE, this->link->mPosX, this->link->mPosY);
}
}
}
int Snake::getEndX()
{
this->link = head;
while ( this->link->next != 0 )
{
this->link = this->link->next;
std::cout << "X: " << this->link->mPosX << std::endl;
}
return this->link->mPosX;
}
int Snake::getEndY()
{
this->link = head;
while ( this->link->next != 0 )
{
this->link = this->link->next;
}
return this->link->mPosY;
}
void Snake::move( Direction direct )
{
head->mDirection = direct;
int origX = head->getPosX();
int origY = head->getPosY();
if( head->mDirection == LEFT )
{
head->mPosX -= 1;
}
else if( head->mDirection == RIGHT )
head->mPosX += 1;
else if( head->mDirection == UP )
head->mPosY -= 1;
else if( head->mDirection == DOWN )
head->mPosY += 1;
mGrid->setCell(mGrid->SNAKE, head->mPosX, head->mPosY);
this->link = head;
this->link = this->link->next;
moveNode( this->link, origX, origY );
}
void Snake::moveNode( Snake *link, int x, int y )
{
int tempX, tempY = 0;
mEndX = getEndX();
mEndY = getEndY();
std::cout << std::endl << std::endl << "EndX: " << mEndX << " EndY: " << mEndY << std::endl << std::endl;
//if( this->link->next != 0 )
//{
tempX = this->link->mPosX;
tempY = this->link->mPosY;
this->link->mPosX = x;
this->link->mPosY = y;
mGrid->setCell(mGrid->SNAKE, this->link->mPosX, link->mPosY);
mGrid->setCell(mGrid->EMPTY, mEndX, mEndY);
if( this->link->next != 0 )
{
this->link = this->link->next;
moveNode( this->link, tempX, tempY );
}
//}
this->link->mPosX = tempX;
this->link->mPosY = tempY;
mGrid->setCell(mGrid->SNAKE, this->link->mPosX, link->mPosY);
}
In Console: