<?php
// 扫雷 —— 用 **PHP** 写的手机小游戏（网格推理）
// Minesweeper -- a small phone game written in **PHP**
//
// 玩法：9×9 共 10 颗雷；点格子翻开，数字是周围 8 格的雷数；翻到 0 会
// 连锁展开；顶部按钮切到「插旗」模式再点就是标雷；把所有非雷格子
// 翻开即通关，踩到雷就结束；ESC 重开。
//
// 跑法 —— 手机：vml run examples/php/minesweeper.php
// 跑法 —— 桌面：vmlcli Examples/php/minesweeper.php --screen 480x640 --frames /tmp/fr

$mine = array(); $opn = array(); $flag = array(); $nbr = array();
$qx = array(); $qy = array();

$k = ui_win_open_ex("扫雷", 0, 0, 0, 0);
ui_scr_set_keep_on(1);
$w = ui_scr_get_w();
$h = ui_scr_get_h();
if ($w <= 0) { $w = 360; }
if ($h <= 0) { $h = 620; }

$cell = intdiv($w - 16, 9);
if (intdiv($h - 200, 9) < $cell) { $cell = intdiv($h - 200, 9); }
if ($cell < 12) { $cell = 12; }
$ox = intdiv($w - $cell * 9, 2);
$oy = 96;
$frames = 0;
$seed = 1234;
$opened = 0;
$dead = 0;
$won = 0;
$flagm = 0;

for ($i = 0; $i < 81; $i++) { $mine[$i] = 0; $opn[$i] = 0; $flag[$i] = 0; $nbr[$i] = 0; }
$i = 0;
while ($i < 10) {
    $seed = ($seed + 7919) % 32768;
    $mx = intdiv($seed, 3641);
    $seed = ($seed + 7919) % 32768;
    $my = intdiv($seed, 3641);
    if ($mine[$mx * 9 + $my] == 0) {
        $mine[$mx * 9 + $my] = 1;
        $i = $i + 1;
    }
}
$tid = ui_timer_set(80, 0);

