【发布时间】:2015-11-23 09:54:43
【问题描述】:
我必须创建一个为扫雷游戏创建游戏板的函数(我对编码也很陌生)。
我打算让电路板最初从每个空间都被覆盖和释放,由字符串 "C " 表示。
然后我需要将地雷分配给我的 2D 列表中的随机空间,将 "C " 的实例替换为 "C*"
# create board function
def createBoard (rows, cols, mines):
board = []
#set the range to the number of rows and columns provided +1
#for every row, create a row and a column
for r in range (0,rows+1):
board.append([])
for c in range (0,cols+1):
board[r].append("C ")
#set # of mines to 0
num_mines=0
while num_mines < mines :
x = random.choice(board)
y = random.choice(x)
if y == "C ":
x[y]= "C*"
num_mines = num_mines+1
我的问题是我不知道如何实际替换字符串。
【问题讨论】:
标签: python python-3.x minesweeper