Experimental Discord bot written in Python
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

gamescog.py 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import math
  2. import random
  3. from discord import Interaction
  4. from discord.app_commands import command
  5. from rocketbot.bot import Rocketbot
  6. from rocketbot.cogs.basecog import BaseCog
  7. class GamesCog(BaseCog, name="Games"):
  8. """Some really basic games or approximations thereof."""
  9. def __init__(self, bot: Rocketbot):
  10. super().__init__(
  11. bot,
  12. config_prefix='games',
  13. short_description='Some stupid, low effort games.',
  14. )
  15. @command(
  16. description='Creates a really terrible Minesweeper puzzle.'
  17. )
  18. async def minesweeper(self, interaction: Interaction):
  19. # Generate grid with randomly placed mines
  20. width = 8
  21. height = 8
  22. mine_count = math.ceil(width * height * 0.15)
  23. grid = [[0 for _ in range(width)] for _ in range(height)]
  24. for _ in range(mine_count):
  25. while True:
  26. x, y = random.randint(0, width - 1), random.randint(0, height - 1)
  27. if grid[x][y] == 0:
  28. grid[x][y] = -1
  29. break
  30. # Update free spaces with mine counts
  31. for y in range(height):
  32. for x in range(width):
  33. if grid[x][y] < 0:
  34. # Mine
  35. continue
  36. nearby_mine_count = 0
  37. for y0 in range(y - 1, y + 2):
  38. for x0 in range(x - 1, x + 2):
  39. if 0 <= x0 < width and 0 <= y0 < height and grid[x0][y0] < 0:
  40. nearby_mine_count += 1
  41. grid[x][y] = nearby_mine_count
  42. # Pick a non-mine starting tile to reveal
  43. start_x = 0
  44. start_y = 0
  45. while True:
  46. x, y = math.floor(random.gauss(width / 2, width / 4)), math.floor(random.gauss(height / 2, height / 4))
  47. if 0 <= x < width and 0 <= y < height and grid[x][y] >= 0:
  48. start_x, start_y = x, y
  49. break
  50. # Render
  51. text = ''
  52. symbols = [
  53. '\u274C', # cross mark (red X)
  54. '0\uFE0F\u20E3', # 0 + variation selector 16 + combining enclosing keycap
  55. '1\uFE0F\u20E3',
  56. '2\uFE0F\u20E3',
  57. '3\uFE0F\u20E3',
  58. '4\uFE0F\u20E3',
  59. '5\uFE0F\u20E3',
  60. '6\uFE0F\u20E3',
  61. '7\uFE0F\u20E3',
  62. '8\uFE0F\u20E3',
  63. ]
  64. for y in range(height):
  65. text += ' '
  66. for x in range(width):
  67. is_revealed = x == start_x and y == start_y
  68. if not is_revealed:
  69. text += '||'
  70. val = grid[x][y]
  71. text += symbols[val + 1]
  72. if not is_revealed:
  73. text += '||'
  74. text += ' '
  75. text += '\n'
  76. await interaction.response.send_message(
  77. f"Here's a really terrible randomized Minesweeper puzzle. Sorry. I did my best.¹\n"
  78. f"Uncover everything except the {mine_count} :x: mines.\n\n"
  79. f"{text}\n"
  80. "-# ¹ I mean, not really.",
  81. ephemeral=True,
  82. )