我一直在用Python编写这个井字游戏程序进行练习。我的问题是我的newgame
和player2
方法。 if
语句块根本不打印,当我格式化它们时,if
和elif
语句都被打印了,我认为这是一个缩进问题,但是美化并没有解决要么。
我该如何解决这个问题?
playercount = 0
game = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
print("Player 1 is X Player 2 is O")
if playercount == 0:
playercount += 1
def whosgo():
mod = playercount % 2
if mod > 0:
print("it's player 1's turn")
else:
print("it's player 2's turn, O's")
def createboard():
tcolumns = " | " * 2
print(tcolumns)
rows = " ---------------"
print(rows)
print(tcolumns)
print(rows)
print(tcolumns)
def newgame():
playercount = 0
row = input("what row do you want to go in? ")
column = input("and what column? ")
rows = " ---------------"
tcolumns = " | " * 2
if row == 1 and column == 1:
print(" X | | ")
print(rows)
print(tcolumns)
print(rows)
print(tcolumns)
playercount += 1
game.insert(0, game[1])
elif row == 2 and column == 2:
print(" X | O | ")
print(rows)
print(" | X | ")
print(rows)
print(tcolumns)
playercount += 1
def player2():
print("it's player 2's turn")
playercount = 1
row = input("what row do you want to go in? ")
column = input("and what column? ")
if row == 1 and column == 2:
game.insert(1, game[2])
tcolumns = " | " * 2
print(" X | O | ")
rows = " ---------------"
print(rows)
print(tcolumns)
print(rows)
print(tcolumns)
playercount += 1
elif row == 2 and column == 2:
game.insert(1, game[2])
tcolumns = " | " * 2
print(" O | | ")
rows = " ---------------"
print(rows)
print(tcolumns)
print(rows)
print(tcolumns)
playercount += 1
# print(playercount)
def whowon():
winner_is_2 = [[2, 2, 0],
[2, 1, 0],
[2, 1, 1]]
winner_is_2d = [[2, 2, 2],
[2, 1, 0],
[0, 1, 1]]
winner_is_1 = [[1, 2, 0],
[2, 1, 0],
[2, 1, 1]]
no_winner = [[1, 2, 0],
[2, 1, 0],
[2, 1, 2]]
also_no_winner = [[1, 2, 0],
[2, 1, 0],
[2, 1, 0]]
if game == winner_is_2:
print("congrats X wins!!")
elif game == winner_is_2d:
print("congrats X wins!")
elif game == winner_is_1:
print("congrats O wins!!")
elif no_winner == game:
print("it's a tie!!")
elif also_no_winner == game:
print("it's a tie!!")
while game != winner_is_2 and game != winner_is_2d and game != winner_is_1 and game != no_winner and game != also_no_winner:
whosgo()
newgame()
player2()
createboard()
whowon()