removed unnecessary

This commit is contained in:
Rorikstr | Rust Dev 2025-09-29 23:19:46 +03:00
parent a0cec98aa7
commit 5ed3450d6f
10 changed files with 17 additions and 119 deletions

View file

@ -1,5 +1,4 @@
#include "01_automato.h"
#include "../../logging.h"
void userInput(UserAction_t action, bool hold) {
(void)hold; // заглушка
@ -14,12 +13,10 @@ void userInput(UserAction_t action, bool hold) {
switch (action) {
case Start:
// Загружаем рекорд при старте игры
state->info->high_score = load_high_score();
state->state = Init;
break;
case Terminate:
// Сохраняем рекорд при выходе, если текущий рекорд побит
if (state->info->score > state->info->high_score) {
state->info->high_score = state->info->score;
save_high_score(state->info->high_score);
@ -51,13 +48,10 @@ void userInput(UserAction_t action, bool hold) {
}
GameInfo_t updateCurrentState() {
LOG_FUNCTION_START("updateCurrentState", "");
GameState_t* state = get_game_state();
state->frame_count++;
// Обновляем логику игры только если игра не на паузе (кроме GameOver)
if (!state->info->pause || state->state == GameOver) {
switch (state->state) {
case Init:
@ -81,14 +75,12 @@ GameInfo_t updateCurrentState() {
}
}
// Подготовка данных для отображения
for (int i = 0; i < FIELD_HEIGHT; i++) {
for (int j = 0; j < FIELD_WIDTH; j++) {
state->info->field[i][j] = state->field[i][j];
}
}
// Накладываем активную фигуру на поле
Figure_t* fig = &state->curr;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
@ -102,15 +94,11 @@ GameInfo_t updateCurrentState() {
}
}
// Копируем next
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
state->info->next[i][j] = state->next.mtrx[i][j];
}
}
LOG_FUNCTION_END("updateCurrentState", "score=%d, level=%d, state=%d",
state->info->score, state->info->level, state->state);
return *state->info;
}

View file

@ -1,13 +1,12 @@
#include "01_automato.h"
#include <stdlib.h>
#include "../../logging.h"
int load_high_score() {
FILE* file = fopen("high_score.txt", "r");
int high_score = 0;
if (file) {
if (fscanf(file, "%d", &high_score) != 1) {
high_score = 0; // Если не удалось прочитать, устанавливаем 0
high_score = 0;
}
fclose(file);
}
@ -27,29 +26,23 @@ GameState_t* get_game_state(void) {
static int initialized = 0;
if (!initialized) {
// Выделяем память для GameInfo_t
state.info = malloc(sizeof(GameInfo_t));
// Выделяем память для field
state.info->field = malloc(FIELD_HEIGHT * sizeof(int*));
for (int i = 0; i < FIELD_HEIGHT; i++) {
state.info->field[i] = malloc(FIELD_WIDTH * sizeof(int));
}
// Выделяем память для next
state.info->next = malloc(4 * sizeof(int*));
for (int i = 0; i < 4; i++) {
state.info->next[i] = malloc(4 * sizeof(int));
}
// Инициализируем начальные значения
state.info->speed = 10;
state.info->score = 0;
state.info->level = 1;
state.info->pause = 0;
state.frame_count = 0;
state.last_move_frame = 0;
state.info->high_score = load_high_score(); // Загружаем рекорд
state.info->high_score = load_high_score();
// Инициализируем следующую фигуру
state.next.sprite = rand() % FIGURE_COUNT;
@ -67,8 +60,6 @@ GameState_t* get_game_state(void) {
}
void terminate_and_free() {
LOG_FUNCTION_START("terminate_and_free", "");
GameState_t* state = get_game_state();
if (state->info) {
@ -97,6 +88,4 @@ void terminate_and_free() {
free(state->info);
state->info = NULL;
}
LOG_FUNCTION_END("terminate_and_free", "");
}

View file

@ -1,11 +1,7 @@
#include "01_automato.h"
#include "../../logging.h"
void do_init(void) {
LOG_FUNCTION_START("do_init", "");
GameState_t* state = get_game_state();
// Очистка поля
for (int i = 0; i < FIELD_HEIGHT; ++i)
for (int j = 0; j < FIELD_WIDTH; ++j)
state->field[i][j] = 0;
@ -14,7 +10,4 @@ void do_init(void) {
state->info->level = 1;
state->info->speed = 10;
state->state = Spawn;
LOG_FUNCTION_END("do_init", "score=%d, level=%d, state=%d",
state->info->score, state->info->level, state->state);
}

View file

@ -1,17 +1,14 @@
#include "01_automato.h"
#include <stdlib.h>
#include "../../logging.h"
void do_spawn(void) {
GameState_t* state = get_game_state();
// Устанавливаем текущую фигуру из следующей (или генерируем первую)
state->curr = state->next;
state->curr.x = FIELD_WIDTH / 2 - 2;
state->curr.y = 0;
state->moving_type = DoNothing;
// Генерим новую следующую фигуру
state->next.sprite = rand() % FIGURE_COUNT;
state->next.rotation = 0;
const int (*shape)[4] = get_figure_shape(state->next.sprite, 0);
@ -19,7 +16,6 @@ void do_spawn(void) {
for (int j = 0; j < 4; ++j)
state->next.mtrx[i][j] = shape[i][j];
// Проверка на GameOver
if (check_collision()) {
state->state = GameOver;
return;

View file

@ -1,7 +1,6 @@
// brick_game/tetris/06_move.c
#include <time.h>
#include "01_automato.h"
#include "../../logging.h"
long long get_time_ms() {
return (long long)time(NULL) * 1000;
@ -9,29 +8,18 @@ long long get_time_ms() {
void do_move(void) {
GameState_t* state = get_game_state();
LOG_FUNCTION_START("do_move", "speed=%d, moving_type=%d, current_pos=(%d,%d)",
state->info->speed, state->moving_type, state->curr.x, state->curr.y);
// Рассчитываем, сколько кадров должно пройти между движениями
int frames_to_wait = (state->moving_type == ToDown) ? 1 : (1000 / state->info->speed);
// Проверяем, прошло ли достаточно кадров
if (state->frame_count - state->last_move_frame < frames_to_wait) {
LOG_FUNCTION_END("do_move", "not enough frames passed, frame_count=%lld, last_move_frame=%lld, frames_to_wait=%d",
state->frame_count, state->last_move_frame, frames_to_wait);
return;
}
// Обновляем время последнего движения
state->last_move_frame = state->frame_count;
// Двигаем вниз
state->curr.y++;
if (check_collision()) {
state->curr.y--; // откат
state->state = Attaching; // переход в Attaching
state->curr.y--;
state->state = Attaching;
}
LOG_FUNCTION_END("do_move", "moved to (%d,%d), state=%d",
state->curr.x, state->curr.y, state->state);
}

View file

@ -1,15 +1,12 @@
#include "01_automato.h"
#include "../../logging.h"
void do_moving(void) {
GameState_t* state = get_game_state();
LOG_FUNCTION_START("do_moving", "moving_type=%d", state->moving_type);
switch (state->moving_type) {
case LeftDown:
case RightDown:
case Rotate:
// Обработка движения/поворота
Figure_t old = state->curr;
switch (state->moving_type) {
case LeftDown:
@ -29,18 +26,17 @@ void do_moving(void) {
break;
}
if (check_collision()) {
state->curr = old; // откат
state->curr = old;
}
state->state = Move;
break;
case ToDown:
// Мгновенное падение: двигаем вниз, пока не упрёмся
while (!check_collision()) {
state->curr.y++;
}
state->curr.y--; // откат на 1 назад, чтобы убрать последний шаг, вызвавший коллизию
state->state = Attaching; // сразу в Attaching
state->curr.y--;
state->state = Attaching;
break;
case DoNothing:
@ -48,7 +44,4 @@ void do_moving(void) {
break;
}
state->state = Move;
LOG_FUNCTION_END("do_moving", "curr=(%d,%d), state=%d",
state->curr.x, state->curr.y, state->state);
}

View file

@ -1,22 +1,14 @@
#include "01_automato.h"
#include "../../logging.h"
void do_attaching(void) {
GameState_t* state = get_game_state();
// Закрепляем фигуру на поле
place_figure();
// Удаляем линии
clear_lines();
// Проверяем GameOver
if (is_game_over()) {
state->state = GameOver;
} else {
state->state = Spawn;
}
LOG_FUNCTION_END("do_attaching", "state=%d", state->state);
}
int check_collision() {
@ -30,17 +22,15 @@ int check_collision() {
int y = fig->y + i;
if (x < 0 || x >= FIELD_WIDTH || y >= FIELD_HEIGHT) {
return 1; // коллизия
return 1;
}
if (y >= 0 && state->field[y][x]) {
return 1; // коллизия с другой фигурой
return 1;
}
}
}
}
LOG_FUNCTION_END("check_collision", "no collision");
return 0; // нет коллизии
return 0;
}
void place_figure() {
@ -78,33 +68,26 @@ void clear_lines() {
}
}
if (full) {
// Сдвигаем строки вниз
for (int y = i; y > 0; --y) {
for (int x = 0; x < FIELD_WIDTH; ++x) {
state->field[y][x] = state->field[y - 1][x];
}
}
// Очищаем верхнюю строку
for (int x = 0; x < FIELD_WIDTH; ++x) {
state->field[0][x] = 0;
}
lines_cleared++;
i++; // проверяем эту строку снова
i++;
}
}
// Начисление очков
if (lines_cleared > 0) {
int points[] = {0, 100, 300, 700, 1500};
int old_score = state->info->score;
state->info->score += points[lines_cleared];
// Обновляем рекорд, если нужно
if (state->info->score > state->info->high_score) {
state->info->high_score = state->info->score;
}
// Увеличиваем уровень каждые 600 очков (максимум 10 уровней)
int new_level = (state->info->score / 600) + 1;
if (new_level > 10) new_level = 10;
@ -112,16 +95,7 @@ void clear_lines() {
state->info->level = new_level;
state->info->speed += new_level * 5;
LOG_FUNCTION_END("clear_lines", "lines_cleared=%d, score=%d->%d, level=%d->%d, speed=%d->%d",
lines_cleared, old_score, state->info->score, old_level, state->info->level,
old_speed, state->info->speed);
return;
}
}
// Добавим лог, даже если линии не очищались
LOG_FUNCTION_END("clear_lines", "lines_cleared=%d, score=%d->%d, level=%d->%d, speed=%d->%d",
lines_cleared, state->info->score, state->info->score, old_level, state->info->level,
old_speed, state->info->speed);
}

View file

@ -1,30 +1,24 @@
#include "01_automato.h"
#include "../../logging.h"
void do_gameover(void) {
GameState_t* state = get_game_state();
// Сохраняем рекорд, если текущий рекорд побит
if (state->info->score > state->info->high_score) {
state->info->high_score = state->info->score;
save_high_score(state->info->high_score);
}
// Сброс next в пустую фигуру
const int (*shape)[4] = empty_fig();
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
state->next.mtrx[i][j] = shape[i][j];
LOG_FUNCTION_END("do_gameover", "");
}
int is_game_over() {
GameState_t* state = get_game_state();
// Проверяем, есть ли блоки в верхних рядах
for (int j = 0; j < FIELD_WIDTH; ++j) {
if (state->field[0][j] || state->field[1][j]) {
LOG_FUNCTION_END("is_game_over", "game over detected");
return 1;
}
}

View file

@ -1,26 +1,22 @@
// src/gui/cli/display.c
#include <ncurses.h>
#include "../../brick_game/tetris/00_tetris.h"
#include "../../logging.h"
// display.c
void display_game(GameInfo_t game_state) {
clear();
// Отображение игрового поля (всегда, даже во время паузы)
for (int i = 0; i < FIELD_HEIGHT; ++i) {
for (int j = 0; j < FIELD_WIDTH; ++j) {
if (game_state.field[i][j] == 2) {
mvaddch(i + 1, j * 2 + 1, '#'); // Закрепленные блоки
mvaddch(i + 1, j * 2 + 1, '#');
} else if (game_state.field[i][j] == 1) {
mvaddch(i + 1, j * 2 + 1, '$'); // Активная фигура
mvaddch(i + 1, j * 2 + 1, '$');
} else {
mvaddch(i + 1, j * 2 + 1, '.'); // Пустые ячейки
mvaddch(i + 1, j * 2 + 1, '.');
}
}
}
// Отображение следующей фигуры
mvaddstr(1, FIELD_WIDTH * 2 + 5, "Next figure:");
for (int i = 0; i < 4; ++i) {
@ -38,13 +34,8 @@ void display_game(GameInfo_t game_state) {
mvprintw(FIELD_HEIGHT + 4, 1, "Level: %d", game_state.level);
mvprintw(FIELD_HEIGHT + 5, 1, "Speed: %d", game_state.speed);
// Показываем надпись "PAUSED" если игра на паузе
if (game_state.pause) {
mvprintw(FIELD_HEIGHT / 2, FIELD_WIDTH * 2 + 1, "PAUSED");
}
refresh();
LOG_FUNCTION_END("display_game", "score=%d, level=%d, pause=%d",
game_state.score, game_state.level, game_state.pause);
}

View file

@ -2,15 +2,10 @@
#include <time.h>
#include <unistd.h>
#include "../../brick_game/tetris/00_tetris.h"
#include "../../logging.h"
void display_game(GameInfo_t game_state);
// gui/cli/main.c
int main() {
init_logger();
LOG_FUNCTION_START("main", "");
initscr();
cbreak();
noecho();
@ -82,14 +77,11 @@ int main() {
}
if (running) {
GameInfo_t game_state = updateCurrentState(); // Обновляем состояние
display_game(game_state); // Отображаем состояние
GameInfo_t game_state = updateCurrentState();
display_game(game_state);
}
}
endwin();
LOG_FUNCTION_END("main", "");
close_logger();
return 0;
}