diff --git a/src/brick_game/tetris/tetris.c b/src/brick_game/tetris/tetris.c index fe6451e..7d5aae2 100644 --- a/src/brick_game/tetris/tetris.c +++ b/src/brick_game/tetris/tetris.c @@ -7,196 +7,320 @@ static GameStateData game_state = {0}; static bool initialized = false; -const int (*get_figure_shape(FigureType type, int rotation))[4] { - static const int shapes[FIGURE_COUNT + 1][4][4][4] = { - // I - { - { - {0, 0, 0, 0}, - {1, 1, 1, 1}, - {0, 0, 0, 0}, +// I +const int (*i_fig_up())[4] { + static const int shape[4][4] = { + {0, 0, 0, 0}, + {1, 1, 1, 1}, + {0, 0, 0, 0}, {0, 0, 0, 0} - }, - { - {0, 0, 1, 0}, - {0, 0, 1, 0}, - {0, 0, 1, 0}, + }; + return (const int (*)[4])shape; +} + +const int (*i_fig_right())[4] { + static const int shape[4][4] = { + {0, 0, 1, 0}, + {0, 0, 1, 0}, + {0, 0, 1, 0}, {0, 0, 1, 0} - }, - { - {0, 0, 0, 0}, - {0, 0, 0, 0}, - {1, 1, 1, 1}, + }; + return (const int (*)[4])shape; +} + +const int (*i_fig_down())[4] { + static const int shape[4][4] = { + {0, 0, 0, 0}, + {0, 0, 0, 0}, + {1, 1, 1, 1}, {0, 0, 0, 0} - }, - { - {0, 1, 0, 0}, - {0, 1, 0, 0}, - {0, 1, 0, 0}, + }; + return (const int (*)[4])shape; +} + +const int (*i_fig_left())[4] { + static const int shape[4][4] = { + {0, 1, 0, 0}, + {0, 1, 0, 0}, + {0, 1, 0, 0}, {0, 1, 0, 0} - } - }, - // O - { - { - {0, 1, 1, 0}, - {0, 1, 1, 0}, - {0, 0, 0, 0}, + }; + return (const int (*)[4])shape; +} + +// O +const int (*o_fig())[4] { + static const int shape[4][4] = { + {0, 1, 1, 0}, + {0, 1, 1, 0}, + {0, 0, 0, 0}, {0, 0, 0, 0} - }, - { - {0, 1, 1, 0}, - {0, 1, 1, 0}, - {0, 0, 0, 0}, + }; + return (const int (*)[4])shape; +} + +// T +const int (*t_fig_up())[4] { + static const int shape[4][4] = { + {0, 1, 0, 0}, + {1, 1, 1, 0}, + {0, 0, 0, 0}, {0, 0, 0, 0} - }, - { - {0, 1, 1, 0}, - {0, 1, 1, 0}, - {0, 0, 0, 0}, + }; + return (const int (*)[4])shape; +} + +const int (*t_fig_right())[4] { + static const int shape[4][4] = { + {0, 1, 0, 0}, + {0, 1, 1, 0}, + {0, 1, 0, 0}, {0, 0, 0, 0} - }, - { - {0, 1, 1, 0}, - {0, 1, 1, 0}, - {0, 0, 0, 0}, + }; + return (const int (*)[4])shape; +} + +const int (*t_fig_down())[4] { + static const int shape[4][4] = { + {0, 0, 0, 0}, + {1, 1, 1, 0}, + {0, 1, 0, 0}, {0, 0, 0, 0} - } - }, - // T - { - { - {0, 1, 0, 0}, - {1, 1, 1, 0}, - {0, 0, 0, 0}, + }; + return (const int (*)[4])shape; +} + +const int (*t_fig_left())[4] { + static const int shape[4][4] = { + {0, 1, 0, 0}, + {1, 1, 0, 0}, + {0, 1, 0, 0}, {0, 0, 0, 0} - }, - { - {0, 1, 0, 0}, - {0, 1, 1, 0}, - {0, 1, 0, 0}, + }; + return (const int (*)[4])shape; +} + +// L +const int (*l_fig_up())[4] { + static const int shape[4][4] = { + {0, 0, 1, 0}, + {1, 1, 1, 0}, + {0, 0, 0, 0}, {0, 0, 0, 0} - }, - { - {0, 0, 0, 0}, - {1, 1, 1, 0}, - {0, 1, 0, 0}, + }; + return (const int (*)[4])shape; +} + +const int (*l_fig_right())[4] { + static const int shape[4][4] = { + {1, 0, 0, 0}, + {1, 0, 0, 0}, + {1, 1, 0, 0}, {0, 0, 0, 0} - }, - { - {0, 1, 0, 0}, - {1, 1, 0, 0}, - {0, 1, 0, 0}, + }; + return (const int (*)[4])shape; +} + +const int (*l_fig_down())[4] { + static const int shape[4][4] = { + {0, 0, 0, 0}, + {1, 1, 1, 0}, + {1, 0, 0, 0}, {0, 0, 0, 0} - } - }, - // L - { - { - {0, 0, 1, 0}, - {1, 1, 1, 0}, - {0, 0, 0, 0}, + }; + return (const int (*)[4])shape; +} + +const int (*l_fig_left())[4] { + static const int shape[4][4] = { + {1, 1, 0, 0}, + {0, 1, 0, 0}, + {0, 1, 0, 0}, {0, 0, 0, 0} - }, - { - {1, 0, 0, 0}, - {1, 0, 0, 0}, - {1, 1, 0, 0}, + }; + return (const int (*)[4])shape; +} + +// J +const int (*j_fig_up())[4] { + static const int shape[4][4] = { + {1, 0, 0, 0}, + {1, 1, 1, 0}, + {0, 0, 0, 0}, {0, 0, 0, 0} - }, - { - {0, 0, 0, 0}, - {1, 1, 1, 0}, - {1, 0, 0, 0}, + }; + return (const int (*)[4])shape; +} + +const int (*j_fig_right())[4] { + static const int shape[4][4] = { + {0, 1, 1, 0}, + {0, 1, 0, 0}, + {0, 1, 0, 0}, {0, 0, 0, 0} - }, - { - {1, 1, 0, 0}, - {0, 1, 0, 0}, - {0, 1, 0, 0}, + }; + return (const int (*)[4])shape; +} + +const int (*j_fig_down())[4] { + static const int shape[4][4] = { + {0, 0, 0, 0}, + {1, 1, 1, 0}, + {0, 0, 1, 0}, {0, 0, 0, 0} - }}, - // J - { - { - {1, 0, 0, 0}, - {1, 1, 1, 0}, - {0, 0, 0, 0}, + }; + return (const int (*)[4])shape; +} + +const int (*j_fig_left())[4] { + static const int shape[4][4] = { + {0, 1, 0, 0}, + {0, 1, 0, 0}, + {1, 1, 0, 0}, {0, 0, 0, 0} - }, - { - {0, 1, 1, 0}, - {0, 1, 0, 0}, - {0, 1, 0, 0}, + }; + return (const int (*)[4])shape; +} + +// S +const int (*s_fig_up())[4] { + static const int shape[4][4] = { + {0, 1, 1, 0}, + {1, 1, 0, 0}, + {0, 0, 0, 0}, {0, 0, 0, 0} - }, - { - {0, 0, 0, 0}, - {1, 1, 1, 0}, - {0, 0, 1, 0}, + }; + return (const int (*)[4])shape; +} + +const int (*s_fig_right())[4] { + static const int shape[4][4] = { + {0, 1, 0, 0}, + {0, 1, 1, 0}, + {0, 0, 1, 0}, {0, 0, 0, 0} - }, - { - {0, 1, 0, 0}, - {0, 1, 0, 0}, - {1, 1, 0, 0}, + }; + return (const int (*)[4])shape; +} + +const int (*s_fig_down())[4] { + static const int shape[4][4] = { + {0, 1, 1, 0}, + {1, 1, 0, 0}, + {0, 0, 0, 0}, {0, 0, 0, 0} - }}, - // S - { - { - {0, 1, 1, 0}, - {1, 1, 0, 0}, - {0, 0, 0, 0}, + }; + return (const int (*)[4])shape; +} + +const int (*s_fig_left())[4] { + static const int shape[4][4] = { + {0, 1, 0, 0}, + {0, 1, 1, 0}, + {0, 0, 1, 0}, {0, 0, 0, 0} - }, - { - {0, 1, 0, 0}, - {0, 1, 1, 0}, - {0, 0, 1, 0}, + }; + return (const int (*)[4])shape; +} + +// Z +const int (*z_fig_up())[4] { + static const int shape[4][4] = { + {1, 1, 0, 0}, + {0, 1, 1, 0}, + {0, 0, 0, 0}, {0, 0, 0, 0} - }, - { - {0, 1, 1, 0}, - {1, 1, 0, 0}, - {0, 0, 0, 0}, + }; + return (const int (*)[4])shape; +} + +const int (*z_fig_right())[4] { + static const int shape[4][4] = { + {0, 0, 1, 0}, + {0, 1, 1, 0}, + {0, 1, 0, 0}, {0, 0, 0, 0} - }, - { - {0, 1, 0, 0}, - {0, 1, 1, 0}, - {0, 0, 1, 0}, + }; + return (const int (*)[4])shape; +} + +const int (*z_fig_down())[4] { + static const int shape[4][4] = { + {1, 1, 0, 0}, + {0, 1, 1, 0}, + {0, 0, 0, 0}, {0, 0, 0, 0} - }}, - // Z - { - { - {1, 1, 0, 0}, - {0, 1, 1, 0}, - {0, 0, 0, 0}, + }; + return (const int (*)[4])shape; +} + +const int (*z_fig_left())[4] { + static const int shape[4][4] = { + {0, 0, 1, 0}, + {0, 1, 1, 0}, + {0, 1, 0, 0}, {0, 0, 0, 0} - }, - { - {0, 0, 1, 0}, - {0, 1, 1, 0}, - {0, 1, 0, 0}, - {0, 0, 0, 0} - }, - { - {1, 1, 0, 0}, - {0, 1, 1, 0}, - {0, 0, 0, 0}, - {0, 0, 0, 0} - }, - { - {0, 0, 1, 0}, - {0, 1, 1, 0}, - {0, 1, 0, 0}, - {0, 0, 0, 0} - } + }; + return (const int (*)[4])shape; +} + +const int (*get_figure_shape(FigureType type, int rotation))[4] { + const int (*result)[4] = NULL; + switch (type) { + case I: + switch (rotation % 4) { + case 0: result = i_fig_up(); break; + case 1: result = i_fig_right(); break; + case 2: result = i_fig_down(); break; + case 3: result = i_fig_left(); break; + } + break; + case O: + result = o_fig(); + break; + case T: + switch (rotation % 4) { + case 0: result = t_fig_up(); break; + case 1: result = t_fig_right(); break; + case 2: result = t_fig_down(); break; + case 3: result = t_fig_left(); break; + } + break; + case L: + switch (rotation % 4) { + case 0: result = l_fig_up(); break; + case 1: result = l_fig_right(); break; + case 2: result = l_fig_down(); break; + case 3: result = l_fig_left(); break; + } + break; + case J: + switch (rotation % 4) { + case 0: result = j_fig_up(); break; + case 1: result = j_fig_right(); break; + case 2: result = j_fig_down(); break; + case 3: result = j_fig_left(); break; + } + break; + case S: + switch (rotation % 4) { + case 0: result = s_fig_up(); break; + case 1: result = s_fig_right(); break; + case 2: result = s_fig_down(); break; + case 3: result = s_fig_left(); break; + } + break; + case Z: + switch (rotation % 4) { + case 0: result = z_fig_up(); break; + case 1: result = z_fig_right(); break; + case 2: result = z_fig_down(); break; + case 3: result = z_fig_left(); break; + } + break; + default: result = NULL; } - }; - return shapes[type][rotation]; + return result; } void user_input(UserAction_t action) { @@ -205,7 +329,7 @@ void user_input(UserAction_t action) { initialized = true; } - if ( Figure1 <= action && action <= Figure7) { + if (Figure1 <= action && action <= Figure7) { FigureType type = (FigureType)(action - Figure1); game_state.current_figure.type = type; game_state.current_figure.x = FIELD_WIDTH / 2 - 2; @@ -219,6 +343,9 @@ void user_input(UserAction_t action) { if (action == Right) game_state.current_figure.x++; if (action == Down) game_state.current_figure.y++; if (action == Up) game_state.current_figure.y--; + if (action == Rotate) { + game_state.current_figure.rotation = (game_state.current_figure.rotation + 1) % 4; + } } } diff --git a/src/brick_game/tetris/tetris.h b/src/brick_game/tetris/tetris.h index c8833e5..925ef2d 100644 --- a/src/brick_game/tetris/tetris.h +++ b/src/brick_game/tetris/tetris.h @@ -45,6 +45,7 @@ typedef enum { Right, Up, Down, + Rotate, Action, Figure1, Figure2, diff --git a/src/gui/cli/main.c b/src/gui/cli/main.c index 8bc3402..0a1a35c 100644 --- a/src/gui/cli/main.c +++ b/src/gui/cli/main.c @@ -18,9 +18,8 @@ int main() { int ch; UserAction_t action = Undefined; - while (action != Terminate) { + while (Terminate != action) { ch = getch(); - switch (ch) { case 'q': action = Terminate; break; @@ -31,10 +30,13 @@ int main() { case '5': action = Figure5; break; case '6': action = Figure6; break; case '7': action = Figure7; break; + case 'r': action = Rotate; break; + case ' ': action = Rotate; break; case KEY_LEFT: action = Left; break; case KEY_RIGHT: action = Right; break; case KEY_DOWN: action = Down; break; case KEY_UP: action = Up; break; + default: action = Undefined; } if (action != Undefined) {