-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.cpp
More file actions
98 lines (81 loc) · 1.57 KB
/
Copy pathSnake.cpp
File metadata and controls
98 lines (81 loc) · 1.57 KB
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
//
// Created by mario on 2/13/24.
//
#include "Snake.h"
Snake::Snake() {
this->direction = 2;
this->x = 1;
this->y = 0;
head = new Node;
head->x = 9;
head->y = 13;
}
int Snake::getX() const {
return x;
}
int Snake::getY() const {
return y;
}
int Snake::getCx() const {
return cx;
}
int Snake::getCy() const {
return cy;
}
Node *Snake::getHead() const {
return head;
}
int Snake::getDirection() const {
return direction;
}
Node *Snake::getTail() const {
Node *tmp = head;
while (tmp->next != nullptr)
tmp = tmp->next;
return tmp;
}
void Snake::setTail(int a, int b) {
this->cx = a;
this->cy = b;
}
void Snake::move(int direction) {
switch (direction) {
case 1: //up
this->x = -1;
this->y = 0;
this->direction = 1;
break;
case 2://down
this->x = 1;
this->y = 0;
this->direction = 2;
break;
case 3://right
this->x = 0;
this->y = 1;
this->direction = 3;
break;
case 4://left
this->x = 0;
this->y = -1;
this->direction = 4;
break;
}
}
void Snake::eat() {
Node *tail = getTail();
Node *extend = new Node;
extend->x = cx;
extend->y = cy;
tail->next = extend;
extend->prev = tail;
extend->next = nullptr;
}
void Snake::remove(Node *node) {
if (node->next != nullptr)
remove(node->next);
delete node;
}
Snake::~Snake() {
remove(getHead());
}