forked from marinhero/nibbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snake.cpp
153 lines (135 loc) · 3.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// Snake.cpp for nibbler in /home/ignatiev/Projects/nibbler
//
// Made by Marin Alcaraz
// Login <alcara_m@epitech.net>
//
// Started on Fri Mar 22 19:52:37 2013 Marin Alcaraz
// Last update Sun Mar 24 18:13:19 2013 ivan ignatiev
//
#include "Snake.hh"
Snake :: Snake(Surface const *surface)
: size(4), direction(X_POS), surface_(surface)
{
std::vector<point_t> b;
point_t var;
var.x = 1;
var.y = 1;
var.type = F_SNAKE_SECT;
body = b;
while(var.x < 4)
{
body.push_back(var);
var.x++;
}
var.type = F_SNAKE_HEAD;
body.push_back(var);
}
Snake :: Snake(const Snake &obj) : body(obj.body), size(obj.size) {}
Snake :: ~Snake() {}
void Snake :: set_direction(direction_t dir)
{
this->direction = dir;
}
direction_t Snake :: get_direction() const
{
return (this->direction);
}
bool Snake :: left(void)
{
if ((this->get_direction() == X_POS))
{
this->set_direction(Y_POS);
return (true);
}
if ((this->get_direction() == X_NEG))
{
this->set_direction(Y_NEG);
return (true);
}
if ((this->get_direction() == Y_POS) || (this->get_direction() == Y_NEG))
{
this->set_direction(X_NEG);
return (true);
}
return (false);
}
bool Snake :: right(void)
{
if (this->get_direction() == X_POS)
{
this->set_direction(Y_NEG);
return (true);
}
if (this->get_direction() == X_NEG)
{
this->set_direction(Y_POS);
return (true);
}
if ((this->get_direction() == Y_POS) || (this->get_direction() == Y_NEG))
{
this->set_direction(X_POS);
return (true);
}
return (false);
}
bool Snake :: move_body(void)
{
int head;
head = get_size() - 1;
for (int i = 0; i <= head - 1; ++i)
{
body[i] = body[i + 1];
body[i].type = F_SNAKE_SECT;
}
return (true);
}
bool Snake :: colition(point_t head) const
{
std::vector<point_t>::const_iterator it = this->body.begin();
std::vector<point_t>::const_iterator snake_end = this->body.end();
for (; it != snake_end; ++it)
{
if (it->x == head.x && it->y == head.y)
return (true);
}
return (false);
}
bool Snake :: move(void)
{
int head;
point_t nh;
head = this->get_size() - 1;
nh = body[head];
switch (this->get_direction())
{
case X_POS: nh.x++; break;
case Y_POS: nh.y--; break;
case X_NEG: nh.x--; break;
case Y_NEG: nh.y++; break;
default: return (false);
}
if (!surface_->check_space(body[head]) || colition(nh))
return (false);
if (food_->try_eat(nh, this->size))
{
body[head].type = F_SNAKE_SECT;
body.push_back(nh);
return (true);
}
move_body();
body[head] = nh;
return (true);
}
std::vector<point_t> const &Snake :: get_snake(void) const
{
return (this->body);
}
size_t Snake :: get_size(void)
{
return (this->size);
}
void Snake :: set_food(Food *food)
{
this->food_ = food;
}