井字棋的checkDraw函数-python

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

我已经编写了一个Tic Tac Toe程序,该程序正在运行,但是我一直在着手编写chekDraw函数(检查游戏是否平局)。所有其他功能都在起作用,我可以玩游戏。我认为for for in range循环将遍历整个电路板,计算x和o,直到整个电路板装满为止。总的来说,我的条件是如果不是胜利(checkWin),那就是平局。我一直在盯着它看,所以很长一段时间以来,我希望我的代码能有新的眼光。任何见解/建议将不胜感激!

# display instructions
def displayInstructions():
    print()
    print("This is a game of Tic-Tac-Toe. You will select an empty location")
    print("and enter its index to select a move. The first player will be X")
    print("and the second player will be O.")
    print()


# display current state
# pass in board
# does not return anything
def showBoard(board):
    for i in range(len(board)):
        print("[" + str(board[i]) + "]", end="")
        if i == 2 or i == 5 or i == 8:
            print(end="\n")


# pass in board
# return updated board
# must validate move (in range, unoccupied square)
def getMove(board, player):
    validMove = False
    while not validMove:
        move = input("{0}, what is your move: ".format(player))
        position = int(move) - 1  # cast input as an integer, and puts player move in correct index
        # in range
        if position < 0 or position > 8:
            print("That is an illegal move.")
        # unoccupied square
        if board[position] == "X" or board[position] == "O":
            print("That is an illegal move.")
        else:
            # if valid move, put player on board
            board[position] = player
            return board


def checkWin(board, player):
    if (board[0] == player and board[1] == player and board[2] == player) or \
            (board[3] == player and board[4] == player and board[5] == player) or \
            (board[6] == player and board[7] == player and board[8] == player) or \
            (board[0] == player and board[3] == player and board[6] == player) or \
            (board[1] == player and board[4] == player and board[7] == player) or \
            (board[2] == player and board[5] == player and board[8] == player) or \
            (board[0] == player and board[4] == player and board[8] == player) or \
            (board[2] == player and board[4] == player and board[6] == player):
        return True
    else:
        return False


def checkDraw(board, player):
    count = 0
    for i in range(len(board)):
        if board[i] == player:
            # if board[i] == "X" or board[i) == "O"
            count += 1
        if count == len(board):
            return True


def main():
    # Repeat play loop
    playGame = True
    while playGame:
        # output instructions
        displayInstructions()
        # initialize board
        board = [1, 2, 3, 4, 5, 6, 7, 8, 9]
        print(len(board))
        # initialize first player to X
        player = "X"
        # play until win or draw
        while not checkWin(board, player) or checkDraw(board, player):
            showBoard(board)
            getMove(board, player)
            checkWin(board, player)
            checkDraw(board, player)
            # if the game is in play (not a win or draw)
            if not checkWin(board, player) or checkDraw(board, player):
                # swap player
                if player == "X":
                    player = "O"
                else:
                    player = "X"
        # if win
        if checkWin(board, player):
            showBoard(board)
            print("{0} wins.".format(player))
            playGame = False
        # if draw
        elif checkDraw(board, player):
            showBoard(board)
            print("It's a draw")
            playGame = False
        # Ask if want to play another game
        playAgain = input("Would you like to play again? (y/n): ").lower()
        if playAgain == "y":
            main()
        else:
            print("Goodbye.")


if __name__ == "__main__":
    main()
python tic-tac-toe
1个回答
0
投票

而不是这些漫长的系列,和/或,您应该构建形成线段(线)的板位置列表,并将其用作在板上获得图案的间接方法:

例如:

segments = [ (0,1,2),(3,4,5),(6,7,8),(0,3,6),(1,4,7),(2,5,8),(1,4,8),(3,4,6) ]

patterns = [ "".join(sorted(board[i] for i in s)) for s in segments ]

playerWins = (player*3) in patterns
xWins      = "XXX" in patterns
oWins      = "OOO" in patterns
draw       = not xWins and not oWins and " " not in board

或者,如果您想更具预测性,并且在没有其他可获胜的招式时提早抽签:

draw = not any( win in patterns for win in ("   ","  X","  O"," XX"," OO") )

因为模式已排序,所以只有5个组合代表可赢得的线

© www.soinside.com 2019 - 2024. All rights reserved.