2017-04-21 40 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搜索。