获胜后重启tictactoe游戏

时间:2013-12-18 20:26:50

标签: python python-2.7

在我的checkWin()下面,我试图在赢得比赛时给出退出或重启的选项。根据我目前的情况,它将允许我“重新启动”,但它实际上只是让我继续玩当前的电路板。换句话说,我无法想出一种开始新鲜的方法。更重要的是,我不认为我在checkWin()所做的事情非常干净。寻找指示做什么。

(我知道这个剧本还有其他的问题,但我一次只做一件事)

'''Tic-tac-toe game'''

import sys
import time
import random


def printBoard():
    print "\n"
    print "  1 |  2 |  3 "
    print "____|____|____"
    print "  4 |  5 |  6 "
    print "____|____|____"
    print "  7 |  8 |  9 "
    print "    |    |    "
    print "\n"

def makeMove():
    move = raw_input("\nIt's your turn. Choose a box between 1 and 9: \n")
    move = int(move)
    return move

def boardCurrent(takenSpots):
    print "",takenSpots[0]," |",takenSpots[1]," |",takenSpots[2],"  "
    print "____|____|____"
    print "",takenSpots[3]," |",takenSpots[4]," |",takenSpots[5],"  "
    print "____|____|____"
    print "",takenSpots[6]," |",takenSpots[7]," |",takenSpots[8],"  "
    print "    |    |    "
    print "\n"

def compMove(takenSpots):
    move = random.randint(0,8)
    if takenSpots[move] == " ":
        takenSpots[move] = "O"
    else:
        compMove(takenSpots)
    return takenSpots

def takeSpot(move):
    if takenSpots[move - 1] != " ":
        print "That spot is taken, choose another."
        takeSpot(makeMove())
    else:
        takenSpots[move - 1] = "X"
    return takenSpots

def checkWin(takenSpots):
    win_positions = [
    (0, 3, 6),
    (1, 4, 7),
    (2, 5, 8),
    (0, 1, 2),
    (3, 4, 5),
    (6, 7, 8),
    (0, 4, 8),
    (2, 4, 6),
    ]
    for line in win_positions:
        if all(takenSpots[position] == "X" for position in line):
            win = True
            print "You win."
            if win == True:
                endgame = int(raw_input("Would you like to play another round?\n1) Yes\n2) No, this sucks. I want to quit."))
            if endgame == 1:
                main()
            if endgame == 2:
                sys.exit()
            return win
        if all(takenSpots[position] == "O" for position in line):
            win = True
            print "The computer won."
            return win

def clearBoard():
    takenSpots = [" "," "," "," "," "," "," "," "," "]
    return takenSpots

def main():
    boardCurrent(takeSpot(makeMove()))
    checkWin(takenSpots)
    print "Now the computer will go...\n"
    time.sleep(1)
    compMove(takenSpots)
    boardCurrent(takenSpots)
    checkWin(takenSpots)

takenSpots = [" "," "," "," "," "," "," "," "," "]

print "\nWelcome to tic-tac-toe."
win = False
printBoard()
main() 
while win == False:
    main()

2 个答案:

答案 0 :(得分:3)

如果你没有准确地告诉你如何做到这一点,我建议你将整个程序嵌套在这样的while循环中:

while True:
    program...

当游戏获胜或并列时,请询问用户输入,例如是退出还是重新启动:

Enter q to quit or r to restart # for instance

如果输入q,只需使用break语句从while循环中断,然后结束程序,否则,重新启动,循环,重新绘制电路板,基本上只需重置任何变量。考虑到循环,这并不太难。

checkwin:

我会重新评估变量'win'的有用性/必要性。真的需要吗?如果是这样,它是否正确使用?也许如果你实现while循环解决方案,这个问题将变得更加明显。我看到你有一个循环while win == False,但我不确定胜利是否会在玩家获胜的情况下得到正确评估。

答案 1 :(得分:0)

command = 'r'
while True:                
    if command == 'r':
        # game #
        clearBoard()
        print "\nWelcome to tic-tac-toe."
        win = False
        printBoard()
        main() 
        while win == False:
            main()

    elif command == 'e':
        return
        break

    else:
        print "Invalid command."

    command = raw_input('Enter r to restart, or e to end game: ')