Advertisement
Guest User

Untitled

a guest
Feb 9th, 2017
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. board = """
  2.                 TIC-TAC-TOE
  3.    
  4.      
  5.                 |       |      
  6.              1  |   2   |   3    
  7.          -------|-------|-------
  8.                 |       |      
  9.              4  |   5   |   6    
  10.          -------|-------|-------
  11.                 |       |      
  12.              7  |   8   |   9
  13.                 |       |
  14. """
  15. allowed_chars = ["1","2","3","4","5","6","7","8","9"]
  16. already_played = []
  17.  
  18. x = "X"
  19. o = "O"
  20. number = 0
  21.  
  22. #[score]
  23. xscore = {
  24. "123":0,
  25. "456":0,
  26. "789":0,
  27. "147":0,
  28. "258":0,
  29. "369":0,
  30. "159":0,
  31. "357":0,
  32. }
  33. oscore = {
  34. "123":0,
  35. "456":0,
  36. "789":0,
  37. "147":0,
  38. "258":0,
  39. "369":0,
  40. "159":0,
  41. "357":0,
  42. }
  43.  
  44. def adjust_score(who_score,num):
  45.     for i in who_score:
  46.         if num in i:
  47.             who_score[i] += 1
  48.        
  49.  
  50. def check_score(who_score,who):
  51.     catsgame = len(already_played)
  52.     for combo,score in who_score.items():
  53.         if score >=3:
  54.             print(who,"Wins")
  55.             no_winner = False
  56.             return no_winner
  57.         elif catsgame == 9 and score  < 3:
  58.             print("Cats Game")
  59.             no_winner = False
  60.             return no_winner
  61.  
  62. #[/score]
  63.  
  64. def remove_number(num):
  65.     already_played.append(num)
  66.  
  67. def check_if_open(num):
  68.     if num in already_played or num not in allowed_chars:
  69.         print("That move is not allowed \n")
  70.         checking = True
  71.         return checking
  72.     elif num not in already_played:
  73.         remove_number(num)  
  74.         checking = False
  75.         return checking
  76.        
  77. def move(num,token):
  78.     if token == x:
  79.             num = input("Player 1, Choose a Square \n")
  80.             return num
  81.     elif token == o:
  82.             num = input("Player 2, Choose a Square \n")
  83.             return num
  84.        
  85. def board_refresh(last_board,num,token):
  86.     num = str(num)
  87.     next_board = last_board.replace(num,token)
  88.     print(next_board)
  89.     return next_board
  90.  
  91. playing = True
  92. no_winner = True
  93. turn = x
  94. scoring_turn = xscore
  95. while no_winner == True:
  96.  
  97.     print(board)
  98.     while playing == True:
  99.         checking = True
  100.         while checking:
  101.             number = move(number,turn)
  102.             checking = check_if_open(number)
  103.  
  104.         board = board_refresh(board,number,turn)
  105.        
  106.         adjust_score(scoring_turn,number)
  107.         no_winner = check_score(scoring_turn,turn)
  108.         if no_winner == False:
  109.             break
  110.            
  111.         if turn == x:
  112.             turn = o
  113.             scoring_turn = oscore
  114.         else:
  115.             turn = x
  116.             scoring_turn = xscore
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement