4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PythonでDiscordのテキストチャンネル上にマインスイーパーを生成する

Last updated at Posted at 2019-06-13

#はじめに
Discordのネタバレ防止機能を用いてマインスイーパを作成している方がいたので、Python(discord.py)で実装してみようと思った。その人はC#とかで作っていた気がする。初心者なのでコードがゴミかもしれない。

#ネタバレ防止機能
Discordのテキストチャンネルにおいて、文字を「||」で囲うことで文字を黒く伏せることができる。(例: ||うんち||)
これはクリックするまで見えない。

#どう実装するかのイメージ(例は3*3)
最終的には

||:zero:||||:one:||||:one:||
||:zero:||||:two:||||:bomb:||
||:zero:||||:two:||||:bomb:||

みたいなものが出力されればよいので、

  1. 爆弾の位置だけ指定したリストを作成
    [[0, 0, 0], [0, 0, 1], [0, 0, 0]] #イメージ

  2. 自分の位置から爆弾の見える個数のカウントをする関数作成。数値が1のものを渡すと:bomb:が返る。

  3. 連結して||で挟んだ文字列を出力

出力文字列を張り付けて、いくつかクリックするとこんな感じ。
※画像ではBotから呼び出してます
image.png

#実装

# -*- coding: utf-8 -*-

import random

num = [":zero:", ":one:", ":two:", ":three:", ":four:", ":five:", ":six:", ":seven:", ":eight:", ":nine:"]
base_l = []

def check_bombs(base_l, myself, i, j,width,height):
    if(myself == 1):
        return ":bomb:"
    else:
        if(i == 0):
            if(j == 0):
                ret = base_l[i][j + 1] + base_l[i + 1][j + 1] + base_l[i + 1][j]
                return num[ret]
            elif(j == (width - 1)):
                ret = base_l[i][j - 1] + base_l[i + 1][j - 1] + base_l[i + 1][j]
                return num[ret]
            else:
                ret = base_l[i][j - 1] + base_l[i + 1][j - 1] + base_l[i + 1][j] + base_l[i + 1][j + 1] + base_l[i][j + 1]
                return num[ret]            
        elif(i == (height - 1)):
            if(j == 0):
                ret = base_l[i][j + 1] + base_l[i - 1][j + 1] + base_l[i - 1][j]
                return num[ret]
            elif(j == (width - 1)):
                ret = base_l[i][j - 1] + base_l[i - 1][j - 1] + base_l[i - 1][j]
                return num[ret]
            else:
                ret = base_l[i][j - 1] + base_l[i - 1][j - 1] + base_l[i - 1][j] + base_l[i - 1][j + 1] + base_l[i][j + 1]
                return num[ret]
        else:
            if(j == 0):
                ret = base_l[i + 1][j] + base_l[i + 1][j + 1] + base_l[i][j + 1] + base_l[i - 1][j + 1] + base_l[i - 1][j]
                return num[ret]
            elif(j == (width - 1)):
                ret = base_l[i + 1][j] + base_l[i + 1][j - 1] + base_l[i][j - 1] + base_l[i - 1][j - 1] + base_l[i - 1][j]
                return num[ret]
            else:
                ret = base_l[i + 1][j] + base_l[i + 1][j + 1] + base_l[i][j + 1] + base_l[i - 1][j + 1] + base_l[i - 1][j] + base_l[i - 1][j - 1] + base_l[i][j - 1] + base_l[i + 1][j - 1]
                return num[ret]

def bomb_main(height, width, base):
    re_str = ""
    for i in range(height):
        base_l.append(base[width*i:width*(i+1)])
    for i in range(height):
        for j in range(width):
            myself = base_l[i][j]
            re_str += ("||"+check_bombs(base_l, myself, i, j, width, height)+"||")
        re_str += ("\n")
    return re_str

def main(diff):
    if diff == "normal" :
        width = 10
        height = 10
    elif diff == "hard":
        width = 12
        height = 12
    elif diff == "easy":
        width = 8
        height =8
    bombs = width*height/10 #Percentage of bombs 10%
    base = [1] * int(bombs)
    base += [0] * (int(width) * int(height) - int(bombs))
    random.shuffle(base)
    return bomb_main(height, width, base)
    
print(main("hard"))
4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?