Created
December 19, 2019 19:36
-
-
Save Vild/fd55688d370b74e46a2e265fc7365837 to your computer and use it in GitHub Desktop.
Minesweeaper thingy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std; | |
string[] toText = [ | |
0 : ":black_large_square:", 1 : ":one:", 2 : ":two:", 3 : ":three:", | |
4 : ":four:", 5 : ":five:", 6 : ":six:", 7 : ":seven:", 8 : ":eight:", | |
9 : ":bomb:" | |
]; | |
string toEmote(size_t i) | |
{ | |
if (i >= toText.length) | |
i = toText.length - 1; | |
return toText[i]; | |
} | |
enum size_t size = 12; | |
enum size_t bombsCount = 16; | |
void flow(ref size_t[size][size] arr, size_t x, size_t y) | |
{ | |
void place(size_t x, size_t y) | |
{ | |
if (x >= 0 && x < arr.length && y >= 0 && y < arr.length) | |
arr[y][x]++; | |
} | |
foreach (dy; -1 .. 2) | |
foreach (dx; -1 .. 2) | |
if (dy != dx || dy != 0) | |
place(x + dx, y + dy); | |
} | |
void main() | |
{ | |
auto r = Random(unpredictableSeed); | |
size_t[size][size] arr; | |
// place bomb | |
size_t bombs = bombsCount; | |
while (bombs > 0) | |
{ | |
size_t x = uniform(0, arr.length, r); | |
size_t y = uniform(0, arr.length, r); | |
if (uniform(0, 4, r) == 0) | |
{ | |
bombs--; | |
arr[y][x] = 9; | |
flow(arr, x, y); | |
} | |
} | |
foreach (y; 0 .. arr.length) | |
{ | |
foreach (x; 0 .. arr.length) | |
writef("||%s||", arr[y][x].toEmote); | |
writeln(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment