tinyfiles.tv
Custom: ร— ๐Ÿ’ฃ
๐Ÿ’ฃ 010 ๐Ÿ™‚ โฑ 000
Left click / tap to reveal โ€ข Right click (or use Flag Mode) to flag โ€ข Smiley face to reset
Backend  ยท  Proposed Architecture ยท Not connected

Minesweeper Backend โ€” Proposed Architecture

Right now this game is 100% local. The board is generated in JS, and if you refresh, the 100×100 grid is gone. This is the map to make it real: the server generates the board, validates the first click, tracks the timer, and stores global high scores.

01 / Files to create
api/minesweeper/create.php    POST /api/minesweeper/create
api/minesweeper/reveal.php    POST /api/minesweeper/reveal
api/minesweeper/flag.php      POST /api/minesweeper/flag
api/minesweeper/finish.php    POST /api/minesweeper/finish
api/minesweeper/leaderboard.php GET /api/minesweeper/leaderboard
sql/minesweeper_tables.sql

Keep the standard tinyfiles PHP pattern: /api/bootstrap.php, json_in() / json_out(), member sessions, and prepared PDO. The server must be authoritative over the mine locations.

02 / Database tables
-- sql/minesweeper_tables.sql
ms_games
  id CHAR(36) PRIMARY KEY
  member VARCHAR(80) NULL              -- NULL = guest
  rows INT NOT NULL
  cols INT NOT NULL
  mines INT NOT NULL
  seed VARCHAR(32) NOT NULL            -- Server-generated RNG seed
  status ENUM('playing','won','lost') NOT NULL DEFAULT 'playing'
  started_at DATETIME NOT NULL
  finished_at DATETIME NULL

ms_moves
  id BIGINT AUTO_INCREMENT PRIMARY KEY
  game_id CHAR(36) NOT NULL
  action ENUM('reveal','flag') NOT NULL
  r INT NOT NULL
  c INT NOT NULL
  created_at DATETIME NOT NULL

ms_scores
  id INT AUTO_INCREMENT PRIMARY KEY
  member VARCHAR(80) NOT NULL
  difficulty VARCHAR(40) NOT NULL      -- e.g. 'custom_100x100'
  time_ms INT NOT NULL
  won_at DATETIME NOT NULL
The server stores the seed, not the 10,000-cell board. When a player makes a move, the server reconstructs the board using the seed + first-click coordinates. This keeps the DB tiny and makes custom 100×100 grids cheap to store.
03 / API endpoints & request flow
POST /api/minesweeper/create
{ "rows": 100, "cols": 100, "mines": 1500 }
-> { "ok": true, "game_id": "uuid...", "seed": "xyz123" }

POST /api/minesweeper/reveal
{ "game_id": "uuid...", "r": 10, "c": 15 }
- Server loads seed. 
- If this is the FIRST reveal, it generates the board now, ensuring (r,c) and its neighbors are safe.
- Server checks the tile state. If it's a mine, status = 'lost'.
- If it's a 0, the server runs the flood-fill algorithm and returns all revealed cells in one JSON array.
- Returns { "ok": true, "safe": true, "revealed": [[r,c], ...], "status": "playing" }

POST /api/minesweeper/flag
{ "game_id": "uuid...", "r": 10, "c": 15 }
- Toggles flag state on the server.

POST /api/minesweeper/finish
{ "game_id": "uuid..." }
- Checks if total_revealed == (rows * cols) - mines. If so, calculates time and saves to ms_scores.
04 / First-Click Safety (Server-side)
// Never generate the board on page load. 
// Wait for the first reveal, then generate mines avoiding that cell.

function generateBoard($rows, $cols, $mines, $safe_r, $safe_c, $seed) {
    mt_srand(crc32($seed));
    $board = array_fill(0, $rows, array_fill(0, $cols, 0));
    
    // Build a list of all coordinates EXCEPT the 3x3 around $safe_r, $safe_c
    $valid_cells = [];
    for($i=0; $i<$rows; $i++){
        for($j=0; $j<$cols; $j++){
            if(abs($i - $safe_r) <= 1 && abs($j - $safe_c) <= 1) continue;
            $valid_cells[] = [$i, $j];
        }
    }
    shuffle($valid_cells);
    
    // Place mines
    for($m=0; $m<$mines; $m++){
        $cell = $valid_cells[$m];
        $board[$cell[0]][$cell[1]] = -1; // -1 = mine
    }
    
    // Calculate numbers
    // ... (standard neighbor counting logic) ...
    return $board;
}