diff --git a/main.c b/main.c index b4b4589..1410901 100644 --- a/main.c +++ b/main.c @@ -1,12 +1,11 @@ +#include +#include +#include +#include + #include "raylib.h" -char game[6][6] = {"001X10", - "111110", - "X20111", - "X201X1", - "110111", - "000000", - }; +#define ARRAT(arr, type, x, y) ((arr) + (y)*(type) + (x)) static inline int min(int a, int b) { return a < b ? a : b; @@ -15,12 +14,28 @@ static inline int max(int a, int b) { return a > b ? a : b; } +char game[16][30] = {0}; + +typedef enum GameType { + BEGINNER, + INTERMEDIATE, + EXPERT, +} GameType; + +typedef struct Vec2i { + int x; + int y; +} Vec2i; + +Vec2i game_size; +int nb_bomb; + int screen_width = 500; int screen_height = 550; Rectangle menu; +Vec2i nb_cell = {9, 9}; // 9-9 16-16 16-30 int grid_len = 0; -int nb_cell = 16; Rectangle grid = {0}; @@ -31,7 +46,8 @@ Texture tile_hover_texture = {0}; Image mine_image_orig = {0}; Texture mine_texture = {0}; -void screen_resize_handle(void) { +void screen_resize_handle(void) +{ screen_width = GetScreenWidth(); screen_height = GetScreenHeight(); @@ -44,19 +60,83 @@ void screen_resize_handle(void) { }; Image tile_copy = ImageCopy(tile_image_orig); - ImageResize(&tile_copy, grid.width/nb_cell, grid.height/nb_cell); + ImageResize(&tile_copy, grid.width/nb_cell.x, grid.height/nb_cell.y); tile_texture = LoadTextureFromImage(tile_copy); Image tile_hover_copy = ImageCopy(tile_hover_image_orig); - ImageResize(&tile_hover_copy, grid.width/nb_cell, grid.height/nb_cell); + ImageResize(&tile_hover_copy, grid.width/nb_cell.x, grid.height/nb_cell.y); tile_hover_texture = LoadTextureFromImage(tile_hover_copy); Image mine_copy = ImageCopy(mine_image_orig); - ImageResize(&mine_copy, grid.width/nb_cell, grid.height/nb_cell); + ImageResize(&mine_copy, grid.width/nb_cell.x, grid.height/nb_cell.y); mine_texture = LoadTextureFromImage(mine_copy); } -int main(void) { +void fill_game(GameType type) +{ + memset(game, 0, 16*30); + + switch (type) { + case BEGINNER: + game_size.x = 9; + game_size.y = 9; + nb_bomb = 10; + break; + case INTERMEDIATE: + game_size.x = 16; + game_size.y = 16; + nb_bomb = 40; + break; + case EXPERT: + game_size.x = 16; + game_size.y = 30; + nb_bomb = 99; + break; + } + + for (int i=0; i game_size.x) + continue; + for (int j=-1; j<=1; j++) { + if (y+j < 0 || y+j > game_size.y) + continue; + if (i == 0 && j == 0) + continue; + if (game[y+j][x+i] == 'X') + count++; + } + } + return count; +} + +int main(void) +{ + srand(time(NULL)); + SetConfigFlags(FLAG_WINDOW_RESIZABLE); menu = (Rectangle){0, 0, screen_width, screen_height - screen_width}; @@ -69,6 +149,8 @@ int main(void) { screen_resize_handle(); + fill_game(BEGINNER); + while (!WindowShouldClose()) { if (IsWindowResized()) screen_resize_handle(); @@ -77,56 +159,45 @@ int main(void) { { ClearBackground(BLACK); + if (IsKeyPressed(KEY_R)) { + fill_game(BEGINNER); + } + DrawRectangleRec(menu, RED); DrawRectangleRec(grid, (Color) { .r = 0xFF, .g = 0xDA, .b = 0x2E, .a = 255 }); -#if 0 - for (int x=0; x