TicTacToe-如果有赢家,我如何停止游戏,

问题描述 投票:0回答:2

所以这是程序将如何接收数据的方法

 x_data = []
    def xuser_input():
        while True:
            x = As("Player X, Please enter a number to place: ",int)
            if (x > 10) or (x < 0):
                print("Input must be Bigger than 0 and Smaller than 9")
                continue
            try:
                board[x] = "X"
                x_data.append(x)
            except (IndexError):
                print("Invalid input")
                continue
            t_Board()
            break

Y也会有另一个。这是tictactoe板

def t_Board():
    print(f"| {board[0]} | {board[1]} | {board[2]} |\n_____________")
    print(f"| {board[3]} | {board[4]} | {board[5]} |\n_____________")
    print(f"| {board[6]} | {board[7]} | {board[8]} |")

如果满足此条件即为获胜公式,则将停止游戏。

    def stops_board():
        if (board[0] == board[1] == board[2]) or (board[3] == board[4] == board[5]) or (
        board[6] == board[7] == board[8]) or (board[0] == board[3] == board[6]) or (
        board[1] == board[4] == board[7]) or (board[2] == board[5] == board[8]) or (
        board[0] == board[4] == 
        board[8]) or (board[2] == board[4] == board[6]):
            return False

目前,这是我要求输入数据并检查是否有胜出的方法

 while True:
        xuser_input()
        stops_board()
        yuser_input()
        stops_board()
python tic-tac-toe
2个回答
0
投票

如我所见,如果游戏停止,stops_board()应该返回True,如果应该继续,则返回False。正确?如果是这样,您可以使用:

 while True:
        xuser_input()
        if stops_board(): break
        yuser_input()
        if stops_board(): break

0
投票
def stops_board():
    if (board[0] == board[1] == board[2]) or (board[3] == board[4] == board[5]) or (
    board[6] == board[7] == board[8]) or (board[0] == board[3] == board[6]) or (
    board[1] == board[4] == board[7]) or (board[2] == board[5] == board[8]) or (
    board[0] == board[4] == board[8]) or (board[2] == board[4] == board[6]):
        return True



 while True:
        xuser_input()
        if stops_board(): break
        yuser_input()
        if stops_board(): break
© www.soinside.com 2019 - 2024. All rights reserved.