4
4

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でOXゲームをできるbotをPythonで初心者なりに作る

Last updated at Posted at 2019-03-26

DiscordのBotを利用してOXゲーム作りたくね?

経緯

DiscordのBotを作りたいと思いどういったものを作ろうと考えた結果OXゲームが初心者的にいい難易度ではないかと考え作ろうと思った。

はじめに

DiscordでBotを作るにあたって必要だったもの

Pythonを書ける環境

筆者の環境
エディタ:Atom
使用言語:Python3

DiscordのBotアカウントの作成

こちらに関しては、@drumath2237さんの記事を参考にさせていただきました。

作成するOXゲームのルール

1.OXゲームのルールは3x3の格子にOとXを交互に置き、自分のマークを3つ並べたら勝ち
2.CPUとの対決とし、先攻後攻はランダムで決める
3.対戦中他の人がゲームを始めれないようにする

pythonには乱数を使用できるモジュールがありそれを利用して先攻後攻やCPUの動きを作ることにした。

完成したコード(長いので軽く目を通す程度でいいです。)

ソースコード(折りたたんでいます)
 
import discord
import random

client = discord.Client()
start = flag = name=0

@client.event
async def on_ready():
    print("ready " + client.user.name)

@client.event
async def on_message(message):
    if message.author != client.user:
        global start,name,flag,xy11,xy21,xy31,xy12,xy22,xy32,xy13,xy23,xy33,senkou,kr,s,k
        if message.content == "/ox" and start == 0:#ゲーム開始
            start = flag =1
            await client.send_message(message.channel, message.author.mention +"さんが◯xゲームを開始しました。")
            name = message.author.name
            xy11,xy21,xy31,xy12,xy22,xy32,xy13,xy23,xy33="1","2","3","4","5","6","7","8","9"
            print(message.author.name + "がゲームを開始しました。")
            senkou = random.randint(0,1)#先攻後攻決め

            if senkou == 0 and flag ==1:
                flag=2
                print("プレイヤーが先攻")
                await client.send_message(message.channel, message.author.mention +"は先攻です")
                await client.send_message(message.channel, xy11 + "┃" + xy21 + "┃" + xy31 + "\r\n━╋━╋━\r\n" + xy12 + "┃" + xy22 + "┃" + xy32 + "\r\n━╋━╋━\r\n" + xy13 + "┃" + xy23 + "┃" + xy33)
                await client.send_message(message.channel, message.author.mention +"さん1~9を選択してください。")
                print("#"+ xy11+ "#"+ xy21+ "#"+ xy31 + "#\r\n#"+ xy12+ "#"+ xy22+ "#"+ xy32+ "#\r\n#"+xy13 + "#"+ xy23+ "#"+ xy33+ "#")
            if senkou == 1 and flag==1:
                print("プレイヤーが後攻")
                cpu=random.randint(1,9)
                if cpu == 1: xy11 ="◯"
                if cpu == 2: xy21 ="◯"
                if cpu == 3: xy31 ="◯"
                if cpu == 4: xy12 ="◯"
                if cpu == 5: xy22 ="◯"
                if cpu == 6: xy32 ="◯"
                if cpu == 7: xy13 ="◯"
                if cpu == 8: xy23 ="◯"
                if cpu == 9: xy33 ="◯"
                cpu,flag= 0,2
                await client.send_message(message.channel, message.author.mention +"は後攻です")
                await client.send_message(message.channel, xy11 + "┃" + xy21 + "┃" + xy31 + "\r\n━╋━╋━\r\n" + xy12 + "┃" + xy22 + "┃" + xy32 + "\r\n━╋━╋━\r\n" + xy13 + "┃" + xy23 + "┃" + xy33)
                await client.send_message(message.channel, message.author.mention +"さん1~9を選択してください。")
                print("#"+ xy11+ "#"+ xy21+ "#"+ xy31 + "#\r\n#"+ xy12+ "#"+ xy22+ "#"+ xy32+ "#\r\n#"+xy13 + "#"+ xy23+ "#"+ xy33+ "#")

        #プレイヤーマス選択
        if message.content == "1" and name==message.author.name and flag==2 and xy11=="1":
            flag=3
            print("1に設置")
            if senkou==0: xy11="◯"
            if senkou==1: xy11="x"
        if message.content == "1" and name==message.author.name and flag==2 and xy11!="1":
            await client.send_message(message.channel, "そこには置けません。")
            await client.delete_message(message)
        if message.content == "2" and name==message.author.name and flag==2 and xy21=="2":
            flag=3
            print("2に設置")
            if senkou==0: xy21="◯"
            if senkou==1: xy21="x"
        if message.content == "2" and name==message.author.name and flag==2 and xy21!="2":
            await client.send_message(message.channel, "そこには置けません。")
            await client.delete_message(message)
        if message.content == "3" and name==message.author.name and flag==2 and xy31=="3":
            flag=3
            print("3に設置")
            if senkou==0: xy31="◯"
            if senkou==1: xy31="x"
        if message.content == "3" and name==message.author.name and flag==2 and xy31!="3":
            await client.send_message(message.channel, "そこには置けません。")
            await client.delete_message(message)
        if message.content == "4" and name==message.author.name and flag==2 and xy12=="4":
            flag=3
            print("4に設置")
            if senkou==0: xy12="◯"
            if senkou==1: xy12="x"
        if message.content == "4" and name==message.author.name and flag==2 and xy12!="4":
            await client.send_message(message.channel, "そこには置けません。")
            await client.delete_message(message)
        if message.content == "5" and name==message.author.name and flag==2 and xy22=="5":
            flag=3
            print("5に設置")
            if senkou==0: xy22="◯"
            if senkou==1: xy22="x"
        if message.content == "5" and name==message.author.name and flag==2 and xy22!="5":
            await client.send_message(message.channel, "そこには置けません。")
            await client.delete_message(message)
        if message.content == "6" and name==message.author.name and flag==2 and xy32=="6":
            flag=3
            print("6に設置")
            if senkou==0: xy32="◯"
            if senkou==1: xy32="x"
        if message.content == "6" and name==message.author.name and flag==2 and xy32!="6":
            await client.send_message(message.channel, "そこには置けません。")
            await client.delete_message(message)
        if message.content == "7" and name==message.author.name and flag==2 and xy13=="7":
            flag=3
            print("7に設置")
            if senkou==0: xy13="◯"
            if senkou==1: xy13="x"
        if message.content == "7" and name==message.author.name and flag==2 and xy13!="7":
            await client.send_message(message.channel, "そこには置けません。")
            await client.delete_message(message)
        if message.content == "8" and name==message.author.name and flag==2 and xy23=="8":
            flag=3
            print("8に設置")
            if senkou==0: xy23="◯"
            if senkou==1: xy23="x"
        if message.content == "8" and name==message.author.name and flag==2 and xy23!="8":
            await client.send_message(message.channel, "そこには置けません。")
            await client.delete_message(message)
        if message.content == "9" and name==message.author.name and flag==2 and xy33=="9":
            print("9に設置")
            flag=3
            if senkou==0: xy33="◯"
            if senkou==1: xy33="x"
        if message.content == "9" and name==message.author.name and flag==2 and xy33!="9":
            await client.send_message(message.channel, "そこには置けません。")
            await client.delete_message(message)

        #CPU
        if flag==2:
            kr=0
        while (1):
            if flag!=3: break
            if flag==3:#◯◯やxxが見つかった場合となりに置く
                if xy11!="1" and xy12!="4" and xy13!="7" and xy21!="2" and xy22!="5" and xy23!="8" and xy31!="3"  and xy32!="6" and xy33!="9": break
                print("CPUのターン")
                if xy11=="◯":
                    if xy21=="◯" and xy31=="3":
                        flag=4
                        if senkou==1:
                            xy31="◯"
                            break
                        if senkou==0:
                            xy31="x"
                            break
                    if xy31=="◯" and xy21=="2":
                        flag=4
                        if senkou==1:
                            xy21="◯"
                            break
                        if senkou==0:
                            xy21="x"
                            break
                    if xy12=="◯" and xy13=="7":
                        flag=4
                        if senkou==1:
                            xy13="◯"
                            break
                        if senkou==0:
                            xy13="x"
                            break
                    if xy13=="◯" and xy12=="4":
                        flag=4
                        if senkou==1:
                            xy12="◯"
                            break
                        if senkou==0:
                            xy12="x"
                            break
                    if xy22=="◯" and xy33=="9":
                        flag=4
                        if senkou==1:
                            xy33="◯"
                            break
                        if senkou==0:
                            xy33="x"
                            break
                    if xy33=="◯" and xy22=="5":
                        flag=4
                        if senkou==1:
                            xy22="◯"
                            break
                        if senkou==0:
                            xy22="x"
                            break
                if xy11=="x":
                    if xy21=="x" and xy31=="3":
                        flag=4
                        if senkou==1:
                            xy31="◯"
                            break
                        if senkou==0:
                            xy31="x"
                            break
                    if xy31=="x" and xy21=="2":
                        flag=4
                        if senkou==1:
                            xy21="◯"
                            break
                        if senkou==0:
                            xy21="x"
                            break
                    if xy12=="x" and xy13=="7":
                        flag=4
                        if senkou==1:
                            xy13="◯"
                            break
                        if senkou==0:
                            xy13="x"
                            break
                    if xy13=="x" and xy12=="4":
                        flag=4
                        if senkou==1:
                            xy12="◯"
                            break
                        if senkou==0:
                            xy12="x"
                            break
                    if xy22=="x" and xy33=="9":
                        flag=4
                        if senkou==1:
                            xy33="◯"
                            break
                        if senkou==0:
                            xy33="x"
                            break
                    if xy33=="x" and xy22=="5":
                        flag=4
                        if senkou==1:
                            xy22="◯"
                            break
                        if senkou==0:
                            xy22="x"
                            break

                if xy21=="◯":
                    if xy31=="◯" and xy11=="1":
                        flag=4
                        if senkou==1:
                            xy11="◯"
                            break
                        if senkou==0:
                            xy11="x"
                            break
                    if xy22=="◯" and xy23=="8":
                        flag=4
                        if senkou==1:
                            xy23="◯"
                            break
                        if senkou==0:
                            xy23="x"
                            break
                    if xy23=="◯" and xy22=="5":
                        flag=4
                        if senkou==1:
                            xy22="◯"
                            break
                        if senkou==0:
                            xy22="x"
                            break
                if xy21=="x":
                    if xy31=="x" and xy11=="1":
                        flag=4
                        if senkou==1:
                            xy11="◯"
                            break
                        if senkou==0:
                            xy11="x"
                            break
                    if xy22=="x" and xy23=="8":
                        flag=4
                        if senkou==1:
                            xy23="◯"
                            break
                        if senkou==0:
                            xy23="x"
                            break
                    if xy23=="x" and xy22=="5":
                        flag=4
                        if senkou==1:
                            xy22="◯"
                            break
                        if senkou==0:
                            xy22="x"
                            break

                if xy12=="◯":
                    if xy13=="◯" and xy11=="1":
                        flag=4
                        if senkou==1:
                            xy11="◯"
                            break
                        if senkou==0:
                            xy11="x"
                            break
                    if xy22=="◯" and xy32=="6":
                        flag=4
                        if senkou==1:
                            xy32="◯"
                            break
                        if senkou==0:
                            xy32="x"
                            break
                    if xy32=="◯" and xy22=="5":
                        flag=4
                        if senkou==1:
                            xy22="◯"
                            break
                        if senkou==0:
                            xy22="x"
                            break
                if xy12=="x":
                    if xy13=="x" and xy11=="1":
                        flag=4
                        if senkou==1:
                            xy11="◯"
                            break
                        if senkou==0:
                            xy11="x"
                            break
                    if xy22=="x" and xy32=="6":
                        flag=4
                        if senkou==1:
                            xy32="◯"
                            break
                        if senkou==0:
                            xy32="x"
                            break
                    if xy32=="x" and xy22=="5":
                        flag=4
                        if senkou==1:
                            xy22="◯"
                            break
                        if senkou==0:
                            xy22="x"
                            break

                if xy31=="◯":
                    if xy32=="◯" and xy33=="9":
                        flag=4
                        if senkou==1:
                            xy33="◯"
                            break
                        if senkou==0:
                            xy33="x"
                            break
                    if xy33=="◯" and xy32=="6":
                        flag=4
                        if senkou==1:
                            xy32="◯"
                            break
                        if senkou==0:
                            xy32="x"
                            break
                    if xy22=="◯" and xy13=="7":
                        flag=4
                        if senkou==1:
                            xy13="◯"
                            break
                        if senkou==0:
                            xy13="x"
                            break
                    if xy13=="◯" and xy22=="5":
                        flag=4
                        if senkou==1:
                            xy22="◯"
                            break
                        if senkou==0:
                            xy22="x"
                            break
                if xy31=="x":
                    if xy32=="x" and xy33=="9":
                        flag=4
                        if senkou==1:
                            xy33="◯"
                            break
                        if senkou==0:
                            xy33="x"
                            break
                    if xy33=="x" and xy32=="6":
                        flag=4
                        if senkou==1:
                            xy32="◯"
                            break
                        if senkou==0:
                            xy32="x"
                            break
                    if xy22=="x" and xy13=="7":
                        flag=4
                        if senkou==1:
                            xy13="◯"
                            break
                        if senkou==0:
                            xy13="x"
                            break
                    if xy13=="x" and xy22=="5":
                        flag=4
                        if senkou==1:
                            xy22="◯"
                            break
                        if senkou==0:
                            xy22="x"
                            break

                if xy22=="◯":
                    if xy23=="◯" and xy21=="2":
                        flag=4
                        if senkou==1:
                            xy21="◯"
                            break
                        if senkou==0:
                            xy21="x"
                            break
                    if xy13=="◯" and xy31=="3":
                        flag=4
                        if senkou==1:
                            xy31="◯"
                            break
                        if senkou==0:
                            xy31="x"
                            break
                    if xy33=="◯" and xy11=="1":
                        flag=4
                        if senkou==1:
                            xy11="◯"
                            break
                        if senkou==0:
                            xy11="x"
                            break
                    if xy32=="◯" and xy12=="4":
                        flag=4
                        if senkou==1:
                            xy12="◯"
                            break
                        if senkou==0:
                            xy12="x"
                            break
                if xy22=="x":
                    if xy23=="x" and xy21=="2":
                        flag=4
                        if senkou==1:
                            xy21="◯"
                            break
                        if senkou==0:
                            xy21="x"
                            break
                    if xy13=="x" and xy31=="3":
                        flag=4
                        if senkou==1:
                            xy31="◯"
                            break
                        if senkou==0:
                            xy31="x"
                            break
                    if xy33=="x" and xy11=="1":
                        flag=4
                        if senkou==1:
                            xy11="◯"
                            break
                        if senkou==0:
                            xy11="x"
                            break
                    if xy32=="x" and xy12=="4":
                        flag=4
                        if senkou==1:
                            xy12="◯"
                            break
                        if senkou==0:
                            xy12="x"
                            break

                if xy32=="◯":
                    if xy33=="◯" and xy31=="3":
                        flag=4
                        if senkou==1:
                            xy31="◯"
                            break
                        if senkou==0:
                            xy31="x"
                            break
                if xy32=="x":
                    if xy33=="x" and xy31=="3":
                        flag=4
                        if senkou==1:
                            xy31="◯"
                            break
                        if senkou==0:
                            xy31="x"
                            break

                if xy13=="◯":
                    if xy23=="◯" and xy33=="9":
                        flag=4
                        if senkou==1:
                            xy33="◯"
                            break
                        if senkou==0:
                            xy33="x"
                            break
                    if xy33=="◯" and xy23=="8":
                        flag=4
                        if senkou==1:
                            xy23="◯"
                            break
                        if senkou==0:
                            xy23="x"
                            break
                if xy13=="x":
                    if xy23=="x" and xy33=="9":
                        flag=4
                        if senkou==1:
                            xy33="◯"
                            break
                        if senkou==0:
                            xy33="x"
                            break
                    if xy33=="x" and xy23=="8":
                        flag=4
                        if senkou==1:
                            xy23="◯"
                            break
                        if senkou==0:
                            xy23="x"
                            break

                if xy23=="◯":
                    if xy33=="◯" and xy13=="7":
                        flag=4
                        if senkou==1:
                            xy13="◯"
                            break
                        if senkou==0:
                            xy13="x"
                            break
                if xy23=="x":
                    if xy33=="x" and xy13=="7":
                        flag=4
                        if senkou==1:
                            xy13="◯"
                            break
                        if senkou==0:
                            xy13="x"
                            break

                cpu=random.randint(1,9)#連続するものが見つからなかった場合
                if cpu==1 and xy11=="1":
                    flag,cpu= 4,0
                    if senkou==1:
                        xy11="◯"
                        break
                    if senkou==0:
                        xy11="x"
                        break
                if cpu==2 and xy21=="2":
                    flag,cpu= 4,0
                    if senkou==1:
                        xy21="◯"
                        break
                    if senkou==0:
                        xy21="x"
                        break
                if cpu==3 and xy31=="3":
                    flag,cpu= 4,0
                    if senkou==1:
                        xy31="◯"
                        break
                    if senkou==0:
                        xy31="x"
                        break
                if cpu==4 and xy21=="4":
                    flag,cpu= 4,0
                    if senkou==1:
                        xy21="◯"
                        break
                    if senkou==0:
                        xy21="x"
                        break
                if cpu==5 and xy22=="5":
                    flag,cpu= 4,0
                    if senkou==1:
                        xy22="◯"
                        break
                    if senkou==0:
                        xy22="x"
                        break
                if cpu==6 and xy32=="6":
                    flag,cpu= 4,0
                    if senkou==1:
                        xy32="◯"
                        break
                    if senkou==0:
                        xy32="x"
                        break
                if cpu==7 and xy13=="7":
                    flag,cpu= 4,0
                    if senkou==1:
                        xy13="◯"
                        break
                    if senkou==0:
                        xy13="x"
                        break
                if cpu==8 and xy23=="8":
                    flag,cpu= 4,0
                    if senkou==1:
                        xy23="◯"
                        break
                    if senkou==0:
                        xy23="x"
                        break
                if cpu==9 and xy33=="9":
                    flag,cpu= 4,0
                    if senkou==1:
                        xy33="◯"
                        break
                    if senkou==0:
                        xy33="x"
                        break
                kr +=1
                if kr>500:
                    await client.send_message(message.channel, "エラーが発生しました。ゲームを終了します。")
                    print("エラー")
                    start,flag,name= 0,0,""
        #勝敗判定
        if start==1 and xy11=="◯" and xy21=="◯" and xy31=="◯":
            await client.send_message(message.channel, xy11 + "┃" + xy21 + "┃" + xy31 + "\r\n━╋━╋━\r\n" + xy12 + "┃" + xy22 + "┃" + xy32 + "\r\n━╋━╋━\r\n" + xy13 + "┃" + xy23 + "┃" + xy33)
            print("ゲーム終了")
            if senkou==0:
                await client.send_message(message.channel,message.author.mention +"さんの勝利です。")
                start= flag= 0
            if senkou==1:
                await client.send_message(message.channel,message.author.mention +"さんの負けです。")
                start= flag= 0
        if start==1 and xy11=="x" and xy21=="x" and xy31=="x":
            await client.send_message(message.channel, xy11 + "┃" + xy21 + "┃" + xy31 + "\r\n━╋━╋━\r\n" + xy12 + "┃" + xy22 + "┃" + xy32 + "\r\n━╋━╋━\r\n" + xy13 + "┃" + xy23 + "┃" + xy33)
            print("ゲーム終了")
            if senkou==1:
                await client.send_message(message.channel,message.author.mention +"さんの勝利です。")
                start= flag= 0
            if senkou==0:
                await client.send_message(message.channel,message.author.mention +"さんの負けです。")
                start= flag= 0
        if start==1 and xy12=="◯" and xy22=="◯" and xy32=="◯":
            await client.send_message(message.channel, xy11 + "┃" + xy21 + "┃" + xy31 + "\r\n━╋━╋━\r\n" + xy12 + "┃" + xy22 + "┃" + xy32 + "\r\n━╋━╋━\r\n" + xy13 + "┃" + xy23 + "┃" + xy33)
            print("ゲーム終了")
            if senkou==0:
                await client.send_message(message.channel,message.author.mention +"さんの勝利です。")
                start= flag= 0
            if senkou==1:
                await client.send_message(message.channel,message.author.mention +"さんの負けです。")
                start= flag= 0
        if start==1 and xy12=="x" and xy22=="x" and xy32=="x":
            await client.send_message(message.channel, xy11 + "┃" + xy21 + "┃" + xy31 + "\r\n━╋━╋━\r\n" + xy12 + "┃" + xy22 + "┃" + xy32 + "\r\n━╋━╋━\r\n" + xy13 + "┃" + xy23 + "┃" + xy33)
            print("ゲーム終了")
            if senkou==1:
                await client.send_message(message.channel,message.author.mention +"さんの勝利です。")
                start= flag= 0
            if senkou==0:
                await client.send_message(message.channel,message.author.mention +"さんの負けです。")
                start= flag= 0
        if start==1 and xy13=="◯" and xy23=="◯" and xy33=="◯":
            await client.send_message(message.channel, xy11 + "┃" + xy21 + "┃" + xy31 + "\r\n━╋━╋━\r\n" + xy12 + "┃" + xy22 + "┃" + xy32 + "\r\n━╋━╋━\r\n" + xy13 + "┃" + xy23 + "┃" + xy33)
            print("ゲーム終了")
            if senkou==0:
                await client.send_message(message.channel,message.author.mention +"さんの勝利です。")
                start= flag= 0
            if senkou==1:
                await client.send_message(message.channel,message.author.mention +"さんの負けです。")
                start= flag= 0
        if start==1 and xy13=="x" and xy23=="x" and xy33=="x":
            await client.send_message(message.channel, xy11 + "┃" + xy21 + "┃" + xy31 + "\r\n━╋━╋━\r\n" + xy12 + "┃" + xy22 + "┃" + xy32 + "\r\n━╋━╋━\r\n" + xy13 + "┃" + xy23 + "┃" + xy33)
            print("ゲーム終了")
            if senkou==1:
                await client.send_message(message.channel,message.author.mention +"さんの勝利です。")
                start= flag= 0
            if senkou==0:
                await client.send_message(message.channel,message.author.mention +"さんの負けです。")
                start= flag= 0
        if start==1 and xy11=="◯" and xy12=="◯" and xy13=="◯":
            await client.send_message(message.channel, xy11 + "┃" + xy21 + "┃" + xy31 + "\r\n━╋━╋━\r\n" + xy12 + "┃" + xy22 + "┃" + xy32 + "\r\n━╋━╋━\r\n" + xy13 + "┃" + xy23 + "┃" + xy33)
            print("ゲーム終了")
            if senkou==0:
                await client.send_message(message.channel,message.author.mention +"さんの勝利です。")
                start= flag= 0
            if senkou==1:
                await client.send_message(message.channel,message.author.mention +"さんの負けです。")
                start= flag= 0
        if start==1 and xy11=="x" and xy12=="x" and xy13=="x":
            await client.send_message(message.channel, xy11 + "┃" + xy21 + "┃" + xy31 + "\r\n━╋━╋━\r\n" + xy12 + "┃" + xy22 + "┃" + xy32 + "\r\n━╋━╋━\r\n" + xy13 + "┃" + xy23 + "┃" + xy33)
            print("ゲーム終了")
            if senkou==1:
                await client.send_message(message.channel,message.author.mention +"さんの勝利です。")
                start= flag= 0
            if senkou==0:
                await client.send_message(message.channel,message.author.mention +"さんの負けです。")
                start= flag= 0
        if start==1 and xy21=="◯" and xy22=="◯" and xy23=="◯":
            await client.send_message(message.channel, xy11 + "┃" + xy21 + "┃" + xy31 + "\r\n━╋━╋━\r\n" + xy12 + "┃" + xy22 + "┃" + xy32 + "\r\n━╋━╋━\r\n" + xy13 + "┃" + xy23 + "┃" + xy33)
            print("ゲーム終了")
            if senkou==0:
                await client.send_message(message.channel,message.author.mention +"さんの勝利です。")
                start= flag= 0
            if senkou==1:
                await client.send_message(message.channel,message.author.mention +"さんの負けです。")
                start= flag= 0
        if start==1 and xy21=="x" and xy22=="x" and xy23=="x":
            await client.send_message(message.channel, xy11 + "┃" + xy21 + "┃" + xy31 + "\r\n━╋━╋━\r\n" + xy12 + "┃" + xy22 + "┃" + xy32 + "\r\n━╋━╋━\r\n" + xy13 + "┃" + xy23 + "┃" + xy33)
            print("ゲーム終了")
            if senkou==1:
                await client.send_message(message.channel,message.author.mention +"さんの勝利です。")
                start= flag= 0
            if senkou==0:
                await client.send_message(message.channel,message.author.mention +"さんの負けです。")
                start= flag= 0
        if start==1 and xy31=="◯" and xy32=="◯" and xy33=="◯":
            await client.send_message(message.channel, xy11 + "┃" + xy21 + "┃" + xy31 + "\r\n━╋━╋━\r\n" + xy12 + "┃" + xy22 + "┃" + xy32 + "\r\n━╋━╋━\r\n" + xy13 + "┃" + xy23 + "┃" + xy33)
            print("ゲーム終了")
            if senkou==0:
                await client.send_message(message.channel,message.author.mention +"さんの勝利です。")
                start= flag= 0
            if senkou==1:
                await client.send_message(message.channel,message.author.mention +"さんの負けです。")
                start= flag= 0
        if start==1 and xy31=="x" and xy32=="x" and xy33=="x":
            await client.send_message(message.channel, xy11 + "┃" + xy21 + "┃" + xy31 + "\r\n━╋━╋━\r\n" + xy12 + "┃" + xy22 + "┃" + xy32 + "\r\n━╋━╋━\r\n" + xy13 + "┃" + xy23 + "┃" + xy33)
            print("ゲーム終了")
            if senkou==1:
                await client.send_message(message.channel,message.author.mention +"さんの勝利です。")
                start= flag= 0
            if senkou==0:
                await client.send_message(message.channel,message.author.mention +"さんの負けです。")
                start= flag= 0
        if start==1 and xy11=="◯" and xy22=="◯" and xy33=="◯":
            await client.send_message(message.channel, xy11 + "┃" + xy21 + "┃" + xy31 + "\r\n━╋━╋━\r\n" + xy12 + "┃" + xy22 + "┃" + xy32 + "\r\n━╋━╋━\r\n" + xy13 + "┃" + xy23 + "┃" + xy33)
            print("ゲーム終了")
            if senkou==0:
                await client.send_message(message.channel,message.author.mention +"さんの勝利です。")
                start= flag= 0
            if senkou==1:
                await client.send_message(message.channel,message.author.mention +"さんの負けです。")
                start= flag= 0
        if start==1 and xy11=="x" and xy22=="x" and xy33=="x":
            await client.send_message(message.channel, xy11 + "┃" + xy21 + "┃" + xy31 + "\r\n━╋━╋━\r\n" + xy12 + "┃" + xy22 + "┃" + xy32 + "\r\n━╋━╋━\r\n" + xy13 + "┃" + xy23 + "┃" + xy33)
            print("ゲーム終了")
            if senkou==1:
                await client.send_message(message.channel,message.author.mention +"さんの勝利です。")
                start= flag= 0
            if senkou==0:
                await client.send_message(message.channel,message.author.mention +"さんの負けです。")
                start= flag= 0
        if start==1 and xy31=="◯" and xy22=="◯" and xy13=="◯":
            await client.send_message(message.channel, xy11 + "┃" + xy21 + "┃" + xy31 + "\r\n━╋━╋━\r\n" + xy12 + "┃" + xy22 + "┃" + xy32 + "\r\n━╋━╋━\r\n" + xy13 + "┃" + xy23 + "┃" + xy33)
            print("ゲーム終了")
            if senkou==0:
                await client.send_message(message.channel,message.author.mention +"さんの勝利です。")
                start= flag= 0
            if senkou==1:
                await client.send_message(message.channel,message.author.mention +"さんの負けです。")
                start= flag= 0
        if start==1 and xy31=="x" and xy22=="x" and xy13=="x":
            await client.send_message(message.channel, xy11 + "┃" + xy21 + "┃" + xy31 + "\r\n━╋━╋━\r\n" + xy12 + "┃" + xy22 + "┃" + xy32 + "\r\n━╋━╋━\r\n" + xy13 + "┃" + xy23 + "┃" + xy33)
            print("ゲーム終了")
            if senkou==1:
                await client.send_message(message.channel,message.author.mention +"さんの勝利です。")
                start= flag= 0
            if senkou==0:
                await client.send_message(message.channel,message.author.mention +"さんの負けです。")
                start= flag= 0

        if start==1 and xy11!="1" and xy12!="4" and xy13!="7" and xy21!="2" and xy22!="5" and xy23!="8" and xy31!="3"  and xy32!="6" and xy33!="9":#引き分け判定
            await client.send_message(message.channel, xy11 + "┃" + xy21 + "┃" + xy31 + "\r\n━╋━╋━\r\n" + xy12 + "┃" + xy22 + "┃" + xy32 + "\r\n━╋━╋━\r\n" + xy13 + "┃" + xy23 + "┃" + xy33)
            await client.send_message(message.channel,"引き分けです。")
            print("ゲーム終了")
            start= flag= 0
        if flag==4 and start==1:#プレイヤーのターン
            flag=2
            await client.send_message(message.channel, xy11 + "┃" + xy21 + "┃" + xy31 + "\r\n━╋━╋━\r\n" + xy12 + "┃" + xy22 + "┃" + xy32 + "\r\n━╋━╋━\r\n" + xy13 + "┃" + xy23 + "┃" + xy33)
            await client.send_message(message.channel, message.author.mention +"さん1~9を選択してください。")
            print("#"+ xy11+ "#"+ xy21+ "#"+ xy31 + "#\r\n#"+ xy12+ "#"+ xy22+ "#"+ xy32+ "#\r\n#"+xy13 + "#"+ xy23+ "#"+ xy33+ "#")
        if message.content == "/ox" and start == 1 and message.author.name!=name: await client.send_message(message.channel, name + "さんが既にゲームを開始しています。ゲームが終了するまでお待ち下さい。")
        if message.content == "/reset" and start == 1 and message.author.name==name:#エラーした場合のリセット
            await client.send_message(message.channel, "ゲームをリセットします。")
            print("ゲームがリセットされました。")
            start,flag,name= 0,0,""

