首页 > 解决方案 > 如何使用嵌套列表在井字游戏中实现 Tie 功能

问题描述

在我的游戏中,我试图通过在我的获胜确定器下使用 else 添加一个平局功能,但我遇到了麻烦。有人可以帮我在 win 确定器下的代码中添加一个 tie 函数吗?我试图在获胜判定器下添加其他内容,但平局最终会打印出游戏随机以平局结束的信息。一些帮助将不胜感激,谢谢!

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!")

标签: nested-loops

解决方案


你的问题是,如果轮到它的玩家赢得了游戏,代码会在每一回合后检查,如果他没有赢(还)它会打印你的“else”,意思是The game ended in a tie!

您的解决方案是向您添加另一个if:else-es 来检查是否所有 9 个字段都已填写,X或者O如果是这种情况,您必须打印您的The game ended in a tie!


推荐阅读