aboutsummaryrefslogtreecommitdiff
path: root/src/graphics.cpp
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-03-19 15:22:00 +0100
committerCharles <sircharlesaze@gmail.com>2020-03-19 15:22:00 +0100
commit19ae4e74aedbbfde9aaed9241f616decc3ec9059 (patch)
treec4eca1b207261e24b645d5d861b3b27631ead75e /src/graphics.cpp
parentc435852cc6147a0af23ef12cc0a07a195ac60756 (diff)
download2048-19ae4e74aedbbfde9aaed9241f616decc3ec9059.tar.gz
2048-19ae4e74aedbbfde9aaed9241f616decc3ec9059.tar.bz2
2048-19ae4e74aedbbfde9aaed9241f616decc3ec9059.zip
Interface for ai (tested with random AI)
Diffstat (limited to 'src/graphics.cpp')
-rw-r--r--src/graphics.cpp14
1 files changed, 13 insertions, 1 deletions
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);
}