我的井字游戏存在问题。在def play_game的第一个循环之后,游戏总是以平局结束。我认为这是由于我的check_for_tie函数中的布尔值引起的。我尝试使用值无济于事。笨蛋总是给我麻烦。
另外,如果我删除平局部分以测试是否有赢家,则只有在两名玩家都去了之后才打破循环。
# Global Variables
grid_values = [[" ", " ", " "],
[" ", " ", " "],
[" ", " ", " "]]
winner = None
game_still_going = True
def display_title():
print("Welcome to Tic Tac Toe")
print()
def print_blank_board():
print("+---+---+---+")
print("|\t|\t|\t|")
print("+---+---+---+")
print("|\t|\t|\t|")
print("+---+---+---+")
print("|\t|\t|\t|")
print("+---+---+---+")
print()
# Function to test if the players move is a untaken spot
def grid_test(xcord, ycord, grid):
if grid[xcord][ycord] == 1:
print("Space is already taken. Please select a different space:")
return False
elif grid[xcord][ycord] == 0:
grid[xcord][ycord] = 1
return True
# checks row for a winner
def check_rows():
# Set global variables
global game_still_going
# Check if any of the rows have all the same value (and is not empty)
row_1 = grid_values[0][0] == grid_values[0][1] == grid_values[0][2] != " "
row_2 = grid_values[1][0] == grid_values[1][1] == grid_values[1][2] != " "
row_3 = grid_values[2][0] == grid_values[2][1] == grid_values[2][2] != " "
# If any row does have a match, flag that there is a win
if row_1 or row_2 or row_3:
game_still_going = False
# Return the winner
if row_1:
return grid_values[0][0]
elif row_2:
return grid_values[0][0]
elif row_3:
return grid_values[0][0]
# Or return None if there was no winner
else:
return None
# checks column for a winner
def check_columns():
# Set global variables
global game_still_going
# Check if any of the rows have all the same value (and is not empty)
column_1 = grid_values[0][0] == grid_values[1][0] == grid_values[2][0] != " "
column_2 = grid_values[0][1] == grid_values[1][1] == grid_values[2][1] != " "
column_3 = grid_values[0][2] == grid_values[1][2] == grid_values[2][2] != " "
# If any row does have a match, flag that there is a win
if column_1 or column_2 or column_3:
game_still_going = False
# Return the winner
if column_1:
return grid_values[0][0]
elif column_2:
return grid_values[0][1]
elif column_3:
return grid_values[0][2]
# Or return None if there was no winner
else:
return None
# # checks diagonals for a winner
def check_diagonals():
# Set global variables
global game_still_going
# Check if any of the rows have all the same value (and is not empty)
diagonal_1 = grid_values[0][0] == grid_values[1][1] == grid_values[2][2] != " "
diagonal_2 = grid_values[0][2] == grid_values[1][1] == grid_values[2][0] != " "
# If any row does have a match, flag that there is a win
if diagonal_1 or diagonal_2:
game_still_going = False
# Return the winner
if diagonal_1:
return grid_values[0][0]
elif diagonal_2:
return grid_values[0][1]
# Or return None if there was no winner
else:
return None
# at the end of of the play_game loop it runs this function that calls 2 more functions to check if there is a winner or a tie
def check_if_game_over():
check_for_winner()
check_for_tie()
# checks for a winner and returns who won.
def check_for_winner():
# Set global variables
global winner
# Check if there was a winner anywhere
row_winner = check_rows()
column_winner = check_columns()
diagonal_winner = check_diagonals()
# Get the winner
if row_winner:
winner = row_winner
elif column_winner:
winner = column_winner
elif diagonal_winner:
winner = diagonal_winner
else:
winner = None
# check if the game is a tie or not
def check_for_tie():
global game_still_going
# If board is full
if " " not in grid_values:
game_still_going = False
return True
# Else there is no tie
else:
return False
def play_game():
turn = 1
grid_tester = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
test = False
while game_still_going:
#print("turn = ", turn) #turn counter. here just for testing.
print("X's Turn")
xtest = False
otest = False
# ASK user to select a row FOR X
while xtest == False:
xrow = int(input("pick a row (1, 2, 3): "))
xrow += - 1
while xrow < 0 or xrow >= 3:
if xrow not in range(0 - 3):
print("Invalid option!!!")
xrow = int(input("pick a row (1, 2, 3): "))
xrow += - 1
# ASk user to select a column FOR X
xcolumn = int(input("pick a column (1, 2, 3): "))
xcolumn += - 1
while xcolumn < 0 or xcolumn >= 3:
if xcolumn not in range(0 - 3):
print("Invalid option!!!")
xcolumn = int(input("pick a column (1, 2, 3): "))
xcolumn += - 1
xtest = grid_test(xrow, xcolumn, grid_tester)
if xtest == True:
grid_values[xrow][xcolumn] = "X"
turn += 1
check_if_game_over()
#turn += 1
print()
print("+---+---+---+")
print("|", grid_values[0][0], "|", grid_values[0][1], "|", grid_values[0][2], "|")
print("+---+---+---+")
print("|", grid_values[1][0], "|", grid_values[1][1], "|", grid_values[1][2], "|")
print("+---+---+---+")
print("|", grid_values[2][0], "|", grid_values[2][1], "|", grid_values[2][2], "|")
print("+---+---+---+")
print()
print("turn = ", turn) ## here for testing. REMOVE!!!!!!!
print("O's Turn")
# ASK user to select a row FOR O
while otest == False:
orow = int(input("pick a row (1, 2, 3): "))
orow += - 1
while orow < 0 or orow >= 3:
if orow not in range(0 - 3):
print("Invalid option!!!")
orow = int(input("pick a row (1, 2, 3): "))
orow += - 1
# ASk user to select a column FOR O
ocolumn = int(input("pick a column (1, 2, 3): "))
ocolumn += - 1
while ocolumn < 0 or ocolumn >= 3:
if ocolumn not in range(0 - 3):
print("Invalid option!!!")
ocolumn = int(input("pick a column (1, 2, 3): "))
ocolumn += - 1
print(turn) ## here for testing. REMOVE!!!!!!!
otest = grid_test(orow, ocolumn, grid_tester)
if otest == True:
grid_values[orow][ocolumn] = "O"
turn += 1
print()
print("+---+---+---+")
print("|", grid_values[0][0], "|", grid_values[0][1], "|", grid_values[0][2], "|")
print("+---+---+---+")
print("|", grid_values[1][0], "|", grid_values[1][1], "|", grid_values[1][2], "|")
print("+---+---+---+")
print("|", grid_values[2][0], "|", grid_values[2][1], "|", grid_values[2][2], "|")
print("+---+---+---+")
print()
check_if_game_over()
if winner == "X" or winner == "O":
print(winner + " won.")
elif winner is None:
print("Game is a tie!")
def main():
display_title()
print_blank_board()
play_game()
# if started as the main module, call the main function
if __name__ == "__main__":
main()
答案 0 :(得分:2)
在check_for_tie
中,您可以编写:
编辑:
global game_still_going
full_rows = 0
for i in range(3):
if " " not in grid_values[i]:
full_rows += 1
if full_rows == 3:
game_still_going = False
return True
else:
return False
添加此
if not game_still_going:
break
一段时间内的第一个check_if_game_over()之后