【问题标题】:Recursive Game issue in pythonpython中的递归游戏问题
【发布时间】:2012-12-06 14:52:12
【问题描述】:

我正在用 python 创建一个扫雷器版本,但遇到了一个小问题。在这段代码中:

if winGame(mines):
        printWorld(gameMap)
        print 'You Win!'
        answer = raw_input('Would you like to play again?')
        if answer == 'y':
            minesweeper()
        else:
            print 'Thanks for playing!'
            break

它再次调用扫雷函数,重新开始游戏。此代码与游戏代码的其余部分一起位于 while True: 循环中。唯一的问题是,如果重新开始游戏,然后赢了并说你不想再玩,它不会打破循环。我认为这与我使用递归再次调用该函数的事实有关。目前唯一可行的方法是使用 sys.exit(),但如果有意义的话,我希望有一个更合法的解决方案。

以下是全部代码:

#Minesweeper Game

#difficulty levels
#make it look better text based

from random import *
import sys

gameMap = '''
#123456789#
1?????????1
2?????????2
3?????????3
4?????????4
#123456789#'''

row = 0
col = 0
coord = []
response = ''
numMines = 5
mines = []
answer = ''


#This runs the game
def minesweeper():

    global gameMap
    global row
    global col
    global coord
    global mines
    global response
    global answer

    #resets the gameboard after replaying.
    gameMap = '''
#123456789#
1?????????1
2?????????2
3?????????3
4?????????4
#123456789#'''

    #generates the mines
    generateMines()

    #converts the world into a list of lists. exception is for when playing again since
    #it would try to convert it to a list of lists again
    try:
        initWorld()
    except Exception, e:
        pass

    #main loop of the game.
    while True:                      

        #checks to see if you won the game
        if winGame(mines):
            printWorld(gameMap)
            print 'You Win!'
            answer = raw_input('Would you like to play again?')
            if answer == 'y':
                minesweeper()
            else:
                print 'Thanks for playing!'
                break

        #joins the list together so it can be printed
        printWorld(gameMap)
        print mines

        #gets user input and converts it to an int, then adds coords to a list
        getCoord()        

        #asks user what they want to do
        clearOrFlag()

        if response.lower() == 'c':
            if isMine(mines):
                answer = raw_input('Would you like to play again?')
                if answer == 'y':
                    minesweeper()
                else:
                    print 'Thanks for playing!'
                    break
            else:
                clearSpace(mines)
                print '\n'
        elif response.lower() == 'f':
            flagSpace()
            print '\n'

#randomly generates the mines and checks for duplicates
def generateMines():
    global numMines
    global mines
    i = 0
    mines = []

    while i < numMines:
        mines.append([randint(1,4),randint(1,9)])
        i += 1

    if checkDuplicateMines(mines):
        generateMines()

#gets coordinates from the user
def getCoord():

    global row
    global col
    global coord
    global gameMap

    row = 0
    col = 0
    coord = []

    try:
        row = raw_input('Enter an Row: ')
        row = int(row)
        col = raw_input('Enter a Column: ')
        col = int(col)
    except ValueError:
        print 'Invalid Coordinates \n'
        getCoord()

    coord.append(row)
    coord.append(col)

def isMine(mines):

    global coord
    global gameMap

    for x in mines:
            if coord == x:
                showSolution(mines, gameMap)
                return True

#asks user if they want to clear or flag a space
def clearOrFlag():

    global response

    response = raw_input("Clear (c), Flag/Unflag (f)")

#clears a space. if it's a mine, the player loses. If not it will write the
#number of surrounding mines to the space. Might break this up later.
def clearSpace(mines):

    #checks to see if selected square is a ? (playable). 
    global gameMap
    global row
    global col
    global coord

    if gameMap[row][col] == '?' or gameMap[row][col] == 'F':                       
        gameMap[row][col] = str(countMines(mines))

