6
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 3 years have passed since last update.

Discordでマインスイーパー作ってみた

Last updated at Posted at 2019-06-15

はじめに

前回、前々回にご紹介したDiscord bot「雲さん」にマインスイーパーを出題してもらう機能を付けました。

Discordには、||hoge||とすることで、クリックするまで黒く塗りつぶされる「伏せ字機能」が搭載されており、「あれ?これマインスイーパーとか作れるんじゃない...?」と天才的なヒラメキを得たと思ったのですが、ぐぐったら既にかなりの方が実装なさっていました笑
折角作ったので公開します。

関数本体

  • 関数の呼び出しはtext.find("級") > -1 or text.find("遊ぼ") > -1のような感じで判定させて使って下さい。
minesweeper.py
def minesweeper(text):
    response_string = ''
    if text.find('低級') > -1:
        difficulty = 0.1
        rows = 8
        clear_range = [3, 4]
        response_string += '[低級]'
    elif text.find('中級') > -1:
        difficulty = 0.15
        rows = 10
        clear_range = [3, 5]
        response_string += '[中級]'
    elif text.find('上級') > -1:
        difficulty = 0.2
        rows = 12
        clear_range = [4, 7]
        response_string += '[上級]'
    elif text.find('超級') > -1:
        difficulty = 0.25
        rows = 12
        clear_range = [4, 6]
        response_string += '[超級]'
    elif text.find('雲さん級') > -1:
        difficulty = 0.3
        rows = 12
        clear_range = [4, 6]
        response_string += '[雲さん級]'
    else:
        difficulty = 0.15
        rows = 10
        clear_range = [3, 5]
        response_string += '[中級]'
    response_string += '\n'
    # 盤面の初期化、および爆弾の設置(100)
    stage = [[100 if random.random()<difficulty else 0 for x in range(rows+1)] for y in range(rows+1)]

    # 爆弾を探索し、周辺マスをカウントアップ
    for x in range(rows):
        for y in range(rows):
            try:
                if stage[x][y] >= 100:
                    stage[x-1][y-1] += 1
                    stage[x][y-1]+=1
                    stage[x+1][y-1] += 1

                    stage[x-1][y] += 1
                    #stage[x][y] += 1
                    stage[x+1][y] += 1
                    
                    stage[x-1][y+1] += 1
                    stage[x][y+1] += 1
                    stage[x+1][y+1] += 1
            except (IndexError) as e:
                pass

    # 伏せ字への変換
    for x in range(rows):
        for y in range(rows):
            if clear_range[0]<=x<=clear_range[1] and clear_range[0]<=y<=clear_range[1]:
                if stage[x][y] == 0:
                    stage[x][y] = ":zero:"
                elif stage[x][y] == 1:
                    stage[x][y] = ":one:"
                elif stage[x][y] == 2:
                    stage[x][y] = ":two:"
                elif stage[x][y] == 3:
                    stage[x][y] = ":three:"
                elif stage[x][y] == 4:
                    stage[x][y] = ":four:"
                elif stage[x][y] == 5:
                    stage[x][y] = ":five:"
                elif stage[x][y] == 6:
                    stage[x][y] = ":six:"
                elif stage[x][y] == 7:
                    stage[x][y] = ":seven:"
                elif stage[x][y] == 8:
                    stage[x][y] = ":eight:"
                elif stage[x][y] >= 100:
                    stage[x][y] = "||:bomb:||"
            else:
                if stage[x][y] >= 100:
                    stage[x][y] = "||:bomb:||"
                elif stage[x][y] == 0:
                    stage[x][y] = "||:zero:||"
                elif stage[x][y] == 1:
                    stage[x][y] = "||:one:||"
                elif stage[x][y] == 2:
                    stage[x][y] = "||:two:||"
                elif stage[x][y] == 3:
                    stage[x][y] = "||:three:||"
                elif stage[x][y] == 4:
                    stage[x][y] = "||:four:||"
                elif stage[x][y] == 5:
                    stage[x][y] = "||:five:||"
                elif stage[x][y] == 6:
                    stage[x][y] = "||:six:||"
                elif stage[x][y] == 7:
                    stage[x][y] = "||:seven:||"
                elif stage[x][y] == 8:
                    stage[x][y] = "||:eight:||"
                elif 9 <= stage[x][y] < 100:
                    return minesweeper(text)
            response_string += str(stage[x][y])
        response_string += "\n"
    return response_string

使い方

  • 雲さんマインスイーパして遊ぼ。上級で!
スクリーンショット 2019-06-16 03.39.02.png スクリーンショット 2019-06-16 03.43.32.png

最後に

そもそもマインスイーパはそんなに熱中してやったことがなかったので知らなかったんですが、運ゲー盤面を生成しないようなアルゴリズムも組めるとかなんとか...?
特に、難易度が上がると爆弾設置の確率が増えるように作っているので、運ゲー盤面が生成されやすいため、いつか挑戦してみたいですね。

6
1
1

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
6
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?