diff --git a/2048.c b/2048.c index c39b31c..8be82a9 100644 --- a/2048.c +++ b/2048.c @@ -39,34 +39,74 @@ void game_destroy(Game game) { } uint8_t game_move(Game *game, Direction direction) { - // setting up stuff for my funny two-way for loop - // well technically speaking about 4-way because there - // are two 2-way for loops - /* - int8_t y_dir = 0; - int8_t x_dir = 0; - size_t start_y = 0; - size_t start_x = 0; - switch(direction) { - case NoDirection: - return 0; - case Up: - y_dir = -1; - start_y = game->field_size - 1; - break; - case Down: - y_dir = 1; - break; - case Left: - x_dir = -1; - start_x = game->field_size - 1; - break; - case Right: - x_dir = 1; - break; - } - */ + // just one direction for testing purposes + for (int i = 0; i < game->field_size_y; i++) { + uint16_t **field = game->field; + // move everything on one side + for (int j = 0; j < game->field_size_x - 1; j++) { + // a, b is the same as i and j. It is just flipped when + // it does not go horizontally so I can access stuff vertically + int a = i; + int b = j; + if (direction == Up || direction == Down) { + a = j; + b = i; + } + + // make it so it can work with different directions + int directionality = b; // TODO: rename ugly name + int movement = 1; + if (direction == Left || direction == Up) { + directionality = game->field_size_x - b - 1; + movement = -1; + } + if(field[a][directionality + movement] == 0) { + field[a][directionality+movement] = field[a][directionality]; + field[a][directionality] = 0; + } + } + // combine same numbers + for (int j = 0; j < game->field_size_x - 1; j++) { + int a = i; + int b = j; + if (direction == Up || direction == Down) { + a = j; + b = i; + } + int directionality = b; + int movement = 1; + if (direction == Left) { + directionality = game->field_size_x - b - 1; + movement = -1; + } + if(field[a][directionality + movement] == field[a][directionality]) { + field[a][directionality+movement] = field[a][directionality]*2; + field[a][directionality] = 0; + j++; + } + } + + // move everything again + for (int j = 0; j < game->field_size_x - 1; j++) { + int a = i; + int b = j; + if (direction == Up || direction == Down) { + a = j; + b = i; + } + int directionality = b; + int movement = 1; + if (direction == Left) { + directionality = game->field_size_x - b - 1; + movement = -1; + } + if(field[a][directionality + movement] == 0) { + field[a][directionality+movement] = field[a][directionality]; + field[a][directionality] = 0; + } + } + } game_add_block(*game); return 0; diff --git a/main.c b/main.c index e4c50a2..ccd3fed 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,3 @@ -#include "SDL.h" #include "tui.h" #include "2048.h"