aboutsummaryrefslogtreecommitdiff
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
parentc435852cc6147a0af23ef12cc0a07a195ac60756 (diff)
download2048-19ae4e74aedbbfde9aaed9241f616decc3ec9059.tar.gz
2048-19ae4e74aedbbfde9aaed9241f616decc3ec9059.tar.bz2
2048-19ae4e74aedbbfde9aaed9241f616decc3ec9059.zip
Interface for ai (tested with random AI)
-rw-r--r--Makefile4
-rw-r--r--README.md12
-rw-r--r--include/ai.hpp17
-rw-r--r--include/graphics.hpp7
-rw-r--r--include/rand_ai.hpp17
-rw-r--r--src/ai.cpp6
-rw-r--r--src/graphics.cpp14
-rw-r--r--src/main.cpp14
-rw-r--r--src/rand_ai.cpp13
9 files changed, 98 insertions, 6 deletions
diff --git a/Makefile b/Makefile
index 5cd4949..681f96e 100644
--- a/Makefile
+++ b/Makefile
@@ -8,7 +8,7 @@ INCDIR = include
OBJDIR = build
CXX = g++
-CXXFLAGS = -Wall -Wextra -I$(INCDIR) $(shell sdl2-config --cflags)
+CXXFLAGS = -Wall -Wextra -I$(INCDIR) $(shell sdl2-config --cflags) -std=c++11
LDFLAGS = $(shell sdl2-config --libs) -lSDL2_ttf
SRC = $(shell find $(SRCDIR) -type f -name "*.cpp")
@@ -18,7 +18,7 @@ OBJ = $(SRC:$(SRCDIR)/%.cpp=$(OBJDIR)/%.o)
all: prebuild $(NAME)
prebuild:
- $(MKDIR) $(OBJDIR)
+ @$(MKDIR) $(OBJDIR)
$(NAME): $(OBJ)
$(CXX) -o $@ $^ $(LDFLAGS)
diff --git a/README.md b/README.md
index 56e5489..cbd136d 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,13 @@
# 2048
+
+## Usage
+
+```
+make all
+./2048
+```
+
+## Dependencies
+
+- [SDL2](libsdl.org)
+- [SDL_ttf](libsdl.org/projects/SDL_ttf)
diff --git a/include/ai.hpp b/include/ai.hpp
new file mode 100644
index 0000000..bdb6764
--- /dev/null
+++ b/include/ai.hpp
@@ -0,0 +1,17 @@
+#ifndef AI_HPP
+# define AI_HPP
+
+#include "2048.hpp"
+#include "game.hpp"
+
+class AI
+{
+ public:
+ // AI(Game *g);
+ virtual Direction move() = 0;
+
+ private:
+ Game *game;
+};
+
+#endif
diff --git a/include/graphics.hpp b/include/graphics.hpp
index ebb596b..6951fc0 100644
--- a/include/graphics.hpp
+++ b/include/graphics.hpp
@@ -11,11 +11,12 @@
#include <SDL2/SDL_ttf.h>
#include "game.hpp"
#include "2048.hpp"
+#include "ai.hpp"
class Graphics
{
public:
- Graphics(Game *g, std::string t, int w, int h);
+ Graphics(Game *g, std::string t, int w, int h, AI *ai = NULL);
~Graphics();
void update();
@@ -34,6 +35,10 @@ class Graphics
SDL_Texture *scoreText;
std::vector< std::pair<int, SDL_Texture*> > numberTexBuf;
std::map< int, SDL_Color > palette;
+ AI *ai;
+ Uint32 aiTimeStep;
+ Uint32 aiNextTime;
+
void drawGame();
void drawCell(int x, int y);
diff --git a/include/rand_ai.hpp b/include/rand_ai.hpp
new file mode 100644
index 0000000..9739dae
--- /dev/null
+++ b/include/rand_ai.hpp
@@ -0,0 +1,17 @@
+#ifndef RAND_AI_HPP
+# define RAND_AI_HPP
+
+#include "ai.hpp"
+#include "game.hpp"
+
+class RandAI : public AI
+{
+ public:
+ RandAI(Game *g);
+ Direction move();
+
+ private:
+ Game *game;
+};
+
+#endif
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];
+}