while ($frames < 4000) {
    $k = ui_2d_clear(-15724520);
    if ($flagm == 0) {
        $k = ui_2d_rect(8, 44, 110, 28, -14013902, 1, 0, 8);
        $k = ui_2d_text(16, 62, "翻开模式", -1513232, 12, 0);
    } else {
        $k = ui_2d_rect(8, 44, 110, 28, -2516442, 1, 0, 8);
        $k = ui_2d_text(16, 62, "插旗模式", -15724520, 12, 0);
    }
    $k = ui_2d_text($w - 60, 62, "" . $opened, -6643536, 14, 0);

    for ($x = 0; $x < 9; $x++) {
        for ($y = 0; $y < 9; $y++) {
            $idx = $x * 9 + $y;
            $px = $ox + $x * $cell;
            $py = $oy + $y * $cell;
            if ($opn[$idx] == 1) {
                if ($mine[$idx] == 1) {
                    $k = ui_2d_rect($px + 1, $py + 1, $cell - 2, $cell - 2, -2073552, 1, 0, 2);
                } else {
                    $k = ui_2d_rect($px + 1, $py + 1, $cell - 2, $cell - 2, -15066590, 1, 0, 2);
                    if ($nbr[$idx] > 0) {
                        $k = ui_2d_text($px + 14, $py + 26, "" . $nbr[$idx], -1513232, 15, 0);
                    }
                }
            } else {
                $k = ui_2d_rect($px + 1, $py + 1, $cell - 2, $cell - 2, -14013902, 1, 0, 2);
                if ($flag[$idx] == 1) {
                    $k = ui_2d_circle($px + intdiv($cell, 2), $py + intdiv($cell, 2), intdiv($cell, 4), -2516442, 1, 0);
                }
            }
        }
    }

    if ($dead == 1) {
        $k = ui_2d_text(8, 24, "踩雷了，ESC 重来", -2073552, 14, 0);
    } else if ($won == 1) {
        $k = ui_2d_text(8, 24, "扫清了！ESC 再来", -10044566, 14, 0);
    } else {
        $k = ui_2d_text(8, 24, "点格子翻开 / 点顶部按钮插旗", -6643536, 11, 0);
    }
    $k = ui_2d_present();

    $msg = ui_msg_fetch_wait(0);
    if ($msg == 10) { $frames = 4000; }
    if ($msg == 6) {
        $ax = ui_msg_get_a();
        $ay = ui_msg_get_b();
        if ($ax >= 8 && $ax <= 118 && $ay >= 44 && $ay <= 72) {
            if ($flagm == 0) { $flagm = 1; } else { $flagm = 0; }
        } else if ($ax >= $ox && $ay >= $oy) {
            $x = intdiv($ax - $ox, $cell);
            $y = intdiv($ay - $oy, $cell);
            if ($x < 9 && $y < 9) {
                $idx = $x * 9 + $y;
                if ($dead == 0 && $won == 0) {
                    if ($flagm == 1) {
                        if ($opn[$idx] == 0) {
                            if ($flag[$idx] == 1) { $flag[$idx] = 0; } else { $flag[$idx] = 1; }
                        }
                    } else {
                        if ($opn[$idx] == 0 && $flag[$idx] == 0) {
                            $qx[0] = $x;
                            $qy[0] = $y;
                            $head = 0;
                            $tail = 0;
                            while ($head <= $tail) {
                                $cx = $qx[$head];
                                $cy = $qy[$head];
                                $head = $head + 1;
                                $idx = $cx * 9 + $cy;
                                if ($opn[$idx] == 0) {
                                    $opn[$idx] = 1;
                                    $opened = $opened + 1;
                                    if ($mine[$idx] == 1) {
                                        $dead = 1;
                                        $head = $tail + 2;
                                    } else {
                                        $nb = 0;
                                        for ($dx = -1; $dx <= 1; $dx++) {
                                            for ($dy = -1; $dy <= 1; $dy++) {
                                                if ($dx != 0 || $dy != 0) {
                                                    $nx = $cx + $dx;
                                                    $ny = $cy + $dy;
                                                    if ($nx >= 0 && $ny >= 0 && $nx < 9 && $ny < 9) {
                                                        if ($mine[$nx * 9 + $ny] == 1) { $nb = $nb + 1; }
                                                    }
                                                }
                                            }
                                        }
                                        $nbr[$idx] = $nb;
                                        if ($nb == 0) {
                                            for ($dx = -1; $dx <= 1; $dx++) {
                                                for ($dy = -1; $dy <= 1; $dy++) {
                                                    if ($dx != 0 || $dy != 0) {
                                                        $nx = $cx + $dx;
                                                        $ny = $cy + $dy;
                                                        if ($nx >= 0 && $ny >= 0 && $nx < 9 && $ny < 9) {
                                                            if ($tail < 80) {
                                                                $tail = $tail + 1;
                                                                $qx[$tail] = $nx;
                                                                $qy[$tail] = $ny;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            if ($opened >= 71) { $won = 1; }
                        }
                    }
                }
            }
        }
    }
    if ($msg == 1) {
        $key = ui_msg_get_a();
        if ($key == 27) {
            for ($i = 0; $i < 81; $i++) { $mine[$i] = 0; $opn[$i] = 0; $flag[$i] = 0; $nbr[$i] = 0; }
            $seed = 1234 + $frames;
            $i = 0;
            while ($i < 10) {
                $seed = ($seed + 7919) % 32768;
                $mx = intdiv($seed, 3641);
                $seed = ($seed + 7919) % 32768;
                $my = intdiv($seed, 3641);
                if ($mine[$mx * 9 + $my] == 0) { $mine[$mx * 9 + $my] = 1; $i = $i + 1; }
            }
            $opened = 0; $dead = 0; $won = 0; $flagm = 0; $frames = 0;
        }
    }
    if ($msg == 9) { $frames = $frames + 1; }
}

ui_timer_kill($tid);
ui_scr_set_keep_on(0);
$k = ui_win_close();
echo "minesweeper done", chr(10);
?>
