我在评估 Tic-Tac-Toe 获胜条件时遇到逻辑问题
I am having a logical issue with evaluating Tic-Tac-Toe winning conditions
我一直在开发井字游戏,获胜逻辑的一部分是我决定将获胜的条件语句(即连续三个、列三个或对角线三个)存储为常量变量。我面临的问题是 boolean False
是我所做的所有语句的结果。我有 print
s 的证据,我已将其插入到我的程序中。
代码:
# 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
的逻辑有什么问题?
如果这个逻辑错误的原因是将条件语句封装成variables/constants,那么请原谅我的无知。我尝试这样做是因为代码后面的 if
语句中的文字条件语句看起来太长且难以阅读。
你在初始化列表后立即评估所有测试,所以没有代码修改列表的值,列表中的每个项目仍然是空字符串' '
。
另一个问题是您没有调用 displayBoard
或 playBoard
.
首先要注意:一个井字游戏只需要 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
! (改变游戏板不会神奇地重新评估你的常量,你需要自己做)
相反,您可以使用一个函数来检查玩家是否完成了 row/col/diag。类似于:
# 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')
以确定是否有玩家获胜。
您没有更新值,所以每次都是错误的。
你可以这样做。
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)
我一直在开发井字游戏,获胜逻辑的一部分是我决定将获胜的条件语句(即连续三个、列三个或对角线三个)存储为常量变量。我面临的问题是 boolean False
是我所做的所有语句的结果。我有 print
s 的证据,我已将其插入到我的程序中。
代码:
# 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
的逻辑有什么问题?
如果这个逻辑错误的原因是将条件语句封装成variables/constants,那么请原谅我的无知。我尝试这样做是因为代码后面的 if
语句中的文字条件语句看起来太长且难以阅读。
你在初始化列表后立即评估所有测试,所以没有代码修改列表的值,列表中的每个项目仍然是空字符串' '
。
另一个问题是您没有调用 displayBoard
或 playBoard
.
首先要注意:一个井字游戏只需要 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
! (改变游戏板不会神奇地重新评估你的常量,你需要自己做)
相反,您可以使用一个函数来检查玩家是否完成了 row/col/diag。类似于:
# 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')
以确定是否有玩家获胜。
您没有更新值,所以每次都是错误的。 你可以这样做。
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)