Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import time
- import keyboard
- loop_exit = False
- def clear_output(player_names=True):
- global player1, player2
- os.system("cls")
- 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")
- if not player_names:
- return
- print(f"Hello, {player1} and {player2}. {player1}, you will be X and {player2}, you will be O.")
- def get_name(player_num, symbol):
- global player1
- while True:
- alpha_num = True
- player = input(f"Q: What is your name Player {player_num} ({symbol})?\n"
- "A: I am ").strip()
- if not player:
- alpha_num = False
- for i in range(len(player)):
- if not player[i].isalpha() and not player[i].isdigit():
- alpha_num = False
- if not alpha_num:
- print("Invalid name! Please enter only letters and/or numbers.")
- time.sleep(2)
- clear_output(False)
- continue
- elif len(player) > 25:
- print("The name is too long! Enter a short version of the name.")
- time.sleep(2)
- clear_output(False)
- continue
- elif player_num == 2 and player == player1:
- print("Both player names cannot be the same! Please make it a bit different.")
- time.sleep(2)
- clear_output(False)
- continue
- clear_output(False)
- break
- return player
- def rules_and_board():
- global player1, player2
- player1_turn = True
- cell1 = cell2 = cell3 = cell4 = cell5 = cell6 = cell7 = cell8 = cell9 = " "
- counter = 0
- while True:
- print(f'''Each player will go one by one. {player1}, you will go first and {player2}, you will go second. \n
- 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 '''
- '''symbol in, as displayed below.
- | |
- 1 | 2 | 3
- _____|_____|_____
- | |
- 4 | 5 | 6
- _____|_____|_____
- | |
- 7 | 8 | 9
- | | \n\n ''')
- if not counter:
- print("Press enter to continue...")
- keyboard.wait("enter")
- counter += 1
- clear_output()
- continue
- print(f''' | |
- {cell1} | {cell2} | {cell3}
- _____|_____|_____
- | |
- {cell4} | {cell5} | {cell6}
- _____|_____|_____
- | |
- {cell7} | {cell8} | {cell9}
- | | \n''')
- if (cell1 == cell2 == cell3 != " " or cell4 == cell5 == cell6 != " " or cell7 == cell8 == cell9 != " " or cell1 == cell4 == cell7 != " " or
- cell2 == cell5 == cell8 != " " or cell3 == cell6 == cell9 != " " or cell1 == cell5 == cell9 != " " or cell3 == cell5 == cell7 != " "):
- if player1_turn:
- print(f"Well done, {player1}. You have won against {player2}.")
- else:
- print(f"Well done, {player2}. You have won against {player1}.")
- break
- else:
- player1_turn = not player1_turn
- try:
- if player1_turn:
- chosen_cell = int(input(f"Your move {player1}: "))
- else:
- chosen_cell = int(input(f"Your move {player2}: "))
- if chosen_cell not in range(1, 10):
- raise ValueError()
- except ValueError:
- print("Invalid number! Your reply can only be a number between 1-9 (incl.)")
- clear_output()
- continue
- if chosen_cell == 1:
- if player1_turn:
- cell1 = "X"
- else:
- cell1 = "O"
- elif chosen_cell == 2:
- if player1_turn:
- cell2 = "X"
- else:
- cell2 = "O"
- elif chosen_cell == 3:
- if player1_turn:
- cell3 = "X"
- else:
- cell3 = "O"
- elif chosen_cell == 4:
- if player1_turn:
- cell4 = "X"
- else:
- cell4 = "O"
- elif chosen_cell == 5:
- if player1_turn:
- cell5 = "X"
- else:
- cell5 = "O"
- elif chosen_cell == 6:
- if player1_turn:
- cell6 = "X"
- else:
- cell6 = "O"
- elif chosen_cell == 7:
- if player1_turn:
- cell7 = "X"
- else:
- cell7 = "O"
- elif chosen_cell == 8:
- if player1_turn:
- cell8 = "X"
- else:
- cell8 = "O"
- elif chosen_cell == 9:
- if player1_turn:
- cell9 = "X"
- else:
- cell9 = "O"
- clear_output()
- 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"
- "Press enter to continue...")
- keyboard.wait("enter")
- while not loop_exit:
- clear_output(False)
- player1 = get_name(1, "X")
- player2 = get_name(2, "O")
- clear_output()
- rules_and_board()
- while True:
- play_again = input("Q: Do you want to play again? (Y/N)\n"
- "A: ")
- if play_again.lower() not in ["y", "n"]:
- print("Invalid reply! Please type a 'Y' for yes or a 'N' for no.")
- continue
- elif play_again.lower() == "n":
- print("Thanks for playing!")
- time.sleep(1)
- loop_exit = True
- break
- else:
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement