I'm new to Python and I just finished a school assignment where I had to make a tictactoe game with focus on OOP. I would love to get some pointers on how I can clean up my code (especially my Tictactoe class) as it's pretty messy right now.
What I've done so far is comment as much as possible to make it understandable, but I feel like my tictactoe class is a mess and I would like to get some pointers on how I can optimize it.
The class below enforces the rules and draws the board:
import os class Board: board = [0,1,2,3,4,5,6,7,8,9] win_combinations = [ (1,2,3), (4,5,6), (7,8,9), (1,5,9), (3,5,7), (1,4,7), (2,5,8), (3,6,9), ] GameOver = False #Draws the board def drawboard(self): print('=========') print(self.board, '|', self.board, '|', self.board) print(self.board, '|', self.board, '|', self.board) print(self.board, '|', self.board, '|', self.board) print('=========') #Checks if the move the player just made, made him/she win the game def checkIfWon(self, choice): for a, b, c in self.win_combinations: if self.board[a] == self.board[b] == self.board[c]: print('Game over, player ' + choice + ' won the game') self.GameOver = True #Update the current board def update(self, input, choice): self.board[input] = choice os.system('clear') self.drawboard() self.checkIfWon(choice) #Resets the board def resetBoard(self): self.board = [0,1,2,3,4,5,6,7,8,9] #Stops the game if tie def tie(self): list =  for x in self.board: if type(x) != int: list.append(x) if len(list) == 9: return True
The class below contains the runGame method which starts the game:
import os from board import Board class Tictactoe(): b = Board() choicePlayer1 = '' choucePlayer2 = '' corretChoice = False correctPlayer1 = False correctPlayer2 = False def runGame(self): os.system('clear') #Resets the game when a new game is started #Is necessary if the players wish to play more than 1 game resetGame(self) #Makes sure the game only starts if player1 picks X or O while self.corretChoice == False: self.choicePlayer1 = input('Do you want to play X or O? ') print() if self.choicePlayer1 == 'X': self.choicePlayer2 = 'O' self.corretChoice = True print('Starting player selected X') elif self.choicePlayer1 == 'O': self.choicePlayer2 = 'X' self.corretChoice = True print('Starting player selected O') else: print('ERROR - input has to be either X or O!') continue os.system('clear') self.b.drawboard() while self.b.GameOver == False: self.correctPlayer1 = False self.correctPlayer2 = False #For player1 while self.correctPlayer1 == False: while True: try: x = int(input(self.choicePlayer1 + ' Where do you want to place your piece? ')) break except: print('Input has to be a number, try again') if x > 0 and x < 10 and type(self.b.board[x]) != str: self.b.update(x, self.choicePlayer1) self.correctPlayer1 = True elif x == 10: quit() else: print('Spot is taken, try again: ') if self.b.GameOver == True: self.correctPlayer2 = True if self.b.tie() == True: self.correctPlayer2 = True self.b.GameOver = True print('Game is a tie') #For player2 while self.correctPlayer2 == False: while True: try: x = int(input(self.choicePlayer2 + ' Where do you want to place your piece? ')) break except: print('Input has to be a number, try again') if x > 0 and x < 10 and type(self.b.board[x]) != str: self.b.update(x, self.choicePlayer2) self.correctPlayer2 = True elif x == 10: quit() else: print('Spot is taken, try again: ') if self.b.tie() == True: self.b.gameOver = True print('Game is a tie') #Resets the game if the players wishes to play again def resetGame(self): self.b = Board() self.choicePlayer1 = '' self.choucePlayer2 = '' self.corretChoice = False self.correctPlayer1 = False self.correctPlayer2 = False self.b.resetBoard()
The script i run to start the game:
from tictac import Tictactoe run = Tictactoe() while True: run.runGame() if input("Play again? (y/n)") == "n": quit()