Created
August 28, 2020 19:22
-
-
Save pixelomer/a8b93fd79cf7af1c139a2b894884b5b6 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python3 | |
# This script creates a message that abuses Janet the Discord | |
# bot to print a minesweeper field | |
import json | |
from sys import stdout | |
from random import randint | |
BOMB_CELL = 9 | |
EMPTY_CELL = 0 | |
FIELD_SIZE = 13 | |
BOMB_COUNT = FIELD_SIZE | |
REVEALED_CELL_MASK = 0xF0 | |
EASY = False | |
def reveal_cells(field, x, y): | |
if field[x][y] & REVEALED_CELL_MASK: | |
return | |
if field[x][y] == BOMB_CELL: | |
return | |
original_cell = field[x][y] | |
field[x][y] |= REVEALED_CELL_MASK | |
if original_cell != EMPTY_CELL: | |
return | |
for sx in range(x-1, x+2): | |
if sx < 0 or sx > FIELD_SIZE-1: | |
continue | |
for sy in range(y-1, y+2): | |
if sy < 0 or sy > FIELD_SIZE-1: | |
continue | |
if x == sx and y == sy: | |
continue | |
reveal_cells(field, sx, sy) | |
def make_field(): | |
safe_x = randint(3, 6) | |
safe_y = randint(3, 6) | |
field = [ [EMPTY_CELL] * FIELD_SIZE for _ in range(FIELD_SIZE) ] | |
bomb_count = 0 | |
# Place bombs at random places | |
while bomb_count < BOMB_COUNT: | |
bomb_x = randint(0, FIELD_SIZE-1) | |
bomb_y = randint(0, FIELD_SIZE-1) | |
if field[bomb_x][bomb_y] == BOMB_CELL or abs(bomb_x - safe_x) < 2 or abs(bomb_y - safe_y) < 2: | |
continue | |
field[bomb_x][bomb_y] = BOMB_CELL | |
bomb_count += 1 | |
# Place numbers based on theses bombs | |
for x in range(FIELD_SIZE): | |
for y in range(FIELD_SIZE): | |
if field[x][y] == BOMB_CELL: | |
continue | |
for sx in range(x-1, x+2): | |
if sx < 0 or sx > FIELD_SIZE-1: | |
continue | |
for sy in range(y-1, y+2): | |
if sy < 0 or sy > FIELD_SIZE-1: | |
continue | |
if field[sx][sy] == BOMB_CELL: | |
field[x][y] += 1 | |
# Recursively reveal cells | |
if EASY: | |
reveal_cells(field, safe_x, safe_y) | |
else: | |
field[safe_x][safe_y] |= REVEALED_CELL_MASK | |
return field | |
field = make_field() | |
emojis = [ | |
"zero", "one", "two", "three", "four", | |
"five", "six", "seven", "eight", "bomb" | |
] | |
stdout.write("!userconf show ```\nMinesweeper! (%dx%d)\n```\n" % (FIELD_SIZE, FIELD_SIZE)) | |
for line in field: | |
for cell in line: | |
is_revealed = cell & REVEALED_CELL_MASK | |
cell &= ~REVEALED_CELL_MASK | |
if not is_revealed: | |
stdout.write("||") | |
stdout.write(":%s:" % (emojis[cell])) | |
if not is_revealed: | |
stdout.write("||") | |
stdout.write("\n") | |
stdout.write("```\nGenerated by @pixelomer's script\n(FOR MODS: No selfbots involved here)\n```") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment