-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
105 lines (80 loc) · 2.67 KB
/
main.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
#include "snake.h"
#include <sstream>
#include <FL/Fl_Button.H>
#include <FL/Fl_Choice.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Int_Input.H>
#include <FL/Fl_Check_Button.H>
using namespace std;
double timeInterval = 0.05;
//GLOBALS
Fl_Double_Window* menuWindow;
Fl_Input* timeStepInput;
Fl_Input* algorithmInput;
Fl_Int_Input* screenWInput;
Fl_Int_Input* screenHInput;
Fl_Choice* algorithmChoice;
Fl_Check_Button* repeatSearchCheck;
//FORWARD DECLARES
void startButtonCallback(Fl_Widget* w, void* v);
int main(int argc, char* argv[])
{
int numSegments = 1;
if (argc > 1)
{
timeInterval = stod(argv[1]);
numSegments = stoi(argv[2]);
}
Snake* game;
menuWindow = new Fl_Double_Window(320, 180, "Menu");
Fl_Button* startButton = new Fl_Button(140, 160, 40, 20, "Start");
startButton->callback(startButtonCallback, game);
repeatSearchCheck = new Fl_Check_Button(20, 120, 40, 20, "Repeat search on each frame (SLOW!)");
timeStepInput = new Fl_Input(180, 20, 120, 20, "Time Step (Seconds)");
algorithmChoice = new Fl_Choice(180, 80, 120, 20, "Search Algorithm");
algorithmChoice->add("AStar");
algorithmChoice->add("GreedyBFS");
algorithmChoice->add("BFS");
algorithmChoice->add("DFS");
algorithmChoice->value(0);
screenWInput = new Fl_Int_Input(180, 40, 120, 20, "Screen Width (px)");
screenHInput = new Fl_Int_Input(180, 60, 120, 20, "Screen Height (px)");
menuWindow->show();
return Fl::run();
}
void gameUpdate(void* game)
{ //Repeated callback, updates the game state (moves snake, checks for collisions)
//at a regular interval
Snake* snake = static_cast<Snake*>(game);
if(!snake->checkDead())
{
snake->move();
snake->checkCollision();
Fl::repeat_timeout(timeInterval, gameUpdate, snake);
}
else
{
cout << "Snake: game over! (size = " << snake->getSize() << ")" << endl;
snake->getPathfinder()->printStats();
}
}
void startButtonCallback(Fl_Widget* w, void* v)
{
Snake* game = (Snake*)v;
menuWindow->hide();
//default values
int screenW = 800;
int screenH = 600;
string algorithm = "AStar";
if (strlen(timeStepInput->value()) != 0)
timeInterval = stod(timeStepInput->value());
if (strlen(screenWInput->value()) != 0)
screenW = stoi(screenWInput->value());
if (strlen(screenHInput->value()) != 0)
screenH = stoi(screenHInput->value());
if (strlen(algorithmChoice->text()) != 0)
algorithm = algorithmChoice->text();
game = new Snake(1, 0, 0, screenW, screenH, algorithm);
game->getPathfinder()->setRepeatSearch(repeatSearchCheck->value());
Fl::add_timeout(timeInterval, gameUpdate, game);
}