Advertisement
MR_Rednax

tick tack to

Dec 5th, 2023
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.81 KB | Gaming | 0 0
  1. def __init__(self):
  2.     Frame.__init__(self, bg = "black")
  3.     self.protocol('WM_DELETE_WINDOW', self.doSomething)
  4.     self.pack(expand = 1, fill = BOTH)
  5.  
  6. def doSomething(self):
  7.     if showinfo.askokcancel("Quit?", "Are you sure you want to quit?"):
  8.         self.quit()
  9.  
  10. # Function to print Tic Tac Toe
  11. def print_tic_tac_toe(values):
  12.     print("\n")
  13.     print("\t     |     |")
  14.     print("\t  {}  |  {}  |  {}".format(values[6], values[7], values[8]))
  15.     print('\t_____|_____|_____')
  16.  
  17.     print("\t     |     |")
  18.     print("\t  {}  |  {}  |  {}".format(values[3], values[4], values[5]))
  19.     print('\t_____|_____|_____')
  20.  
  21.     print("\t     |     |")
  22.  
  23.     print("\t  {}  |  {}  |  {}".format(values[0], values[1], values[2]))
  24.     print("\t     |     |")
  25.     print("\n")
  26.  
  27.  
  28. # Function to print the score-board
  29. def print_scoreboard(score_board):
  30.     print("\t--------------------------------")
  31.     print("\t              SCOREBOARD       ")
  32.     print("\t--------------------------------")
  33.  
  34.     players = list(score_board.keys())
  35.     print("\t   ", players[0], "\t    ", score_board[players[0]])
  36.     print("\t   ", players[1], "\t    ", score_board[players[1]])
  37.  
  38.     print("\t--------------------------------\n")
  39.  
  40. # Function to check if any player has won
  41. def check_win(player_pos, cur_player):
  42.  
  43.     # All possible winning combinations
  44.     soln = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 4, 7], [2, 5, 8], [3, 6, 9], [1, 5, 9], [3, 5, 7]]
  45.  
  46.     # Loop to check if any winning combination is satisfied
  47.     for x in soln:
  48.         if all(y in player_pos[cur_player] for y in x):
  49.  
  50.             # Return True if any winning combination satisfies
  51.             return True
  52.     # Return False if no combination is satisfied      
  53.     return False      
  54.  
  55. # Function to check if the game is drawn
  56. def check_draw(player_pos):
  57.     if len(player_pos['X']) + len(player_pos['O']) == 9:
  58.         return True
  59.     return False      
  60.  
  61. # Function for a single game of Tic Tac Toe
  62. def single_game(cur_player):
  63.  
  64.     # Represents the Tic Tac Toe
  65.     values = [' ' for x in range(9)]
  66.      
  67.     # Stores the positions occupied by X and O
  68.     player_pos = {'X':[], 'O':[]}
  69.      
  70.     # Game Loop for a single game of Tic Tac Toe
  71.     while True:
  72.         print_tic_tac_toe(values)
  73.          
  74.         # Try exception block for MOVE input
  75.         try:
  76.             print("Player ", cur_player, " turn. Which box? : ", end="")
  77.             move = int(input())
  78.         except ValueError:
  79.             print("Wrong Input!!! Try Again")
  80.             continue
  81.  
  82.         # Sanity check for MOVE inout
  83.         if move < 1 or move > 9:
  84.             print("Wrong Input!!! Try Again")
  85.             continue
  86.  
  87.         # Check if the box is not occupied already
  88.         if values[move-1] != ' ':
  89.             print("Place already filled. Try again!!")
  90.             continue
  91.  
  92.         # Update game information
  93.  
  94.         # Updating grid status
  95.         values[move-1] = cur_player
  96.  
  97.         # Updating player positions
  98.         player_pos[cur_player].append(move)
  99.  
  100.         # Function call for checking win
  101.         if check_win(player_pos, cur_player):
  102.             print_tic_tac_toe(values)
  103.             print("Player ", cur_player, " has won the game!!")    
  104.             print("\n")
  105.             return cur_player
  106.  
  107.         # Function call for checking draw game
  108.         if check_draw(player_pos):
  109.             print_tic_tac_toe(values)
  110.             print("Game Drawn")
  111.             print("\n")
  112.             return 'D'
  113.  
  114.         # Switch player moves
  115.         if cur_player == 'X':
  116.             cur_player = 'O'
  117.         else:
  118.             cur_player = 'X'
  119.  
  120. if __name__ == "__main__":
  121.  
  122.     print("Player 1")
  123.     player1 = input("Enter the name : ")
  124.     print("\n")
  125.  
  126.     print("Player 2")
  127.     player2 = input("Enter the name : ")
  128.     print("\n")
  129.      
  130.     # Stores the player who chooses X and O
  131.     cur_player = player1
  132.  
  133.     # Stores the choice of players
  134.     player_choice = {'X' : "", 'O' : ""}
  135.  
  136.     # Stores the options
  137.     options = ['X', 'O']
  138.  
  139.     # Stores the scoreboard
  140.     score_board = {player1: 0, player2: 0}
  141.     print_scoreboard(score_board)
  142.  
  143.     # Game Loop for a series of Tic Tac Toe
  144.     # The loop runs until the players quit
  145.     while True:
  146.  
  147.         # Player choice Menu
  148.         print("Turn to choose for", cur_player)
  149.         print("1 for X")
  150.         print("2 for O")
  151.         print("3 to Quit")
  152.  
  153.         # Try exception for CHOICE input
  154.         try:
  155.             choice = int(input())  
  156.         except ValueError:
  157.             print("Wrong Input!!! Try Again\n")
  158.             continue
  159.  
  160.         # Conditions for player choice  
  161.         if choice == 1:
  162.             player_choice['X'] = cur_player
  163.             if cur_player == player1:
  164.                 player_choice['O'] = player2
  165.             else:
  166.                 player_choice['O'] = player1
  167.  
  168.         elif choice == 2:
  169.             player_choice['O'] = cur_player
  170.             if cur_player == player1:
  171.                 player_choice['X'] = player2
  172.             else:
  173.                 player_choice['X'] = player1
  174.          
  175.         elif choice == 3:
  176.             print("Final Scores")
  177.             print_scoreboard(score_board)
  178.             break  
  179.  
  180.         else:
  181.             print("Wrong Choice!!!! Try Again\n")
  182.  
  183.         # Stores the winner in a single game of Tic Tac Toe
  184.         winner = single_game(options[choice-1])
  185.          
  186.         # Edits the scoreboard according to the winner
  187.         if winner != 'D' :
  188.             player_won = player_choice[winner]
  189.             score_board[player_won] = score_board[player_won] + 1
  190.  
  191.         print_scoreboard(score_board)
  192.         # Switch player who chooses X or O
  193.         if cur_player == player1:
  194.             cur_player = player2
  195.         else:
  196.             cur_player = player1
  197.            
  198.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement