import math import random from discord import Interaction from discord.app_commands import command from rocketbot.bot import Rocketbot from rocketbot.cogs.basecog import BaseCog class GamesCog(BaseCog, name="Games"): """Some really basic games or approximations thereof.""" def __init__(self, bot: Rocketbot): super().__init__( bot, config_prefix='games', short_description='Some stupid, low effort games.', ) @command( description='Creates a really terrible Minesweeper puzzle.' ) async def minesweeper(self, interaction: Interaction): # Generate grid with randomly placed mines width = 8 height = 8 mine_count = math.ceil(width * height * 0.15) grid = [[0 for _ in range(width)] for _ in range(height)] for _ in range(mine_count): while True: x, y = random.randint(0, width - 1), random.randint(0, height - 1) if grid[x][y] == 0: grid[x][y] = -1 break # Update free spaces with mine counts for y in range(height): for x in range(width): if grid[x][y] < 0: # Mine continue nearby_mine_count = 0 for y0 in range(y - 1, y + 2): for x0 in range(x - 1, x + 2): if 0 <= x0 < width and 0 <= y0 < height and grid[x0][y0] < 0: nearby_mine_count += 1 grid[x][y] = nearby_mine_count # Pick a non-mine starting tile to reveal start_x = 0 start_y = 0 while True: x, y = math.floor(random.gauss(width / 2, width / 4)), math.floor(random.gauss(height / 2, height / 4)) if 0 <= x < width and 0 <= y < height and grid[x][y] >= 0: start_x, start_y = x, y break # Render text = '' symbols = [ '\u274C', # cross mark (red X) '0\uFE0F\u20E3', # 0 + variation selector 16 + combining enclosing keycap '1\uFE0F\u20E3', '2\uFE0F\u20E3', '3\uFE0F\u20E3', '4\uFE0F\u20E3', '5\uFE0F\u20E3', '6\uFE0F\u20E3', '7\uFE0F\u20E3', '8\uFE0F\u20E3', ] for y in range(height): text += ' ' for x in range(width): is_revealed = x == start_x and y == start_y if not is_revealed: text += '||' val = grid[x][y] text += symbols[val + 1] if not is_revealed: text += '||' text += ' ' text += '\n' await interaction.response.send_message( f"Here's a really terrible randomized Minesweeper puzzle. Sorry. I did my best.¹\n" f"Uncover everything except the {mine_count} :x: mines.\n\n" f"{text}\n" "-# ¹ I mean, not really.", ephemeral=True, )