fixed to more readable state

This commit is contained in:
Rorikstr | Rust Dev 2025-09-26 14:11:45 +03:00
parent f8b74bf43d
commit 851d303aab
4 changed files with 187 additions and 37 deletions

View file

@ -10,40 +10,192 @@ static bool initialized = false;
const int (*get_figure_shape(FigureType type, int rotation))[4] { const int (*get_figure_shape(FigureType type, int rotation))[4] {
static const int shapes[FIGURE_COUNT][4][4][4] = { static const int shapes[FIGURE_COUNT][4][4][4] = {
// I // I
{{{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}, {0, 0, 1, 0}}, {
{{0, 0, 0, 0}, {0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}}, {0, 0, 0, 0},
{{0, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 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},
{0, 0, 1, 0}
},
{
{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},
{0, 1, 0, 0}
}
},
// O // O
{{{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}, {0, 0, 0, 0}}, {
{{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, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 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},
{0, 0, 0, 0}
},
{
{0, 1, 1,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0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, {1, 1, 0, 0},
{{0, 0, 1, 0}, {0, 1, 1, 0}, {0, 1, 0, 0}, {0, 0, 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}
},
{
{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 shapes[type][rotation]; return shapes[type][rotation];
} }
@ -53,7 +205,7 @@ void user_input(UserAction_t action) {
initialized = true; initialized = true;
} }
if (action >= Figure1 && action <= Figure7) { if ( Figure1 <= action && action <= Figure7) {
FigureType type = (FigureType)(action - Figure1); FigureType type = (FigureType)(action - Figure1);
game_state.current_figure.type = type; game_state.current_figure.type = type;
game_state.current_figure.x = FIELD_WIDTH / 2 - 2; game_state.current_figure.x = FIELD_WIDTH / 2 - 2;

View file

@ -57,7 +57,7 @@ typedef enum {
// Основные функции библиотеки // Основные функции библиотеки
void user_input(UserAction_t action); void user_input(UserAction_t action);
GameStateData* getGameState(void); GameStateData* get_game_state(void);
const int (*get_figure_shape(FigureType type, int rotation))[4]; const int (*get_figure_shape(FigureType type, int rotation))[4];

View file

@ -5,7 +5,7 @@
void display_game() { void display_game() {
clear(); clear();
GameStateData* state = getGameState(); GameStateData* state = get_game_state();
// Очистка поля // Очистка поля
for (int i = 0; i < FIELD_HEIGHT; i++) { for (int i = 0; i < FIELD_HEIGHT; i++) {

View file

@ -18,10 +18,10 @@ int main() {
int ch; int ch;
bool hold = false; bool hold = false;
while (1) {
ch = getch();
UserAction_t action = Undefined; UserAction_t action = Undefined;
while (!Terminate) {
ch = getch();
switch (ch) { switch (ch) {
case 'q': action = Terminate; break; case 'q': action = Terminate; break;
@ -43,8 +43,6 @@ int main() {
} }
display_game(); display_game();
if (action == Terminate) break;
} }
endwin(); endwin();