-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSnake.cpp
96 lines (83 loc) · 2.08 KB
/
Snake.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// Snake.cpp
// cpp_algo
//
//
//
#include "Snake.hpp"
#include <stdlib.h>
using namespace std;
//constructor
Snake::Snake(int num) {
this -> top = -1;
this -> snakeBody = new SnakeBody[num];
this -> dir_x = 0;
this -> dir_y = -1;
}
//getter
int Snake::getTop() const{
return this -> top;
}
int Snake::getDirX() const{
return this -> dir_x;
}
int Snake::getDirY() const{
return this -> dir_y;
}
SnakeBody* Snake::getSnakeBody() const{
return this -> snakeBody;
}
void Snake::getSnakePos(int num, int *x, int *y) const{
*x = this -> snakeBody[num].getX();
*y = this -> snakeBody[num].getY();
}
//setter
void Snake::setTop(int top) {
this -> top = top;
}
void Snake::setDirection(int x, int y) {
this -> dir_x = x;
this -> dir_y = y;
}
//push
void Snake::snakePush(int x, int y, const char* shape) {
this -> top++;
this -> snakeBody[this -> top].setX(x);
this -> snakeBody[this -> top].setY(y);
this -> snakeBody[this -> top].setShape(shape);
}
//pop
void Snake::snakePop() {
this -> top--;
}
//move
void Snake::snakeMove() {
for (int i = this -> top; i > 0; i--) {
this -> snakeBody[i].setX(this -> snakeBody[i-1].getX());
this -> snakeBody[i].setY(this -> snakeBody[i-1].getY());
if (i == 1) break;
this -> snakeBody[i].setShape(this -> snakeBody[i-1].getShape());
}
this -> snakeBody[0].setX(this -> snakeBody[0].getX() + this -> dir_x);
this -> snakeBody[0].setY(this -> snakeBody[0].getY() + this -> dir_y);
}
//event
int Snake::eventDeath() const{
int headX, headY;
headX = this -> snakeBody[0].getX();
headY = this -> snakeBody[0].getY();
for (int i = 1; i <= this -> top; i++) {
if (this -> snakeBody[i].getX() == headX && this -> snakeBody[i].getY() == headY) {
return 1;
}
}
return 0;
}
void Snake::getHeadPos(int *x, int *y) const{
*x = this -> snakeBody[0].getX();
*y = this -> snakeBody[0].getY();
}
void Snake::setHeadPos(int x, int y) {
this -> snakeBody[0].setX(x);
this -> snakeBody[0].setY(y);
}