-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
149 lines (138 loc) · 3.5 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
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
//
// main.cpp for Nibbler in /home/lks/rendu/cpp_nibbler/auzou_t
//
// Made by Thibaud Auzou
// Login <auzou_t@epitech.net>
//
// Started on Tue Mar 24 18:24:58 2015 Thibaud Auzou
// Last update Sun Apr 5 17:45:07 2015 Thibaud Auzou
//
#include "Nibbler.hpp"
#include "Board.hpp"
#include "Snake.hpp"
#include "DLLoader.hpp"
#include "Error.hpp"
int main(int argc, char **argv)
{
std::stringstream av1;
std::stringstream av2;
unsigned int sizeX;
unsigned int sizeY;
if (argc != 4 || argv == NULL)
return (printError("Usage : ./nibbler [width] [height] [lib]"));
av1 << argv[1];
av2 << argv[2];
av1 >> sizeX;
av2 >> sizeY;
if (checkArg(sizeX, sizeY) == ERROR)
return (ERROR);
try
{
playNibbler(sizeX, sizeY, argv[3]);
}
catch (Error const &e)
{
std::cerr << "EXCEPTION IN " << e.who() << " [" << e.what() << "]" << std::endl;
return (ERROR);
}
catch (std::exception const &e)
{
std::cerr << "EXCEPTION STD [" << e.what() << "]" << std::endl;
return (ERROR);
}
return (RAS);
}
int checkArg(unsigned int sizeX, unsigned int sizeY)
{
if (sizeX > MAX_WIDTH || sizeX < MIN_WIDTH)
{
std::cerr << "The Width Has To Be Between " << MIN_WIDTH
<< " and " << MAX_WIDTH << std::endl;
return (ERROR);
}
if (sizeY > MAX_HEIGHT || sizeY < MIN_HEIGHT)
{
std::cerr << "The Height Has To Be Between " << MIN_HEIGHT
<< " and " << MAX_HEIGHT << std::endl;
return (ERROR);
}
return (RAS);
}
static int swapLib(int action,
DLLoader *loader,
IDisplayPlugin **lib,
unsigned int sizeX,
unsigned int sizeY)
{
if (action == 1)
{
(*lib)->stop();
*lib = loader->getNextInstance();
if (*lib == NULL)
return (0);
(*lib)->init(sizeX, sizeY);
}
else if (action == -1)
{
(*lib)->stop();
*lib = loader->getPrevInstance();
if (*lib == NULL)
return (0);
(*lib)->init(sizeX, sizeY);
}
return (*lib != NULL);
}
static void my_usleep(int msec)
{
std::clock_t goal = msec + std::clock();
while (goal > std::clock());
}
int playNibbler(unsigned int sizeX,
unsigned int sizeY,
std::string const &lib_name)
{
Board board(sizeX, sizeY);
Snake snake(&board);
Snake::Event handler(&snake);
IDisplayPlugin *lib;
DLLoader loader;
GameData data;
if ((lib = loader.getInstance(lib_name)) == NULL)
return (ERROR);
lib->init(sizeX, sizeY);
snake.placeSnake();
while (snake.isAlive() && !snake.hasWon())
{
lib->display(board.getBoard(), getGameData(data, snake));
if (!swapLib(handler.handleEvent(lib->waitEvent()), &loader, &lib, sizeX, sizeY))
return (ERROR);
board.placeFood();
if (!snake.getPause())
snake.moveSnake();
my_usleep((snake.getSpeed() + 1) * SPEED_COEF);
}
if (board.getScore() > board.getBestScore())
saveNewScore(board.getScore(), lib);
lib->stop();
return (RAS);
}
int saveNewScore(unsigned int score, IDisplayPlugin *lib)
{
char buff[512];
std::ofstream file(".score", std::ofstream::trunc);
if (!file.fail() && !file.bad())
{
lib->getNewScoreName(buff);
file << buff << ";" << score << std::endl;
}
return (RAS);
}
GameData const &getGameData(GameData &data, Snake const &snake)
{
data._score = snake.getBoard()->getScore();
data._speed = snake.getSpeed();
data._bestScoreName = snake.getBoard()->getBestScoreName();
data._bestScore = snake.getBoard()->getBestScore();
data._pause = snake.getPause();
return (data);
}