なにこれ
ポケモンのタイプ相性クイズです。
記事の最後にあるコードをpythonで実行すると遊ぶことができます。
Pythonistaに入れればiPhoneでもポチポチできます。
遊び方
実行すると「Electric_to_(Fire_and_Ghost)=?」みたいにきかれます。
何倍かを答えてあげるとよいです(この場合は1が正解です)
ランダムに10問出題されます。最後に10問中何問正解かスコアが表示されます。
動機
ポケモンをやっていたときにタイプ相性が全然覚えられなくて作りました。
当時はまだエンジニアではなく、大学で使っていたPythonとRしか経験がなかったのでPythonを選びました。
ソースコード
何も面白味のないコードですが、よければどうぞ。
pokemon.py
import random
type = ["Normal","Fire","Water","Electric","Grass","Ice","Fighting","Poison","Ground","Flying","Psychic","Bug","Rock","Ghost","Dragon","Dark","Steel","Fairy"]
compatibility = []
compatibility.append([1,1,1,1,1,1,1,1,1,1,1,1,0.5,0,1,1,0.5,1]) # normal
compatibility.append([1,0.5,0.5,1,2,2,1,1,1,1,1,2,0.5,1,0.5,1,2,1]) # fire
compatibility.append([1,2,0.5,1,0.5,1,1,1,2,1,1,1,2,1,0.5,1,1,1]) # water
compatibility.append([1,1,2,0.5,0.5,1,1,1,0,2,1,1,1,1,0.5,1,1,1]) # electric
compatibility.append([1,0.5,2,1,0.5,1,1,0.5,2,0.5,1,0.5,2,1,0.5,1,0.5,1]) # grass
compatibility.append([1,0.5,0.5,1,2,0.5,1,1,2,2,1,1,1,1,2,1,0.5,1]) # ice
compatibility.append([2,1,1,1,1,2,1,0.5,1,0.5,0.5,0.5,2,0,1,2,2,0.5]) # figting
compatibility.append([1,1,1,1,2,1,1,0.5,0.5,1,1,1,0.5,0.5,1,1,0,2]) # poison
compatibility.append([1,2,1,2,0.5,1,1,2,1,0,1,0.5,2,1,1,1,2,1]) # ground
compatibility.append([1,1,1,0.5,2,1,2,1,1,1,1,2,0.5,1,1,1,0.5,1]) # flying
compatibility.append([1,1,1,1,1,1,2,2,1,1,0.5,1,1,1,1,0,0.5,1]) # psychic
compatibility.append([1,0.5,1,1,2,1,0.5,0.5,1,0.5,2,1,1,0.5,1,2,0.5,0.5]) # bug
compatibility.append([1,2,1,1,1,2,0.5,1,0.5,2,1,2,1,1,1,1,0.5,1,1]) # rock
compatibility.append([0,1,1,1,1,1,1,1,1,1,2,1,1,2,1,0.5,1,1]) # ghost
compatibility.append([1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,0.5,0]) # dragon
compatibility.append([1,1,1,1,1,1,0.5,1,1,1,2,1,1,2,1,0.5,1,0.5]) # dark
compatibility.append([1,0.5,0.5,0.5,1,2,1,1,1,1,1,1,2,1,1,1,0.5,2]) # steel
compatibility.append([1,0.5,1,1,1,1,2,0.5,1,1,1,1,1,1,2,2,0.5,1]) # fairy
"""
print("number of type,",len(type))
for i in range(len(compatibility)):
print(len(compatibility[i]))
"""
l = len(compatibility)-1
score = 0
for i in range(10):
a = random.randint(0,l)
b = random.randint(0,l)
c = random.randint(0,l)
#print(a,b,c)
#a,b,c = 1,1,2
print()
print(str(i+1)+"/10")
if b != c :
print(type[a],"_to_(",type[b],"_and_",type[c]," )_=_?")
else :
print(type[a],"_to_",type[b],"_=_?")
answer = float(input())
if b != c:
if answer == float(compatibility[a][b]*compatibility[a][c]):
print("collect!")
score = score + 1
else:
print("wrong!")
print(type[a],"_to_(",type[b],"_and_",type[c],")_=_",str(compatibility[a][b]*compatibility[a][c]))
print(type[a],"_to_",type[b],"_=_",str(compatibility[a][b]))
print(type[a],"_to_",type[c],"_=_",str(compatibility[a][c]))
else:
if answer == float(compatibility[a][b]):
print("collect!")
score = score + 1
else:
print("wrong!")
print(type[a],"_to_",type[b],"_=_",str(compatibility[a][b]))
print(type[a],"_to_",type[b],"_=_",str(compatibility[a][b]))
print()
print("score : ",str(score),"/10")