ゲーム作ってみた．

Pythonの機能を色々使うためにゲームを作ってみた．

ゲームルール
まず，コンソールにゲームに相当する，ある図が4つ表示される．これは，各々ランダムに生成された確率分布に従う標本を100個プロットしたものである．この標本は，　-100%~100%の割合である．
ゲームは，掛け金に対して上記確率分布からランダムに引いた標本ｎの割合を掛け金にかけた金額がもらえる．ただし，参加料は掛け金*0.1%であり，ゲーム1回ごとに自動で精算される．

このようなルールを持つゲームを作りたい．
さらに，コマンドを押してゲームを継続したり，終了したりというったことができるようなプログラムを実装したい．
関数定義の仕様とか，グローバル/ローカル変数の仕様とか諸々を理解してなくて冗長かも知れない．
そういうところの指摘がどんどん欲しいところ．

まず，グローバル変数を用意した．
後々正規分布を使いたいから，そのパラメータをランダムに毎回設定するようにしたい．
global mu2
global v2
global mu3
global v3
global mu4
global v4
global mu5
global v5

そしてそれらの変数がこんな感じで一様分布に従って設定するようにした．
mu2 = np.random.uniform(-0.05, 0.05)
v2 =np.random.uniform(0.005, 1.0)
mu3 = np.random.uniform(-0.05, 0.05)
v3 =np.random.uniform(0.005, 1.0)
mu4 = np.random.uniform(-0.05, 0.05)
v4 =np.random.uniform(0.005, 1.0)
mu5 = np.random.uniform(-0.05, 0.05)
v5 =np.random.uniform(0.005, 1.0)

プロットした．
fig = plt.figure(1)
ax2 = fig.add_subplot(321)
X2 = np.random.normal(mu2, v2, 100)
plt.title('game2')
ax2.set_ylim(-4, 4)
plt.plot(X2)

ax3 = fig.add_subplot(322)
X3 = np.random.normal(mu3, v3, 100)
plt.title('game3')
ax3.set_ylim(-4, 4)
plt.plot(X3)

ax4 = fig.add_subplot(325)
X4 = np.random.normal(mu4, v4, 100)
plt.title('game4')
ax4.set_ylim(-4, 4)
plt.plot(X4)


ax5 = fig.add_subplot(326)
X5 = np.random.normal(mu5, v5, 100)
plt.title('game5')
ax5.set_ylim(-4, 4)
plt.plot(X5)

次にゲームの関数を定義した．
P_Startに掛け金，Feeに参加料金，nに参加回数を入力する必要がある．
def game2(P_start, Fee, n) :
    
    global mu2
    global v2
    
    x = np.random.normal(mu2, v2)
    P_end = P_start + P_start * x - Fee
    
    print("Rate of Change(%): " + str(100 * x) + "  This game's Fee(JPY): " + str(Fee))
    
    return P_end

def game3(P_start, Fee, n) :
    
    global mu3
    global v3
    
    x = np.random.normal(mu3, v3)
    P_end = P_start + P_start * x - Fee
    
    print("Rate of Change(%): " + str(100 * x) + "  This game's Fee(JPY): " + str(Fee))
    
    return P_end

def game4(P_start, Fee, n) :
    
    global mu4
    global v4
    
    x = np.random.normal(mu4, v4)
    P_end = P_start + P_start * x - Fee
    
    print("Rate of Change(%): " + str(100 * x) + "  This game's Fee(JPY): " + str(Fee))
    
    return P_end

def game5(P_start, Fee, n) :
    
    global mu5
    global v5
    
    x = np.random.normal(mu5, v5)
    P_end = P_start + P_start * x - Fee
    
    print("Rate of Change(%): " + str(100 * x) + "  This game's Fee(JPY): " + str(Fee))
    
    return P_end

それぞれのゲームを実行する関数を定義した．
def game2start():
    
    global n1
    global n2
    global z
    global z1
    global Fee
    global percent
    global Fee_total
    
    percent = 0.001
    
    print("How much money (JPY) do you bet?")
    n0 = input()
    n1 = int(n0)
    n2 = 0
    Fee_total = 0
    
    def nnn():
        
        global n1
        global n2
        global z
        global z1
        global Fee
        global Fee_total
        
        print("If you challenge this game, click the key 's'. If you end this game, click the key 'e'.")
        command = input()
        if command == 's' and n2 == 0 :
                n2 += 1
                Fee = n1 * percent
                Fee_total += Fee
                z1 = np.floor(game2(n1, Fee, n2))
                print("Your possession: " + str(z1) + "  Your challenges: " + str(n2))
                return z1, Fee, Fee_total, nnn()
        if command =='s' and n2 > 0:
                n2 += 1
                Fee = n1 * percent
                Fee_total += Fee
                z1 = np.floor(game2(z1, Fee, n2))
                print("Your possession: " + str(z1) + "  Your challenges: " + str(n2))
                return z1, Fee, Fee_total, nnn()
        else :
                print("Game is End..." + "Your possession: " + str(z1) + "  Your challenges: " + str(n2) + "  Total Fee: " + str(Fee_total))
                
    nnn()
    

