Created
July 30, 2021 20:38
-
-
Save vgmoose/2db52efa904075a1fdd4358e97ca59ce to your computer and use it in GitHub Desktop.
Discord minesweeper grid generator with emojis and spoiler tags
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
#!/bin/python3 | |
import sys, random | |
emojis = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "regional_indicator_x"] | |
mine = -1 | |
def gen_grid(size, mines): | |
grid = [[0]*size for _ in range(size)] | |
half = size // 2 | |
# generate mines randomly | |
for _ in range(mines): | |
y = random.randint(0, size-1) | |
x = random.randint(0, size-1) | |
grid[y][x] = mine | |
# the position of the zero we're going to reveal | |
zero_pos = (-1, -1) | |
# go through the grid and count up neighboring mines | |
for y, row in enumerate(grid): | |
for x, cell in enumerate(row): | |
if cell != mine: | |
# for ±1 cell in each direction | |
for j in range(-1, 2): | |
for i in range(-1, 2): | |
ny = y+j | |
nx = x+i | |
# out of bounds check | |
if ny >= 0 and nx >= 0 and ny < size and nx < size: | |
grid[y][x] += grid[ny][nx] == -1 | |
if grid[y][x] == 0: | |
# save the zero position if it's closer than our previous one to the middle | |
zy, zx = zero_pos | |
if abs(y - half) + abs(x - half) < abs(zy - half) + abs(zx - half): | |
zero_pos = (y, x) | |
# print out grid | |
for y, row in enumerate(grid): | |
for x, cell in enumerate(row): | |
emoji = f":{emojis[cell]}:" | |
if zero_pos != (y, x): | |
# spoiler it, not our starting 0 | |
emoji = f"||{emoji}||" | |
sys.stdout.write(emoji) | |
print("") | |
# 8x8 with 10-ish mines | |
gen_grid(8, 10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment