grid-dy
This commit is contained in:
parent
4b959ef449
commit
57e637a0ae
165
main.c
165
main.c
|
@ -1,12 +1,11 @@
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
|
||||||
char game[6][6] = {"001X10",
|
#define ARRAT(arr, type, x, y) ((arr) + (y)*(type) + (x))
|
||||||
"111110",
|
|
||||||
"X20111",
|
|
||||||
"X201X1",
|
|
||||||
"110111",
|
|
||||||
"000000",
|
|
||||||
};
|
|
||||||
|
|
||||||
static inline int min(int a, int b) {
|
static inline int min(int a, int b) {
|
||||||
return a < b ? a : b;
|
return a < b ? a : b;
|
||||||
|
@ -15,12 +14,28 @@ static inline int max(int a, int b) {
|
||||||
return a > b ? a : 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_width = 500;
|
||||||
int screen_height = 550;
|
int screen_height = 550;
|
||||||
|
|
||||||
Rectangle menu;
|
Rectangle menu;
|
||||||
|
Vec2i nb_cell = {9, 9}; // 9-9 16-16 16-30
|
||||||
int grid_len = 0;
|
int grid_len = 0;
|
||||||
int nb_cell = 16;
|
|
||||||
|
|
||||||
Rectangle grid = {0};
|
Rectangle grid = {0};
|
||||||
|
|
||||||
|
@ -31,7 +46,8 @@ Texture tile_hover_texture = {0};
|
||||||
Image mine_image_orig = {0};
|
Image mine_image_orig = {0};
|
||||||
Texture mine_texture = {0};
|
Texture mine_texture = {0};
|
||||||
|
|
||||||
void screen_resize_handle(void) {
|
void screen_resize_handle(void)
|
||||||
|
{
|
||||||
screen_width = GetScreenWidth();
|
screen_width = GetScreenWidth();
|
||||||
screen_height = GetScreenHeight();
|
screen_height = GetScreenHeight();
|
||||||
|
|
||||||
|
@ -44,19 +60,83 @@ void screen_resize_handle(void) {
|
||||||
};
|
};
|
||||||
|
|
||||||
Image tile_copy = ImageCopy(tile_image_orig);
|
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);
|
tile_texture = LoadTextureFromImage(tile_copy);
|
||||||
|
|
||||||
Image tile_hover_copy = ImageCopy(tile_hover_image_orig);
|
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);
|
tile_hover_texture = LoadTextureFromImage(tile_hover_copy);
|
||||||
|
|
||||||
Image mine_copy = ImageCopy(mine_image_orig);
|
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);
|
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<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));
|
||||||
|
|
||||||
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
|
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
|
||||||
|
|
||||||
menu = (Rectangle){0, 0, screen_width, screen_height - screen_width};
|
menu = (Rectangle){0, 0, screen_width, screen_height - screen_width};
|
||||||
|
@ -69,6 +149,8 @@ int main(void) {
|
||||||
|
|
||||||
screen_resize_handle();
|
screen_resize_handle();
|
||||||
|
|
||||||
|
fill_game(BEGINNER);
|
||||||
|
|
||||||
while (!WindowShouldClose()) {
|
while (!WindowShouldClose()) {
|
||||||
if (IsWindowResized())
|
if (IsWindowResized())
|
||||||
screen_resize_handle();
|
screen_resize_handle();
|
||||||
|
@ -77,56 +159,45 @@ int main(void) {
|
||||||
{
|
{
|
||||||
ClearBackground(BLACK);
|
ClearBackground(BLACK);
|
||||||
|
|
||||||
|
if (IsKeyPressed(KEY_R)) {
|
||||||
|
fill_game(BEGINNER);
|
||||||
|
}
|
||||||
|
|
||||||
DrawRectangleRec(menu, RED);
|
DrawRectangleRec(menu, RED);
|
||||||
DrawRectangleRec(grid, (Color) {
|
DrawRectangleRec(grid, (Color) {
|
||||||
.r = 0xFF, .g = 0xDA, .b = 0x2E, .a = 255
|
.r = 0xFF, .g = 0xDA, .b = 0x2E, .a = 255
|
||||||
});
|
});
|
||||||
|
|
||||||
#if 0
|
for (int x=0; x<nb_cell.x; x++) {
|
||||||
for (int x=0; x<nb_cell; x++) {
|
for (int y=0; y<nb_cell.y; y++) {
|
||||||
for (int y=0; y<nb_cell; y++) {
|
switch (game[y][x]) {
|
||||||
DrawTexture(
|
|
||||||
tile_texture,
|
|
||||||
grid.x + grid.width/nb_cell * x,
|
|
||||||
grid.y + grid.height/nb_cell * y,
|
|
||||||
WHITE
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
for (int x=0; x<6; x++) {
|
|
||||||
for (int y=0; y<6; y++) {
|
|
||||||
switch (game[x][y]) {
|
|
||||||
default:
|
|
||||||
case '0':
|
case '0':
|
||||||
DrawTexture(
|
DrawTexture(
|
||||||
tile_texture,
|
tile_texture,
|
||||||
grid.x + grid.width/nb_cell * x,
|
grid.x + grid.width/nb_cell.x * x,
|
||||||
grid.y + grid.height/nb_cell * y,
|
grid.y + grid.height/nb_cell.y * y,
|
||||||
WHITE
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
case '1':
|
|
||||||
case '2':
|
|
||||||
DrawTexture(
|
|
||||||
tile_hover_texture,
|
|
||||||
grid.x + grid.width/nb_cell * x,
|
|
||||||
grid.y + grid.height/nb_cell * y,
|
|
||||||
WHITE
|
WHITE
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
case 'X':
|
case 'X':
|
||||||
DrawTexture(
|
DrawTexture(
|
||||||
mine_texture,
|
mine_texture,
|
||||||
grid.x + grid.width/nb_cell * x,
|
grid.x + grid.width/nb_cell.x * x,
|
||||||
grid.y + grid.height/nb_cell * y,
|
grid.y + grid.height/nb_cell.y * y,
|
||||||
|
WHITE
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
DrawTexture(
|
||||||
|
tile_hover_texture,
|
||||||
|
grid.x + grid.width/nb_cell.x * x,
|
||||||
|
grid.y + grid.height/nb_cell.y * y,
|
||||||
WHITE
|
WHITE
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
int mouse_x = GetMouseX();
|
int mouse_x = GetMouseX();
|
||||||
int mouse_y = GetMouseY();
|
int mouse_y = GetMouseY();
|
||||||
|
@ -135,13 +206,13 @@ int main(void) {
|
||||||
|
|
||||||
// boundary
|
// boundary
|
||||||
mouse_x = max(mouse_x, grid_x);
|
mouse_x = max(mouse_x, grid_x);
|
||||||
mouse_x = min(mouse_x, grid_x + grid.width - (grid_len/nb_cell));
|
mouse_x = min(mouse_x, grid_x + grid.width - (grid_len/nb_cell.x));
|
||||||
mouse_y = max(mouse_y, grid_y);
|
mouse_y = max(mouse_y, grid_y);
|
||||||
mouse_y = min(mouse_y, grid_y + grid.height - (grid_len/nb_cell));
|
mouse_y = min(mouse_y, grid_y + grid.height - (grid_len/nb_cell.y));
|
||||||
|
|
||||||
// remove extra
|
// remove extra
|
||||||
mouse_x -= (mouse_x - grid_x) % (grid_len/nb_cell);
|
mouse_x -= (mouse_x - grid_x) % (grid_len/nb_cell.x);
|
||||||
mouse_y -= (mouse_y - grid_y) % (grid_len/nb_cell);
|
mouse_y -= (mouse_y - grid_y) % (grid_len/nb_cell.y);
|
||||||
|
|
||||||
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
|
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
|
||||||
DrawTexture(mine_texture, mouse_x, mouse_y, WHITE);
|
DrawTexture(mine_texture, mouse_x, mouse_y, WHITE);
|
||||||
|
|
Loading…
Reference in New Issue