-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.cpp
More file actions
135 lines (108 loc) · 3.66 KB
/
Copy pathsnake.cpp
File metadata and controls
135 lines (108 loc) · 3.66 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "snake.h"
void snake::initBoard()
{
for( uint8_t row = 0; row < leds::HEIGHT; row++ )
{
for( uint8_t col = 0; col < leds::WIDTH; col++ )
{
if( row == 0 || row == leds::HEIGHT - 1 || col == 0 || col == leds::WIDTH - 1 )
snake::board[ row * leds::WIDTH + col ] = '/';
else
snake::board[ row * leds::WIDTH + col ] = ' ';
}
}
}
void snake::initSnake()
{
for( uint8_t i = 0; i < config::INIT_SNAKE_LENGTH; i++ )
{
snake::snakeVec.push_back( SnakeSegment( leds::HEIGHT / 2,i + 1 ) );
snake::board[ ( leds::HEIGHT / 2 * leds::WIDTH ) + i + 1 ] = 'O';
}
}
void snake::initFood()
{
do
{
uint8_t foodRow = random( 1,leds::HEIGHT - 1 );
uint8_t foodCol = random( 1,leds::WIDTH - 1 );
if( snake::board[ foodRow * leds::WIDTH + foodCol ] == ' ' )
{
snake::_food = FoodSegment( foodRow,foodCol );
snake::board[ foodRow * leds::WIDTH + foodCol ] = 'X';
break;
}
}
while( true );
leds::displayBoard( snake::board,snake::linearSnakeHeadIndex );
}
void snake::move()
{
if( !snake::_directionQueue.isEmpty() )
{
int8_t nextDirInt;
snake::_directionQueue.pop( &nextDirInt );
snake::lastDir = static_cast< snake::direction >( nextDirInt );
}
SnakeSegment head = snake::snakeVec.back();
switch( snake::lastDir )
{
case snake::direction::UP:
head.decRow();
break;
case snake::direction::DOWN:
head.incRow();
break;
case snake::direction::LEFT:
head.decCol();
break;
case snake::direction::RIGHT:
head.incCol();
break;
}
snake::linearSnakeHeadIndex = head.getRow() * leds::WIDTH + head.getCol();
snake::snakeVec.push_back( head );
if( !snake::hasWon() )
{
if( head == snake::_food ) snake::initFood();
else snake::deleteEndOfSnake();
}
snake::board[ snake::linearSnakeHeadIndex ] = 'O';
}
void snake::enqueueDirection( String direction )
{
int8_t dir = -1;
snake::direction lastTmpDir;
if( !snake::_directionQueue.isEmpty() )
{
int8_t lastQueueEntry;
snake::_directionQueue.peekPrevious( &lastQueueEntry );
lastTmpDir = static_cast< snake::direction >( lastQueueEntry );
}
else lastTmpDir = snake::lastDir;
if( ( direction == "U" || direction == "Y" ) && !( lastTmpDir == snake::direction::DOWN || lastTmpDir == snake::direction::UP ) ) dir = static_cast< int8_t >( snake::direction::UP );
else if( ( direction == "D" || direction == "A" ) && !( lastTmpDir == snake::direction::UP || lastTmpDir == snake::direction::DOWN ) ) dir = static_cast< int8_t >( snake::direction::DOWN );
else if( ( direction == "R" || direction == "B" ) && !( lastTmpDir == snake::direction::LEFT || lastTmpDir == snake::direction::RIGHT ) ) dir = static_cast< int8_t >( snake::direction::RIGHT );
else if( ( direction == "L" || direction == "X" ) && !( lastTmpDir == snake::direction::RIGHT || lastTmpDir == snake::direction::LEFT ) ) dir = static_cast< int8_t >( snake::direction::LEFT );
if( dir >= 0 && dir <= 3 ) snake::_directionQueue.push( &dir );
}
void snake::deleteEndOfSnake()
{
SnakeSegment tail = snake::snakeVec[ 0 ];
snake::board[ tail.getRow() * leds::WIDTH + tail.getCol() ] = ' ';
snake::snakeVec.remove( 0 );
}
bool snake::hasWon()
{
return snake::snakeVec.size() == leds::SIZE - 2 * ( leds::WIDTH - 1 ) - 2 * ( leds::HEIGHT - 1 );
}
bool snake::hasLost()
{
SnakeSegment head = snake::snakeVec.back();
if( head.getRow() == leds::HEIGHT - 1 || head.getCol() == leds::WIDTH - 1 || head.getRow() == 0 || head.getCol() == 0 ) return true;
for( uint8_t i = 0; i < snake::snakeVec.size() - 1; i++ )
{
if( head == snake::snakeVec[ i ] ) return true;
}
return false;
}