-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnake.cpp
179 lines (155 loc) · 4.45 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "snake.h"
#include "food.h"
#include <FL/Fl_Box.H>
#include <FL/Fl_JPEG_Image.H>
using namespace std;
//Fl_JPEG_Image background("nicc.jpeg");
Snake::Snake(int numSegments, int x, int y, int screenW, int screenH, string searchAlgorithm) :
startX(x),startY(y),
maxBufferSize(1000),
screenW(screenW), screenH(screenH),
Fl_Double_Window(screenW, screenH, searchAlgorithm.c_str()),
searchAlgorithm(searchAlgorithm),
direction("RIGHT"), dead(false)
{ /*
Fl_Box* bgImage = new Fl_Box(0, 0, 800, 600);
bgImage->image(background);
*/
//instantiate pathfinder
pathfinder = new Pathfinder(this);
this->color(FL_BLACK);
srand(time(NULL));
for (int i = 0; i < numSegments; i++)
{
Segment* bodySeg = new Segment(x + (20*i), y);
body.push_back(bodySeg);
}
food = new Food(0, 0);
food->move(this->w(), this->h(), body); //randomize initial food position
this->show();
}
bool Snake::checkDead()
{
return this->dead;
}
void Snake::addSegment()
{
//get tail pos
int tailX = body.back()->getX();
int tailY = body.back()->getY();
Segment* newSeg = new Segment(tailX, tailY);
this->add(newSeg); //add the segment as a child of the window so it is drawn
body.push_back(newSeg);
return;
}
void Snake::checkCollision()
{
int headX = body.front()->getX();
int headY = body.front()->getY();
int foodX = food->getX();
int foodY = food->getY();
//check for snake eating itself
for(int i = 1; i < body.size(); i++) //start loop at 1 so we exclude the head!
{
if(body.at(i)->getX() == headX && body.at(i)->getY() == headY)
{
//cout << "hit snake" << endl;
this->dead = true;
}
}
if(headX == foodX && headY == foodY)
{ //check for food collision
food->move(this->w(), this->h(), body);
pathfinder->resetPathFlag();
this->addSegment();
cout << "Snake: ate food (size = " << body.size() << ")" << endl;
}
//check for out of bounds
if(headX >= this->w() || headX < 0 || headY >= this->h() || headY < 0)
{
this->dead = true;
}
}
void Snake::move()
{
//~ pathfinder->printGameState(pathfinder->updateGameState());
//get head & food pos
int headX = body.front()->getX();
int headY = body.front()->getY();
int foodX = food->getX();
int foodY = food->getY();
//only run A* if a valid path has not already been found
if (!pathfinder->checkPathFound() || pathfinder->checkRepeatSearch())
{
//divide by 20 to convert values from pixels to grid coords (gross)
if(searchAlgorithm == "AStar")
pathfinder->AStar(headX / 20, headY / 20, foodX / 20, foodY / 20);
else if (searchAlgorithm == "GreedyBFS")
pathfinder->greedyBFS(headX / 20, headY / 20, foodX / 20, foodY / 20);
else if (searchAlgorithm == "BFS")
pathfinder->BFS(headX / 20, headY / 20, foodX / 20, foodY / 20);
else if (searchAlgorithm == "DFS")
pathfinder->DFS(headX / 20, headY / 20, foodX / 20, foodY / 20);
}
if (!buffer.empty())
{ //read direction from the input buffer
this->direction = buffer.back();
buffer.pop_back();
}
Segment* moveMe = body.back(); //select the segment at the end of the vector
body.pop_back();
//move head based on current direction
if(this->direction == "UP")
moveMe->move(headX, headY - 20);
else if(this->direction == "DOWN")
moveMe->move(headX, headY + 20);
else if(this->direction == "LEFT")
moveMe->move(headX - 20, headY);
else
moveMe->move(headX + 20, headY);
//insert the last tail segment as the new head
body.insert(body.begin(), moveMe);
return;
}
Pathfinder* Snake::getPathfinder() { return this->pathfinder; }
int Snake::getSize() { return body.size(); }
int Snake::handle(int event)
{
switch(event)
{
case FL_KEYDOWN:
if(Fl::event_key() == FL_Up)
{ //note that the snake cannot reverse directions (eat itself)
if(this->direction != "DOWN" && buffer.size() < maxBufferSize)
{
buffer.push_back("UP"); //add player input to input buffer
}
return 1;
}
else if(Fl::event_key() == FL_Down)
{
if(this->direction != "UP" && buffer.size() < maxBufferSize)
{
buffer.push_back("DOWN");
}
return 1;
}
else if(Fl::event_key() == FL_Left)
{
if(this->direction != "RIGHT" && buffer.size() < maxBufferSize)
{
buffer.push_back("LEFT");
}
return 1;
}
else if(Fl::event_key() == FL_Right)
{
if(this->direction != "LEFT" && buffer.size() < maxBufferSize)
{
buffer.push_back("RIGHT");
}
return 1;
}
default: return Fl_Widget::handle(event);
}
}