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