Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- # Definition of variables and dictionary
- board = {
- 1: " ", 2: " ", 3: " ", 4: " ", 5: " ", 6: " ", 7: " ", 8: " ", 9: " "
- }
- game_on = True
- turn = "p1"
- # Function to clear the print the board
- def print_board():
- os.system('clear')
- print("")
- print(f" {board[7]} | {board[8]} | {board[9]}")
- print("---------------")
- print(f" {board[4]} | {board[5]} | {board[6]}")
- print("---------------")
- print(f" {board[1]} | {board[2]} | {board[3]}")
- print("")
- print("")
- # Function that will set "X" or "O" to players
- def set_player():
- p1 = ""
- while p1 != "X" and p1 != "O":
- p1 = input("Player 1: Choose 'X' or 'O' and type it down on the "
- "keyboard: ")
- if p1 == "X":
- p2 = "O"
- elif p1 == "O":
- p2 = "X"
- os.system('clear')
- print("Ready! \n \nPlayer 1 =", p1, ", Player 2 =", p2)
- return p1, p2
- # Function to place an "X" or an "O" on the board
- def players_turn(p1, p2, turn):
- valid_position = False
- while valid_position is False:
- if turn == "p1":
- pos = int(input("Player 1: It's your turn. "
- "Choose the position to play (1-9): "))
- valid_position = validate_open_position_on_board(board, pos)
- # Validates an open and valid position to place user's choice
- if valid_position:
- board[pos] = p1
- turn = "p2"
- else:
- pos = int(input("Player 2: It's your turn. "
- "Choose the position to play (1-9): "))
- valid_position = validate_open_position_on_board(board, pos)
- # Validates an open and valid position to place user's choice
- if valid_position:
- board[pos] = p2
- turn = "p1"
- return board, turn
- # Function that validates if someone has already won
- def validate_win(board):
- # Horizontal validation
- if (((board[7] == board[8] and board[8] == board[9]) and
- (board[7] == "X" or board[7] == "O")) or
- ((board[4] == board[5] and board[5] == board[6]) and
- (board[4] == "X" or board[4] == "O")) or
- ((board[1] == board[2] and board[2] == board[3]) and
- (board[2] == "X" or board[2] == "O"))):
- horizontal_validation = True
- else:
- horizontal_validation = False
- # Vertical validation
- if (((board[7] == board[4] and board[4] == board[1]) and
- (board[7] == "X" or board[7] == "O")) or
- ((board[8] == board[5] and board[5] == board[2]) and
- (board[8] == "X" or board[8] == "O")) or
- ((board[9] == board[6] and board[6] == board[3]) and
- (board[9] == "X" or board[9] == "O"))):
- vertical_validation = True
- else:
- vertical_validation = False
- # Diagonal validation
- if (((board[7] == board[5] and board[5] == board[3]) and
- (board[7] == "X" or board[7] == "O")) or
- ((board[1] == board[5] and board[5] == board[9]) and
- (board[1] == "X" or board[1] == "O"))):
- diagonal_validation = True
- else:
- diagonal_validation = False
- return horizontal_validation or vertical_validation or diagonal_validation
- # Function that validates tie
- def validate_tie(board):
- board_piece_count = 0
- if " " in board.values():
- return False
- else:
- return True
- # Function that validates if position as already being taken
- def validate_open_position_on_board(board, pos):
- return True if board[pos] == " " else False
- # Main code that will loop through and ask if players want to continue
- while game_on is True:
- os.system('clear')
- print("Welcome to Tic Tac Toe!")
- p1, p2 = set_player()
- while validate_win(board) is False and validate_tie(board) is False:
- print_board()
- board, turn = players_turn(p1, p2, turn)
- print_board()
- # Validate if the board is full and there has being a tie
- if validate_tie(board) is True:
- print("It's a tie!")
- # Otherwise, check who's turn is it, to determine the winner
- else:
- if turn == "p1":
- print("Congratulations, Player 2! You are the winner! \n")
- else:
- print("Congratulations, Player 1! You are the winner! \n")
- # End of game. Clears the board
- board = {
- 1: " ", 2: " ", 3: " ", 4: " ", 5: " ", 6: " ", 7: " ", 8: " ", 9: " "
- }
- # Validates if users want to play again
- end_response = input("Do you want to play again? (y/n): ")
- if end_response == "y":
- game_on = True
- else:
- game_on = False
- print("\nBye!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement