Advertisement
CodingGuyTutorials

Tic-Tac-Toe | Python

Jan 17th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.54 KB | None | 0 0
  1. import random, array, sys
  2.  
  3. s = array.array("u", [" ", " ", " ", " ", " ", " ", " ", " ", " "])
  4. b = "-|-|-"
  5.  
  6. def board():
  7.     print(f"\n{s[0]}|{s[1]}|{s[2]}\n{b}\n{s[3]}|{s[4]}|{s[5]}\n{b}\n{s[6]}|{s[7]}|{s[8]}")
  8.  
  9. def win():
  10.     if s[0] == "x" and s[1] == "x" and s[2] == "x" or s[3] == "x" and s[4] == "x" and s[5] == "x" or s[6] == "x" and s[7] == "x" and s[8] == "x" or s[0] == "x" and s[3] == "x" and s[6] == "x" or s[1] == "x" and s[4] == "x" and s[7] == "x" or s[2] == "x" and s[5] == "x" and s[8] == "x" or s[0] == "x" and s[4] == "x" and s[8] == "x" or s[2] == "x" and s[4] == "x" and s[6] == "x":
  11.         print("Player 1 wins!")
  12.         input("")
  13.     elif s[0] == "o" and s[1] == "o" and s[2] == "o" or s[3] == "o" and s[4] == "o" and s[5] == "o" or s[6] == "o" and s[7] == "o" and s[8] == "o" or s[0] == "o" and s[3] == "o" and s[6] == "o" or s[1] == "o" and s[4] == "o" and s[7] == "o" or s[2] == "o" and s[5] == "o" and s[8] == "o" or s[0] == "o" and s[4] == "o" and s[8] == "o" or s[2] == "o" and s[4] == "o" and s[6] == "o":
  14.         print("Player 2 wins!")
  15.         input("")
  16.        
  17. def turns():
  18.     if choice == "1":
  19.         s[0] = psymbol
  20.     elif choice == "2":
  21.         s[1] = psymbol
  22.     elif choice == "3":
  23.         s[2] = psymbol
  24.     elif choice == "4":
  25.         s[3] = psymbol
  26.     elif choice == "5":
  27.         s[4] = psymbol
  28.     elif choice == "6":
  29.         s[5] = psymbol
  30.     elif choice == "7":
  31.         s[6] = psymbol
  32.     elif choice == "8":
  33.         s[7] = psymbol
  34.     elif choice == "9":
  35.         s[8] = psymbol
  36.     else:
  37.         print("Please enter a number between 1 and 9!")
  38.        
  39. def chosen():
  40.     print("That square has already been chosen!")
  41.  
  42. print("Welcome to Tic-Tac-Toe!\nHow To Play:\nEnter a number between 1 and 9 which correlates to a square on the board below:")
  43. print(f"\n1|2|3\n{b}\n4|5|6\n{b}\n7|8|9")
  44.  
  45. choice = p1t1 = input("\nPlayer 1. Choose a square:\n")
  46. psymbol = "x"
  47. turns()
  48. board()
  49. win()
  50.  
  51. choice = p2t1 = input("\nPlayer 2. Choose a square:\n")
  52. psymbol = "o"
  53. if p2t1 == p1t1:
  54.     chosen()
  55. else:
  56.     turns()
  57. board()
  58. win()
  59.  
  60. choice = p1t2 = input("\nPlayer 1. Choose a 2nd square:\n")
  61. psymbol = "x"
  62. if p1t2 == p1t1 or p1t2 == p2t1:
  63.     chosen()
  64. else:
  65.     turns()
  66. board()
  67. win()
  68.  
  69. choice = p2t2 = input("\nPlayer 2. Choose a 2nd square:\n")
  70. psymbol = "o"
  71. if p2t2 == p1t1 or p2t2 == p1t2 or p2t2 == p2t1:
  72.     chosen()
  73. else:
  74.     turns()
  75. board()
  76. win()
  77.  
  78. choice = p1t3 = input("\nPlayer 1. Choose a 3rd square:\n")
  79. psymbol = "x"
  80. if p1t3 == p1t1 or p1t3 == p1t2 or p1t3 == p2t1 or p1t3 == p2t2:
  81.     chosen()
  82. else:
  83.     turns()
  84. board()
  85. win()
  86.  
  87. choice = p2t3 = input("\nPlayer 2. Choose a 3rd square:\n")
  88. psymbol = "o"
  89. if p2t3 == p1t1 or p2t3 == p1t2 or p2t3 == p1t3 or p2t3 == p2t1 or p2t3 == p2t2:
  90.     chosen()
  91. else:
  92.     turns()
  93. board()
  94. win()
  95.  
  96. choice = p1t4 = input("\nPlayer 1. Choose a 4th square:\n")
  97. psymbol = "x"
  98. if p1t4 == p1t1 or p1t4 == p1t2 or p1t4 == p1t3 or p1t4 == p2t1 or p1t4 == p2t2 or p1t4 == p2t3:
  99.     chosen()
  100. else:
  101.     turns()
  102. board()
  103. win()
  104.  
  105. choice = p2t4 = input("\nPlayer 2. Choose a 4th square:\n")
  106. psymbol = "o"
  107. if p2t4 == p1t1 or p2t4 == p1t2 or p2t4 == p1t3 or p2t4 == p1t4 or p2t4 == p2t1 or p2t4 == p2t2 or p2t4 == p2t3:
  108.     chosen()
  109. else:
  110.     turns()
  111. board()
  112. win()
  113.  
  114. choice = p1t5 = input("\nPlayer 1. Choose a 5th square:\n")
  115. psymbol = "x"
  116. if p1t5 == p1t1 or p1t5 == p1t2 or p1t5 == p1t3 or p1t5 == p1t4 or p1t5 == p2t1 or p1t5 == p2t2 or p1t5 == p2t3 or p1t5 == p2t4:
  117.     chosen()
  118. else:
  119.     turns()
  120. board()
  121. win()
  122. print("It's a draw!")
  123. input("")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement