-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake_class.cpp
More file actions
75 lines (73 loc) · 1.74 KB
/
Copy pathsnake_class.cpp
File metadata and controls
75 lines (73 loc) · 1.74 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
#include "snake_class.h"
Snake :: Snake(){
body.push_back({1,1});
this->direction = "RIGHT";
}
void Snake :: Move(){
if(direction == "RIGHT")
{
auto front = body.back();
body.push_back({front.first , front.second+1});
}
else if(direction == "LEFT"){
auto front = body.back();
body.push_back({front.first , front.second-1});
}
else if(direction == "DOWN")
{
auto front = body.back();
body.push_back({front.first+1 , front.second});
}
else{
auto front = body.back();
body.push_back({front.first-1 , front.second});
}
body.pop_front();
if(Check_self_collision())
{
//return game finished
}
}
void Snake :: Grow(){
if(direction == "RIGHT")
{
auto front = body.back();
body.push_back({front.first , front.second+1});
}
else if(direction == "LEFT"){
auto front = body.back();
body.push_back({front.first , front.second-1});
}
else if(direction == "DOWN")
{
auto front = body.back();
body.push_back({front.first+1 , front.second});
}
else{
auto front = body.back();
body.push_back({front.first-1 , front.second});
}
if(Check_self_collision())
{
//finish game
}
}
void Snake :: Change_direction(std :: string newDirection){
this->direction = newDirection;
if(Check_self_collision())
{
//finish game
}
}
bool Snake :: Check_self_collision(){
auto head = body.back();
for (size_t i = 0; i < body.size() - 1; ++i) {
if (body[i] == head) {
return true;
}
}
return false;
}
std::deque<std::pair<int,int>> Snake :: getBody() const{
return this->body;
}