Advertisement
Guest User

My code

a guest
Jul 8th, 2022
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.76 KB | None | 0 0
  1. import os
  2. import time
  3. import keyboard
  4.  
  5. loop_exit = False
  6.  
  7.  
  8. def clear_output(player_names=True):
  9.     global player1, player2
  10.     os.system("cls")
  11.     print("Welcome to my TicTacToe game. \nIn this game, your objective is to get three of your own symbols in a row before the other player does so.\n")
  12.  
  13.     if not player_names:
  14.         return
  15.  
  16.     print(f"Hello, {player1} and {player2}. {player1}, you will be X and {player2}, you will be O.")
  17.  
  18.  
  19. def get_name(player_num, symbol):
  20.     global player1
  21.     while True:
  22.         alpha_num = True
  23.  
  24.         player = input(f"Q: What is your name Player {player_num} ({symbol})?\n"
  25.                        "A: I am ").strip()
  26.  
  27.         if not player:
  28.             alpha_num = False
  29.  
  30.         for i in range(len(player)):
  31.             if not player[i].isalpha() and not player[i].isdigit():
  32.                 alpha_num = False
  33.  
  34.         if not alpha_num:
  35.             print("Invalid name! Please enter only letters and/or numbers.")
  36.             time.sleep(2)
  37.             clear_output(False)
  38.             continue
  39.         elif len(player) > 25:
  40.             print("The name is too long! Enter a short version of the name.")
  41.             time.sleep(2)
  42.             clear_output(False)
  43.             continue
  44.         elif player_num == 2 and player == player1:
  45.             print("Both player names cannot be the same! Please make it a bit different.")
  46.             time.sleep(2)
  47.             clear_output(False)
  48.             continue
  49.  
  50.         clear_output(False)
  51.         break
  52.  
  53.     return player
  54.  
  55.  
  56. def rules_and_board():
  57.     global player1, player2
  58.     player1_turn = True
  59.     cell1 = cell2 = cell3 = cell4 = cell5 = cell6 = cell7 = cell8 = cell9 = " "
  60.     counter = 0
  61.  
  62.     while True:
  63.         print(f'''Each player will go one by one. {player1}, you will go first and {player2}, you will go second. \n
  64. You will see a drawing board in front of you and to place a symbol on the board you have to type in the number of the desired box you want to place your '''
  65.               '''symbol in, as displayed below.
  66.      |     |    
  67.   1  |  2  |  3  
  68. _____|_____|_____
  69.      |     |    
  70.   4  |  5  |  6  
  71. _____|_____|_____
  72.      |     |    
  73.   7  |  8  |  9  
  74.      |     |    \n\n ''')
  75.  
  76.         if not counter:
  77.             print("Press enter to continue...")
  78.             keyboard.wait("enter")
  79.             counter += 1
  80.             clear_output()
  81.             continue
  82.  
  83.         print(f'''      |     |    
  84.   {cell1}  |  {cell2}  |  {cell3}  
  85. _____|_____|_____
  86.      |     |    
  87.   {cell4}  |  {cell5}  |  {cell6}  
  88. _____|_____|_____
  89.      |     |    
  90.   {cell7}  |  {cell8}  |  {cell9}  
  91.      |     |     \n''')
  92.  
  93.         if (cell1 == cell2 == cell3 != " " or cell4 == cell5 == cell6 != " " or cell7 == cell8 == cell9 != " " or cell1 == cell4 == cell7 != " " or
  94.                 cell2 == cell5 == cell8 != " " or cell3 == cell6 == cell9 != " " or cell1 == cell5 == cell9 != " " or cell3 == cell5 == cell7 != " "):
  95.             if player1_turn:
  96.                 print(f"Well done, {player1}. You have won against {player2}.")
  97.             else:
  98.                 print(f"Well done, {player2}. You have won against {player1}.")
  99.             break
  100.         else:
  101.             player1_turn = not player1_turn
  102.  
  103.         try:
  104.             if player1_turn:
  105.                 chosen_cell = int(input(f"Your move {player1}: "))
  106.             else:
  107.                 chosen_cell = int(input(f"Your move {player2}: "))
  108.  
  109.             if chosen_cell not in range(1, 10):
  110.                 raise ValueError()
  111.  
  112.         except ValueError:
  113.             print("Invalid number! Your reply can only be a number between 1-9 (incl.)")
  114.             clear_output()
  115.             continue
  116.  
  117.         if chosen_cell == 1:
  118.             if player1_turn:
  119.                 cell1 = "X"
  120.             else:
  121.                 cell1 = "O"
  122.         elif chosen_cell == 2:
  123.             if player1_turn:
  124.                 cell2 = "X"
  125.             else:
  126.                 cell2 = "O"
  127.         elif chosen_cell == 3:
  128.             if player1_turn:
  129.                 cell3 = "X"
  130.             else:
  131.                 cell3 = "O"
  132.         elif chosen_cell == 4:
  133.             if player1_turn:
  134.                 cell4 = "X"
  135.             else:
  136.                 cell4 = "O"
  137.         elif chosen_cell == 5:
  138.             if player1_turn:
  139.                 cell5 = "X"
  140.             else:
  141.                 cell5 = "O"
  142.         elif chosen_cell == 6:
  143.             if player1_turn:
  144.                 cell6 = "X"
  145.             else:
  146.                 cell6 = "O"
  147.         elif chosen_cell == 7:
  148.             if player1_turn:
  149.                 cell7 = "X"
  150.             else:
  151.                 cell7 = "O"
  152.         elif chosen_cell == 8:
  153.             if player1_turn:
  154.                 cell8 = "X"
  155.             else:
  156.                 cell8 = "O"
  157.         elif chosen_cell == 9:
  158.             if player1_turn:
  159.                 cell9 = "X"
  160.             else:
  161.                 cell9 = "O"
  162.  
  163.         clear_output()
  164.  
  165.  
  166. print("Welcome to my TicTacToe game. \nIn this game, your objective is to get three of your own symbols in a row before the other player does so.\n"
  167.       "Press enter to continue...")
  168. keyboard.wait("enter")
  169.  
  170. while not loop_exit:
  171.     clear_output(False)
  172.     player1 = get_name(1, "X")
  173.     player2 = get_name(2, "O")
  174.     clear_output()
  175.  
  176.     rules_and_board()
  177.  
  178.     while True:
  179.         play_again = input("Q: Do you want to play again? (Y/N)\n"
  180.                            "A: ")
  181.  
  182.         if play_again.lower() not in ["y", "n"]:
  183.             print("Invalid reply! Please type a 'Y' for yes or a 'N' for no.")
  184.             continue
  185.         elif play_again.lower() == "n":
  186.             print("Thanks for playing!")
  187.             time.sleep(1)
  188.             loop_exit = True
  189.             break
  190.         else:
  191.             break
  192.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement