diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/ai.cpp | 6 | ||||
| -rw-r--r-- | src/graphics.cpp | 14 | ||||
| -rw-r--r-- | src/main.cpp | 14 | ||||
| -rw-r--r-- | src/rand_ai.cpp | 13 |
4 files changed, 44 insertions, 3 deletions
diff --git a/src/ai.cpp b/src/ai.cpp new file mode 100644 index 0000000..ca3ec57 --- /dev/null +++ b/src/ai.cpp @@ -0,0 +1,6 @@ +#include "ai.hpp" + +// AI::AI(Game *g) +// { +// game = g; +// } diff --git a/src/graphics.cpp b/src/graphics.cpp index 4985b5a..7732bd2 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -2,8 +2,9 @@ #define CELL_GAP 5 #define GRID_BORDER 10 +#define AI_TIME_STEP 100 -Graphics::Graphics(Game *g, std::string t, int w, int h) +Graphics::Graphics(Game *g, std::string t, int w, int h, AI *a) { running = true; game = g; @@ -11,6 +12,9 @@ Graphics::Graphics(Game *g, std::string t, int w, int h) width = w; height = h; gridSize = std::min(width, height); + ai = a; + aiTimeStep = AI_TIME_STEP; + aiNextTime = SDL_GetTicks(); if (SDL_Init(SDL_INIT_VIDEO) < 0) error(); @@ -58,6 +62,14 @@ void Graphics::update() drawGame(); drawScore(); SDL_RenderPresent(renderer); + if (ai != NULL) + { + if (SDL_TICKS_PASSED(SDL_GetTicks(), aiNextTime)) + { + aiNextTime = SDL_GetTicks() + aiTimeStep; + game->move(ai->move()); + } + } SDL_Delay(3); } diff --git a/src/main.cpp b/src/main.cpp index 7d4d0fc..a6da033 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,17 +2,27 @@ #include <cstdlib> #include "graphics.hpp" #include "game.hpp" +#include "ai.hpp" +#include "rand_ai.hpp" #define WINDOW_TITLE "2048" #define WINDOW_WIDTH 640 #define WINDOW_HEIGHT 480 #define GAME_GRID_SIZE 6 -int main() +int main(int argc, char **argv) { + Graphics *graphics; + srand(time(NULL)); Game game = Game(GAME_GRID_SIZE); - Graphics *graphics = new Graphics(&game, WINDOW_TITLE, WINDOW_WIDTH, WINDOW_HEIGHT); + if (argc == 2 && strcmp(argv[1], "--ai") == 0) + { + RandAI ai = RandAI(&game); + graphics = new Graphics(&game, WINDOW_TITLE, WINDOW_WIDTH, WINDOW_HEIGHT, &ai); + } + else + graphics = new Graphics(&game, WINDOW_TITLE, WINDOW_WIDTH, WINDOW_HEIGHT); while (graphics->isRunning()) graphics->update(); delete graphics; diff --git a/src/rand_ai.cpp b/src/rand_ai.cpp new file mode 100644 index 0000000..db2cf4f --- /dev/null +++ b/src/rand_ai.cpp @@ -0,0 +1,13 @@ +#include "rand_ai.hpp" + +RandAI::RandAI(Game *g) +{ + game = g; +} + +Direction RandAI::move() +{ + Direction directions[4] = {DIRECTION_LEFT, DIRECTION_RIGHT, + DIRECTION_DOWN, DIRECTION_UP}; + return directions[rand() % 4]; +} |
