Guest User

Untitled

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