获胜后重新开始tictactoe游戏

2024-09-26 17:44:32 发布

您现在位置:Python中文网/ 问答频道 /正文

在我下面的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()

Tags: importmovereturnifmaindefpositionwin
2条回答

在不向您展示具体的操作方法的情况下,我建议您将整个程序嵌套在while循环中,如下所示:

while True:
    program...

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

^{pr2}$

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

支票赢:

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

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: ')

相关问题 更多 >

    热门问题