我正在为学校做一个棋盘游戏,我想能够找到他们的位置编号索引,并用他们的计数器(“x”或“y”)替换棋盘上的编号。
board = [
["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 "]
]
for line in board:
print (line)
roll = input("Player " + player + " press enter to roll the dice")
print ("Your counter is",counter)
if roll != "blablabla":
die1 = random.randint(1,6)
die2 = random.randint(1,6)
dice = die1 + die2
print (die1)
print (die2)
print ("You rolled",dice)
if player == "one":
place1 =(place1+dice)
print ("P1's place is",place1)
else:
place2 =(place2+dice)
print ("P2's place is",place2)
我怎样才能在电路板中找到字符串版本的“place1”或“place2”,并用其他内容替换该索引?
谢谢你!
ind = np.where(np.array(board) == str(place1))
将返回board
数组中所有元素的索引,等于place
。要替换这些值,请执行以下操作:board[ind] = newval
。基本上
您需要遍历主列表,然后可以使用
list.index()
查找子列表索引,例如:它的作用与
list.index()
完全一样,但对于二维数组,在您的情况下:我在下面加了一行。数组接受整数值,但不接受元组/列表。上面@zwer已经给出了代码片段的下面一行。感谢@zwer。
输出将类似,注意“替换”在其中。
相关问题 更多 >
编程相关推荐