嗨,所以我基本上必须创建一个像Candy Crush Saga这样的游戏,并且我创建了一个二维数组才能启动,并且我能够基于数组的索引来创建带有相应图片的游戏板。但是我必须确保一开始我不能连续吃3张相同的图片或糖果。首先要创建数组:
react-router
创建一个9x7的电路板,基本上每个1-6的数字都对应一张特定的图片,但我无法弄清楚如何进行迭代并检查是否连续有三个相同的数字。有任何想法吗?谢谢
答案 0 :(得分:0)
此解决方案将不会赢得选美比赛,但它会一次性生成木板,这将比生成整个木板并在其上运行计数带来显着的性能优势,直到您设法获得一个幸运的木板为止(大约有10%随机板将是有效的)。
def generate_board(rows=9, cols=7):
board = []
for i in range(rows):
board.append([0] * cols)
for j in range(cols):
while not board[i][j]:
c = random.randint(1, 6)
if (i < 2 or board[i-2][j] != c or board[i-1][j] != c) and \
(j < 2 or board[i][j-2] != c or board[i][j-1] != c):
board[i][j] = c
return board
一些示例输出板:
[6, 1, 3, 3, 2, 5, 5] [3, 5, 6, 5, 2, 1, 1] [2, 2, 1, 5, 4, 6, 6] [4, 1, 1, 5, 2, 6, 2]
[4, 2, 5, 5, 3, 5, 3] [4, 2, 2, 1, 4, 3, 1] [5, 3, 6, 2, 3, 4, 5] [1, 3, 6, 1, 2, 4, 2]
[5, 2, 6, 5, 1, 4, 6] [2, 4, 1, 4, 3, 3, 6] [4, 1, 4, 1, 4, 1, 3] [4, 3, 4, 3, 1, 6, 3]
[5, 6, 2, 6, 3, 6, 6] [4, 5, 5, 3, 4, 5, 5] [5, 5, 3, 3, 1, 6, 3] [2, 4, 1, 5, 5, 1, 5]
[2, 1, 4, 2, 4, 3, 5] [4, 6, 5, 1, 5, 3, 4] [1, 3, 2, 3, 6, 2, 4] [4, 6, 4, 3, 6, 4, 2]
[1, 3, 4, 1, 6, 3, 1] [6, 2, 1, 5, 1, 2, 1] [6, 3, 3, 2, 4, 6, 6] [2, 3, 3, 6, 2, 1, 5]
[2, 1, 1, 3, 2, 5, 4] [6, 5, 5, 4, 5, 3, 3] [2, 4, 2, 2, 3, 6, 3] [2, 1, 3, 6, 1, 2, 6]
[3, 1, 2, 3, 3, 5, 1] [5, 3, 6, 1, 5, 4, 1] [4, 2, 1, 6, 5, 3, 6] [3, 6, 5, 2, 1, 5, 5]
[2, 6, 3, 1, 5, 6, 4] [6, 6, 4, 2, 3, 4, 2] [5, 4, 6, 6, 4, 2, 6] [5, 3, 1, 6, 3, 6, 2]