/*- * Copyright (C) 2025 Alessandro Iezzi * * This file is part of Tris Game. * * Tris Game is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Tris Game is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Tris Game. If not, see . */ #include #include #include #include "../engine/engine.h" #include "game.h" #include "cell.h" #define WIN_PATTERNS 8 static const char SIGN_CIRCLE = 'o'; static const char SIGN_CROSS = 'x'; /* There are 8 lines to win: * - 0, 1, 2 (horizontal) * - 3, 4, 5 (horizontal) * - 6, 7, 8 (horizontal) * - 0, 3, 6 (vertical) * - 1, 4, 7 (vertical) * - 2, 5, 8 (vertical) * - 0, 4, 8 (diagonal) * - 2, 4, 6 (diagonal) */ static const int win_patterns[WIN_PATTERNS][3] = { {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, /* horizontal */ {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, /* vertical */ {0, 4, 8}, {2, 4, 6} /* diagonal */ }; static Log *log = NULL; static void init_log(void) { if (log != NULL) return; log = log_create("Game"); } static void game_draw_field(TrisGame *game, float box_size); static char game_next_sign(TrisGame *game); static void draw_frames(void *data) { if (data == NULL) return; TrisGame *game = data; for (int i = 0; i < BOARD_SIZE; i++) { Cell *cell = game->cells[i]; if (cell->sign == SIGN_CIRCLE) { engine_render_circle(cell->shape); } } } static void game_restart(TrisGame *game) { game->ended = false; for (int i = 0; i < BOARD_SIZE; i++) { Cell *cell = game->cells[i]; cell->filled = false; cell->sign = '\0'; if (cell->shape != NULL) { free(cell->shape); } } engine_set_rendering_background_c(game->engine, game->board->default_color); } static void check_win(TrisGame *game) { /* The minimum number of moves to win the play */ if (game->moves < 5) return; for (int i = 0; i < WIN_PATTERNS; i++) { /* These are the indexes of every pattern */ int i0 = win_patterns[i][0]; int i1 = win_patterns[i][1]; int i2 = win_patterns[i][2]; if (game->cells[i0]->sign != '\0' && game->cells[i0]->sign == game->cells[i1]->sign && game->cells[i1]->sign == game->cells[i2]->sign) { engine_set_rendering_background_c(game->engine, game->board->wining_color); game->ended = true; return; } } if (game->moves >= 9) { engine_set_rendering_background_c(game->engine, game->board->draft_color); game->ended = true; } } static void draw_sign(TrisGame *game, Cell *cell) { float x = cell->cx; float y = cell->cy; float l = 0.20f; char sign = game_next_sign(game); cell_set_sign(cell, sign); if (sign == SIGN_CIRCLE) { cell->shape = engine_circle_new(x, y, l, 120, 1); game->sign = SIGN_CROSS; } else if (sign == SIGN_CROSS) { engine_draw_line(x - l, y + l, x + l, y - l); engine_draw_line(x + l, y + l, x - l, y - l); game->sign = SIGN_CIRCLE; } game->moves++; check_win(game); } static void loop_cells(TrisGame *game, float x, float y) { init_log(); for (int i = 0; i < BOARD_SIZE; i++) { Cell *cell = game->cells[i]; if (!cell_is_filled(cell) && cell_within_bounds(cell, x, y)) { draw_sign(game, cell); cell_set_filled(cell, true); } } } static void game_mouse_button_pressed(float x, float y, void *data) { TrisGame *game = data; if (!game->ended) { loop_cells(game, x, y); } else { game_restart(game); } } TrisGame * game_init(int width, int height) { init_log(); /* Init of the TrisGame */ TrisGame *game = malloc(sizeof(TrisGame)); if (game == NULL) { log_error(log, "Error allocating memory for the game"); exit(EXIT_FAILURE); } game->width = width; game->height = height; game->sign = SIGN_CIRCLE; game->moves = 0; game->engine = engine_new(width, height); game->board = board_new(); game->ended = false; engine_set_window_title(game->engine, "Tris Game"); engine_set_mouse_button_listener(game_mouse_button_pressed, game); engine_set_rendering_listener(game->engine, draw_frames, game); game_draw_field(game, 0.5f); return game; } void game_start() { engine_loop(); } static void game_draw_field(TrisGame *game, float box_size) { float x = 0.0f; float y = 0.0f; float half_box = box_size / 2; engine_draw_line(x - half_box, y + half_box * 3, x - half_box, y - half_box * 3); engine_draw_line(x + half_box, y + half_box * 3, x + half_box, y - half_box * 3); engine_draw_line(x - half_box * 3, y + half_box, x + half_box * 3, y + half_box); engine_draw_line(x - half_box * 3, y - half_box, x + half_box * 3, y - half_box); float xmin = x - half_box * 3; float xmax = x + half_box * 3; float ymin = y - half_box * 3; float ymax = y + half_box * 3; float first_row = ymax - box_size; float second_row = ymax - box_size * 2; float first_col = xmin + box_size; float second_col = xmin + box_size * 2; /* First row */ game->cells[0] = cell_new(xmin, first_col, ymax, first_row); game->cells[1] = cell_new(first_col, second_col, ymax, first_row); game->cells[2] = cell_new(second_col, xmax, ymax, first_row); /* Second row */ game->cells[3] = cell_new(xmin, first_col, first_row, second_row); game->cells[4] = cell_new(first_col, second_col, first_row, second_row); game->cells[5] = cell_new(second_col, xmax, first_row, second_row); /* Third row */ game->cells[6] = cell_new(xmin, first_col, second_row, ymin); game->cells[7] = cell_new(first_col, second_col, second_row, ymin); game->cells[8] = cell_new(second_col, xmax, second_row, ymin); } static char game_next_sign(TrisGame *game) { return game->sign; }