PhotoShaman

Корявые крестики нолики на питоне

Aug 16th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. def writeMap(map):
  2.     p = ""
  3.     for i in range(9):
  4.         p += " {0}".format(map[i])
  5.         if (i + 1) % 3 == 0:
  6.             print(p + "\n")
  7.             p = ""
  8.  
  9.  
  10. def setCell(map, turnX):
  11.     cell = -1
  12.     while True:
  13.         command = input("Выберите ячейку(1-9): ")
  14.         if command == "restart":
  15.             main()
  16.         elif command == "exit":
  17.             exit()
  18.         elif command == "help":
  19.             print("\trestart - начать игру заново\n\texit - выход")
  20.         else:
  21.             try:
  22.                 cell = int(command)
  23.                 if (cell > 0 and cell < 10 and map[cell - 1] == '-'):
  24.                     return map[:cell - 1] + XorO(turnX) + map[cell:]
  25.             except ValueError:
  26.                 pass
  27.  
  28.  
  29. def XorO(turnX):
  30.     return 'X' if turnX else 'O'
  31.  
  32.  
  33. def clear():
  34.     print("\n" * 100)
  35.  
  36.  
  37. def checkmap(map):
  38.     combo = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6],
  39.              [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]]
  40.     for i in combo:
  41.         if (map[i[0]] == map[i[1]] == map[i[2]] != "-"):
  42.             result("Победа {}".format(map[i[0]]))
  43.  
  44.  
  45. def result(res):
  46.     print(res)
  47.     while True:
  48.         ans = input("Играть еще?(Y/N)").lower()
  49.         print(ans) //тут шо не нажмешь игра заново будет. Раньше работало..
  50.         if (ans == "y" or "д" or "+"):
  51.             main()
  52.         elif (ans == "n" or "н" or "-"):
  53.             print("123123")
  54.             exit()
  55.  
  56.  
  57. def main():
  58.     turnX = True
  59.     map = '-' * 9
  60.     for _ in range(9):
  61.         clear()
  62.         writeMap(map)
  63.         print("Ход {}:".format(XorO(turnX)))
  64.         map = setCell(map, turnX)
  65.         checkmap(map)
  66.         turnX = not turnX
  67.     result("Ничья")
  68.  
  69.  
  70. if __name__ == '__main__':
  71.     main()
Advertisement
Add Comment
Please, Sign In to add comment