如何使用Python检查行和列的匹配情况?

时间:2018-10-08 00:05:47

标签: python

我正在构建ticTacToe游戏,我已经弄清楚了所有内容,但是如何检查连续三列或一列三列。我认为有一些简单的遗漏,但我可能完全错了。这是代码:

def isWin(gameBoard):
    #check for row matches
    for row in range(0, 3):
        if gameBoard[row][0] == gameBoard[row][1] and gameBoard[row][0] == gameBoard[row][2]:
            return True

3 个答案:

答案 0 :(得分:1)

也许您可以考虑使用set?像这样:

def isWin(gameBoard):
    for row in gameBoard:
        if len(set(row)) == 1:
            return true

答案 1 :(得分:0)

我想我明白了!

def isWin(gameBoard):

#check for row and column matches
for row in range(0, 3):
    if gameBoard[row][0] == "X" and gameBoard[row][1] == "X" and gameBoard[row][2] == "X":
        return True
    if gameBoard[row][0] == "O" and gameBoard[row][1] == "O" and gameBoard[row][2] == "O":
        return True
    if gameBoard[0][row] == "X" and gameBoard[1][row] == "X" and gameBoard[2][row] == "X":
        return True
    if gameBoard[0][row] == "O" and gameBoard[1][row] == "O" and gameBoard[2][row] == "O":
        return True

答案 2 :(得分:0)

所以我想通了,我非常兴奋!我是编码的新手,非常感谢大家的支持!如果您认为这样做会更有效,请告诉我。 :) 这是完整的代码:

import sys

def main():
    gameBoard = [[" ", " ", " "],
                 [" ", " ", " "],
                 [" ", " ", " "]]
printBoard(gameBoard)

while True:
    #
    playerXturn(gameBoard)
    printBoard(gameBoard)
    if isWin(gameBoard):
        print("Player X wins!")
        sys.exit()
    elif boardFull(gameBoard):
        print("Draw. No more plays left.")
        sys.exit()

    playerOturn(gameBoard)
    printBoard(gameBoard)
    if isWin(gameBoard):
        print("Player O wins!")
        sys.exit()
    elif boardFull(gameBoard):
        print("Draw. No more plays left.")
        sys.exit()

def printBoard(gameBoard):
    print("---------")
    print(gameBoard[0][0], "|", gameBoard[0][1], "|", gameBoard[0][2])
    print("---------")
    print(gameBoard[1][0], "|", gameBoard[1][1], "|", gameBoard[1][2])
    print("---------")
    print(gameBoard[2][0], "|", gameBoard[2][1], "|", gameBoard[2][2])
    print("---------")

def playerXturn(gameBoard):
    playerXrow = int(input("Enter a row (0, 1, or 2) for player X: "))
    playerXcolumn = int(input("Enter a column (0, 1, or 2) for player X: "))

    while gameBoard[playerXrow][playerXcolumn] == "X" or gameBoard[playerXrow][playerXcolumn] == "O":
        print("This spot is already taken. Try again:")
        playerXrow = int(input("Enter a row (0, 1, or 2) for player X: "))
        playerXcolumn = int(input("Enter a column (0, 1, or 2) for player X: "))

    gameBoard[playerXrow][playerXcolumn] = "X"

    return gameBoard

def playerOturn(gameBoard):
    playerOrow = int(input("Enter a row (0, 1, or 2) for player O: "))
    playerOcolumn = int(input("Enter a column (0, 1, or 2) for player O: "))

    while gameBoard[playerOrow][playerOcolumn] == "X" or gameBoard[playerOrow][playerOcolumn] == "O":
        print("This spot is already taken. Try again:")
        playerOrow = int(input("Enter a row (0, 1, or 2) for player O: "))
        playerOcolumn = int(input("Enter a column (0, 1, or 2) for player O: "))

    gameBoard[playerOrow][playerOcolumn] = "O"
    return gameBoard

#check for empty spaces on the board
def boardFull(gameBoard):
    for i in range(3):
        for j in range(3):
            if gameBoard[i][j] == " ":
                return False
    return True

#check for 3 in a row
def isWin(gameBoard):
    #check for row matches
    for row in range(0, 3):
        if gameBoard[row][0] == "X" and gameBoard[row][1] == "X" and gameBoard[row][2] == "X":
            return True
        if gameBoard[row][0] == "O" and gameBoard[row][1] == "O" and gameBoard[row][2] == "O":
            return True
        if gameBoard[0][row] == "X" and gameBoard[1][row] == "X" and gameBoard[2][row] == "X":
            return True
        if gameBoard[0][row] == "O" and gameBoard[1][row] == "O" and gameBoard[2][row] == "O":
            return True

    #check for diagonal matches
    if gameBoard[1][1] != " " and gameBoard[0][0] == gameBoard [1][1] and gameBoard[0][0] == gameBoard [2][2]:
        return True

    if gameBoard[1][1] != " " and gameBoard[0][2] == gameBoard [1][1] and gameBoard[0][2] == gameBoard [2][0]:
        return True

    return False

main()