Guest User

Untitled

a guest
May 24th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. """Given a 3 by 3 list of lists that represents a Tic Tac Toe game board, tell me whether anyone has won, and tell me which player won, if any.
  2. Where a 0 means an empty square, a 1 means that player 1 put their token in that space, and a 2 means that player 2 put their token in that space."""
  3.  
  4. def Check_hor(board):
  5. """Checks whether a player had 3 in a row horizontally"""
  6. win = 0
  7. rowa = board[0]
  8. rowb = board[1]
  9. rowc = board[2]
  10. if rowa.count(1) == 3 or rowb.count(1) == 3 or rowc.count(1) == 3:
  11. win = 1
  12. elif rowa.count(2) == 3 or rowb.count(2) == 3 or rowc.count(2) == 3:
  13. win = 2
  14. return win
  15.  
  16. def Check_ver(board):
  17. """Checks whether a player had 3 in a row vertically"""
  18. win = 0
  19. ver0 = []
  20. ver1 = []
  21. ver2 = []
  22. for i in range(3):
  23. ver0.append(board[i][0])
  24. ver1.append(board[i][1])
  25. ver2.append(board[i][2])
  26. if ver0.count(1) == 3 or ver1.count(1) == 3 or ver2.count(1) == 3:
  27. win = 1
  28. elif ver0.count(2) == 3 or ver1.count(2) == 3 or ver2.count(21) == 3:
  29. win = 2
  30. return win
  31.  
  32. def Check_dia(board):
  33. """Checks whether a player had 3 in a row diagonally"""
  34. win = 0
  35. diaM = [board[i][i] for i in range(3)]
  36. diam = [board[i][2 - i] for i in range(3)]
  37. if diaM.count(1) == 3 or diam.count(1) == 3:
  38. win = 1
  39. elif diaM.count(2) == 3 or diam.count(2) == 3:
  40. win = 2
  41. return win
  42.  
  43. if __name__=="__main__":
  44. nums = [int(x) for x in input('Enter a board as 9 integers:\n').split()]
  45. rowa = nums[:3]
  46. rowb = nums[3:6]
  47. rowc = nums[6:]
  48. board = [rowa, rowb, rowc]
  49. if Check_hor(board) == 1 or Check_ver(board) == 1 or Check_dia(board) == 1:
  50. print('Player 1 wins!')
  51. elif Check_hor(board) == 2 or Check_ver(board) == 2 or Check_dia(board) == 1:
  52. print('Player 2 wins!')
  53. else:
  54. print('No winner')
Add Comment
Please, Sign In to add comment