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

时间:2017-04-21 16:16:19

标签: 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.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)

1 个答案:

答案 0 :(得分:0)

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

到目前为止,您编写的递归函数定义了要做出决定的有趣事项,但它既不会做出决策也不会递归。通常在每个用户的输入之后调用此函数,以决定下一步响应移动的操作。

您可能会发现一些有关宝石迷战游戏逻辑的其他StackOverflow问题的见解。例如,logic behind a bejeweled-like game上的这个,虽然不是在Python中,但讨论的逻辑可能与您需要的类似,其中一个响应是递归的。

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