Advertisement
PowerCell46

Tic-Tac-Toe Python

Apr 5th, 2023
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.80 KB | None | 0 0
  1. import random
  2. import pyfiglet
  3. from colorama import Fore
  4. import time
  5.  
  6. print("\n" + time.asctime())
  7. print("\n")
  8. first_person_symbol = '✘'
  9. second_person_symbol = '⬤'
  10.  
  11. Welcome = pyfiglet.figlet_format("Welcome!", font="big")
  12. print(Fore.CYAN + Welcome)
  13. time.sleep(1.5)
  14. tic_tac_toe = pyfiglet.figlet_format("Let's play Tic_Tac_Toe!", font="big")
  15. print(Fore.CYAN + tic_tac_toe)
  16. time.sleep(1.5)
  17. the_players_want_to_play = True
  18.  
  19. do_you_know_the_rules = str(input(Fore.LIGHTWHITE_EX + "Are you familiar with the game? "))
  20.  
  21. if do_you_know_the_rules == "no" or do_you_know_the_rules == "No":
  22.     print("\n Two players take turns marking the spaces in a three-by-three grid with X or O. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row is the winner.")
  23.     time.sleep(8)
  24.     more_info = str(input("\nDo you need more explanation before starting the game? "))
  25.     if more_info == "yes" or more_info == "Yes":
  26.         print("\n https://www.youtube.com/watch?v=USEjXNCTvcc")
  27.         time.sleep(15)
  28.  
  29. player_vs_player_or_player_vs_bot = str(input("How do you wish to play? Versus a bot or versus another player? (with a bot/with another player): "))
  30.  
  31. while the_players_want_to_play:
  32.     first_row = ["□", "□", "□"]
  33.     second_row = ["□", "□", "□"]
  34.     third_row = ["□", "□", "□"]
  35.     print("\n")
  36.     print(Fore.LIGHTWHITE_EX + ' '.join(first_row))
  37.     time.sleep(0.3)
  38.     print(' '.join(second_row))
  39.     time.sleep(0.3)
  40.     print(' '.join(third_row))
  41.  
  42.     index = -1
  43.     the_game_is_running = True
  44.     game_over = pyfiglet.figlet_format("Game Over!", font="slant")
  45.  
  46.     while the_game_is_running:
  47.         index += 1
  48.         if index % 2 == 0:
  49.             current_symbol = first_person_symbol
  50.             time.sleep(0.2)
  51.             selected_line = int(input(Fore.CYAN + "\nPlayer 1, choose a line:  "))
  52.             time.sleep(0.2)
  53.             selected_position = int(input(Fore.CYAN + "Player 1, choose a position: "))
  54.             time.sleep(0.2)
  55.             if selected_position > 3:
  56.                 print(Fore.YELLOW + "You have entered an invalid position and wasted your move!")
  57.             else:
  58.                 if selected_line == 1:
  59.                     if first_row[selected_position - 1] == "□":
  60.                         first_row.insert((selected_position - 1), current_symbol)
  61.                         first_row.pop(selected_position)
  62.                     else:
  63.                         print(Fore.YELLOW + "This position is already taken! You have wasted your move!")
  64.  
  65.                 elif selected_line == 2:
  66.                     if second_row[selected_position - 1] == "□":
  67.                         second_row.insert((selected_position - 1), current_symbol)
  68.                         second_row.pop(selected_position)
  69.                     else:
  70.                         print(Fore.YELLOW + "This position is already taken! You have wasted your move!")
  71.  
  72.                 elif selected_line == 3:
  73.                     if third_row[selected_position - 1] == "□":
  74.                         third_row.insert((selected_position - 1), current_symbol)
  75.                         third_row.pop(selected_position)
  76.                     else:
  77.                         print(Fore.YELLOW + "This position is already taken! You have wasted your move!")
  78.  
  79.                 else:
  80.                     print(Fore.YELLOW + "This line doesn't exist! You have wasted your move!")
  81.  
  82.                 print(Fore.CYAN + ' '.join(first_row))
  83.                 print(Fore.CYAN + ' '.join(second_row))
  84.                 print(Fore.CYAN + ' '.join(third_row))
  85.  
  86.                 if first_row[0] == "✘" and first_row[1] == "✘" and first_row[2] == "✘" or second_row[0] == "✘" and second_row[
  87.                     1] == "✘" and second_row[2] == "✘" or third_row[0] == "✘" and third_row[1] == "✘" and third_row[2] == "✘" or \
  88.                         first_row[0] == "✘" and second_row[1] == "✘" and third_row[2] == "✘" or first_row[2] == "✘" and \
  89.                         second_row[1] == "✘" and third_row[0] == "✘" or first_row[0] == "✘" and second_row[0] == "✘" and \
  90.                         third_row[0] == "✘" or first_row[1] == "✘" and second_row[1] == "✘" and third_row[1] == "✘" or \
  91.                         first_row[2] == "✘" and second_row[2] == "✘" and third_row[2] == "✘":
  92.                     first_player_won = "First player won!"
  93.                     time.sleep(1)
  94.                     print_1 = ""
  95.                     for i in range(0, len(first_player_won)):
  96.                         time.sleep(0.1)
  97.                         print_1 += first_player_won[i]
  98.                         print(Fore.RED + print_1)
  99.                     time.sleep(0.2)
  100.                     print(game_over)
  101.                     the_game_is_running = False
  102.                 elif first_row.count("□") == 0 and second_row.count("□") == 0 and third_row.count("□") == 0 and the_game_is_running:
  103.                     print(Fore.YELLOW + "Draw!")
  104.                     time.sleep(0.2)
  105.                     print(game_over)
  106.                     the_game_is_running = False
  107.  
  108.         else:
  109.             current_symbol = second_person_symbol
  110.             time.sleep(0.2)
  111.             if player_vs_player_or_player_vs_bot == "with another player":
  112.                 selected_line = int(input(Fore.GREEN + "\nPlayer 2, choose a line:  "))
  113.                 time.sleep(0.2)
  114.                 selected_position = int(input(Fore.GREEN + "Player 2, choose a position: "))
  115.                 time.sleep(0.2)
  116.             elif player_vs_player_or_player_vs_bot == "with a bot":
  117.                 selected_line = random.randint(1, 3)
  118.                 print(Fore.LIGHTWHITE_EX + 'The bot is choosing a position.')
  119.                 time.sleep(0.5)
  120.                 while True:
  121.                     if selected_line == 1:
  122.                         if "□" in first_row:
  123.                             break
  124.                     elif selected_line == 2:
  125.                         if "□" in second_row:
  126.                             break
  127.                     elif selected_line == 3:
  128.                         if "□" in third_row:
  129.                             break
  130.                     selected_line = random.randint(1, 3)
  131.                 time.sleep(0.2)
  132.                 selected_position = random.randint(1, 3)
  133.             while True:
  134.                 if selected_position > 3:
  135.                     print(Fore.YELLOW + "You have entered an invalid position and wasted your move!")
  136.                     break
  137.                 else:
  138.                     if selected_line == 1:
  139.                         if first_row[selected_position - 1] == "□":
  140.                             first_row.insert((selected_position-1), current_symbol)
  141.                             first_row.pop(selected_position)
  142.                             break
  143.                         else:
  144.                             if player_vs_player_or_player_vs_bot == "with a bot":
  145.                                 selected_position = random.randint(1, 3)
  146.                             else:
  147.                                 print(Fore.YELLOW + "This position is already taken! You have wasted your move!")
  148.                                 break
  149.  
  150.                     elif selected_line == 2:
  151.                         if second_row[selected_position - 1] == "□":
  152.                             second_row.insert((selected_position-1), current_symbol)
  153.                             second_row.pop(selected_position)
  154.                             break
  155.                         else:
  156.                             if player_vs_player_or_player_vs_bot == "with a bot":
  157.                                 selected_position = random.randint(1, 3)
  158.                             else:
  159.                                 print(Fore.YELLOW + "This position is already taken! You have wasted your move!")
  160.                                 break
  161.  
  162.                     elif selected_line == 3:
  163.                         if third_row[selected_position - 1] == "□":
  164.                             third_row.insert((selected_position-1), current_symbol)
  165.                             third_row.pop(selected_position)
  166.                             break
  167.                         else:
  168.                             if player_vs_player_or_player_vs_bot == "with a bot":
  169.                                 selected_position = random.randint(1, 3)
  170.                             else:
  171.                                 print(Fore.YELLOW + "This position is already taken! You have wasted your move!")
  172.                                 break
  173.  
  174.                     else:
  175.                         print(Fore.YELLOW + "This line doesn't exist! You have wasted your move!")
  176.                         break
  177.             if player_vs_player_or_player_vs_bot == "with a bot":
  178.                 print(Fore.LIGHTWHITE_EX + ' '.join(first_row))
  179.                 print(Fore.LIGHTWHITE_EX + ' '.join(second_row))
  180.                 print(Fore.LIGHTWHITE_EX + ' '.join(third_row))
  181.             else:
  182.                 print(Fore.GREEN + ' '.join(first_row))
  183.                 print(Fore.GREEN + ' '.join(second_row))
  184.                 print(Fore.GREEN + ' '.join(third_row))
  185.  
  186.             if first_row[0] == "⬤" and first_row[1] == "⬤" and first_row[2] == "⬤" or second_row[0] == "⬤" and second_row[
  187.                 1] == "⬤" and second_row[2] == "⬤" or third_row[0] == "⬤" and third_row[1] == "⬤" and third_row[2] == "⬤" or \
  188.                     first_row[0] == "⬤" and second_row[1] == "⬤" and third_row[2] == "⬤" or first_row[2] == "⬤" and \
  189.                     second_row[1] == "⬤" and third_row[0] == "⬤" or first_row[0] == "⬤" and second_row[0] == "⬤" and \
  190.                     third_row[0] == "⬤" or first_row[1] == "⬤" and second_row[1] == "⬤" and third_row[1] == "⬤" or \
  191.                     first_row[2] == "⬤" and second_row[2] == "⬤" and third_row[2] == "⬤":
  192.                 second_player_won = "Second player won!"
  193.                 time.sleep(1)
  194.                 print_2 = ""
  195.                 for i in range(0, len(second_player_won)):
  196.                     time.sleep(0.1)
  197.                     print_2 += second_player_won[i]
  198.                     print(Fore.GREEN + print_2)
  199.                 time.sleep(0.2)
  200.                 print(game_over)
  201.                 the_game_is_running = False
  202.             elif first_row.count("□") == 0 and second_row.count("□") == 0 and third_row.count("□") == 0 and the_game_is_running:
  203.                 print(Fore.YELLOW + "Draw!")
  204.                 the_game_is_running = False
  205.     time.sleep(3)
  206.     continuation = str(input("Do you wish to play another game? "))
  207.  
  208.     if continuation == "no":
  209.         print(Fore.CYAN + "Thanks for playing!")
  210.         time.sleep(0.5)
  211.         print(Fore.CYAN + "Bye!")
  212.         the_players_want_to_play = False
  213.     elif continuation == "yes":
  214.         the_players_want_to_play = True
  215.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement