Guest User

Untitled

a guest
Dec 13th, 2017
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. # Omer Fadil Usta < omerusta@gmail.com > , 2017
  5. # Tic Tac Toe 1.3
  6.  
  7. import sys,os,time
  8. clearScreen = lambda : os.system("clear") or None
  9. board = [["---","---","---"],
  10. ["---","---","---"],
  11. ["---","---","---"]]
  12.  
  13. towin =[[[0,0],[0,1],[0,2]],
  14. [[1,0],[1,1],[1,2]],
  15. [[2,0],[2,1],[2,2]],
  16. [[0,0],[1,0],[2,0]],
  17. [[0,1],[1,1],[2,1]],
  18. [[0,2],[1,2],[2,2]],
  19. [[0,0],[1,1],[2,2]],
  20. [[0,2],[1,1],[2,0]]]
  21.  
  22. def showBoard():
  23. clearScreen()
  24. print ("{:>21} {:>4} {:>4}".format(1,2,3))
  25. for i in range(3):
  26. print("{:>18} {:^4} {:^4} {:^4}".format(i+1,board[i][0],board[i][1],board[i][2]))
  27.  
  28. def takeInput(title):
  29. input_check = ["1", "2", "3", "q", "Q"]
  30. while True:
  31. inp = input("{} - enter [1,2,3 or q to quit] :".format(title).ljust(40))
  32. if inp in input_check:
  33. if inp.lower() == "q":
  34. print("ByeBye")
  35. sys.exit(0)
  36. return int(inp)
  37. break
  38. else:
  39. print("Wrong Entry. RETRY Again !")
  40. time.sleep(3)
  41. showBoard()
  42.  
  43. store={"X":[],"O":[]}
  44. situation={"X":[],"O":[]}
  45.  
  46. turn = 1
  47.  
  48. while True:
  49. showBoard()
  50. sign = "X" if turn % 2 == 0 else "O"
  51. print("Player {}\n".format(sign))
  52. x = takeInput("From top to down") -1
  53. y = takeInput("From left to right") -1
  54.  
  55. if board[x][y] == "---":
  56. board[x][y] = sign
  57. store[sign].append([x,y])
  58. turn += 1
  59. else:
  60. print("\nOops! Not an empty space. Try again.\n")
  61. time.sleep(3)
  62.  
  63. showBoard()
  64.  
  65. for i in towin:
  66. situation["X"]=[z for z in i if z in store["X"]]
  67. situation["O"]=[z for z in i if z in store["O"]]
  68.  
  69. if len(situation["X"]) == len(i):
  70. print(" X WON!")
  71. sys.exit(0)
  72. elif len(situation["O"]) == len(i):
  73. print(" O WON!")
  74. sys.exit(0)
  75. if turn == 10:
  76. print(" DRAW!")
  77. sys.exit(0)
Add Comment
Please, Sign In to add comment