【问题标题】:I am having a logical issue with evaluating Tic-Tac-Toe winning conditions我在评估井字游戏获胜条件时遇到逻辑问题
【发布时间】:2021-07-19 03:24:53
【问题描述】:

我一直在研究井字游戏,获胜逻辑的一部分是我决定将获胜的条件语句(即连续三个、三个列或三个对角线)存储在常量变量中。我面临的问题是布尔 False 是我所做的所有陈述的结果。我有prints 的证明,我已插入我的程序。

代码:

# Create game cells
list = [' ' for n in range(10)]

# Coniditions for winning
O_ROW = (
    (list[1] == 'O' and list[2] == 'O' and list[3] == 'O') or
    (list[4] == 'O' and list[5] == 'O' and list[6] == 'O') or
    (list[7] == 'O' and list[8] == 'O' and list[9] == 'O')
)
O_COL = (
    (list[1] == 'O' and list[4] == 'O' and list[7] == 'O') or
    (list[2] == 'O' and list[5] == 'O' and list[8] == 'O') or
    (list[3] == 'O' and list[6] == 'O' and list[9] == 'O')
)
X_ROW = (
    (list[1] == 'X' and list[2] == 'X' and list[3] == 'X') or
    (list[4] == 'X' and list[5] == 'X' and list[6] == 'X') or
    (list[7] == 'X' and list[8] == 'X' and list[9] == 'X')
)
X_COL = (
    (list[1] == 'X' and list[4] == 'X' and list[7] == 'X') or
    (list[2] == 'X' and list[5] == 'X' and list[8] == 'X') or
    (list[3] == 'X' and list[6] == 'X' and list[9] == 'X')
)
O_DIAG = (
    (list[1] == 'O' and list[5] == 'O' and list[9] == 'O') or
    (list[3] == 'O' and list[5] == 'O' and list[7] == 'O')
)
X_DIAG = (
    (list[1] == 'X' and list[5] == 'X' and list[9] == 'X') or
    (list[3] == 'X' and list[5] == 'X' and list[7] == 'X')
)

def displayBoard():
    # Printing the game board
    print()
    print(f'\t {list[1]} | {list[2]} | {list[3]}')
    print('\t-----------')
    print(f'\t {list[4]} | {list[5]} | {list[6]}')
    print('\t-----------')
    print(f'\t {list[7]} | {list[8]} | {list[9]}')
    print()

def playBoard(pos, pl):
    # Position the play in the cell and check for already-placed cells
    if list[pos] != 'X' and list[pos] != 'O':
        list[pos] = pl.upper()
    else:
        print("Already played! Try again")

    # Determine winning condition or tie
    if O_ROW and O_COL and O_DIAG:
        print("O Wins!")
        sys.exit()
    elif X_ROW and X_COL and X_DIAG:
        print("X Wins!")
        sys.exit()


print('''Welcome to the game of Tic-Tac-Toe.
You will pick a play (X or O) and then pick from a place of 1 to 9 on
the board. Let's begin
''')

# Print for debugging
print(X_ROW)
print(X_COL)
print(X_DIAG)
print(O_ROW)
print(O_COL)
print(O_DIAG)

输出:

Welcome to the game of Tic-Tac-Toe.
You will pick a play (X or O) and then pick from a place of 1 to 9 on
the board. Let's begin

False
False
False
False
False
False
Play X or O?

他们都评估为False的逻辑有什么问题?

如果这个逻辑错误的原因是将条件语句封装成变量/常量,那么请原谅我的无知。我尝试这样做是因为代码后面的 if 语句中的文字条件语句看起来太长且难以阅读。

【问题讨论】:

  • 在你开始游戏之前当然是False
  • If the reason for this logic error is encapsulating conditional statements into variables/constants - 你已经知道问题出在哪里了 :) 它们都是常量错误的,因为它们只在板为空时在启动时运行一次
  • 我确实有一个主游戏循环,但我故意将其从粘贴的代码中删除,因为我认为代码已经很长了。

标签: python python-3.x variables conditional-statements tic-tac-toe


【解决方案1】:

您没有更新值,因此每次都是错误的。 你可以这样做。

import sys
# Create game cells
List = ['-' for n in range(10)]


