aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar vivax3794 <[email protected]>2019-08-09 18:09:55 +0200
committerGravatar vivax3794 <[email protected]>2019-08-09 18:09:55 +0200
commitdc488fb963e680c9ba8cef8bbd4f0bd6f1c8ed32 (patch)
treedd71cee68fa379fa22f3d01caf495935af6ed3b7
parentadded logic for when a user already have a game running (diff)
add generate_board
(it might have a bug, will begone more apparent when adding formatting for discord.)
-rw-r--r--bot/seasons/evergreen/minesweeper.py26
1 files changed, 25 insertions, 1 deletions
diff --git a/bot/seasons/evergreen/minesweeper.py b/bot/seasons/evergreen/minesweeper.py
index 3540df80..39428000 100644
--- a/bot/seasons/evergreen/minesweeper.py
+++ b/bot/seasons/evergreen/minesweeper.py
@@ -1,4 +1,5 @@
import typing
+from random import random
import discord
from discord.ext import commands
@@ -11,6 +12,29 @@ class Minesweeper(commands.Cog):
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(9)]
+ 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 != 9:
+ to_check.append(board[yt][xt])
+
+ bombs = sum(map(self.is_bomb, to_check))
+ board[y][x] = bombs
+ return board
+
@commands.command(name="minesweeper")
async def minesweeper_command(self, ctx: commands.Context) -> None:
"""Start a game of minesweeper."""
@@ -21,7 +45,7 @@ class Minesweeper(commands.Cog):
return
# Add game to list
-
+ await ctx.send(str(self.generate_board()))
self.games[ctx.author] = {
}