IT WORKS NOW

This commit is contained in:
Rorikstr | Rust Dev 2025-09-29 19:23:14 +03:00
parent f5b65a390b
commit 0f2d03526e
14 changed files with 19477 additions and 31 deletions

View file

@ -24,7 +24,9 @@ CLIDIR = gui/cli
# Файлы
TETRIS_SRC = $(shell find $(TETRISDIR) -name "*.c")
TETRIS_OBJ = $(TETRIS_SRC:.c=.o)
LOGGING_SRC = logging.c
LOGGING_OBJ = logging.o
TETRIS_OBJ = $(TETRIS_SRC:.c=.o) $(LOGGING_OBJ)
CLI_SRC = $(shell find $(CLIDIR) -name "*.c")
CLI_OBJ = $(CLI_SRC:.c=.o)
@ -51,6 +53,9 @@ brick_game/tetris/%.o: brick_game/tetris/%.c
gui/cli/%.o: gui/cli/%.c
$(CC) $(CFLAGS) -c $< -o $@
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
install: $(TARGET)
install -m 755 $(TARGET) $(BINDIR)/
@ -58,7 +63,7 @@ uninstall:
rm -f $(BINDIR)/$(TARGET)
clean:
rm -f $(CLI_OBJ) $(TETRIS_OBJ) $(TARGET) $(LIB_TETRIS) *.gcda *.gcno *.gcov
rm -f $(CLI_OBJ) $(TETRIS_OBJ) $(TARGET) $(LIB_TETRIS) *.gcda *.gcno *.gcov tetris.log
test:
@echo "Running tests..."

View file

@ -1,9 +1,19 @@
#include "01_automato.h"
#include "../../logging.h"
void userInput(UserAction_t action, bool hold) {
LOG_FUNCTION_START("userInput", "action=%d, hold=%d", action, hold);
(void)hold; // заглушка
GameState_t* state = get_game_state();
// Команды движения игнорируются до первого спавна (пока state = Init или первый Spawn)
if ((state->state == Init || state->state == Spawn) &&
(action == Left || action == Right || action == Down || action == Up || action == Action)) {
LOG_FUNCTION_END("userInput", "ignored movement command during initialization, state=%d", state->state);
return;
}
switch (action) {
case Start:
state->state = Init;
@ -36,9 +46,13 @@ void userInput(UserAction_t action, bool hold) {
default:
break;
}
LOG_FUNCTION_END("userInput", "state=%d", state->state);
}
GameInfo_t updateCurrentState() {
LOG_FUNCTION_START("updateCurrentState", "");
GameState_t* state = get_game_state();
switch (state->state) {
case Init:
@ -61,13 +75,28 @@ GameInfo_t updateCurrentState() {
break;
}
// Копируем данные в уже выделенную память
// Копируем state->field в info->field
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++) {
if (fig->mtrx[i][j]) {
int x = fig->x + j;
int y = fig->y + i;
if (y >= 0 && y < FIELD_HEIGHT && x >= 0 && x < FIELD_WIDTH) {
state->info->field[y][x] = 1; // активная фигура
}
}
}
}
// Копируем 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];
@ -75,5 +104,9 @@ GameInfo_t updateCurrentState() {
}
state->info->pause = 0;
LOG_FUNCTION_END("updateCurrentState", "score=%d, level=%d, state=%d",
state->info->score, state->info->level, state->state);
return *state->info;
}

View file

