aboutsummaryrefslogtreecommitdiffstats
path: root/bot/seasons/evergreen/minesweeper.py
blob: 644991111422c56f49840331abf5c274fe876462 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import typing
from random import random

import discord
from discord.ext import commands


class Minesweeper(commands.Cog):
    """Play a game of minesweeper."""

    def __init__(self, bot: commands.Bot) -> None:
        self.bot = bot
        self.games: typing.Dict[discord.member, typing.Dict] = {}  # Store the currently running games

    @staticmethod
    def is_bomb(cell: typing.Union[str, int]) -> int:
        """Returns 1 if `cell` is a bomb if not 0"""
        return 1 if cell == "bomb" else 0

    def generate_board(self) -> typing.List[typing.List[typing.Union[str, int]]]:
        """Generate a 2d array for the board."""
        board: typing.List[typing.List[typing.Union[str, int]]] = [
            ["bomb" if random() <= .2 else "number" for _ in range(10)] for _ in range(10)]
        for y, row in enumerate(board):
            for x, cell in enumerate(row):
                if cell == "number":
                    # calculate bombs near it
                    to_check = []
                    for xt in [x - 1, x, x + 1]:
                        for yt in [y - 1, y, y + 1]:
                            if xt != -1 and xt != 10 and yt != -1 and yt != 10:
                                to_check.append(board[yt][xt])

                    bombs = sum(map(self.is_bomb, to_check))
                    board[y][x] = bombs
        return board

    @staticmethod
    def format_for_discord(board: typing.List[typing.List[typing.Union[str, int]]]):
        """Format the board to a string for discord."""
        mapping = {
            0: ":stop_button:",
            1: ":one:",
            2: ":two:",
            3: ":three:",
            4: ":four:",
            5: ":five:",
            6: ":six:",
            7: ":seven:",
            8: ":eight:",
            9: ":nine:",
            10: ":keycap_ten:",
            "bomb": ":bomb:"
        }

        discord_msg = ":stop_button:    :one::two::three::four::five::six::seven::eight::nine::keycap_ten:\n\n"
        rows: typing.List[str] = []
        for row_number, row in enumerate(board):
            new_row = mapping[row_number + 1] + "    "
            for cell in row:
                new_row += mapping[cell]
            rows.append(new_row)

        discord_msg += "\n".join(rows)
        return discord_msg

    @commands.command(name="minesweeper")
    async def minesweeper_command(self, ctx: commands.Context) -> None:
        """Start a game of minesweeper."""
        if ctx.author in self.games.keys():  # Player is already playing
            msg = await ctx.send(f"{ctx.author.mention} you already have a game running")
            await msg.delete(delay=2)
            await ctx.message.delete(delay=2)
            return

        # Add game to list
        board = self.generate_board()
        await ctx.send(self.format_for_discord(board))
        self.games[ctx.author] = {
            "board": board
        }


def setup(bot: commands.Bot) -> None:
    """Cog load."""
    bot.add_cog(Minesweeper(bot))