-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snake.cpp
165 lines (133 loc) · 5.75 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
154
155
156
157
158
159
160
161
162
163
164
165
#include "Snake.h"
#include <cassert>
#include "olcPixelGameEngine/olcPixelGameEngine.h"
#include <stdlib.h>
const size_t Snake::cellSizeHalf = 64;
const size_t Snake::cellSize = cellSizeHalf * 2;
const uint8_t Snake::opacity = 150;
const double Snake::limbSizeMult = 0.8;
olc::vi2d Snake::randomPos()
{
static double foo = this->_width / (double)RAND_MAX;
static double bar = this->_height / (double)RAND_MAX;
return olc::vi2d( std::min(int32_t(std::rand() * foo), int32_t(this->_width-1)), std::min(int32_t(std::rand() * bar), int32_t(this->_height-1)));
}
Snake::Snake(size_t width, size_t height)
: _width(width)
, _height(height)
, _graph(_width, _height)
, _snek()
, _apple(this->randomPos())
{
assert(_width % 2 == 0);
assert(_height % 2 == 0);
_graph.initializeHamiltonian();
// set apple
this->_graph.getVertex(this->_apple.x, this->_apple.y)->hasApple = true;
// place head
Vertex* headVert = this->_graph.getVertex(olc::vi2d{2,3});
Limb head(headVert);
this->_snek.moveTo(head);
}
Snake::~Snake()
{ }
void Snake::draw(olc::PixelGameEngine* const pge)
{
pge->SetPixelMode(olc::Pixel::NORMAL);
olc::vi2d horz(Snake::cellSizeHalf, 0);
olc::vi2d vert( 0, Snake::cellSizeHalf);
// cell background, checkerboard, indices
for(size_t col = 0; col < this->_width; ++col)
{
for(size_t row = 0; row < this->_height; ++row)
{
olc::vi2d center(int32_t(col * Snake::cellSize + Snake::cellSizeHalf), int32_t(row * Snake::cellSize + Snake::cellSizeHalf));
if( (col + row) % 2 == 0)
pge->FillRect(center -horz -vert, olc::vi2d(Snake::cellSize, Snake::cellSize), olc::Pixel(20, 20, 20));
pge->DrawString(center -olc::vi2d{10,10}, std::to_string(row) + "," + std::to_string(col), olc::VERY_DARK_GREY, 1);
}
}
this->_graph.calculateDistances(this->_graph.getVertex(this->_apple));
for(size_t col = 0; col < this->_width; ++col)
{
for(size_t row = 0; row < this->_height; ++row)
{
Vertex* const v = this->_graph.getVertex(row, col);
olc::vi2d center(int32_t(col * Snake::cellSize + Snake::cellSizeHalf), int32_t(row * Snake::cellSize + Snake::cellSizeHalf));
const olc::Pixel& enabledColor = olc::BLUE;
const olc::Pixel& disabledColor = olc::DARK_GREY;
if(v->north != nullptr)
pge->DrawLine(center, center - vert, v->walkableNorth ? enabledColor : disabledColor);
if(v->south != nullptr)
pge->DrawLine(center, center + vert, v->walkableSouth ? enabledColor : disabledColor);
if(v->east != nullptr)
pge->DrawLine(center, center + horz, v->walkableEast ? enabledColor : disabledColor);
if(v->west != nullptr)
pge->DrawLine(center, center - horz, v->walkableWest ? enabledColor : disabledColor);
// draw distances
pge->DrawString(center -olc::vi2d{24, 24}, std::to_string(v->distanceToApple), olc::DARK_GREY, 1);
}
}
pge->SetPixelMode(olc::Pixel::ALPHA);
olc::vi2d limbSize = Snake::limbSizeMult * olc::vi2d(Snake::cellSize, Snake::cellSize);
olc::vi2d limbSizeOffset = 0.5 * (olc::vi2d(Snake::cellSize, Snake::cellSize) - limbSize);
std::cout << limbSize.x << ", " << limbSize.y << std::endl;
// draw snek
for(Limb limb : this->_snek)
{
olc::vi2d pos = limb.getPos() * Snake::cellSize;
// std::cout << "limbpos: (" << limb.getPos().x << ", " << limb.getPos().y << ")" << std::endl;
pge->FillRect(pos + limbSizeOffset, limbSize, olc::Pixel(0, 255, 0, Snake::opacity));
//TODO fill space betweem this limb and next, if there is a next
//Vertex* appleVert = this->_graph.getVertex(this->_apple);
//pge->FillCircle(appleVert->pos * Snake::cellSize + horz + vert, 8, olc::Pixel(255, 0, 0, Snake::opacity));
// debug code:
//Vertex* lv = limb.getVert();
//pge->FillCircle(lv->pos * Snake::cellSize + horz + vert, 8, olc::Pixel(255, 255, 255, Snake::opacity));
//
//if(lv->north != nullptr)
// pge->FillCircle(lv->north->pos * Snake::cellSize + horz + vert, 8, olc::Pixel(255, 0, 0, Snake::opacity));
//if(lv->east != nullptr)
// pge->FillCircle(lv->east ->pos * Snake::cellSize + horz + vert, 8, olc::Pixel( 0, 255, 0, Snake::opacity));
//if(lv->south != nullptr)
// pge->FillCircle(lv->south->pos * Snake::cellSize + horz + vert, 8, olc::Pixel( 0, 0, 255, Snake::opacity));
//if(lv->west != nullptr)
// pge->FillCircle(lv->west ->pos * Snake::cellSize + horz + vert, 8, olc::Pixel(255, 0, 255, Snake::opacity));
}
// draw apple
pge->FillCircle(this->_apple * Snake::cellSize + horz + vert, int32_t(Snake::cellSizeHalf*0.8), olc::Pixel(255, 0, 0, Snake::opacity));
}
// returns success of iteration
bool Snake::iterate()
{
Limb& head = this->_snek.getHead();
Vertex* headVert = head.getVert();
Limb nextPos;
bool lengthONE = this->_snek.length() <= 1;
if(headVert->walkableNorth && ((!lengthONE && !headVert->north->hasLimb) || (lengthONE && !headVert->north->hadTail)))
nextPos.setVert(headVert->north);
else if(headVert->walkableEast && ((!lengthONE &&!headVert->east->hasLimb) || (lengthONE && !headVert->east->hadTail)))
nextPos.setVert(headVert->east);
else if(headVert->walkableSouth && ((!lengthONE &&!headVert->south->hasLimb) || (lengthONE && !headVert->south->hadTail)))
nextPos.setVert(headVert->south);
else if(headVert->walkableWest && ((!lengthONE &&!headVert->west->hasLimb) || (lengthONE && !headVert->west->hadTail)))
nextPos.setVert(headVert->west);
else
return false; // cant make a move along the path
bool hasEaten = this->_snek.moveTo(nextPos);
if(hasEaten)
{
if(this->_snek.length() == this->_width * this->_height)
{
// board full, one has won
return false;
}
// choose next apple position, until it finds a spot where there is no limb
while(this->_graph.getVertex(this->_apple)->hasLimb)
{
this->_apple = this->randomPos();
}
this->_graph.getVertex(this->_apple)->hasApple = true;
}
return true;
}