📄 snake.cc
字号:
#include "Snake.h"
#include <cmath>
void Foods::init( int max, int x_max, int x_min, int y_max, int y_min ) {
food.resize( max );
for ( vector<Pos>::iterator iter = food.begin(); iter != food.end(); ++iter ) {
iter->x = rand() % ( x_max - x_min ) + x_min;
iter->y = rand() % ( y_max - y_min ) + y_min;
}
}
int Foods::getX( int index ) {
return food[index].x;
}
int Foods::getY( int index ) {
return food[index].y;
}
Snake::Snake() {
tail = head = new Body;
head->prev = NULL;
head->next = NULL;
head->pos.x = 0;
head->pos.y = 0;
directionOld = direction = "right";
}
Snake::~Snake() {
while ( tail != head ) {
tail = tail->next;
delete tail->prev;
}
delete tail;
}
void Snake::turn( string direction ) {
this->direction = direction;
}
int Snake::length() {
int count = 0;
Body *temp = tail;
while ( temp != NULL ) {
++count;
temp = temp->next;
}
return count;
}
void Snake::move() {
cout << "Receive move signal, head's pos is " << head->pos.x << ", " << head->pos.y << ". the next's pos is " << head->prev->pos.x << ", " << head->prev->pos.y << endl;
cout << "move begin" << endl << "check direction" << endl;
if ( direction != "right" && direction != "left" && direction != "up" && direction != "down" ) {
direction = directionOld;
}
cout << "check tail == head ?" << endl;
if ( tail == head ) {
if ( direction == "right" ) {
head->pos.x += 1;
}
else if ( direction == "left" ) {
head->pos.x -= 1;
}
else if ( direction == "up" ) {
head->pos.y += 1;
}
else if ( direction == "down" ) {
head->pos.y -= 1;
}
cout << "tail == head" << endl;
return;
}
cout << "head != tail" << endl << "check direction right" << endl;
if ( direction == "right" ) {
if ( head->pos.x + 1 == head->prev->pos.x && head->pos.y == head->prev->pos.y ) {
cout << "Can not turn right" << endl;
direction = directionOld;
}
}
else if ( direction == "left" ) {
cout << "check direction left" << endl;
if ( head->pos.x - 1 == head->prev->pos.x && head->pos.y == head->prev->pos.y ) {
cout << "Can not turn left" << endl;
direction = directionOld;
}
}
else if ( direction == "up" ) {
cout << "check direction up" << endl;
if ( head->pos.y + 1 == head->prev->pos.y && head->pos.x == head->prev->pos.x ) {
cout << "Can not turn up" << endl;
direction = directionOld;
}
}
else if ( direction == "down" ) {
cout << "check direction down" << endl;
if ( head->pos.y - 1 == head->prev->pos.y && head->pos.x == head->prev->pos.x ) {
cout << "Can not turn down" << endl;
direction = directionOld;
}
}
cout << "begin to move" << endl;
Body *temp = tail;
while ( temp != head ) {
temp->pos = temp->next->pos;
temp = temp->next;
}
if ( direction == "right" ) {
cout << "direction = [right], so move right" << endl;
head->pos.x += 1;
}
else if ( direction == "left" ) {
cout << "direction = [left], so move left" << endl;
head->pos.x -= 1;
}
else if ( direction == "up" ) {
cout << "direction = [up], so move up" << endl;
head->pos.y += 1;
}
else if ( direction == "down" ) {
cout << "direction = [down], so move down" << endl;
head->pos.y -= 1;
}
directionOld = direction;
}
void Snake::grow() {
head->next = new Body;
head->next->prev = head;
head = head->next;
head->next = NULL;
if ( direction == "right" ) {
head->pos.x = head->prev->pos.x + 1;
head->pos.y = head->prev->pos.y;
}
else if ( direction == "left" ) {
head->pos.x = head->prev->pos.x - 1;
head->pos.y = head->prev->pos.y;
}
else if ( direction == "up" ) {
head->pos.y = head->prev->pos.y + 1;
head->pos.x = head->prev->pos.x;
}
else if ( direction == "down" ) {
head->pos.y = head->prev->pos.y - 1;
head->pos.x = head->prev->pos.x;
}
}
bool Snake::biteSelf() {
Body *temp = tail;
while ( temp != head ) {
if ( head->pos.x == temp->pos.x && head->pos.y == temp->pos.y ) {
return true;
}
temp = temp->next;
}
return false;
}
Body* Snake::getSnakeTail() {
return tail;
}
Body* Snake::getSnakeHead() {
return head;
}
void Snake::display() {
int map[20][20];
for ( int i = 0; i != 20; ++i ) {
for ( int j = 0; j != 20; ++j ) {
map[i][j] = 0;
}
}
Body *temp = tail;
while ( temp != NULL ) {
map[temp->pos.x][temp->pos.y] = 1;
temp = temp->next;
}
for ( int j = 0; j != 20; ++j ) {
for ( int i = 0; i != 20; ++i ) {
cout << map[i][j] << " ";
}
cout << endl;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -