如何使用嵌套列表实现领带功能到井字游戏

时间:2018-05-17 05:03:52

标签: nested-loops

在我的游戏中,我试图通过在我的胜利确定者下使用else添加一个平局功能,但我遇到了麻烦。有人可以帮助我在胜利确定者下的代码中添加一个平局功能吗?我曾尝试在胜利确定者下添加其他内容,但领带将最终打印出游戏已经随机结束。一些帮助将不胜感激,谢谢!

player1 = str(input("What is player X's name?"))
print()
player2 = str(input("What is player O's name?"))
turn = 1
location = [
  [' ',' ',' '],
  [' ',' ',' '],
  [' ',' ',' '],
]
while turn <= 9:
  if turn % 2  == 1:
    print()
    print("It is currently " +player1+ "'s turn")
    row = int(input("What row would you like to mark?"))-1
    print()
    col = int(input(" What column would you like to mark?"))-1
    print()
    while "O" in location[row][col]:
      print(player1+" that spot has already been taken")
      print("Please pick again")
      row = int(input("What row would you like to mark?"))-1
      print()
      col = int(input(" What column would you like to mark?"))-1
      print()
    location[row][col] = "X"
    print(location[0][0] + '|' + location[0][1] + '|' + location[0][2])
    print("-----")
    print(location[1][0] + '|' + location[1][1] + '|' + location[1][2])
    print("-----")
    print(location[2][0] + '|' + location[2][1] + '|' + location[2][2])
    turn = turn + 1
    if ((location[0][0] == 'X' and location[0][1] == 'X' and location[0][2] 
    == 'X') or 
      (location[1][0] == 'X' and location[1][1] == 'X' and location[1][2] == 
    'X') or 
      (location[2][0] == 'X' and location[2][1] == 'X' and location[2][2] == 
    'X') or
      (location[0][0] == 'X' and location[1][0] == 'X' and location[2][0] == 
    'X') or 
      (location[0][1] == 'X' and location[1][1] == 'X' and location[2][1] == 
    'X') or
      (location[0][2] == 'X' and location[1][2] == 'X' and location[2][2] == 
    'X') or
      (location[0][0] == 'X' and location[1][1] == 'X' and location[2][2] == 
    'X') or 
      (location[0][2] == 'X' and location[1][1] == 'X' and location[2][0] == 
    'X')):
      print (player1+" won the game!")
      turn = 10
    else:
      print("The game ended in a tie!")
  else:
    print("It is currently " +player2+ "'s turn")
    row = int(input("What row would you like to mark?"))-1
    print()
    col = int(input(" What column would you like to mark?"))-1
    print()
    while "X" in location[row][col]:
      print(player2+" that spot has already been taken")
      print("Please pick again")
      row = int(input("What row would you like to mark?"))-1
      print()
      col = int(input(" What column would you like to mark?"))-1
      print()
    location[row][col] = "O"
    print(location[0][0] + '|' + location[0][1] + '|' + location[0][2])
    print("-----")
    print(location[1][0] + '|' + location[1][1] + '|' + location[1][2])
    print("-----")
    print(location[2][0] + '|' + location[2][1] + '|' + location[2][2])
    turn = turn + 1
    if ((location[0][0] == 'O' and location[0][1] == 'O' and location[0][2] 
    == 'O') or 
      (location[1][0] == 'O' and location[1][1] == 'O' and location[1][2] == 
    'O') or 
      (location[2][0] == 'O' and location[2][1] == 'O' and location[2][2] == 
    'O') or
      (location[0][0] == 'O' and location[1][0] == 'O' and location[2][0] == 
    'O') or 
      (location[0][1] == 'O' and location[1][1] == 'O' and location[2][1] == 
    'O') or
      (location[0][2] == 'O' and location[1][2] == 'O' and location[2][2] == 
    'O') or
      (location[0][0] == 'O' and location[1][1] == 'O' and location[2][2] == 
    'O') or 
      (location[0][2] == 'O' and location[1][1] == 'O' and location[2][0] == 
    'O')):
      print (player2+" won the game!")
      turn = 10
    else:
      print("The game ended in a tie!")

1 个答案:

答案 0 :(得分:0)

你的问题是,代码检查每一回合如果玩家转过来就赢了游戏,如果他没有赢(还)它打印你的&#34;否则&# 34;意思是The game ended in a tie!

您的解决方案是向您添加另一个if:,检查是否所有9个字段都填充了XO,如果是这样,您必须打印您的The game ended in a tie!