client.run("Your token")

解説

作成中に起きた問題

連投するとどうしてもクールタイムが発生してしまう。

OXゲームのため3x3の格子は毎回表示しなければならないし先攻後攻など表示するなど
メッセージを大量に送らなければならないが一気に送ると仕様なのかメッセージが全部表示されるのに時間がかかってしまう。

解決法

こちらに関しては改行を使うことで一気に格子を書き込むことに成功した。
使用しているところ

ランダムで選ぶためどうしても残り1が選べない
kr +=1
if kr>500:
    await client.send_message(message.channel, "エラーが発生しました。ゲームを終了します。")
    print("エラー")
    start,flag,name= 0,0,""
解決法

1か9が出たら次は2から8までみたいに範囲を狭めるなど考えましたがうまく行かなかったため
長く選ぶようだったらエラーとして強制終了にすることにしました。
理由としては2つ並んだ際に3つ目に置くといった処理にしたところ発生する可能性が大幅に下がったからです。

await client.send_message(message.channel, xy11 + "┃" + xy21 + "┃" + xy31 + "\r\n━╋━╋━\r\n" + xy12 + "┃" + xy22 + "┃" + xy32 + "\r\n━╋━╋━\r\n" + xy13 + "┃" + xy23 + "┃" + xy33)

システム面