@ -1,7 +1,10 @@
#include "01_automato.h"
#include <stdlib.h>
#include "../../logging.h"
GameState_t* get_game_state(void) {
LOG_FUNCTION_START("get_game_state", "");
static GameState_t state = {0};
static int initialized = 0;
@ -21,13 +24,31 @@ GameState_t* get_game_state(void) {
state.info->next[i] = malloc(4 * sizeof(int));
}
// Инициализируем начальные значения
state.info->speed = 1;
state.info->score = 0;
state.info->level = 1;
state.info->pause = 0;
// Инициализируем следующую фигуру
state.next.sprite = rand() % FIGURE_COUNT;
state.next.rotation = 0;
const int (*shape)[4] = get_figure_shape(state.next.sprite, 0);
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
state.next.mtrx[i][j] = shape[i][j];
state.state = GameOver;
initialized = 1;
}
LOG_FUNCTION_END("get_game_state", "state=%d", state.state);
return &state;
}
void terminate_and_free() {
LOG_FUNCTION_START("terminate_and_free", "");
GameState_t* state = get_game_state();
if (state->info) {
@ -56,4 +77,6 @@ void terminate_and_free() {
free(state->info);
state->info = NULL;
}
LOG_FUNCTION_END("terminate_and_free", "");
}

View file

@ -1,6 +1,9 @@
#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)
@ -11,4 +14,7 @@ void do_init(void) {
state->info->level = 1;
state->info->speed = 1;
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,14 +1,18 @@
#include "01_automato.h"
#include <stdlib.h>
#include "../../logging.h"
void do_spawn(void) {
LOG_FUNCTION_START("do_spawn", "");
GameState_t* state = get_game_state();
// Присваиваем curr = next
// Устанавливаем текущую фигуру из следующей (или генерируем первую)
state->curr = state->next;
state->curr.x = FIELD_WIDTH / 2 - 2;
state->curr.y = 0;
// Генерим следующую фигуру
// Генерим новую следующую фигуру
state->next.sprite = rand() % FIGURE_COUNT;
state->next.rotation = 0;
const int (*shape)[4] = get_figure_shape(state->next.sprite, 0);
@ -19,8 +23,12 @@ void do_spawn(void) {
// Проверка на GameOver
if (check_collision()) {
state->state = GameOver;
LOG_FUNCTION_END("do_spawn", "collision detected, state=%d", state->state);
return;
}
state->state = Move;
LOG_FUNCTION_END("do_spawn", "curr=(%d,%d), next_sprite=%d, state=%d",
state->curr.x, state->curr.y, state->next.sprite, state->state);
}

View file

@ -1,5 +1,7 @@
// 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;
@ -7,12 +9,18 @@ long long get_time_ms() {
void do_move(void) {
GameState_t* state = get_game_state();
LOG_FUNCTION_START("do_move", "");
// Добавляем проверку, чтобы избежать деления на ноль
if (state->info->speed <= 0) {
state->info->speed = 1; // Устанавливаем минимальное значение
}
long long current_time = get_time_ms();
int delay = (state->moving_type == ToDown) ? 50 : (1000 / state->info->speed);
if (current_time - state->last_time < delay) {
LOG_FUNCTION_END("do_move", "not enough time passed, delay=%d", delay);
return; // ещё не время
}
state->last_time = current_time;
@ -23,4 +31,7 @@ void do_move(void) {
state->curr.y--; // откат
state->state = Attaching; // переход в Attaching
}
LOG_FUNCTION_END("do_move", "curr=(%d,%d), state=%d",
state->curr.x, state->curr.y, state->state);
}

View file

@ -1,7 +1,9 @@
#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:
@ -34,10 +36,10 @@ void do_moving(void) {
case ToDown:
// Мгновенное падение: двигаем вниз, пока не упрёмся
do {
while (!check_collision()) {
state->curr.y++;
} while (!check_collision());
state->curr.y--; // откат на 1 назад
}
state->curr.y--; // откат на 1 назад, чтобы убрать последний шаг, вызвавший коллизию
state->state = Attaching; // сразу в Attaching
break;
@ -45,4 +47,7 @@ void do_moving(void) {
state->state = Move;
break;
}
LOG_FUNCTION_END("do_moving", "curr=(%d,%d), state=%d",
state->curr.x, state->curr.y, state->state);
}

View file

@ -1,6 +1,9 @@
#include "01_automato.h"
#include "../../logging.h"
void do_attaching(void) {
LOG_FUNCTION_START("do_attaching", "");
GameState_t* state = get_game_state();
// Закрепляем фигуру на поле
place_figure();
@ -15,9 +18,13 @@ void do_attaching(void) {
} else {
state->state = Spawn;
}
LOG_FUNCTION_END("do_attaching", "state=%d", state->state);
}
int check_collision() {
LOG_FUNCTION_START("check_collision", "");
GameState_t* state = get_game_state();
Figure_t* fig = &state->curr;
@ -28,19 +35,25 @@ int check_collision() {
int y = fig->y + i;
if (x < 0 || x >= FIELD_WIDTH || y >= FIELD_HEIGHT) {
LOG_FUNCTION_END("check_collision", "collision with boundary, x=%d, y=%d", x, y);
return 1; // коллизия
}
if (y >= 0 && state->field[y][x]) {
LOG_FUNCTION_END("check_collision", "collision with field, x=%d, y=%d", x, y);
return 1; // коллизия с другой фигурой
}
}
}
}
LOG_FUNCTION_END("check_collision", "no collision");
return 0; // нет коллизии
}
void place_figure() {
GameState_t* state = get_game_state();
LOG_FUNCTION_START("place_figure", "curr=(%d,%d)", state->curr.x, state->curr.y);
Figure_t* fig = &state->curr;
for (int i = 0; i < 4; ++i) {
@ -54,10 +67,14 @@ void place_figure() {
}
}
}
LOG_FUNCTION_END("place_figure", "");
}
void clear_lines() {
GameState_t* state = get_game_state();
LOG_FUNCTION_START("clear_lines", "score=%d", state->info->score);
int lines_cleared = 0;
for (int i = FIELD_HEIGHT - 1; i >= 0; --i) {
@ -93,4 +110,7 @@ void clear_lines() {
state->info->speed = state->info->level;
}
}
LOG_FUNCTION_END("clear_lines", "lines_cleared=%d, score=%d, level=%d",
lines_cleared, state->info->score, state->info->level);
}

View file

@ -1,21 +1,31 @@
#include "01_automato.h"
#include "../../logging.h"
void do_gameover(void) {
LOG_FUNCTION_START("do_gameover", "");
GameState_t* state = get_game_state();
// Сброс 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() {
LOG_FUNCTION_START("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;
}
}
LOG_FUNCTION_END("is_game_over", "game not over");
return 0;
}

