viligen

tic_tac_toe_2

Oct 5th, 2021
892
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. import numpy
  2.  
  3. line1 = input().split()
  4. line2 = input().split()
  5. line3 = input().split()
  6. matrix = numpy.array([line1, line2, line3])
  7.  
  8. cntr_first = 0
  9. cntr_second = 0
  10. winner = " "
  11.  
  12.  
  13. if (set(line1) == {"1"} and len(set(line1)) == 1) or (set(line2) == {"1"} and len(set(line2)) == 1) or (set(line3) == {"1"} and len(set(line3)) == 1):
  14.     winner = "First"
  15. elif (set(line1) == {"2"} and len(set(line1)) == 1) or (set(line2) == {'2'} and len(set(line2)) == 1) or (set(line3) == {"2"} and len(set(line3)) == 1):
  16.     winner = "Second"
  17. for col in range(len(matrix)):
  18.     for row in range(len(matrix)):
  19.         if matrix[row, col] == "1":
  20.             cntr_first += 1
  21.         elif matrix[row, col] == "2":
  22.             cntr_second += 1
  23.         if cntr_first == 3:
  24.             winner = "First"
  25.             break
  26.         elif cntr_second == 3:
  27.             winner = "Second"
  28.             break
  29.     cntr_first = 0
  30.     cntr_second = 0
  31. cntr_first = 0
  32. cntr_second = 0
  33. for i in range(len(matrix)):
  34.     if matrix[i, i] == "1":
  35.         cntr_first += 1
  36.     elif matrix[i, i] == "2":
  37.         cntr_second += 1
  38.     if cntr_first == 3:
  39.         winner = "First"
  40.         break
  41.     elif cntr_second == 3:
  42.         winner = "Second"
  43.         break
  44. if matrix[0, 2] == "1" and matrix[1, 1] == "1" and matrix[2, 0] == "1":
  45.     winner = "First"
  46. elif matrix[0, 2] == "2" and matrix[1, 1] == "2" and matrix[2, 0] == "2":
  47.     winner = "Second"
  48.  
  49.  
  50. if winner == " ":
  51.     print("Draw!")
  52. else:
  53.     print(f"{winner} player won")
  54.  
Advertisement
Add Comment
Please, Sign In to add comment