-3

I've tried to make a tic tac toe game in Python. From what I make I feel that my code is working however it is really bad. Is there anyway to improve from this code?

class Board():
    def __init__(self): 
        self.board = ["[]","[]","[]","[]","[]","[]","[]","[]","[]"]
    def createboard(self):
        board = ["[]","[]","[]","[]","[]","[]","[]","[]","[]"]
        print(self.board[0],self.board[1],self.board[2])
        print(self.board[3],self.board[4],self.board[5])
        print(self.board[6],self.board[7],self.board[8])
    def checkwin(self):
        if self.board[0]=="[O]" and self.board[1]=="[O]" and self.board[2]=="[O]":
            print("O Wins!")
            #break
        elif self.board[3]=="[O]"and self.board[4]=="[O]"and self.board[5]=="[O]":
            print("O Wins!")
           # break
        elif self.board[6]=="[O]"and self.board[7]=="[O]"and self.board[8]=="[O]":
            print("O Wins!")
           # break
        elif self.board[0]=="[O]"and self.board[3]=="[O]"and self.board[6]=="[O]":
            print("O Wins!")
          #  break
        elif self.board[1]=="[O]"and self.board[4]=="[O]"and self.board[7]=="[O]":
            print("O Wins!")
          #  break
        elif self.board[2]=="[O]"and self.board[5]=="[O]"and self.board[8]=="[O]":
            print("O Wins!")
         #   break
        elif self.board[0]=="[O]"and self.board[4]=="[O]"and self.board[8]=="[O]":
            print("O Wins!")
           # break
        elif self.board[2]=="[O]"and self.board[4]=="[O]"and self.board[6]=="[O]":
            print("O Wins!")
          #  break
        elif self.board[0]=="[X]"and self.board[1]=="[X]"and self.board[2]=="[X]":
            print("X Wins!")
           # break
        elif self.board[3]=="[X]"and self.board[4]=="[X]"and self.board[5]=="[X]":
            print("X Wins!")
          #  break
        elif self.board[6]=="[X]"and self.board[7]=="[X]"and self.board[8]=="[X]":
            print("X Wins!")
          #  break
        elif self.board[0]=="[X]"and self.board[3]=="[X]"and self.board[6]=="[X]":
            print("X Wins!")
         #   break
        elif self.board[1]=="[X]"and self.board[4]=="[X]"and self.board[7]=="[X]":
            print("X Wins!")
        #    break
        elif self.board[2]=="[X]"and self.board[5]=="[X]"and self.board[8]=="[X]":
            print("X Wins!")
         #   break
        elif self.board[0]=="[X]"and self.board[4]=="[X]"and self.board[8]=="[X]":
            print("X Wins!")
         #   break
        elif self.board[2]=="[X]"and self.board[4]=="[X]"and self.board[6]=="[X]":
            print("O Wins!")
         #   break
        
    def omove(self):
        o = int(input("Its 'O's'move please insert 1-9"))
        if o == 1:
            self.board[0] = "[O]"
            self.createboard()
        elif o == 2:
            self.board[1] = "[O]"
            self.createboard()
        elif o == 3:
            self.board[2] = "[O]"
            self.createboard()
        elif o == 4:
            self.board[3] = "[O]"
            self.createboard()
        elif o == 5:
            self.board[4] = "[O]"
            self.createboard()
        elif o == 6:
            self.board[5] = "[O]"
            self.createboard()
        elif o == 7:
            self.board[6] = "[O]"
            self.createboard()
        elif o == 8:
            self.board[7] = "[O]"
            self.createboard()
        elif o == 9:
            self.board[8] = "[O]"
            self.createboard()
        else:
            print('that column is out of range')
            o = int(input("please insert 1-9"))
    def xmove(self):
        x = int(input("Its 'x's'move please insert 1-9"))
        if x == 1:
            self.board[0] = "[X]"
            self.createboard()
        elif x == 2:
            self.board[1] = "[X]"
            self.createboard()
        elif x == 3:
            self.board[2] = "[X]"
            self.createboard()
        elif x == 4:
            self.board[3] = "[X]"
            self.createboard()
        elif x == 5:
            self.board[4] = "[X]"
            self.createboard()
        elif x == 6:
            self.board[5] = "[X]"
            self.createboard()
        elif x == 7:
            self.board[6] = "[X]"
            self.createboard()
        elif x == 8:
            self.board[7] = "[X]"
            self.createboard()
        elif x == 9:
            self.board[8] = "[X]"
            self.createboard()
        else:
            print('that column is out of range')
            o = int(input("please insert 1-9"))
    
a = Board()
a.createboard()
a.omove()
a.checkwin()
a.xmove()
a.checkwin()
a.omove()
a.checkwin()
a.xmove()
a.checkwin()
a.omove()
a.checkwin()
a.xmove()
a.checkwin()
a.omove()
a.checkwin()
a.xmove()
a.checkwin()
a.omove()
a.checkwin()

I would love any kind of feedback to improve my skills in programming. What I thought about this is I think I used too much if condition maybe it can be swapped by another simple method and how can I even break when the game is finished ex: if X wins the game, the game is then closed

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • 5
    For general code review, this is the wrong site. Maybe [Code Review.SE](https://codereview.stackexchange.com/) can help. – Jonathan Hall Feb 25 '21 at 12:27
  • If your board is an array rather than a list, it would be easy to check win-conditions row- and line-wise. – Dschoni Feb 25 '21 at 12:30

2 Answers2

0

A big change would be to refactor the move functions to pass in the token you need and avoid the elifs with maths.

def move(self, token):
    o = int(input(f"Its '{token}'s'move please insert 1-9"))
    
    while not (0 < o <= 9):
        print('that column is out of range')
        o = int(input("please insert 1-9"))
        
    self.board[o-1] = f"[{token}]"
    self.createboard()

used via...

move("O")
move("X")
Sayse
  • 42,633
  • 14
  • 77
  • 146
0
self.board = 9*['[]']

def createboard(self):
    for i,j in enumerate(board, 1):
        print(j)
        if i%3 == 0: print('\n')

Have a look at python tutorial: https://docs.python.org/3/tutorial/

Jacek Błocki
  • 452
  • 3
  • 9