diff options
Diffstat (limited to 'explode')
| -rw-r--r-- | explode/Makefile | 8 | ||||
| -rw-r--r-- | explode/explode.c | 128 | 
2 files changed, 136 insertions, 0 deletions
| diff --git a/explode/Makefile b/explode/Makefile new file mode 100644 index 0000000..f0eca91 --- /dev/null +++ b/explode/Makefile @@ -0,0 +1,8 @@ +# Makefile template, copy and remove comments + +SRC := ../util.c explode.c +OUT := ../bin/explode +LIBS := -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 + +make all: +	gcc $(SRC) -o $(OUT) $(LIBS) diff --git a/explode/explode.c b/explode/explode.c new file mode 100644 index 0000000..d8e0916 --- /dev/null +++ b/explode/explode.c @@ -0,0 +1,128 @@ +#include <raylib.h> +#include <stdlib.h> +#include <stdio.h> + +const int WindowHeight = 600; +const int WindowWidth = 800; + +int rows; +int cols; +int cellSize; + +enum color { +    red, blue, green, violet, black +}; + +typedef char byte; +typedef struct Tile { +    byte number; +    enum color color; +} Tile; + +Tile* board; + +Color colorFromEnum(enum color c){ +    switch(c){ +    case red: +        return RED; +        break; +    case blue: +        return BLUE; +        break; +    case green: +        return GREEN; +        break; +    case violet: +        return VIOLET; +        break; +    case black: +    default: +        return BLACK; +    } +} + +void drawBoard(){ +    int ymax = cols * cellSize; +    int xmax = rows * cellSize; +    int offset = 2; +    for (int i = 1; i <= rows; i++){ +        DrawLine(i * cellSize, 0, i * cellSize, ymax, BLACK); +    } +    for (int j = 1; j <= cols; j++){ +        DrawLine(0 , j * cellSize, xmax, j * cellSize, BLACK); +    } +    for (int i = 0; i < rows; i++){ +        for (int j = 0; j < cols; j++){ +            char text[16]; +            Tile tile = board[i * cols + j]; +            snprintf(text, 16, "%d", tile.number); +            DrawText(text, j * cellSize + offset, i * cellSize + offset, cellSize - 2 * offset, colorFromEnum(tile.color)); +        } +    } +} + +int selectTile(int x, int y){ +    int r = y / cellSize; +    int c = x / cellSize; +    return r * cols + c; +} + +void explode(int r, int c, enum color color){ +    int index = r * cols + c; +    board[index].number++; +    board[index].color = color; +    if (board[index].number > 3){ +        board[index].number = 0; +        board[index].color = black; +        if (r > 0)        explode(r - 1, c, color); +        if (r < rows - 1) explode(r + 1, c, color); +        if (c > 0)        explode(r, c - 1, color); +        if (c < cols - 1) explode(r, c + 1, color); +    } +} + +int main(){ +    InitWindow(WindowHeight, WindowWidth, "Explode"); +    rows = cols = 6; +    cellSize = 60; +    board = (Tile*) calloc(sizeof(Tile), rows * cols); + +    Color turns[] = {RED, BLUE, GREEN, VIOLET}; +    int alive[] = {1, 1, 1, 1}; +    int turn_index = 0; +    for (int i = 0; i < rows * cols; i++){ +        board[i].color = black; +    } + +    board[1 + 1 * cols].color = red; +    board[1 + 4 * cols].color = blue; +    board[4 + 1 * cols].color = green; +    board[4 + 4 * cols].color = violet; + +    while (!WindowShouldClose()){ +        BeginDrawing(); +        ClearBackground(WHITE); +        drawBoard(); +        if (!alive[turn_index]){ +            turn_index = (turn_index + 1) % 4; +        } +        if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){ +            int index = selectTile(GetMouseX(), GetMouseY()); +            if (index < rows * cols && ColorIsEqual(colorFromEnum(board[index].color), turns[turn_index])){ +                explode(index / cols, index % cols, board[index].color); +                int number_sums[] = {0, 0, 0, 0}; +                for (int i = 0; i < rows * cols; i++){ +                    number_sums[board[i].color]++; +                } +                for (int i = 0; i < 4; i++){ +                    if (number_sums[i] == 0){ +                        alive[i] = 0; +                    } +                } +                turn_index = (turn_index + 1) % 4; +            } +        } +        EndDrawing(); +    } +    free(board); +} | 
