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