# Coniditions for winning
def runGame():
    O_ROW = (
        not(List[1] == 'O' and List[2] == 'O' and List[3] == 'O') or
        not(List[4] == 'O' and List[5] == 'O' and List[6] == 'O') or
        not(List[7] == 'O' and List[8] == 'O' and List[9] == 'O')
    )
    O_COL = (
        not(List[1] == 'O' and List[4] == 'O' and List[7] == 'O') or
        not(List[2] == 'O' and List[5] == 'O' and List[8] == 'O') or
        not(List[3] == 'O' and List[6] == 'O' and List[9] == 'O')
    )
    O_DIAG = (
        not(List[1] == 'O' and List[5] == 'O' and List[9] == 'O') or
        not(List[3] == 'O' and List[5] == 'O' and List[7] == 'O')
    )
    X_ROW = (
        not(List[1] == 'X' and List[2] == 'X' and List[3] == 'X') or
        not(List[4] == 'X' and List[5] == 'X' and List[6] == 'X') or
        not(List[7] == 'X' and List[8] == 'X' and List[9] == 'X')
    )
    X_COL = (
        not(List[1] == 'X' and List[4] == 'X' and List[7] == 'X') or
        not(List[2] == 'X' and List[5] == 'X' and List[8] == 'X') or
        not(List[3] == 'X' and List[6] == 'X' and List[9] == 'X')
    )
    X_DIAG = (
        not(List[1] == 'X' and List[5] == 'X' and List[9] == 'X') or
        not(List[3] == 'X' and List[5] == 'X' and List[7] == 'X')
    )
    return(O_DIAG,O_ROW,O_COL,X_ROW,X_DIAG,X_COL)

def displayBoard():
    # Printing the game board
    print()
    print(f'\t {List[1]} | {List[2]} | {List[3]}')
    print('\t-----------')
    print(f'\t {List[4]} | {List[5]} | {List[6]}')
    print('\t-----------')
    print(f'\t {List[7]} | {List[8]} | {List[9]}')
    print()

def playBoard(pos, pl):
    # Position the play in the cell and check for already-placed cells
    if List[pos] != 'X' and List[pos] != 'O':
        List[pos] = pl.upper()
    else:
        print("Already played! Try again")

    # Determine winning condition or tie
    if not runGame()[0] or not runGame()[1] or not runGame()[2]  :
        displayBoard()
        print("O Wins!")
        sys.exit()
        
    elif not runGame()[3] or not runGame()[4] or not runGame()[5]:
        displayBoard()
        print("X Wins!")
        sys.exit()
    elif "-" not in List:
        print("It's a draw")
        sys.exit()


print('''Welcome to the game of Tic-Tac-Toe.
You will pick a play (X or O) and then pick from a place of 1 to 9 on
the board. Let's begin
''')

while (all(runGame())):
    displayBoard()
    pos=int(input("pos"))
    p1=input("X/O")
    playBoard(pos,p1)
    


playBoard(pos,p1)

【讨论】:

  • 谢谢,这就是解决方案。我想一直以来最好把它自己的功能放在里面。
【解决方案2】:

你在初始化列表之后立即评估所有测试,所以没有代码修改列表的值,列表中的每一项仍然是空字符串' '
另一个问题是你没有打电话给displayBoardplayBoard

【讨论】:

    【解决方案3】:

    第一句话:井字游戏只需要 9 个单元格,但是您创建了一个包含 10 个元素的数组。您可以将 10 替换为 9 并将所有索引移 1(索引从 0 开始):

    list = [' ' for n in range(10)]
    

    您甚至可以将其重写为 list = [' '] * 9(等效但可能更易于阅读)

    所以此时,您的游戏板只有 10(或 9)个单元格,全部包含 ' '

    然后你评估一个 row/col/diag 是否已经被填充

    O_ROW = (
        (list[1] == 'O' and list[2] == 'O' and list[3] == 'O') or
        (list[4] == 'O' and list[5] == 'O' and list[6] == 'O') or
        (list[7] == 'O' and list[8] == 'O' and list[9] == 'O')
    )
    

    条件逻辑是正确的(就井字游戏规则而言),但显然结果是False -> 你的游戏板仍然是空的(' ' 到处都是)。所以O_ROW(和其他人)将永远是False! (改变游戏板不会神奇地重新评估你的常量,你需要自己做)

    相反,您可以使用函数来检查玩家是否完成了行/列/诊断。比如:

    # board is your list
    # player is 'O' or 'X'
    def has_won(board, player):
        finished_row = (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)
        # TODO!
        # finished_col =
        # finished_diag =
        return finished_row or finished_col or finished_diag
    

    然后,每当您的棋盘更新时,您都会检查has_won(board, 'O')has_won(board, 'X') 以查看是否有一名玩家获胜。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多