Skip to content

Commit

Permalink
Refactor Snake
Browse files Browse the repository at this point in the history
- Removed coordinates property
  • Loading branch information
tigertv committed Mar 13, 2020
1 parent 01c504a commit 2da1654
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 63 deletions.
4 changes: 0 additions & 4 deletions include/Snake.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ struct SnakeBodyPart {

class Snake {
private:
int length = 1;
Direction direction;
std::list<std::pair<int,int>> coords;
std::deque<SnakeBodyPart> bodyParts;
void go();
void go(Direction direction);
Expand All @@ -37,10 +35,8 @@ class Snake {
Snake();
virtual ~Snake();
void update();
std::pair<int, int>getHead();
void grow();
int getLength();
std::list<std::pair<int,int>>* getCoordinates();
std::deque<SnakeBodyPart>* getBodyParts();
void changeDirection(Direction direction);
Direction getDirection();
Expand Down
24 changes: 12 additions & 12 deletions src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,25 @@ void Game::update() {
snake.update();

// collisions?
std::pair<int,int> head = snake.getHead();
std::deque<SnakeBodyPart> *parts = snake.getBodyParts();
SnakeBodyPart* head = &(*parts)[0];

// itself collision
std::list<std::pair<int, int>> *coords = snake.getCoordinates();
std::list<std::pair<int, int>>::iterator it = coords->begin();
for(it++; it != coords->end(); it++) {
if (it->first == head.first && it->second == head.second) {
// we have a collision
this->isPlaying = false;
int size = (int)parts->size();
for(int i = 1; i < size; i++) {
SnakeBodyPart& part = (*parts)[i];
if(part.point.x == head->point.x && part.point.y == head->point.y) {
// game over
this->isPlaying = false;
return;
}
}

// border collision
if (((head.first == borderFrame.x || head.first == borderFrame.x + borderFrame.width) &&
head.second >= borderFrame.y && head.second < borderFrame.y + borderFrame.height) ||
((head.second == borderFrame.y || head.second == borderFrame.y + borderFrame.height) &&
head.first >= borderFrame.x && head.first < borderFrame.x + borderFrame.width)
if(((head->point.x == borderFrame.x || head->point.x == borderFrame.x + borderFrame.width) &&
head->point.y >= borderFrame.y && head->point.y < borderFrame.y + borderFrame.height) ||
((head->point.y == borderFrame.y || head->point.y == borderFrame.y + borderFrame.height) &&
head->point.x >= borderFrame.x && head->point.x < borderFrame.x + borderFrame.width)
) {
// you don't have to go out of the border space
this->isPlaying = false;
Expand All @@ -77,7 +77,7 @@ void Game::update() {
}

// has eaten food?
if (head.first == food->x && head.second == food->y) {
if (head->point.x == food->x && head->point.y == food->y) {
snake.grow();
this->score += food->value;
food = foodBuilder.getFood();
Expand Down
36 changes: 4 additions & 32 deletions src/Snake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
Snake::Snake() {
direction = Direction::RIGHT;
// the head of the snake
std::pair<int,int> p = {5,5};
coords.push_front(p);
this->grow();

SnakeBodyPart part;
part.direction.x = 1;
Expand All @@ -14,7 +11,7 @@ Snake::Snake() {
part.point.y = 5;
bodyParts.push_back(part);


this->grow();
}

Snake::~Snake() {
Expand All @@ -27,53 +24,40 @@ void Snake::go(Direction direction) {
}

void Snake::go() {
std::pair<int,int> head = this->getHead();
int x = head.first;
int y = head.second;

SnakeBodyPart addHead;
addHead.direction.x = 0;
addHead.direction.y = 0;

switch(this->direction) {
case Direction::UP:
y--;
addHead.direction.y = -1;
break;
case Direction::DOWN:
y++;
addHead.direction.y = 1;
break;
case Direction::LEFT:
x--;
addHead.direction.x = -1;
break;
case Direction::RIGHT:
x++;
addHead.direction.x = 1;
break;
}

addHead.point.x = bodyParts[0].point.x + addHead.direction.x;
addHead.point.y = bodyParts[0].point.y + addHead.direction.y;

if ((addHead.direction.x != bodyParts[0].direction.x) &&
(addHead.direction.y != bodyParts[0].direction.y)) {

bodyParts[0].direction.x -= addHead.direction.x;
bodyParts[0].direction.y -= addHead.direction.y;
}

head = {x, y};
coords.push_front(head);

addHead.point.x = x;
addHead.point.y = y;
bodyParts.push_front(addHead);

if (this->isGrowing) {
this->length++;
this->isGrowing = false;
} else {
coords.pop_back();

int x = bodyParts.back().direction.x;
int y = bodyParts.back().direction.y;
bodyParts.pop_back();
Expand All @@ -88,22 +72,10 @@ void Snake::go() {

}

std::pair<int, int> Snake::getHead() {
return coords.front();
}

void Snake::grow() {
this->isGrowing = true;
}

int Snake::getLength() {
return this->length;
}

std::list<std::pair<int,int>>* Snake::getCoordinates() {
return &(this->coords);
}

void Snake::update() {
this->go();
}
Expand Down
12 changes: 7 additions & 5 deletions src/graphics/NCursesGraphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,20 @@ void NCursesGraphics::render() {
Food* food = game->getFood();

// render snake
std::list<std::pair<int, int>> *coords = snake->getCoordinates();
std::list<std::pair<int, int>>::iterator it = coords->begin();
std::deque<SnakeBodyPart> *parts = snake->getBodyParts();
SnakeBodyPart* part = &(*parts)[0];

// render head
attron(COLOR_PAIR(SNAKE_HEAD_PAIR));
mvaddch(it->second, it->first, '@');
mvaddch(part->point.y, part->point.x, '@');
attroff(COLOR_PAIR(SNAKE_HEAD_PAIR));

// render body
attron(COLOR_PAIR(SNAKE_BODY_PAIR));
for(it++; it != coords->end(); it++) {
mvaddch(it->second, it->first, 'o');
int size = parts->size();
for (int i = 1; i < size; i++) {
part = &(*parts)[i];
mvaddch(part->point.y, part->point.x, 'o');
}
attroff(COLOR_PAIR(SNAKE_BODY_PAIR));

Expand Down
12 changes: 7 additions & 5 deletions src/graphics/NoLibLinuxGraphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,19 @@ void NoLibLinuxGraphics::render() {
std::system("clear");

// render snake
std::list<std::pair<int, int>> *coords = snake->getCoordinates();
std::list<std::pair<int, int>>::iterator it = coords->begin();
std::deque<SnakeBodyPart> *parts = snake->getBodyParts();
SnakeBodyPart* part = &(*parts)[0];

// render head
std::cout << "\033[1;33m"; // change color
renderChar(it->first, it->second, '@');
renderChar(part->point.x, part->point.y, '@');

// render body
std::cout << "\033[1;32m"; // change color
for(it++; it != coords->end(); it++) {
renderChar(it->first, it->second, 'o');
int size = parts->size();
for (int i = 1; i < size; i++) {
part = &(*parts)[i];
renderChar(part->point.x, part->point.y, 'o');
}

// render boarders
Expand Down
12 changes: 7 additions & 5 deletions src/graphics/OpenGlGraphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,21 @@ void OpenGlGraphics::render() {
Food* food = game->getFood();

// render snake
std::list<std::pair<int, int>> *coords = snake->getCoordinates();
std::list<std::pair<int, int>>::iterator it = coords->begin();
std::deque<SnakeBodyPart> *parts = snake->getBodyParts();
SnakeBodyPart* part = &(*parts)[0];

const int cellSize = 1;

// render head
u_int color = 0x00ff10;
this->renderBox(it->first, it->second, cellSize, color);
this->renderBox(part->point.x, part->point.y, cellSize, color);

// render body
color = 0x00ea3c;
for(it++; it != coords->end(); it++) {
this->renderBox(it->first, it->second, cellSize, color);
int size = parts->size();
for (int i = 1; i < size; i++) {
part = &(*parts)[i];
this->renderBox(part->point.x, part->point.y, cellSize, color);
}

// render boarders
Expand Down

0 comments on commit 2da1654

Please sign in to comment.