棋盘游戏Python矩阵纸

时间:2018-08-12 05:54:17

标签: python arrays matrix vector

今年夏天,我有一个数学学校项目要完成,因此我尝试学习python,并找到一个棋手完成Candyland棋盘游戏的平均举动数。问题是我从代码中得到的动作很少。我用类似的纸检查过,果然,我的电话太少了。我想知道是否有人可以花一些时间为我查看代码,因为我找不到问题。我对问题的格式感到非常抱歉,我只是想看看是否有人能找到一个错误来影响用来解决此问题的过渡矩阵,或者有人对我的代码有任何基本改进,因为我只是一个初学者,我也希望能得到一些有关清洁此代码的建议。

非常感谢您!!! -埃文

import numpy as np


# Make the matrix and the vector
v = np.zeros(129)
v[0] = 1
T = np.zeros((129, 129))
# There is a 8/66 chance that you pick a regular card of a specific colour, advancing 1-6 squares...
for i in range(1, 123):
    T[i-1, i:i+6] = 8/66
# There is a 2/66 chance that you pick up a shortcut card, advancing, 7-12 squares; this makes it impossible to land
# on one of the first 6 squares after drawing this card...
for i in range(7, 129):
    T[i-7, i:i+6] = 2/66

# There is also a possibility that the player draws a special card before they reach the 128th square and finish the
# game. This would move to their respective pink squares in Candyland. However, in our simulation they will be moved to
# the square right before the pink square because this doesn't affect anything and allows our functions to run smoother.
for i in range(1, 129):
    T[i-1, 8] += 1/66
    T[i-1, 16] += 1/66
    T[i-1, 40] += 1/66
    T[i-1, 71] += 1/66
    T[i-1, 91] += 1/66
    T[i-1, 98] += 1/66
# If you land on a sticky square you need to draw a card with a specific colour on it. For instance, you need to draw a
# yellow or double yellow to escape square 45.
for i in range(1, 129):
    T[45, i-1] = 0
T[45, 51] = 8/66
T[45, 57] = 2/66
T[45, 45] = 56/66

for i in range(1, 129):
    T[82, i-1] = 0
T[82, 88] = 8/66
T[82, 94] = 2/66
T[82, 82] = 56/66

for i in range(1, 129):
    T[115, i-1] = 0
T[115, 121] = 8/66
T[82, 127] = 2/66
T[115, 115] = 56/66

# In the last 6 squares before the finish line will either reach the finish line or draw a special card and when you
# reach the last square you will always return...
T[122:128, 128] = 60/66
T[128, 128] = 1

n, P = 0, []
cumulative_prob = 0

for i in range(40):
    n += 1
    v = v.dot(T)
    P.append(v[128])
    cumulative_prob += P[-1]
print("the mode number of moves is")
print(n)

0 个答案:

没有答案
相关问题