2019-03-03 05:26:39 Python

Pythonで作成したDiscordのBotでOXゲームしよう的なプログラム 改行コードを用いること

Copy Copied! Full
import discord import random client = discord.Client() start=0 flag=0 name=0 @client.event async def on_ready(): print("logged in as " + client.user.name) @client.event async def on_message(message): if message.author != client.user: global start global name global flag global xy11 global xy21 global xy31 global xy12 global xy22 global xy32 global xy13 global xy23 global xy33 global senkou #ゲーム開始 if message.content == "/ox" and start == 0: start=1 flag=1 await client.send_message(message.channel, message.author.mention +"さんが◯xゲームを開始しました。") name = message.author.name xy11 = "1" xy21 = "2" xy31 = "3" xy12 = "4" xy22 = "5" xy32 = "6" xy13 = "7" xy23 = "8" xy33 = "9" print(message.author.name + "がゲームを開始しました。") #先攻後攻決め0先攻1後攻 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 + "#") print("#"+ xy12 + "#"+ xy22 + "#"+ xy32 + "#") print("#"+ xy13 + "#"+ xy23 + "#"+ xy33 + "#") if senkou == 1 and flag==1: flag=2 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=0 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 + "#") print("#"+ xy12 + "#"+ xy22 + "#"+ xy32 + "#") print("#"+ 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 while (1): s=1 k=9 if flag!=3: break if flag==3: 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のターン") cpu=random.randint(s,k) if cpu==1 and xy11=="1": flag=4 cpu=0 if senkou==1: xy11="◯" break if senkou==0: xy11="x" break elif cpu ==1 and s==1: s==2 if cpu==2 and xy21=="2": flag=4 cpu=0 if senkou==1: xy21="◯" break if senkou==0: xy21="x" break elif cpu==2 and k==2: k=1 elif cpu==2 and s==2: s=3 if cpu==3 and xy31=="3": flag=4 cpu=0 if senkou==1: xy31="◯" break if senkou==0: xy31="x" break elif cpu==3 and k==3: k=2 elif cpu==3 and s==3: s=4 if cpu==4 and xy21=="4": flag=4 cpu=0 if senkou==1: xy21="◯" break if senkou==0: xy21="x" break elif cpu==4 and k==4: k=3 elif cpu==4 and s==4: s=5 if cpu==5 and xy22=="5": flag=4 cpu=0 if senkou==1: xy22="◯" break if senkou==0: xy22="x" break elif cpu==5 and k==5: k=4 elif cpu==5 and s==5: s=6 if cpu==6 and xy32=="6": flag=4 cpu=0 if senkou==1: xy32="◯" break if senkou==0: xy32="x" break elif cpu==6 and k==6: k=5 elif cpu==6 and s==6: s=7 if cpu==7 and xy13=="7": flag=4 cpu=0 if senkou==1: xy13="◯" break if senkou==0: xy13="x" break elif cpu==7 and k==7: k=6 elif cpu==7 and s==7: s=8 if cpu==8 and xy23=="8": flag=4 cpu=0 if senkou==1: xy23="◯" break if senkou==0: xy23="x" break elif cpu==8 and k==8: k=7 elif cpu==8 and s==8: s=9 if cpu==9 and xy33=="9": flag=4 cpu=0 if senkou==1: xy33="◯" break if senkou==0: xy33="x" break elif cpu==9 and k==9: k=8 #勝敗判定 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=0 flag=0 if senkou==1: await client.send_message(message.channel,message.author.mention +"さんの負けです。") start=0 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=0 flag=0 if senkou==0: await client.send_message(message.channel,message.author.mention +"さんの負けです。") start=0 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=0 flag=0 if senkou==1: await client.send_message(message.channel,message.author.mention +"さんの負けです。") start=0 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=0 flag=0 if senkou==0: await client.send_message(message.channel,message.author.mention +"さんの負けです。") start=0 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=0 flag=0 if senkou==1: await client.send_message(message.channel,message.author.mention +"さんの負けです。") start=0 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=0 flag=0 if senkou==0: await client.send_message(message.channel,message.author.mention +"さんの負けです。") start=0 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=0 flag=0 if senkou==1: await client.send_message(message.channel,message.author.mention +"さんの負けです。") start=0 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=0 flag=0 if senkou==0: await client.send_message(message.channel,message.author.mention +"さんの負けです。") start=0 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=0 flag=0 if senkou==1: await client.send_message(message.channel,message.author.mention +"さんの負けです。") start=0 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=0 flag=0 if senkou==0: await client.send_message(message.channel,message.author.mention +"さんの負けです。") start=0 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=0 flag=0 if senkou==1: await client.send_message(message.channel,message.author.mention +"さんの負けです。") start=0 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=0 flag=0 if senkou==0: await client.send_message(message.channel,message.author.mention +"さんの負けです。") start=0 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=0 flag=0 if senkou==1: await client.send_message(message.channel,message.author.mention +"さんの負けです。") start=0 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=0 flag=0 if senkou==0: await client.send_message(message.channel,message.author.mention +"さんの負けです。") start=0 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=0 flag=0 if senkou==1: await client.send_message(message.channel,message.author.mention +"さんの負けです。") start=0 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=0 flag=0 if senkou==0: await client.send_message(message.channel,message.author.mention +"さんの負けです。") start=0 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=0 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 + "#") print("#"+ xy12 + "#"+ xy22 + "#"+ xy32 + "#") print("#"+ xy13 + "#"+ xy23 + "#"+ xy33 + "#") #乱戦防止 if message.content == "/ox" and start == 1 and message.author.name!=name: await client.send_message(message.channel, name + "さんが既にゲームを開始しています。ゲームが終了するまでお待ち下さい。") client.run("token")
Pythonで作成したDiscordのBotでOXゲームしよう的なプログラム
改行コードを用いることで表示が遅くなる問題を解決しました。
無限ループに入っちゃうバグとかも修正しました。
テストプレイは何回もしましたのでだいぶ大丈夫だと思います。
Discordの自作Botの導入方法は各自調べてください。
RECOMMEND