简化 Python If 语句
Simplifying Python If Statements
我是 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和winner变量,也不想消除列表S的列表索引方法。尽管如此,有没有办法简化所有这些If语句,保留这些东西?如果是这样,如何?
您的代码查找 "trios" 个位置;您还不如拥有一个包含此信息的对象:
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 会循环遍历每个三重奏,检查您正在做的事情,如果三重奏匹配,return 就会获胜。这样,CheckWin 将少于 10 行代码。
我不想放弃一切,因为我相信你能做到:)
此外,您不需要在 CheckWin 中使用名为 "winner" 的全局变量;只需让 CheckWin return 获胜者(或“”),并将结果存储在函数本身之外的变量中。
即
winner = CheckWin(S)
您尝试过使用循环代替吗?
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)
我是 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和winner变量,也不想消除列表S的列表索引方法。尽管如此,有没有办法简化所有这些If语句,保留这些东西?如果是这样,如何?
您的代码查找 "trios" 个位置;您还不如拥有一个包含此信息的对象:
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 会循环遍历每个三重奏,检查您正在做的事情,如果三重奏匹配,return 就会获胜。这样,CheckWin 将少于 10 行代码。
我不想放弃一切,因为我相信你能做到:)
此外,您不需要在 CheckWin 中使用名为 "winner" 的全局变量;只需让 CheckWin return 获胜者(或“”),并将结果存储在函数本身之外的变量中。
即
winner = CheckWin(S)
您尝试过使用循环代替吗?
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)