summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/game/cell.c4
-rw-r--r--src/main/game/game.c70
-rw-r--r--src/main/game/game.h6
3 files changed, 60 insertions, 20 deletions
diff --git a/src/main/game/cell.c b/src/main/game/cell.c
index cb56330..e8322ff 100644
--- a/src/main/game/cell.c
+++ b/src/main/game/cell.c
@@ -50,6 +50,8 @@ cell_new(float left, float right, float top, float bottom)
cell->cx = (cell->right + cell->left) / 2;
cell->cy = (cell->top + cell->bottom) / 2;
+ cell->sign = '\0';
+
return cell;
}
@@ -82,4 +84,4 @@ void
cell_set_sign(Cell *cell, char sign)
{
cell->sign = sign;
-} \ No newline at end of file
+}
diff --git a/src/main/game/game.c b/src/main/game/game.c
index b813906..ee1a1b5 100644
--- a/src/main/game/game.c
+++ b/src/main/game/game.c
@@ -25,8 +25,26 @@
#include "cell.h"
#include "../engine/ui/ui.h"
-const char SIGN_CIRCLE = 'o';
-const char SIGN_CROSS = 'x';
+#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;
@@ -41,6 +59,26 @@ static void game_draw_field(TrisGame *game, float box_size);
static char game_next_sign(TrisGame *game);
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) {
+ printf("Win!\n");
+ }
+ }
+}
+
+static void
draw_sign(TrisGame *game, Cell *cell)
{
float x = cell->cx;
@@ -59,6 +97,7 @@ draw_sign(TrisGame *game, Cell *cell)
}
game->moves++;
+ check_win(game);
}
static void
@@ -66,15 +105,13 @@ loop_cells(TrisGame *game, float x, float y)
{
init_log();
- list_node_t *current = game->cells->head;
- do {
- Cell *cell = current->data;
+ 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);
}
- current = current->next;
- } while (current != NULL);
+ }
}
static void
@@ -96,7 +133,6 @@ game_init(int width, int height)
}
game->width = width;
game->height = height;
- game->cells = list_create();
game->sign = SIGN_CIRCLE;
game->moves = 0;
@@ -139,19 +175,19 @@ game_draw_field(TrisGame *game, float box_size)
float second_col = xmin + box_size * 2;
/* 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));
+ 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 */
- 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));
+ 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 */
- 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));
+ 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
diff --git a/src/main/game/game.h b/src/main/game/game.h
index 6b9231c..3d10bea 100644
--- a/src/main/game/game.h
+++ b/src/main/game/game.h
@@ -20,11 +20,13 @@
#ifndef __GAME_H__
#define __GAME_H__
-#include "../util/list.h"
+#define BOARD_SIZE 9
+
+#include "cell.h"
typedef struct TrisGame {
int width, height;
- list_t *cells;
+ Cell *cells[BOARD_SIZE];
char sign; /* x = cross and o = circle */
int moves;
} TrisGame;