您好我已经创建了一个非常简单的蛇和梯子程序,我想完成数值分析。我想找到赢得一个滑道和小伙子游戏所需的平均滚动次数。
我有数据,如果我只选择一个直方图来绘制,但是当我尝试改变游戏的迭代次数时,Python不喜欢它并且我不确定我做错了什么
import numpy as np
import numpy as np #importing numpy
get_ipython().magic('matplotlib inline')
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt # importing matplotlib.pyplot
import statistics
#totalNumberOfGames = int(input("PLease enter the number of games you want to run to run:",))
numberOfGames = 1
player1Wins = 0
player2Wins = 0
player1Moves = 0
player2Moves = 0
numberOfDiceRolls = 0
gameNumberList = list()
player1MovesInEachGame = list()
player2MovesInEachGame = list()
winner = list()
while(numberOfGames <= totalNumberOfGames):
player1 = 0
player2 = 0
a = range(1,11);b = range(11,21)[::-1];c = range(21,31);d = range(31,41)[::-1];e = range(41,51);f = range(51,61)[::-1];g = range(61,71);
h = range(71,81)[::-1];i = range(81,91);j = range(91,101)[::-1]
#print ("Snakes and Ladders game")
#print (j,'\n',i,'\n',h,'\n',g,'\n',f,'\n',e,'\n',d,'\n',c,'\n',b,'\n',a)
def check_for_snakes_and_ladders(n):
#This method checks for the presence of snakes or ladders on the board
ladders = {1:26,5:15,11:36,21:42,28:84,36:44,51:67,71:91,80:100}
snakes = {98:78,95:75,93:73,87:24,64:60,62:19,56:53,49:11,48:26,16:6}
if n in ladders:
#print("Its a ladder,Climb up")
n = ladders[n]
elif n in snakes:
#print("Its a snake!!,Come down")
n = snakes[n]
return n
def roll_dice(r,numberOfDiceRolls):
#This method takes input from each of the players, #prints the current position of the players and checks for the winner of the game
d = np.random.randint(1,7)
#print('you have rolled', d)
d = r + d
numberOfDiceRolls += 1
return d
while (player1 < 100 or player2 < 100):
#print ("Its turn of player1\n")
player1 = roll_dice(player1,numberOfDiceRolls)
player1Moves += 1
player1 = check_for_snakes_and_ladders(player1)
#print ("Current status of Player1:",player1,"and Player2:",player2)
#print ('Player 1 moves', player1Moves)
#print ('Player 2 moves', player2Moves)
if (player1 > 99):
winner.append(1)
#print ("Winner of the game is player1")
break
#print ("Its turn of player2\n")
player2 = roll_dice(player2,numberOfDiceRolls)
player2Moves += 1
player2 = check_for_snakes_and_ladders(player2)
#print ("Current status of Player1:",player1," and Player2:",player2)
if (player2 > 99):
winner.append(1)
#print ("Winner of the game is player2")
break
gameNumberList.append(numberOfGames)
numberOfGames += 1
##print ('Player 1 moves', player1Moves)
##print ('Player 2 moves', player2Moves)
#print(winner)
#print("-"*50)
player1MovesInEachGame.append(player1Moves)
player2MovesInEachGame.append(player2Moves)
player1Moves = 0
player2Moves = 0
##print("-"*20," Data Analysis ","-"*20)
for n in winner:
if (n == 1):
player1Wins += 1
elif (n == 2):
player2Wins += 1
print("Player 1 won",player1Wins,"times")
#print("Player 2 won",player2Wins,"times")
#print(gameNumberList)
#
# In[30]:
num_bins = 100
fig, axes = plt.subplots(nrows=2, ncols=2)
ax0, ax1, ax2 = axes.flatten()
def hist0(totalNumberOfGames):
totalNumberOfGames = 100
mu0 = statistics.mean(player1MovesInEachGame)
print("Mean amount of rolls is: ",mu0,)
sigma0 = statistics.stdev(player1MovesInEachGame)
print("The standard deviation of this data set is: ",sigma0,)
# the histogram of the data
n, bins, patches = ax0.hist(player1MovesInEachGame, num_bins, normed=1,
stacked = True, color = 'red', label = 'Turns to Win')
# add a 'best fit' line
y = mlab.normpdf(bins, mu0, sigma0)
ax0.plot(bins, y, '-', color = 'y', label = 'Standard Deviation of Turns')
ax0.set_xlim(5,120)
ax0.set_xlabel('Turns')
ax0.set_ylabel('Probability density')
ax0.set_title('Percentage of Finished Games in n-turns')
ax0.legend(prop={'size': 10})
def hist1(totalNumberOfGames):
totalNumberOfGames = 500
mu1 = statistics.mean(player1MovesInEachGame)
print("Mean amount of rolls is: ",mu1,)
sigma1 = statistics.stdev(player1MovesInEachGame)
print("The standard deviation of this data set is: ",sigma1,)
# the histogram of the data
n, bins, patches = ax1.hist(player1MovesInEachGame, num_bins, normed=1,
stacked = True, color = 'red', label = 'Turns to Win')
# add a 'best fit' line
y = mlab.normpdf(bins, mu1, sigma1)
ax1.plot(bins, y, '-', color = 'y', label = 'Standard Deviation of Turns')
ax1.set_xlim(5,120)
ax1.set_xlabel('Turns')
ax1.set_ylabel('Probability density')
ax1.set_title('Percentage of Finished Games in n-turns')
ax1.legend(prop={'size': 10})
def hist2(totalNumberOfGames):
totalNumberOfGames = 500
mu2 = statistics.mean(player1MovesInEachGame)
print("Mean amount of rolls is: ",mu2,)
sigma2 = statistics.stdev(player1MovesInEachGame)
print("The standard deviation of this data set is: ",sigma2,)
# the histogram of the data
n, bins, patches = ax2.hist(player1MovesInEachGame, num_bins, normed=1,
stacked = True, color = 'red', label = 'Turns to Win')
# add a 'best fit' line
y = mlab.normpdf(bins, mu2, sigma2)
ax2.plot(bins, y, '-', color = 'y', label = 'Standard Deviation of Turns')
ax2.set_xlim(5,120)
ax2.set_xlabel('Turns')
ax2.set_ylabel('Probability density')
ax2.set_title('Percentage of Finished Games in n-turns')
ax2.legend(prop={'size': 10})
# Tweak spacing to prevent clipping of ylabel
fig.tight_layout()
plt.show()
def main():
hist0(totalNumberOfGames)
hist1(totalNumberOfGames)
hist2(totalNumberOfGames)
if __name__ == "__main__":
main()
错误
追踪(最近一次呼叫最后一次):
文件“”,第106行,in ax0,ax1,ax2 = axes.flatten()
ValueError:解压缩的值太多(预期为3)
答案 0 :(得分:0)
您正在创建一个包含2行和2列的子绘图网格。 2次2给4
现在使用ax0, ax1, ax2 = axes.flatten()
您尝试将4个值分配给3个变量。
使用
fig, axes = plt.subplots(nrows=2, ncols=2)
ax0, ax1, ax2, forgottenaxes = axes.flatten()
或直接
fig, (ax0, ax1, ax2, forgottenaxes) = plt.subplots(nrows=2, ncols=2)