I have built a project OOP Tic Tac Toe. I am beginner-intermediate developer. Can you tell me what I can improve, what I did wrong and what to look up to? This is my first OOP project. I'm happy that I built in looking for any reviews I struggled a little bit with play_the_game() function because I got some times that I have to write two times X computer not once but somehow I solve it.
Do you have any suggestions what to build next or what to add to this project and what I can say that for this type of project. I think it is better to use OOP because if it wasn't OOP I would use a lot of global variables. Some people told me that global are bad practice.
class Tic_Tac_Toe:
def __init__(self,playerX):
self.playerX = playerX
self.winner = None
self.board = ["-","-","-" ,
"-","-","-",
"-","-","-",]
self.current_player = "X"
self.game_over = False
def display_board(self):
print(f" {self.board[0]} | {self.board[1]} | {self.board[2]} ")
print("-----------")
print(f" {self.board[3]} | {self.board[4]} | {self.board[5]} ")
print("-----------")
print(f" {self.board[6]} | {self.board[7]} | {self.board[8]} ")
def user_move(self):
guess = int(input("Please enter a number between 0-8: "))
if guess < 0 or guess > 8:
print("Enter a number between 1-8")
else:
if self.board[guess] == "-":
self.board[guess] = "X"
print(f"You taken place--> {guess}")
else:
print("The place is occupied please choose another!")
self.user_move()
def computer_move(self):
if self.current_player == "Y":
if "-" in self.board:
position = random.randint(0,8)
if self.board[position] == "-":
self.board[position] = "Y"
print(f"Computer taken place--> {position}")
else:
self.computer_move()
def handle_turn(self):
if self.current_player == "X":
self.current_player = "Y"
else:
self.current_player = "Y"
def check_if_tie(self):
if "-" not in self.board and self.winner == None:
print("Tie no one won!")
print("Best of luck next time!")
self.game_over = True
def possible_horizontal_win(self):
if self.board[0] == self.board[3] == self.board[6] != "-":
self.game_over = True
self.winner = self.board[0]
if self.winner == "X":
print(f"The winner is -- > {self.playerX}")
else:
print(f"The winner is -- > {self.winner}")
elif self.board[1] == self.board[4] == self.board[7] != "-":
self.game_over = True
self.winner = self.board[1]
if self.winner == "X":
print(f"The winner is -- > {self.playerX}")
else:
print(f"The winner is -- > {self.winner}")
elif self.board[2] == self.board[5] == self.board[8] != "-":
self.game_over = True
self.winner = self.board[2]
if self.winner == "X":
print(f"The winner is -- > {self.playerX}")
else:
print(f"The winner is -- > {self.winner}")
def possible_vertical_win(self):
if self.board[0] == self.board[1] == self.board[2] != "-":
self.game_over = True
self.winner = self.board[0]
if self.winner == "X":
print(f"The winner is -- > {self.playerX}")
else:
print(f"The winner is -- > {self.winner}")
elif self.board[3] == self.board[4] == self.board[5] != "-":
self.game_over = True
self.winner = self.board[3]
if self.winner == "X":
print(f"The winner is -- > {self.playerX}")
else:
print(f"The winner is -- > {self.winner}")
elif self.board[6] == self.board[7] == self.board[8] != "-":
self.game_over = True
self.winner = self.board[6]
if self.winner == "X":
print(f"The winner is -- > {self.playerX}")
else:
print(f"The winner is -- > {self.winner}")
def possible_sideways_win(self):
if self.board[0] == self.board[4] == self.board[8] != "-":
self.game_over = True
self.winner = self.board[0]
if self.winner == "X":
print(f"The winner is -- > {self.playerX}")
else:
print(f"The winner is -- > {self.winner}")
elif self.board[2] == self.board[4] == self.board[6] != "-":
self.game_over = True
self.winner = self.board[2]
if self.winner == "X":
print(f"The winner is -- > {self.playerX}")
else:
print(f"The winner is -- > {self.winner}")
def play_the_game(self):
while self.game_over == False and self.winner == None:
self.user_move()
self.handle_turn()
self.computer_move()
self.possible_horizontal_win()
self.possible_sideways_win()
self.possible_vertical_win()
self.check_if_tie()
self.display_board()
tictactoe = Tic_Tac_Toe("Joelinton")
tictactoe.play_the_game()