2017-04-21 50 views
-4

我正在为我的python类进行任务,并且我们正在为一个Bejewled游戏添加递归函数。我老实说有点失落,不知道在我未完成的递归函数中要添加什么。在我的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

你有没有可能将你的代码缩小到你想要做的相关部分? – MooingRawr

+0

欢迎来到StackOverflow。请阅读并遵守帮助文档中的发布准则。 [最小,完整,可验证的示例](http://stackoverflow.com/help/mcve)适用于此处。在发布您的MCVE代码并准确描述问题之前,我们无法为您提供有效的帮助。 StackOverflow不是一个编码或教程服务。 – Prune

回答

0

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

到目前为止,您所写的递归函数定义了有趣的事情来决定,但它既不做决定也不递归。通常会在每个用户的输入之后调用这个函数,以决定下一步该做什么来响应他们的移动。

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

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