/*- * 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" #include "../engine/ui/ui.h" static Log *log = NULL; static void init_log(void) { if (log != NULL) return; log = log_create("Game"); } float xmin, xmax, ymin, ymax; float box_size; int field_matrix[3][3]; float first_row, half_first_row; float first_col, half_first_col; float second_row, half_second_row; float second_col, half_second_col; float third_row, half_third_row; float third_col, half_third_col; /* 1 = circle, 2 = cross */ int sign = 1; int moves = 0; static void game_draw_field(TrisGame *game, float box_size, float x, float y); static void draw_sign(float x, float y) { float l = 0.20; if (sign == 1) { engine_draw_circle(x, y, l, 120, 1); sign = 2; } else if (sign == 2) { engine_draw_line(x - l, y + l, x + l, y - l); engine_draw_line(x + l, y + l, x - l, y - l); sign = 1; } moves++; } static void loop_cells(list_t *cells, float x, float y) { init_log(); list_node_t *current = cells->head; do { Cell *cell = current->data; if (!cell_is_filled(cell) && cell_within_bounds(cell, x, y)) { draw_sign(cell->cx, cell->cy); cell_set_filled(cell, true); } current = current->next; } while (current != NULL); } static void game_mouse_button_pressed(float x, float y, void *data) { TrisGame *game = data; loop_cells(game->cells, x, y); /* Click inside the field */ if (x >= xmin && x <= xmax && y >= ymin && y <= ymax) { if (y >= first_row) { if (x <= first_col && field_matrix[0][0] == -1) { field_matrix[0][0] = sign; } else if (x >= first_col && x <= second_col && field_matrix[0][1] == -1) { field_matrix[0][1] = sign; } else if (x >= second_col && field_matrix[0][2] == -1) { field_matrix[0][2] = sign; } } else if (y >= second_row) { if (x <= first_col && field_matrix[1][0] == -1) { field_matrix[1][0] = sign; } else if (x >= first_col && x <= second_col && field_matrix[1][1] == -1) { field_matrix[1][1] = sign; } else if (x >= second_col && field_matrix[1][2] == -1) { field_matrix[1][2] = sign; } } else if (y >= third_row) { if (x <= first_col && field_matrix[2][0] == -1) { field_matrix[2][0] = sign; } else if (x >= first_col && x <= second_col && field_matrix[2][1] == -1) { field_matrix[2][1] = sign; } else if (x >= second_col && field_matrix[2][2] == -1) { field_matrix[2][2] = sign; } } } check_win(); } 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->cells = list_create(); engine_init(width, height); ui_set_title("Tris Game"); engine_set_mouse_button_listener(game_mouse_button_pressed, game); game_draw_field(game, 0.5, 0, 0); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { field_matrix[i][j] = -1; } } return game; } void game_start() { engine_loop(); } static void game_draw_field(TrisGame *game, float _box_size, float x, float y) { 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); xmin = x - half_box * 3; xmax = x + half_box * 3; ymin = y - half_box * 3; ymax = y + half_box * 3; box_size = _box_size; first_row = ymax - box_size; second_row = ymax - box_size * 2; third_row = ymax - box_size * 3; half_first_row = ymax - box_size / 2; half_second_row = ymax - (box_size / 2) * 3; half_third_row = ymax - (box_size / 2) * 5; first_col = xmin + box_size; second_col = xmin + box_size * 2; third_col = xmin + box_size * 3; half_first_col = xmin + box_size / 2; half_second_col = xmin + (box_size / 2) * 3; half_third_col = xmin + (box_size / 2) * 5; /* First row */ list_add(game->cells, cell_new(xmin, first_col, ymax, first_row)); list_add(game->cells, cell_new(first_col, second_col, ymax, first_row)); list_add(game->cells, cell_new(second_col, xmax, ymax, first_row)); /* Second row */ list_add(game->cells, cell_new(xmin, first_col, first_row, second_row)); list_add(game->cells, cell_new(first_col, second_col, first_row, second_row)); list_add(game->cells, cell_new(second_col, xmax, first_row, second_row)); /* Third row */ list_add(game->cells, cell_new(xmin, first_col, second_row, ymin)); list_add(game->cells, cell_new(first_col, second_col, second_row, ymin)); list_add(game->cells, cell_new(second_col, xmax, second_row, ymin)); }