我有以下代码,需要一些建议,以便我如何根据骰子滚动的数字访问矩阵中的特定元素。例如,在游戏开始时,如果滚动的数字是8,矩阵位置将是8.我不确定语法,特别是因为我使用了稍微不同的方法来创建矩阵:
矩阵输出
[43, 44, 45, 46, 47, 48, 49]
[42, 41, 40, 39, 38, 37, 36]
[29, 30, 31, 32, 33, 34, 35]
[28, 27, 26, 25, 24, 23, 22]
[15, 16, 17, 18, 19, 20, 21]
[14, 13, 12, 11, 10, 9, 8]
[1, 2, 3, 4, 5, 6, 7]
注意:如果用户滚动7,它将评估第一个子列表中的第6个元素,但是如果用户滚动8,则它将是下一个子列表中的最后一个元素(与第一个子列表相对),因为布局。
我到目前为止的代码如下。我需要一些建议的唯一一点是如何根据骰子滚动数访问和计算矩阵中的元素:
def RollTwoDiceP1(player1,player2):
turn=input("Player 1, it's your turn to roll the dice: Press r to roll:>>>")
#create two variables here and assign them random numbers
die1=random.randint(1,6)
die2=random.randint(1,6)
#add the two die numbers together
roll=die1+die2
#when you are printing an integer, you need to cast it into a string before you printit
print("Player1: You rolled a:", die1, "and a", die2, "which gives you a:", roll)
playing = False
for i in matrix(7):
#list=[i[0]]#this gives me the first column, or the first index in each sub list
moves=roll
print("You have moved", roll, "spaces to position:.......")#here I want to access the element in the list that corresponds to the number rolled. e.g. if 8 rolled, it would be on position, 8
playerturns(player1,player2,playing)
创建网格/矩阵并显示它的代码是:
def matrix(n):
grid = [[1 + i + n * j for i in range(n)] for j in range(n)]
for row in grid[1::2]:
row.reverse()
return grid[::-1][:]
def callmatrix(player1,player2, n):
print("*************LOADING GAME******************")
print("Welcome:", player1,"and", player2)
for i in matrix(n):
print(i)
playing = True
playerturns(player1,player2,playing)