-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.cpp
More file actions
118 lines (99 loc) · 2.83 KB
/
Copy pathsnake.cpp
File metadata and controls
118 lines (99 loc) · 2.83 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "snake.h"
#include <random>
std::atomic<int64_t> Snake::GlobalSnakeCounter{1};
Snake::Snake()
: kSnakeId(GlobalSnakeCounter.fetch_add(1))
{
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(1, 4);
Direction startDirection = static_cast<Direction>(distribution(generator));
_headDirection = startDirection;
_lastMoveDirection = startDirection;
// Initialize initial cells
Coordinates coord;
coord.x = 0;
coord.y = 0;
Direction tailDirection = oppositeDirection(startDirection);
for (int i = 0; i < kDefaultSnakeSize; ++i)
{
auto currentCoordinates = coord.shift(tailDirection, i);
_snakeCells.push_back(currentCoordinates);
}
}
void Snake::setField(std::shared_ptr<Field> field)
{
auto orientation = directionOrientation(_headDirection);
auto initialPosition = field->getFreeCells(_snakeCells.size(), orientation);
auto diff = _snakeCells.front().diff(initialPosition);
Cell snakeCell(CellType::Snake);
snakeCell.internalId = id();
for (auto & cell : _snakeCells)
{
cell = cell.shift(diff.xdir, diff.xdiff);
cell = cell.shift(diff.ydir, diff.ydiff);
field->set(cell, snakeCell);
}
// set head
snakeCell.type = CellType::SnakeHead;
field->set(headPosition(), snakeCell);
_field = std::move(field);
}
void Snake::leaveField()
{
for (auto & cell : _snakeCells)
{
_field->set(cell, CellType::Empty);
}
_field.reset();
}
Coordinates Snake::headPosition() const
{
return _snakeCells.front();
}
void Snake::moveHead()
{
if (!_field) return;
auto nextPosition = _field->nextPosition(headPosition(), _headDirection);
auto cell = _field->get(nextPosition);
if (cell.isFree())
{
// Move head
auto prevHeadPosition = headPosition();
_snakeCells.push_front(nextPosition);
// Consume apple if exists
if (cell.type == CellType::Apple)
{
_satiety++;
_field->consumeApple();
}
Cell snakeCell(CellType::SnakeHead);
snakeCell.internalId = id();
_field->set(nextPosition, snakeCell);
snakeCell.type = CellType::Snake;
_field->set(prevHeadPosition, snakeCell);
_lastMoveDirection = _headDirection;
}
// Move tail if necessary
if (_satiety)
--_satiety;
else
if (_snakeCells.size() > kDefaultSnakeSize)
{
auto coordinates = _snakeCells.back();
_field->set(coordinates, CellType::Empty);
_snakeCells.pop_back();
}
}
void Snake::setDirection(Direction dir)
{
if (oppositeDirection(dir) == _lastMoveDirection) return;
_headDirection = dir;
}
int64_t Snake::id() const
{
return kSnakeId;
}
size_t Snake::length() const
{
return _snakeCells.size();
}