3x3のマスを選択する際にどのような手段をとったかというと左上から横に1~9の数字を入力してもらうことにしました。(一部から抜粋)

if message.content == "1" and name==message.author.name and flag==2 and xy11=="1":
    flag=3
    print("1に設置")
    if senkou==0: xy11="◯"
    if senkou==1: xy11="x"

CPUの動き

最初はランダムに設置としていたのですが、プレイした際につまらなかったため2つ並んでる際に3つ目の場所に置くように改良しました。(全パターンを探すためここがソースを長くしてしまった原因)
(一部から抜粋)

if xy11=="◯":
    if xy21=="◯" and xy31=="3":
        flag=4
        if senkou==1:
            xy31="◯"
            break
        if senkou==0:
            xy31="x"
            break

ランダムに数字を選択しそこが開いてるか確認
空いてなかったらまた選び直すといった感じ(一部から抜粋)

cpu=random.randint(1,9)#連続するものが見つからなかった場合
if cpu==1 and xy11=="1":
    flag,cpu= 4,0
    if senkou==1:
        xy11="◯"
        break
    if senkou==0:
         xy11="x"
         break

勝敗判定

こちらもいい方法が思いつかなかったので全パターンチェックしてます。
これもスパゲティソースの理由(一部から抜粋)

if start==1 and xy11=="◯" and xy21=="◯" and xy31=="◯":
    await client.send_message(message.channel, xy11 + "┃" + xy21 + "┃" + xy31 + "\r\n━╋━╋━\r\n" + xy12 + "┃" + xy22 + "┃" + xy32 + "\r\n━╋━╋━\r\n" + xy13 + "┃" + xy23 + "┃" + xy33)
    print("ゲーム終了")
    if senkou==0:
        await client.send_message(message.channel,message.author.mention +"さんの勝利です。")
        start= flag= 0
    if senkou==1:
        await client.send_message(message.channel,message.author.mention +"さんの負けです。")
        start= flag= 0

重複阻止処理

見たままゲームが開始されてるフラグでありゲームをプレイしている人以外出ない場合行う処理

if message.content == "/ox" and start == 1 and message.author.name!=name:
    await client.send_message(message.channel, name + "さんが既にゲームを開始しています。ゲームが終了するまでお待ち下さい。")

リセット処理

誰かが放置しちゃった場合など様々な用途で使えるように作った機能

if message.content == "/reset" and start == 1 and message.author.name==name:#エラーした場合のリセット
    await client.send_message(message.channel, "ゲームをリセットします。")
    print("ゲームがリセットされました。")
    start,flag,name= 0,0,""

動作画面

cap.png
caca.PNG

まとめ

今回DiscordBotでOXゲームを作るという点ではちゃんと動作するものを作成できたのでとても満足した。
しかし、勝敗判定などでとても長い処理を書いているのでどうにか効率かさせるべく精進して勉強しないといけないなと噛み締めた次第です。
クラスなど使ってない理由は理解ができてないからでもあります。
また、なにか気になる点やアドバイスなどありましたら遠慮なく書き込んでください。

4
4
6

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
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?