Python 在我的bejewled游戏中添加递归函数

Python 在我的bejewled游戏中添加递归函数,python,function,recursion,turtle-graphics,Python,Function,Recursion,Turtle Graphics,我正在为我的python类做一个赋值,我们正在为bejewled游戏添加一个递归函数。老实说,我有点不知所措,不知道下一步在我的递归函数中添加什么 import turtle import random turtle.setup(800,600) window = turtle.Screen() t = turtle.Turtle() t.speed('fastest') def drawDot(t, x, y, diameter, colorP): t.up() t.g

我正在为我的python类做一个赋值,我们正在为bejewled游戏添加一个递归函数。老实说,我有点不知所措,不知道下一步在我的递归函数中添加什么

import turtle
import random

turtle.setup(800,600)
window = turtle.Screen()

t = turtle.Turtle()
t.speed('fastest')


def drawDot(t, x, y, diameter, colorP):
    t.up()
    t.goto(x, y)
    t.pencolor(colorP)
    t.dot(diameter)

def drawboard(t, x, y, diameter,board):
    for i in range(len(board)):
        for j in range(len(board[i])):
            drawDot(t, x, y, diameter + 5, 'black')
            drawDot(t, x, y, diameter, board[i][j])
            x = x + 60      
        y = y - 50
        x = -300

def shuffleboard(t, x, y, diameter, line):
    for i in range(len(line)):
        for j in range(len(line[i])):
            drawDot(t, x, y, diameter + 5, 'black')
            drawDot(t, x, y, diameter, line[i][j])
            x = x + 60      
        y = y - 50
        x = -300

def randomColor():
    randomColor = ['ivory', 'snow', 'gold', 'sky blue', 'indian red', 'slate gray', 'orange']
    num = random.randint(0,6)
    color = randomColor[num]
    return color

def generateBoard():
    board = []
    for r in range(10):
        row = []
        for i in range(10):
            color = randomColor()
            row.append(color)
        board.append(row)

    return board

t1 = turtle.Turtle()
x = -300
y = 240
diameter = 20

letterX = -300
letterY = 260

for r in range(10):
    t1.penup()
    t1.goto(letterX, letterY)
    t1.write(chr(r+ord('A')), move = False, align = 'center', font = ('Arial', 24, "normal"))
    letterX += 60
letterX = -300
for r in range(10):
    t1.penup()
    t1.goto(letterX - 35, letterY - 40)
    t1.write(r + 1, move = False, align = "center", font = ('Arial', 24, "normal"))
    letterY -= 50
t1.hideturtle()

start = input('previous board? (Y/N) ')
if start == "N":
    aList=[]
    board = generateBoard()
    generateBoard()
    drawboard(t, x, y, 25, board)


if start== "Y":
    fileName = input('enter file name(.txt): ')
    input_file=(fileName, 'r')
    for i in input_file:
        aList.append(i)

coordinate = input("enter your coordinate: ")
letter= coordinate[0]
number=int(coordinate[1:])
number=number-1

if letter == 'A':
    letIndex = 0
if letter == 'B':
    letIndex = 1
if letter == 'C':
    letIndex = 2
if letter == 'D':
    letIndex = 3
if letter == 'E':
    letIndex = 4
if letter == 'F':
    letIndex = 5
if letter == 'G':
    letIndex = 6
if letter == 'H':
    letIndex = 7
if letter == 'I':
    letIndex = 8
if letter == 'J':
    letIndex = 9    
number = int(coordinate[1:])
number = number - 1
print(board[number][letIndex])
finalBoard = []
save = input("save current board? (Y/N) ")
if save == "Y":
    outputFileName = input("enter output file: ")
    output_file=open(outputFileName, "w")
    board = str(board)
    output_file.write(board)
    output_file.close()
while save == "N":
    coord = input("enter your coordinate: ")
    letter = coord[0]
    if letter == 'A':
        letIndex = 0
    if letter == 'B':
        letIndex = 1
    if letter == 'C':
        letIndex = 2
    if letter == 'D':
        letIndex = 3
    if letter == 'E':
        letIndex = 4
    if letter == 'F':
        letIndex = 5
    if letter == 'G':
        letIndex = 6
    if letter == 'H':
        letIndex = 7
    if letter == 'I':
        letIndex = 8
    if letter == 'J':
        letIndex = 9    
    number = int(coordinate[1:])
    number = number - 1
    print(board[number][letIndex])
    save = input('save and quit? (Y/N)')
    if save == 'Y':
        outputFileName = input('enter output file: ')
        output_file=open(outputFileName, 'w')
        for i in board:
            finalBoard.append(i)
        finalBoard = str(finalBoard)
        output_file.write(finalBoard)
        output_file.close()

checked = []
def recursive(coordinate, checked, board):
    let = coord[0]
    num = coord[1:]
    num1 = num-1
    intnum = int(num1)
    topnum = intnum-1
    bottomnum = intnum+1
    letindex = ord(let)-65
    rightlet = letindex+1
    leftlet = letindex-1
    newright = rightlet + 65
    newleft = leftlet + 65
    rightcharacter = chr(newright)
    leftcharacter = chr(newleft)
    topcoord = let+str(topnum)
    bottomcoord = let+str(bottomnum)
    leftcoord = leftcharacter+num
    rightcoord = rightcharacter+num
    topcolor = board[topnum][letindex]
    bottomcolor = board[bottomnum][letindex]
    leftcolor = board[intnum][leftlet]
    rightcolor = board[intnum][rightlet]
    coordcolor = board[intnum][letindex]
    print(topcoord)
    print(bottomcoord)
    print(leftcoord)
    print(rightcoord)
    print(topcolor)
    print(bottomcolor)
    print(leftcolor)
    print(rightcolor)
recursive(coordinate, checked, board)

到目前为止,您已经为游戏创建了基本的数据结构,以及加载、保存、生成和显示棋盘的基础。但您没有添加的是评估用户移动的逻辑。这就是递归函数可以使用的地方

到目前为止,您编写的递归函数定义了在做决策时需要注意的有趣的事情,但它既不做决策,也不是递归的。此函数通常在每个用户输入后调用,以决定下一步如何响应他们的移动

您可能会在其他与遗留游戏逻辑相关的问题中找到一些见解。例如,上的这一个,虽然不是用Python,但讨论了可能与您需要的类似的逻辑,其中一个响应是递归的


如果没有帮助,请尝试您自己的StackOverflow或Internet搜索。

您是否有机会将代码缩小到您想要执行的相关部分?欢迎使用StackOverflow。请阅读并遵循帮助文档中的发布指南。适用于这里。在您发布MCVE代码并准确描述问题之前,我们无法有效地帮助您。StackOverflow不是编码或教程服务。