View file

@ -1,23 +1,19 @@
// src/gui/cli/display.c
#include <ncurses.h>
#include "../../brick_game/tetris/00_tetris.h"
#include "../../logging.h"
void display_game() {
// display.c
void display_game(GameInfo_t game_state) {
LOG_FUNCTION_START("display_game", "");
clear();
GameInfo_t game_state = updateCurrentState();
// Проверяем, является ли состояние GameOver
if (game_state.next[0][0] == 0 && game_state.next[0][1] == 0 && game_state.next[0][2] == 0 && game_state.next[0][3] == 0) {
mvprintw(FIELD_HEIGHT / 2, FIELD_WIDTH - 4, "GAME OVER");
refresh();
return;
}
// Проверяем pause
// Убираем проверку на GameOver из display
if (game_state.pause) {
mvprintw(FIELD_HEIGHT / 2, FIELD_WIDTH - 4, "PAUSED");
mvprintw(FIELD_HEIGHT / 2, FIELD_WIDTH * 2 + 1, "PAUSED");
refresh();
LOG_FUNCTION_END("display_game", "paused");
return;
}
@ -53,9 +49,11 @@ void display_game() {
mvprintw(FIELD_HEIGHT + 5, 1, "Speed: %d", game_state.speed);
if (game_state.pause) {
mvprintw(FIELD_HEIGHT / 2, FIELD_WIDTH - 4, "PAUSED");
mvprintw(FIELD_HEIGHT / 2, FIELD_WIDTH * 2 + 1, "PAUSED");
}
refresh();
printf("DEBUG: display_game completed\n");
LOG_FUNCTION_END("display_game", "score=%d, level=%d",
game_state.score, game_state.level);
}

View file

@ -1,23 +1,40 @@
// src/gui/cli/main.c
#include <ncurses.h>
#include <time.h>
#include <unistd.h>
#include "../../brick_game/tetris/00_tetris.h"
#include "../../logging.h"
void display_game();
void display_game(GameInfo_t game_state);
// gui/cli/main.c
int main() {
init_logger();
LOG_FUNCTION_START("main", "");
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
nodelay(stdscr, TRUE);
nodelay(stdscr, FALSE);
curs_set(0);
// Цикл ожидания нажатия F/f
mvprintw(FIELD_HEIGHT / 2, FIELD_WIDTH - 4, "Press F to Start");
refresh();
int ch = 0;
while (1) {
ch = getch();
if (ch == 'f' || ch == 'F') {
userInput(Start, false);
break;
}
}
nodelay(stdscr, TRUE);
timeout(100);
int ch;
UserAction_t current_action;
UserAction_t current_action = {0};
bool action_valid = false;
bool running = true;
@ -64,12 +81,15 @@ int main() {
userInput(current_action, false);
}
if (running) { // Обновляем состояние только если не завершаемся
updateCurrentState();
display_game();
if (running) {
GameInfo_t game_state = updateCurrentState(); // Обновляем состояние
display_game(game_state); // Отображаем состояние
}
}
endwin();
LOG_FUNCTION_END("main", "");
close_logger();
return 0;
}

32
src/logging.c Normal file
View file

@ -0,0 +1,32 @@
#include "logging.h"
#include <stdio.h>
#include <stdlib.h>
FILE* log_file = NULL;
void init_logger() {
log_file = fopen("tetris.log", "w");
if (log_file == NULL) {
fprintf(stderr, "Error: Could not open log file\n");
exit(1);
}
time_t now = time(0);
char* time_str = ctime(&now);
time_str[strlen(time_str) - 1] = '\0';
fprintf(log_file, "[INIT] %s: Logger initialized\n", time_str);
fflush(log_file);
}
void close_logger() {
if (log_file != NULL) {
time_t now = time(0);
char* time_str = ctime(&now);
time_str[strlen(time_str) - 1] = '\0';
fprintf(log_file, "[CLOSE] %s: Logger closed\n", time_str);
fclose(log_file);
log_file = NULL;
}
}

43
src/logging.h Normal file
View file

@ -0,0 +1,43 @@
#ifndef LOGGING_H
#define LOGGING_H
#include <stdio.h>
#include <time.h>
#include <string.h>
// Макрос для логгирования начала функции
#define LOG_FUNCTION_START(func_name, ...) \
do { \
time_t now = time(0); \
char* time_str = ctime(&now); \
time_str[strlen(time_str) - 1] = '\0'; \
fprintf(log_file, "[START] %s: %s", time_str, func_name); \
if (sizeof(#__VA_ARGS__) > 1) { \
fprintf(log_file, " - " __VA_ARGS__); \
} \
fprintf(log_file, "\n"); \
fflush(log_file); \
} while(0)
// Макрос для логгирования конца функции
#define LOG_FUNCTION_END(func_name, ...) \
do { \
time_t now = time(0); \
char* time_str = ctime(&now); \
time_str[strlen(time_str) - 1] = '\0'; \
fprintf(log_file, "[END] %s: %s", time_str, func_name); \
if (sizeof(#__VA_ARGS__) > 1) { \
fprintf(log_file, " - " __VA_ARGS__); \
} \
fprintf(log_file, "\n"); \
fflush(log_file); \
} while(0)
// Инициализация логгера
void init_logger();
// Закрытие логгера
void close_logger();
// Глобальная переменная для файла лога
extern FILE* log_file;
#endif

19232
src/tetris.log Normal file

File diff suppressed because it is too large Load diff