递归函数,记住它在全局列表python中的路径

时间:2018-03-02 18:52:29

标签: python arrays function recursion

我的功能有问题:

tested_zeros = [(-1, -1)]

def reveal_zeros(x, y):
board_visible[y*row+x] = board[y*row+x]
for p in range(x - 1, x + 2):
    for o in range(y - 1, y + 2):
        if o >= 0 and p >= 0 and o <= row and p <= col:
            for zero in tested_zeros:
                if zero != (p, o):
                    tested_zeros.append((p, o))    <--broke part in my 'super' idea xD
                    if board[o*row+p] == " ":
                        return reveal_zeros(p, o)

所以正在做的是检查是否在数组&#39; board [(x,y)]&#39;是零和它的邻居,当它是,它复制到&#39; board_visible&#39;,问题是它在(0,0),[并且无休止地调用它]的错误所以我的想法是添加列表tests_zeros所以他记得它测试了哪些点,但递归函数不能正常工作:),我知道我可以通过(x,y)检查最后但它会在其他地方循环。 我的问题是如何处理这个?

以下是我的全部代码:

import random
import numpy as np

row = 10
col = row
mine_list = list()
board = list()
board_visible = list()
num = 0
is_over = False


def get_num_of_mines():
global num
while True:
    print("Podaj liczbe min: \n(z zakresu od 1 do ", (row * col) - 1, ")")
    num = int(input())
    if 1 <= num <= (row * col) - 1:
        return num
    else:
        print("Błędna liczba. Podaj poprawną z zakresu")


def deploy_mines():
global mine_list
mine_x = random.randint(0, row - 1)
mine_y = random.randint(0, col - 1)
par = mine_x, mine_y
for l in mine_list:
    if par == l:
        return deploy_mines()
return mine_x, mine_y


def number_of_neighboring_mines(x, y):
global mine_list
num_of_neighbors = 0
for p in range(x - 1, x + 2):
    for o in range(y - 1, y + 2):
        if o >= 0 and p >= 0 and o <= row and p <= col:
            par = p, o
            for l in mine_list:
                if par == l:
                    num_of_neighbors += 1

if num_of_neighbors == 0:
    return " "
else:
    return num_of_neighbors


def add_bomb(x, y):
for l in mine_list:
    if (x, y) == l:
        board.append("*")
        return False
return True


def create_board():
global mine_list, num, board_visible
for k in range(0, num):
    mine_list.append(deploy_mines())
for i in range(0, col):
    for j in range(0, row):
        if add_bomb(i, j):
            board.append(number_of_neighboring_mines(i, j))
for i in range(0, col):
    for j in range(0, row):
        board_visible.append(chr(9552))


def show_board():
for l in range(0, col+1):
    print('{0:2d}'.format(l), end="\t")
print()
for l in range(0, col):
    print('{0:2d}'.format(l+1), end=" ")
    print(np.split(np.asarray(board), row)[l])


def print_board():
for l in range(0, col+1):
    print('{0:2d}'.format(l), end="\t")
print()
for l in range(0, col):
    print('{0:2d}'.format(l+1), end=" ")
    print(np.split(np.asarray(board_visible), row)[l])


tested_zeros = [(-1, -1)]


def reveal_zeros(x, y):
board_visible[y*row+x] = board[y*row+x]
for p in range(x - 1, x + 2):
    for o in range(y - 1, y + 2):
        if o >= 0 and p >= 0 and o <= row and p <= col:
            for zero in tested_zeros:
                if zero != (p, o) or zero is None:
                    print(p, o)
                    tested_zeros.append((p, o))
                    if board[o*row+p] == " ":
                        return reveal_zeros(p, o)


def reveal_squares(x, y):
global is_over
if board[y*row+x] == "*":
    show_board()
    print("Koniec gry!")
    is_over = True
elif board[y*row+x] == " ":
    reveal_zeros(x, y)
else:
    board_visible[y*row+x] = board[y*row+x]


def sapper():
get_num_of_mines()
create_board()
while not is_over:
    print_board()
    reveal_squares(int(input("Podaj współrzędną x: "))-1, int(input("Podaj 
współrzędną y: "))-1)


sapper()

1 个答案:

答案 0 :(得分:0)

由于没有人能真正帮助我,所以我为那个对这个解决方案感兴趣的人提出了工作功能。

def reveal_zeros(x, y, steps):
board_visible[y*row+x] = board[y*row+x]
can_return = True
for p in range(x - 1, x + 2):
    for o in range(y - 1, y + 2):
        if o >= 0 and p >= 0 and o < row and p < col:
            for i in steps:
                if (p, o) == i:
                    can_return = False
            if board[o*row+p] == " " and can_return:
                return reveal_zeros(p, o, steps + [(p, o)])
        if p == x or o == y or o < 0 or p < 0 or not can_return:
            can_return = True
            continue
return

一点也不感谢,在这里你去没有获胜的sapper模拟:)