2024-03-12 09:02:56 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2024-03-11 12:43:20 +01:00
|
|
|
#include "raylib.h"
|
|
|
|
|
2024-03-12 09:02:56 +01:00
|
|
|
#define ARRAT(arr, type, x, y) ((arr) + (y)*(type) + (x))
|
2024-03-11 17:01:03 +01:00
|
|
|
|
2024-03-11 12:43:20 +01:00
|
|
|
static inline int min(int a, int b) {
|
|
|
|
return a < b ? a : b;
|
|
|
|
}
|
2024-03-11 16:43:17 +01:00
|
|
|
static inline int max(int a, int b) {
|
|
|
|
return a > b ? a : b;
|
|
|
|
}
|
2024-03-11 12:43:20 +01:00
|
|
|
|
2024-03-12 09:02:56 +01:00
|
|
|
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;
|
|
|
|
|
2024-03-11 12:43:20 +01:00
|
|
|
int screen_width = 500;
|
|
|
|
int screen_height = 550;
|
|
|
|
|
|
|
|
Rectangle menu;
|
2024-03-12 09:02:56 +01:00
|
|
|
Vec2i nb_cell = {9, 9}; // 9-9 16-16 16-30
|
2024-03-11 12:43:20 +01:00
|
|
|
int grid_len = 0;
|
|
|
|
|
|
|
|
Rectangle grid = {0};
|
|
|
|
|
2024-03-11 16:43:17 +01:00
|
|
|
Image tile_image_orig = {0};
|
|
|
|
Texture tile_texture = {0};
|
|
|
|
Image tile_hover_image_orig = {0};
|
|
|
|
Texture tile_hover_texture = {0};
|
2024-03-11 12:43:20 +01:00
|
|
|
Image mine_image_orig = {0};
|
|
|
|
Texture mine_texture = {0};
|
|
|
|
|
2024-03-12 09:02:56 +01:00
|
|
|
void screen_resize_handle(void)
|
|
|
|
{
|
2024-03-11 12:43:20 +01:00
|
|
|
screen_width = GetScreenWidth();
|
|
|
|
screen_height = GetScreenHeight();
|
|
|
|
|
|
|
|
menu.width = screen_width;
|
|
|
|
grid_len = min(screen_width, screen_height - menu.height);
|
2024-03-11 16:43:17 +01:00
|
|
|
grid = (Rectangle){
|
|
|
|
(screen_width - grid_len) / 2,
|
|
|
|
screen_height - grid_len,
|
|
|
|
grid_len, grid_len
|
|
|
|
};
|
|
|
|
|
|
|
|
Image tile_copy = ImageCopy(tile_image_orig);
|
2024-03-12 09:02:56 +01:00
|
|
|
ImageResize(&tile_copy, grid.width/nb_cell.x, grid.height/nb_cell.y);
|
2024-03-11 16:43:17 +01:00
|
|
|
tile_texture = LoadTextureFromImage(tile_copy);
|
|
|
|
|
|
|
|
Image tile_hover_copy = ImageCopy(tile_hover_image_orig);
|
2024-03-12 09:02:56 +01:00
|
|
|
ImageResize(&tile_hover_copy, grid.width/nb_cell.x, grid.height/nb_cell.y);
|
2024-03-11 16:43:17 +01:00
|
|
|
tile_hover_texture = LoadTextureFromImage(tile_hover_copy);
|
2024-03-11 12:43:20 +01:00
|
|
|
|
|
|
|
Image mine_copy = ImageCopy(mine_image_orig);
|
2024-03-12 09:02:56 +01:00
|
|
|
ImageResize(&mine_copy, grid.width/nb_cell.x, grid.height/nb_cell.y);
|
2024-03-11 12:43:20 +01:00
|
|
|
mine_texture = LoadTextureFromImage(mine_copy);
|
|
|
|
}
|
|
|
|
|
2024-03-12 09:02:56 +01:00
|
|
|
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<nb_bomb; i++) {
|
|
|
|
int x = rand() % game_size.x;
|
|
|
|
int y = rand() % game_size.y;
|
|
|
|
|
|
|
|
if (game[y][x] == 'X')
|
|
|
|
i--;
|
|
|
|
else
|
|
|
|
game[y][x] = 'X';
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int x=0; x<game_size.x; x++) {
|
|
|
|
for (int y=0; y<game_size.y; y++) {
|
|
|
|
if (game[y][x] == 'X')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int count = count_bomb(x, y);
|
|
|
|
game[y][x] = count + 48;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int count_bomb(int x, int y)
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
for (int i=-1; i<=1; i++) {
|
|
|
|
if (x+i < 0 || x+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));
|
|
|
|
|
2024-03-11 12:43:20 +01:00
|
|
|
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
|
|
|
|
|
|
|
|
menu = (Rectangle){0, 0, screen_width, screen_height - screen_width};
|
2024-03-11 16:43:17 +01:00
|
|
|
tile_image_orig = LoadImage("ressources/tile.png");
|
|
|
|
tile_hover_image_orig = LoadImage("ressources/tile_hover.png");
|
2024-03-11 12:43:20 +01:00
|
|
|
mine_image_orig = LoadImage("ressources/mine.png");
|
|
|
|
|
|
|
|
InitWindow(screen_width, screen_height, "mineur");
|
2024-03-11 16:43:17 +01:00
|
|
|
SetTargetFPS(60);
|
2024-03-11 12:43:20 +01:00
|
|
|
|
|
|
|
screen_resize_handle();
|
|
|
|
|
2024-03-12 09:02:56 +01:00
|
|
|
fill_game(BEGINNER);
|
|
|
|
|
2024-03-11 12:43:20 +01:00
|
|
|
while (!WindowShouldClose()) {
|
|
|
|
if (IsWindowResized())
|
|
|
|
screen_resize_handle();
|
|
|
|
|
|
|
|
BeginDrawing();
|
|
|
|
{
|
2024-03-11 16:43:17 +01:00
|
|
|
ClearBackground(BLACK);
|
2024-03-11 12:43:20 +01:00
|
|
|
|
2024-03-12 09:02:56 +01:00
|
|
|
if (IsKeyPressed(KEY_R)) {
|
|
|
|
fill_game(BEGINNER);
|
|
|
|
}
|
|
|
|
|
2024-03-11 12:43:20 +01:00
|
|
|
DrawRectangleRec(menu, RED);
|
2024-03-11 16:43:17 +01:00
|
|
|
DrawRectangleRec(grid, (Color) {
|
|
|
|
.r = 0xFF, .g = 0xDA, .b = 0x2E, .a = 255
|
|
|
|
});
|
2024-03-11 12:43:20 +01:00
|
|
|
|
2024-03-12 09:02:56 +01:00
|
|
|
for (int x=0; x<nb_cell.x; x++) {
|
|
|
|
for (int y=0; y<nb_cell.y; y++) {
|
|
|
|
switch (game[y][x]) {
|
2024-03-11 17:01:03 +01:00
|
|
|
case '0':
|
|
|
|
DrawTexture(
|
|
|
|
tile_texture,
|
2024-03-12 09:02:56 +01:00
|
|
|
grid.x + grid.width/nb_cell.x * x,
|
|
|
|
grid.y + grid.height/nb_cell.y * y,
|
2024-03-11 17:01:03 +01:00
|
|
|
WHITE
|
|
|
|
);
|
|
|
|
break;
|
2024-03-12 09:02:56 +01:00
|
|
|
case 'X':
|
2024-03-11 17:01:03 +01:00
|
|
|
DrawTexture(
|
2024-03-12 09:02:56 +01:00
|
|
|
mine_texture,
|
|
|
|
grid.x + grid.width/nb_cell.x * x,
|
|
|
|
grid.y + grid.height/nb_cell.y * y,
|
2024-03-11 17:01:03 +01:00
|
|
|
WHITE
|
|
|
|
);
|
|
|
|
break;
|
2024-03-12 09:02:56 +01:00
|
|
|
default:
|
2024-03-11 17:01:03 +01:00
|
|
|
DrawTexture(
|
2024-03-12 09:02:56 +01:00
|
|
|
tile_hover_texture,
|
|
|
|
grid.x + grid.width/nb_cell.x * x,
|
|
|
|
grid.y + grid.height/nb_cell.y * y,
|
2024-03-11 17:01:03 +01:00
|
|
|
WHITE
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-11 16:43:17 +01:00
|
|
|
|
|
|
|
int mouse_x = GetMouseX();
|
|
|
|
int mouse_y = GetMouseY();
|
|
|
|
int grid_x = (int) grid.x;
|
|
|
|
int grid_y = (int) grid.y;
|
|
|
|
|
2024-03-11 17:01:03 +01:00
|
|
|
// boundary
|
2024-03-11 16:43:17 +01:00
|
|
|
mouse_x = max(mouse_x, grid_x);
|
2024-03-12 09:02:56 +01:00
|
|
|
mouse_x = min(mouse_x, grid_x + grid.width - (grid_len/nb_cell.x));
|
2024-03-11 16:43:17 +01:00
|
|
|
mouse_y = max(mouse_y, grid_y);
|
2024-03-12 09:02:56 +01:00
|
|
|
mouse_y = min(mouse_y, grid_y + grid.height - (grid_len/nb_cell.y));
|
2024-03-11 16:43:17 +01:00
|
|
|
|
2024-03-11 17:01:03 +01:00
|
|
|
// remove extra
|
2024-03-12 09:02:56 +01:00
|
|
|
mouse_x -= (mouse_x - grid_x) % (grid_len/nb_cell.x);
|
|
|
|
mouse_y -= (mouse_y - grid_y) % (grid_len/nb_cell.y);
|
2024-03-11 16:43:17 +01:00
|
|
|
|
|
|
|
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
|
|
|
|
DrawTexture(mine_texture, mouse_x, mouse_y, WHITE);
|
|
|
|
} else {
|
|
|
|
DrawTexture(tile_hover_texture, mouse_x, mouse_y, WHITE);
|
|
|
|
}
|
2024-03-11 12:43:20 +01:00
|
|
|
}
|
|
|
|
EndDrawing();
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseWindow();
|
|
|
|
return 0;
|
|
|
|
}
|