我用我的逻辑来做这个游戏。不过我觉得代码很长,谁能帮我缩短代码,帮我理解怎么写?我应该创建函数吗?另外,当这个地方被占用时,它应该显示消息“place is already taked”,这只是第一次发生,第二次当我输入相同的数字时,它就通过了。代码如下:
board = [" " for i in range(9)]
def print_board():
row1="|{}|{}|{}|".format(board[0],board[1],board[2])
row2="|{}|{}|{}|".format(board[3],board[4],board[5])
row3="|{}|{}|{}|".format(board[6],board[7],board[8])
print()
print(row1)
print(row2)
print(row3)
print()
while True:
print_board()
choice=int(input("enter your choice, player 1").strip())
if board[choice-1]==" ":
board[choice-1]= "X"
print_board()
else:
print("space is taken, try again")
choice=int(input("enter your choice, player 1").strip())
if board[choice-1]==" ":
board[choice-1]= "X"
print_board()
choice=int(input("enter your choice, player 2").strip())
if board[choice-1]==" ":
board[choice-1]= "0"
else:
print("space is taken")
choice=int(input("enter your choice, player 2").strip())
if board[choice-1]==" ":
board[choice-1]= "X"
print_board()
相关问题 更多 >
编程相关推荐