【问题标题】:Simplifying Python If Statements简化 Python If 语句
【发布时间】:2023-03-23 13:12:01
【问题描述】:

我是 python 的初学者,我正在终端中创建一个两人的井字游戏。总共,这段代码占用了 139 行,(下面是我遇到问题的代码的相关部分),然而,这个 CheckWin 函数占用了大约 40 行代码,我认为这与数量相比相当多此代码中的行数,并考虑到它执行一些基本功能。基本上,在游戏中,此函数检查行、列或对角线是否有三个 X 或三个 O,如果有,则将 X 分配给获胜者,将 O 分配给获胜者。无论如何,这是代码。

X = "X"
O = "O"
empty = " " 
S = [" ", " ", " ", " ", " ", " ", " ", " ", " "]

def CheckWin(S):
    global winner
    winner = ""
    if S[0] == S[1] == S[2] != empty:
        if S[0] == X:
            winner = X
        if S[0] == O:
            winner = O
    if S[3] == S[4] == S[5] != empty:
        if S[3] == X:
            winner = X
        if S[3] == O:
            winner = O
    if S[6] == S[7] == S[8] != empty:
        if S[6] == X:
            winner = X
        if S[6] == O:
            winner = O
    if S[0] == S[3] == S[6] != empty:
        if S[0] == X:
            winner = X
        if S[0] == O:
            winner = O
    if S[1] == S[4] == S[7] != empty:
        if S[1] == X:
            winner = X
        if S[1] == O:
            winner = O
    if S[2] == S[5] == S[8] != empty:
        if S[2] == X:
            winner = X
        if S[2] == O:
            winner = O
    if S[0] == S[4] == S[8] != empty:
        if S[0] == X:
            winner = X
        if S[0] == O:
            winner = O
    if S[2] == S[4] == S[6] != empty:
        if S[2] == X:
            winner = X
        if S[2] == O:
            winner = O

基本上,我需要帮助使功能更简单。但是,我不想消除 X、O 和获胜者变量,也不想消除带有列表 S 的列表索引方法。尽管如此,有没有办法简化所有这些 If 语句,保留这些东西?如果是这样,怎么做?

【问题讨论】:

  • 对于初学者,您可以通过将所有 if S[n] == X: winner = X / if S[x] == O: winner = O 缩短为 winner = S[n] 来摆脱负载。这将为您节省 24 行代码。 :)
  • 考虑使用for 循环和range
  • 这个问题更适合codereview.stackexchange.com,它的重点是让现有的运行代码更好。
  • 我假设当有赢家时游戏停止?您可能可以为具有最后输入的任何行/列编写 testRowtestCol 函数。如果该条目在对角线上,则只需进行测试对角线。

标签: python if-statement terminal tic-tac-toe


【解决方案1】:

您的代码查找“三人组”职位;你还不如拥有一个包含此信息的对象:

trios = ((0,1,2), (3,4,5), (6,7,8), (0,3,6), (1,4,7), (2,5,8), (0,4,8), (2,4,6))

然后,CheckWin 将遍历每个三重奏,检查您正在做的检查,如果三重奏匹配,则返回获胜者。这样,CheckWin 将少于 10 行代码。

我不想全部放弃,因为我相信你可以做到:)

此外,您不需要在 CheckWin 中使用名为“winner”的全局变量;只需让 CheckWin 返回获胜者(或“”),并将结果存储在函数本身之外的变量中。

winner = CheckWin(S)

【讨论】:

  • 哇,谢谢,这要容易得多,但是,如何使 trios 变量与列表 S 及其索引一起使用。
  • 类似for trio in triosif S[trio[0]] == S[trio[1]] == S[trio[2]]...
【解决方案2】:

您是否尝试过使用循环?

X, O = 'X', 'O'
S    = [X,O,X,O,X,O,O,X,O] # Test values
def CheckWin(S):
    index      = 0
    has_winner = False
    while index < len(S):
        print index
        if index <= 6: # after this point you won't find a winner (or you run this after every turn?)
            if (index % 3 == 0 and S[index] == S[index + 1] and S[index] == S[index + 2]): # horizontals
                has_winner = True
            elif index < 3: # verticals and diagonals (you only need the first row to know this)
                if (S[index] == S[(index + 3)] and S[index] == S[index + 6]) or \
                   (index == 0 and S[index] == S[4] and S[index] == S[8]) or \
                   (index == 2 and S[index] == S[4] and S[index] == S[6]):
                    has_winner = True
        if has_winner: # I'm using this to prevent duplicating code above (one if less)
            if S[index] in [X,O]:
                return S[index]
        index += 1
    return has_winner # If we have a winner but the X or O criterion isn't met, it returns False
winner = CheckWin(S)

【讨论】:

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