aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/2048.hpp12
-rw-r--r--include/game.hpp6
-rw-r--r--include/graphics.hpp1
-rw-r--r--src/game.cpp73
-rw-r--r--src/graphics.cpp19
-rw-r--r--src/main.cpp2
6 files changed, 113 insertions, 0 deletions
diff --git a/include/2048.hpp b/include/2048.hpp
new file mode 100644
index 0000000..fff7a80
--- /dev/null
+++ b/include/2048.hpp
@@ -0,0 +1,12 @@
+#ifndef H2048_HPP
+# define H2048_HPP
+
+typedef enum
+{
+ DIRECTION_LEFT,
+ DIRECTION_RIGHT,
+ DIRECTION_DOWN,
+ DIRECTION_UP
+} Direction;
+
+#endif
diff --git a/include/game.hpp b/include/game.hpp
index a644799..488f74b 100644
--- a/include/game.hpp
+++ b/include/game.hpp
@@ -1,6 +1,9 @@
#ifndef GAME_HPP
# define GAME_HPP
+#include <cstdlib>
+#include "2048.hpp"
+
class Game
{
public:
@@ -8,6 +11,9 @@ class Game
~Game();
int getSize();
int at(int x, int y);
+ void move(Direction direction);
+ void spawn();
+ bool lost();
private:
int size;
diff --git a/include/graphics.hpp b/include/graphics.hpp
index 0990247..44f631c 100644
--- a/include/graphics.hpp
+++ b/include/graphics.hpp
@@ -6,6 +6,7 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include "game.hpp"
+#include "2048.hpp"
class Graphics
{
diff --git a/src/game.cpp b/src/game.cpp
index 11a603b..5bd3763 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -25,3 +25,76 @@ int Game::at(int x, int y)
{
return grid[y][x];
}
+
+void Game::move(Direction direction)
+{
+ switch (direction)
+ {
+ case DIRECTION_LEFT:
+ break;
+ case DIRECTION_RIGHT:
+ // for (int i = 0; i < size; i++)
+ // for (int j = size - 2; j >= 0; j--)
+ // {
+ // if (grid[i][j + 1] == grid[i][j])
+ // {
+ // grid[i][j + 1] *= 2;
+ // for (int k = j; k >= 0; k--)
+ // grid[i][k] = grid[i][k - 1];
+ // grid[i][shifts] = 0;
+ // shifts++;
+ // }
+ // }
+ break;
+ case DIRECTION_DOWN:
+ break;
+ case DIRECTION_UP:
+ // for (int i = size - 2; i >= 0; i--)
+ // for (int j = 0; j < size; j++)
+ // {
+ // if (grid[i + 1][j] == grid[i][j])
+ // {
+ // grid[i + 1][j 1] *= 2;
+ // for (int k = j; k >= 0; k--)
+ // grid[i][k] = grid[i][k - 1];
+ // grid[i][shifts] = 0;
+ // shifts++;
+ // }
+ // }
+ break;
+ }
+ spawn();
+}
+
+void Game::spawn()
+{
+ int i;
+ int j;
+
+ do
+ {
+ i = rand() % size;
+ j = rand() % size;
+ }
+ while (grid[i][j] != 0);
+ grid[i][j] = 2;
+}
+
+bool Game::lost()
+{
+ for (int i = 0; i < size; i++)
+ for (int j = 0; j < size; j++)
+ {
+ if (grid[i][j] == 0)
+ return false;
+ if (i - 1 >= 0 && grid[i - 1][j] == grid[i][j])
+ return false;
+ if (i + 1 < size && grid[i + 1][j] == grid[i][j])
+ return false;
+ if (j - 1 >= 0 && grid[i][j - 1] == grid[i][j])
+ return false;
+ if (j + 1 < size && grid[i][j + 1] == grid[i][j])
+ return false;
+ }
+ return true;
+}
diff --git a/src/graphics.cpp b/src/graphics.cpp
index 3fca69b..12a2607 100644
--- a/src/graphics.cpp
+++ b/src/graphics.cpp
@@ -32,6 +32,8 @@ Graphics::~Graphics()
void Graphics::update()
{
+ if (game->lost())
+ running = false;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
handleEvent();
@@ -91,6 +93,23 @@ void Graphics::handleEvent()
{
case SDL_QUIT:
running = false;
+ break;
+ case SDL_KEYDOWN:
+ switch (e.key.keysym.sym)
+ {
+ case SDLK_LEFT:
+ game->move(DIRECTION_LEFT);
+ break;
+ case SDLK_RIGHT:
+ game->move(DIRECTION_RIGHT);
+ break;
+ case SDLK_DOWN:
+ game->move(DIRECTION_DOWN);
+ break;
+ case SDLK_UP:
+ game->move(DIRECTION_UP);
+ break;
+ }
}
}
diff --git a/src/main.cpp b/src/main.cpp
index 7fe08bf..b0c98a0 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,4 +1,5 @@
#include <iostream>
+#include <cstdlib>
#include "graphics.hpp"
#include "game.hpp"
@@ -9,6 +10,7 @@
int main()
{
+ srand(time(NULL));
Game game = Game(GAME_GRID_SIZE);
Graphics *graphics = new Graphics(&game, WINDOW_TITLE, WINDOW_WIDTH, WINDOW_HEIGHT);