#flags a space, or unflags it if already flagged.
def flagSpace():

    global gameMap
    global row
    global col

    try:
        if gameMap[row][col] == '?' :
            gameMap[row][col] = 'F'
        elif gameMap[row][col] == 'F':
            gameMap[row][col] = '?'
    except Exception, OutOfBounds:
        print 'Invalid Coordinates \n'

#Prints the world
def printWorld(gameMap):

    #just prints spaces to keep things tidy
    print '\n' * 100 

    for row in gameMap:
        print ' '.join(row)
        print '\n'

    print '\n'

#initializes the world so it can be printed
def initWorld():

    global gameMap

    # convert the gamemap into a list of lists
    gameMap = gameMap.split('\n')
    del gameMap[0]

    for index in range(0, len(gameMap)):
        gameMap[index] = list(gameMap[index])

#prints the gameBoard with all of the mines visible
def showSolution(mines, gameMap):

    for x in mines:
        gameMap[x[0]][x[1]] = 'M' 


    printWorld(gameMap)
    print 'You Lose'

#counts the number of surrounding mines in a space
def countMines(mines):
    global row
    global col
    count = 0

    #theres probably a much better way to do this    
    for x in mines:
        if [row+1,col] == x:
            count += 1
        if [row-1,col] == x:
            count += 1
        if [row,col+1] == x:
            count += 1
        if [row,col-1] == x:
            count += 1
        if [row+1,col+1] == x:
            count += 1
        if [row+1,col-1] == x:
            count += 1
        if [row-1,col+1] == x:
            count += 1
        if [row-1,col-1] == x:
            count += 1

    return count

#counts the number of flags on the board
def countFlags(mines):

    global gameMap
    numFlags = 0

    for i in range (0, len(gameMap)):
        for j in range (1, len(gameMap[0])-1):
            if gameMap[i][j]=='F':
                numFlags += 1

    if numFlags == len(mines):
        return True
    else:
        return False

#counts the number of mines flagged
def minesFlagged(mines):

    global gameMap
    count = 0

    for x in mines:
        if gameMap[x[0]][x[1]] == 'F':
            count += 1

    if count == numMines:
        return True
    else:
        return False

#checks to see if there were duplicate mines generated
def checkDuplicateMines(mines):

    mines.sort()

    for x in range(0, len(mines)-1):
        if mines[x] == mines[x+1]:
            return True
        x += 1

    return False

#checks to see if player won the game
def winGame(mines):

    if countFlags(mines):
        if minesFlagged(mines):
            return True
    else:
        return False

minesweeper()

【问题讨论】:

  • 递归调用可能不是解决此问题的最佳方法。您应该从另一个函数处理退出/启动/重新启动。
  • 感谢您的提示。更改了 while 循环的条件并将播放再次移动到另一个函数并使其全部正常工作。

标签: python recursion logic minesweeper


【解决方案1】:

如果你想使用递归,返回函数调用,不要只是调用它。

if winGame(mines):
        printWorld(gameMap)
        print 'You Win!'
        answer = raw_input('Would you like to play again?')
        if answer == 'y':
            return minesweeper()
        else:
            print 'Thanks for playing!'
            return

这样,当你的一个递归函数结束时,它会将None返回到前一个函数,然后再次将None返回到前一个函数,依此类推,直到最后一个调用return结束整个递归循环。

它可能不是这个问题的最佳解决方案(看看 MathieuW 的回答,基本上是一样的)但它适用于任何情况,并且主要用于递归函数。

【讨论】:

  • 谢谢大家的帮助!
【解决方案2】:

如果您不回复“y”,它确实会中断循环。

但是如果你玩过N场,它只会打破第N场的循环,你会回到第(N-1)个函数调用的循环中。

对于实际的解决方案,我同意您已经实施的 AshRj 评论。

这是另一个解决方案,仍然使用您以前的设计(但不太正确),只是移动了中断。

if winGame(mines):
        printWorld(gameMap)
        print 'You Win!'
        answer = raw_input('Would you like to play again?')
        if answer == 'y':
            minesweeper()
        else:
            print 'Thanks for playing!'
        break

【讨论】:

  • 谢谢大家的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-25
相关资源
最近更新 更多