def game3start():
    
    global n1
    global n2
    global z
    global z1
    global Fee
    global percent
    global Fee_total
    
    percent = 0.001
    
    print("How much money (JPY) do you bet?")
    n0 = input()
    n1 = int(n0)
    n2 = 0
    Fee_total = 0
    
    def nnn():
        
        global n1
        global n2
        global z
        global z1
        global Fee
        global Fee_total
        
        print("If you challenge this game, click the key 's'. If you end this game, click the key 'e'.")
        command = input()
        if command == 's' and n2 == 0 :
                n2 += 1
                Fee = n1 * percent
                Fee_total += Fee
                z1 = np.floor(game3(n1, Fee, n2))
                print("Your possession: " + str(z1) + "  Your challenges: " + str(n2))
                return z1, Fee, Fee_total, nnn()
        if command =='s' and n2 > 0:
                n2 += 1
                Fee = n1 * percent
                Fee_total += Fee
                z1 = np.floor(game3(z1, Fee, n2))
                print("Your possession: " + str(z1) + "  Your challenges: " + str(n2))
                return z1, Fee, Fee_total, nnn()
        else :
                print("Game is End..." + "Your possession: " + str(z1) + "  Your challenges: " + str(n2) + "  Total Fee: " + str(Fee_total))
                
    nnn()    

def game4start():
    
    global n1
    global n2
    global z
    global z1
    global Fee
    global percent
    global Fee_total
    
    percent = 0.001
    
    print("How much money (JPY) do you bet?")
    n0 = input()
    n1 = int(n0)
    n2 = 0
    Fee_total = 0
    
    def nnn():
        
        global n1
        global n2
        global z
        global z1
        global Fee
        global Fee_total
        
        print("If you challenge this game, click the key 's'. If you end this game, click the key 'e'.")
        command = input()
        if command == 's' and n2 == 0 :
                n2 += 1
                Fee = n1 * percent
                Fee_total += Fee
                z1 = np.floor(game4(n1, Fee, n2))
                print("Your possession: " + str(z1) + "  Your challenges: " + str(n2))
                return z1, Fee, Fee_total, nnn()
        if command =='s' and n2 > 0:
                n2 += 1
                Fee = n1 * percent
                Fee_total += Fee
                z1 = np.floor(game4(z1, Fee, n2))
                print("Your possession: " + str(z1) + "  Your challenges: " + str(n2))
                return z1, Fee, Fee_total, nnn()
        else :
                print("Game is End..." + "Your possession: " + str(z1) + "  Your challenges: " + str(n2) + "  Total Fee: " + str(Fee_total))
                
    nnn()
    
def game5start():
    
    global n1
    global n2
    global z
    global z1
    global Fee
    global percent
    global Fee_total
    
    percent = 0.001
    
    print("How much money (JPY) do you bet?")
    n0 = input()
    n1 = int(n0)
    n2 = 0
    Fee_total = 0
    
    def nnn():
        
        global n1
        global n2
        global z
        global z1
        global Fee
        global Fee_total
        
        print("If you challenge this game, click the key 's'. If you end this game, click the key 'e'.")
        command = input()
        if command == 's' and n2 == 0 :
                n2 += 1
                Fee = n1 * percent
                Fee_total += Fee
                z1 = np.floor(game5(n1, Fee, n2))
                print("Your possession: " + str(z1) + "  Your challenges: " + str(n2))
                return z1, Fee, Fee_total, nnn()
        if command =='s' and n2 > 0:
                n2 += 1
                Fee = n1 * percent
                Fee_total += Fee
                z1 = np.floor(game5(z1, Fee, n2))
                print("Your possession: " + str(z1) + "  Your challenges: " + str(n2))
                return z1, Fee, Fee_total, nnn()
        else :
                print("Game is End..." + "Your possession: " + str(z1) + "  Your challenges: " + str(n2) + "  Total Fee: " + str(Fee_total))
                
    nnn()