Python スロットマシンを作ってみる

pythonで空がとべないとわかっているのに、ずっと空を飛ぶ方法を探す病気をなおしたい。

スポンサーリンク

pythonでスロットマシンをつくるには?

スロットマシンの仕組み自体は簡単なのですが、ここで問題となってくるのが「確率」です。

pythonには実はnumpyモジュールを使うことによって、「リストの中から確率でえらぶ」ということが簡単にできるようになっているようです。

スポンサーリンク

スロットマシンのサンプルコード

import numpy as np

coin = 150
rell = 0
while True:
    input("try?: ")
    coin -= 3
    rell += 1
    print("coin: " + str(coin) + "rell: " + str(rell))
    a = np.random.choice(['replay', 'chery', 'grape', 'piero', 'bell', 'bar', 'big'])
    b = np.random.choice(['replay', 'chery', 'grape', 'piero', 'bell', 'bar', 'big'])
    c = np.random.choice(['replay', 'chery', 'grape', 'piero', 'bell', 'bar', 'big'])

    print(a + "-" + b + "-" + c)
    if a == "replay" and b == "replay" and c == "replay":
        coin += 3
        print("replay")
        print(coin)
    if a == "chery" and b == "chery" and c == "chery":
        coin += 2
        print("chery")
        print(coin)
    if a == "grape" and b == "grape" and c == "grape":
        coin += 7
        print("grape")
        print(coin)
    if a == "piero" and b == "piero" and c == "piero":
        coin += 10
        print("piero")
        print(coin)
    if a == "bell" and b == "bell" and c == "bell":
        coin += 14
        print("bell")
        print(coin)
    if a == "bar" and b == "bar" and c == "bar":
        coin += 110
        print("bar")
        print(coin)
    if a == "big" and b == "big" and c == "big":
        coin += 332
        print("bell")
        print(coin)
    if coin == 0:
        print("GAME OVER!!!!!!")
        break

    print("はずれ")

もうしわけございません。このコードは、欠陥ありのコードです。未完成です。

スロットマシンの仕様はネットで公開されていますが、そこに書かれているのは「〇分の1」の確率である子役が揃うということが書いてあるだけで、リール1個分の確率は書かれていないのです。所詮これは言い訳ですから、内部の仕組みを知っている人からすると滑稽にうつるかもしれません。

numpyを使ってリストから確率でえらぶ

こちらです。

np.random.choice(["a", "b", "c"], p=[0.75, 0.15, 0.10])

np.random.choice()の第二引数に p=[] という形で確率を指定すればよいそうです。こちら既出情報で、参考は「静かなる名辞」というサイト。

雑感

numpyって結構おもしろいのかもなあ・・・

コメント

タイトルとURLをコピーしました