我做了一个井字游戏,但当有人赢了时它显示错误的玩家
I have made a Tic Tac Toe game but it shows the wrong player when someone has won
我在 python 中做了一个井字游戏,当 X 赢时,它表明 O 赢了,当 O 赢时,它表明 X 赢了。我很确定问题是它改变了玩家,然后检查是否有人赢了,我试着让它在玩家切换之前切换,但它仍然没有用。
我还尝试在 is_win 函数中更改播放器,但这也没有解决问题。
有人可以看看这个并帮我解决这个问题吗?
initial_board = [['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']]
board = initial_board
def empty_board(): #use board = empty_board() everytime you want to empty the board
board = [['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']]
return(board)
def switch_turn(player): #use player = switch_turn(player) everytime you want to switch players
if player == 'X':
return 'O'
return 'X'
def print_board(board):
print(*board, sep = "\n")
def is_full(board):
return all('_' not in box for box in board)
def is_valid(board, row, col):
x = board[row]
if x[col] == '_':
return True
return False
def set_cell(board, row, col, player):
x = board[row]
if is_valid(board,row,col) is True:
x[col] = player
print_board(board)
else:
print("already taken")
def get_user_input():
while True:
while True:
row = int(input("Enter the row you want (0-2): "))
if row > 2 or row < 0:
print("The number you entered is not valid")
continue
break
while True:
col = int(input("Enter the column you want (0-2): "))
if col > 2 or col < 0:
print("The number you entered is not valid")
continue
break
if is_valid(board,row,col) is True:
return row,col
else:
print("This place is taken")
continue
def is_win(board,player):
row1 = board[0]
row2 = board[1]
row3 = board[2]
if row1[0] == row1[1] == row1[2] != '_':
print(player + " Has won!")
return True
elif row2[0] == row2[1] == row2[2] != '_':
print(player + " Has won!")
return True
elif row3[0] == row3[1] == row3[2] != '_':
print(player + " Has won!")
return True
elif row1[0] == row2[0] == row3[0] != '_':
print(player + " Has won!")
return True
elif row1[1] == row2[1] == row3[1] != '_':
print(player + " Has won!")
return True
elif row1[2] == row2[2] == row3[2] != '_':
print(player + " Has won!")
return True
elif row1[0] == row2[1] == row3[2] != '_':
print(player + " Has won!")
return True
elif row1[2] == row2[1] == row3[0] != '_':
print(player + " Has won!")
return True
else:
return False
def game():
player = 'X'
print_board(board)
while is_win(board, player) is False:
if is_full(board) is True and is_win(board, player) is False:
print("draw")
break
elif is_full(board) is True and is_win(board, player) is True:
print(is_win(board, player))
break
row, col = get_user_input()
set_cell(board, row, col, player)
player = switch_turn(player)
game()
我认为这是因为,在您的游戏结束之前,玩家在 game() 函数的位置进行了切换。
player = switch_turn(player)
当 X 做出获胜动作时,玩家被切换,当前玩家现在是玩家 'O'。一种想法是在切换前检查 is_win。
在不重写太多代码的情况下,我只是稍微重新安排一下代码。您不必在循环开始时检查它是赢还是平,因为它在游戏开始时永远不会为真。这意味着您可以请求输入、设置单元格,然后才检查 win/draw。这让你在切换回合之前写出获胜者。
您也不想在 is_win 函数中打印任何内容。因为这是你的 while 循环标准,它会在代码执行后 运行 函数,并使它 运行 与错误的播放器一起使用。通过不在 is_win 函数中打印来解决此问题,仅在满足 if 语句时打印。
另一个问题是,在您的 elif (win) 语句中,您检查了 board is_full AND is_win 是否。只有当你在最后一个位置获胜时才会发生这种情况。
您也不必真的写出“是假”或“是真”。我认为如果您删除它们并使用单词 not 来代替 False 会更容易阅读。
游戏循环的粗略代码:
def game():
player = 'X'
print_board(board)
while not is_win(board, player):
row, col = get_user_input()
set_cell(board, row, col, player)
if is_full(board) and not is_win(board, player):
print("draw")
break
elif is_win(board, player):
print(player + " has won!")
break
player = switch_turn(player)
我在 python 中做了一个井字游戏,当 X 赢时,它表明 O 赢了,当 O 赢时,它表明 X 赢了。我很确定问题是它改变了玩家,然后检查是否有人赢了,我试着让它在玩家切换之前切换,但它仍然没有用。 我还尝试在 is_win 函数中更改播放器,但这也没有解决问题。 有人可以看看这个并帮我解决这个问题吗?
initial_board = [['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']]
board = initial_board
def empty_board(): #use board = empty_board() everytime you want to empty the board
board = [['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']]
return(board)
def switch_turn(player): #use player = switch_turn(player) everytime you want to switch players
if player == 'X':
return 'O'
return 'X'
def print_board(board):
print(*board, sep = "\n")
def is_full(board):
return all('_' not in box for box in board)
def is_valid(board, row, col):
x = board[row]
if x[col] == '_':
return True
return False
def set_cell(board, row, col, player):
x = board[row]
if is_valid(board,row,col) is True:
x[col] = player
print_board(board)
else:
print("already taken")
def get_user_input():
while True:
while True:
row = int(input("Enter the row you want (0-2): "))
if row > 2 or row < 0:
print("The number you entered is not valid")
continue
break
while True:
col = int(input("Enter the column you want (0-2): "))
if col > 2 or col < 0:
print("The number you entered is not valid")
continue
break
if is_valid(board,row,col) is True:
return row,col
else:
print("This place is taken")
continue
def is_win(board,player):
row1 = board[0]
row2 = board[1]
row3 = board[2]
if row1[0] == row1[1] == row1[2] != '_':
print(player + " Has won!")
return True
elif row2[0] == row2[1] == row2[2] != '_':
print(player + " Has won!")
return True
elif row3[0] == row3[1] == row3[2] != '_':
print(player + " Has won!")
return True
elif row1[0] == row2[0] == row3[0] != '_':
print(player + " Has won!")
return True
elif row1[1] == row2[1] == row3[1] != '_':
print(player + " Has won!")
return True
elif row1[2] == row2[2] == row3[2] != '_':
print(player + " Has won!")
return True
elif row1[0] == row2[1] == row3[2] != '_':
print(player + " Has won!")
return True
elif row1[2] == row2[1] == row3[0] != '_':
print(player + " Has won!")
return True
else:
return False
def game():
player = 'X'
print_board(board)
while is_win(board, player) is False:
if is_full(board) is True and is_win(board, player) is False:
print("draw")
break
elif is_full(board) is True and is_win(board, player) is True:
print(is_win(board, player))
break
row, col = get_user_input()
set_cell(board, row, col, player)
player = switch_turn(player)
game()
我认为这是因为,在您的游戏结束之前,玩家在 game() 函数的位置进行了切换。
player = switch_turn(player)
当 X 做出获胜动作时,玩家被切换,当前玩家现在是玩家 'O'。一种想法是在切换前检查 is_win。
在不重写太多代码的情况下,我只是稍微重新安排一下代码。您不必在循环开始时检查它是赢还是平,因为它在游戏开始时永远不会为真。这意味着您可以请求输入、设置单元格,然后才检查 win/draw。这让你在切换回合之前写出获胜者。
您也不想在 is_win 函数中打印任何内容。因为这是你的 while 循环标准,它会在代码执行后 运行 函数,并使它 运行 与错误的播放器一起使用。通过不在 is_win 函数中打印来解决此问题,仅在满足 if 语句时打印。
另一个问题是,在您的 elif (win) 语句中,您检查了 board is_full AND is_win 是否。只有当你在最后一个位置获胜时才会发生这种情况。
您也不必真的写出“是假”或“是真”。我认为如果您删除它们并使用单词 not 来代替 False 会更容易阅读。
游戏循环的粗略代码:
def game():
player = 'X'
print_board(board)
while not is_win(board, player):
row, col = get_user_input()
set_cell(board, row, col, player)
if is_full(board) and not is_win(board, player):
print("draw")
break
elif is_win(board, player):
print(player + " has won!")
break
player = switch_turn(player)