河内AutoSolve塔

时间:2018-03-04 03:19:29

标签: python python-2.7

我正在努力创建一个用户可以在当前状态下自动解决游戏的功能。我收到了这个错误。任何帮助我的人都很高兴。

    #main method
 def main(self):

    for i in range(self.iniNumTower):
        tempStack = MyStack("Tower" + str(i + 1))
        self.TowerList.append(tempStack)

    #User to choose their diffculty
    self.options = raw_input("Select your Difficulty: ")
    if self.options == "Beginner":
       self.numDisk = 3
    elif self.options == "Advance":
        self.numDisk = 6
    else:
        self.numDisk = 10

    for i in range(self.numDisk, 0, -1):
        self.endGameList.append(i)
        self.TowerList[0].push(i)
    print("[1] - Continue Steps")
    print("[2] - Auto Solve")
    print("[0=9] - Quit")
    choice = int(raw_input("Enter choice: "))
    while choice != 9:
        if choice == 0:
            break
        elif choice == 1:
            # while checkEndGame(endGameList,targetTower._data,helperTower._data) != 1:
            moveFrom = 0
            moveTo = 0



            a.printAllTowers()
            a.printTowerOptions()
            moveFrom = int(raw_input("Move from: "))
            # if(moveFrom!=9):
            moveTo = int(raw_input("Move to : "))

            if a.checkDiskMove(a.TowerList[moveFrom - 1], a.TowerList[moveTo - 1]) == 1:
                a.moveDisk(a.TowerList[moveFrom - 1], a.TowerList[moveTo - 1])
            else:
                print "Unable to move from " + a.TowerList[moveFrom - 1].name + " to " + a.TowerList[
                    moveTo - 1].name
            if a.checkEndGame() == 1:
                a.printAllTowers()
                print "You've Win!"
                print "Game End"
                break
        elif choice == 2:

            a.autoSolve(a.numDisk, a.TowerList[0], a.TowerList[2], a.TowerList[1])
            if a.checkEndGame() == 1:
                a.printAllTowers()
                print "You've Win!"
                print "Game End"
                break




def autoSolve(self,n, from_rod, to_rod, aux_rod):
    if n == 1:
        print "Move disk 1 from rod", from_rod.name, "to rod", to_rod.name
        cap = CurrentCapture()
        to_rod.push(from_rod.pop())
        cap.capture(self.TowerList[0], self.TowerList[2], self.TowerList[1])
        self.answerKey.append(cap)
        return

    TowerOfHanoi().autoSolve(n - 1, from_rod, aux_rod, to_rod)
    print "Move disk", n, "from rod", from_rod.name, "to rod", to_rod.name
    to_rod.push(from_rod.pop())
    TowerOfHanoi().autoSolve(n - 1, aux_rod, to_rod, from_rod)

这是我得到的错误," IndexError:列表索引超出范围"。 在此编辑主要方法。我希望这有帮助。当我打印出来时,我不确定为什么列表超出范围,显示为3.

带有追溯的Edit2

追踪(最近一次调用最后一次):

TowerOfHanoi().autoSolve(n - 1, from_rod, aux_rod, to_rod)
cap.capture(self.TowerList[0], self.TowerList[2], self.TowerList[1])

IndexError:列表索引超出范围

1 个答案:

答案 0 :(得分:0)

从您只访问第6行的列表这一事实来看,错误必须在那里。如果没有关于其中的代码和功能的更多信息,遗憾的是,我无能为力!

希望这有帮助。