minesweeper

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 08dc9c97e17d45a758f77d6363ff50c9858c1d30
parent b82f341e5b92475652b6ad9cda9c7a938c3973e9
Author: Samdal <samdal@protonmail.com>
Date:   Thu, 29 Apr 2021 23:38:52 +0200

actually a working window

Diffstat:
Mmain.c | 120+++++++++++++++++++++++++++++++++++++------------------------------------------
1 file changed, 56 insertions(+), 64 deletions(-)

diff --git a/main.c b/main.c @@ -1,14 +1,14 @@ #include <raylib.h> +#include <stdio.h> #include <stdlib.h> // Global variables -static bool shouldRedraw = true; static int screenWidth = 800; static int screenHeight = 800; static Vector2 squareSize; static int TilesX = 20; static int TilesY = 20; -static int TotalBombs = 20; +static int TotalBombs = 40; typedef struct Bombs { int x, y; } Bomb; @@ -16,11 +16,8 @@ static Bomb *bombPos; // all bombs position static bool *shown; // if a tile is hidden or not // function declerations -static void InitGame(void); -static void RedrawAllTiles(void); static void ExplodeAllBombs(void); static void NewGame(void); -static void UpdateDrawFrame(void); static void UpdateGame(void); static void DrawGame(void); static void DrawTile(int x, int y); @@ -32,22 +29,29 @@ int main(int argc, char *argv[]) { TotalBombs = strtol(argv[3], NULL, 0); case 3: TilesY = strtol(argv[2], NULL, 0); + if (TilesY <= 0) + TilesY = 20; case 2: TilesX = strtol(argv[1], NULL, 0); - break; + if (TilesX <= 0) + TilesX = 20; + if ((argc == 4) && (TotalBombs <= 0 || TotalBombs >= TilesX * TilesY)) + TotalBombs = (TilesX * TilesY) / 7; } + shown = malloc(TilesX * TilesY * sizeof(bool)); + bombPos = malloc(TotalBombs * sizeof(Bomb)); + if (bombPos == NULL || shown == NULL) + return 255; // if memory allocation failed - // enable Vsync and resize window SetConfigFlags(FLAG_WINDOW_RESIZABLE); SetConfigFlags(FLAG_VSYNC_HINT); InitWindow(screenWidth, screenHeight, "Minesweeper in C99"); + NewGame(); - InitGame(); - if (bombPos == NULL || shown == NULL) // if memory allocation failed - return 255; - - while (!WindowShouldClose()) - UpdateDrawFrame(); + while (!WindowShouldClose()) { + UpdateGame(); + DrawGame(); + } // close down and free memory free(bombPos); @@ -56,67 +60,40 @@ int main(int argc, char *argv[]) { return 0; } -void InitGame(void) { - // do not allow illegal values - if (TilesX <= 0) - TilesX = 20; - if (TilesY <= 0) - TilesY = 20; - - int TileArea = TilesX * TilesY; - if (TotalBombs <= 0 || TotalBombs >= TileArea) - TotalBombs = 20; - - bombPos = malloc(20 * sizeof(Bomb)); - bool(*shown)[TilesX] = calloc(TileArea, sizeof(bool)); - - // square size - squareSize.x = (float)screenWidth / (float)TilesX; - squareSize.y = (float)screenHeight / (float)TilesY; - - NewGame(); -} - void NewGame(void) { + // reset board for (int i = 0; i < TotalBombs; i++) { - int randomx = rand() % TilesX; - int randomy = rand() % TilesY; + bombPos[i].x = 0; + bombPos[i].y = 0; } - RedrawAllTiles(); -} - -void ExplodeAllBombs(void) {} + for (int x = 0; x < TilesX; x++) + for (int y = 0; y < TilesY; y++) + shown[x * TilesX + y] = false; -void RedrawAllTiles(void) { - ClearBackground(RAYWHITE); - shouldRedraw = false; - EndDrawing(); -} - -void UpdateDrawFrame(void) { - // if screen is rezised - const int newScreenWidth = GetScreenWidth(); - const int newScreenHeight = GetScreenHeight(); - if (screenWidth != newScreenWidth) { - screenWidth = newScreenWidth; - shouldRedraw = true; - } - if (screenHeight != newScreenHeight) { - screenHeight = newScreenHeight; - shouldRedraw = true; + // place bombs (with no duplicates) + for (int i = 0; i < TotalBombs; i++) { + bombPos[i].x = rand() % TilesX; + bombPos[i].y = rand() % TilesY; + for (int j = 0; j < TotalBombs; j++) + if ((j != i) && + (bombPos[i].x == bombPos[j].x && bombPos[i].y == bombPos[j].y)) + i--; // repeat bomb } - - UpdateGame(); - DrawGame(); } +void ExplodeAllBombs(void) {} + void UpdateGame(void) {} void DrawGame(void) { - if (shouldRedraw) { - RedrawAllTiles(); - return; - } + squareSize.x = GetScreenWidth() / TilesX; + squareSize.y = GetScreenHeight() / TilesY; + ClearBackground(RAYWHITE); + for (int x = 0; x < TilesX; x++) + for (int y = 0; y < TilesY; y++) { + // if (shown[x * TilesX + y]) + DrawTile(x, y); + } EndDrawing(); } @@ -124,5 +101,20 @@ void DrawTile(int x, int y) { // find naboring bombs int nearby = 0; for (int i = 0; i < TotalBombs; i++) { + if ((bombPos[i].x == x - 1 || bombPos[i].x == x + 1 || bombPos[i].x == x) && + (bombPos[i].y == y - 1 || bombPos[i].y == y + 1 || bombPos[i].y == y)) + nearby++; + + if (bombPos[i].x == x && bombPos[i].y == y) { + DrawText("x", (x + 0.4f) * squareSize.x, (y + 0.3f) * squareSize.y, + squareSize.y * 0.6f, RED); + return; + } } + + if (nearby == 0) + return; // TODO: Draw empty square + const char c[] = {nearby + '0', '\0'}; + DrawText(c, (x + 0.4f) * squareSize.x, (y + 0.3f) * squareSize.y, + squareSize.y * 0.6f, BLACK); }