I created a Tic_Tac_Toe
class to play the game Tic Tac Toe.
Is there a better way of coding a game like this? I started learning python 5 days ago and my knowledge is limited.
class Tic_Tac_Toe:
x1 = x2 = x3 = x4 = x5 = x6 = x7 = x8 = x9 = ''
l = '|'
a1 = a3 = b1 = b3 = c1 = c3 = a4 = a6 = b4 = b6 = c4 = c6 = a7 = a9 = b7 = b9 = c7 = c9 = ' '
a2 = ' 1 '
b2 = ' 2 '
c2 = ' 3 '
a5 = ' 4 '
b5 = ' 5 '
c5 = ' 6 '
a8 = ' 7 '
b8 = ' 8 '
c8 = ' 9 '
def already_taken(self):
print('Already taken')
def pr_r(self):
print(self.a1 + self.l + self.b1 + self.l + self.c1)
print(self.a2 + self.l + self.b2 + self.l + self.c2)
print(self.a3 + self.l + self.b3 + self.l + self.c3)
print('---+---+---')
print(self.a4 + self.l + self.b4 + self.l + self.c4)
print(self.a5 + self.l + self.b5 + self.l + self.c5)
print(self.a6 + self.l + self.b6 + self.l + self.c6)
print('---+---+---')
print(self.a7 + self.l + self.b7 + self.l + self.c7)
print(self.a8 + self.l + self.b8 + self.l + self.c8)
print(self.a9 + self.l + self.b9 + self.l + self.c9)
def turn_x(self):
player_choice = input()
if player_choice == '1' and Tic_Tac_Toe.a2 == ' X ' or player_choice == '1' and Tic_Tac_Toe.a2 == '|+|':
self.already_taken()
elif player_choice == '1':
Tic_Tac_Toe.x1 = 1
Tic_Tac_Toe.a1 = '\ /'
Tic_Tac_Toe.a2 = ' X '
Tic_Tac_Toe.a3 = '/ \\'
elif player_choice == '2' and Tic_Tac_Toe.b2 == ' X ' or player_choice == '2' and Tic_Tac_Toe.b2 == '|+|':
self.already_taken()
elif player_choice == '2':
Tic_Tac_Toe.x2 = 1
Tic_Tac_Toe.b1 = '\ /'
Tic_Tac_Toe.b2 = ' X '
Tic_Tac_Toe.b3 = '/ \\'
elif player_choice == '3' and Tic_Tac_Toe.c2 == ' X ' or player_choice == '3' and Tic_Tac_Toe.c2 == '|+|':
self.already_taken()
elif player_choice == '3':
Tic_Tac_Toe.x3 = 1
Tic_Tac_Toe.c1 = '\ /'
Tic_Tac_Toe.c2 = ' X '
Tic_Tac_Toe.c3 = '/ \\'
elif player_choice == '4' and Tic_Tac_Toe.a5 == ' X ' or player_choice == '4' and Tic_Tac_Toe.a5 == '|+|':
self.already_taken()
elif player_choice == '4':
Tic_Tac_Toe.x4 = 1
Tic_Tac_Toe.a4 = '\ /'
Tic_Tac_Toe.a5 = ' X '
Tic_Tac_Toe.a6 = '/ \\'
elif player_choice == '5' and Tic_Tac_Toe.b5 == ' X ' or player_choice == '5' and Tic_Tac_Toe.b5 == '|+|':
self.already_taken()
elif player_choice == '5':
Tic_Tac_Toe.x5 = 1
Tic_Tac_Toe.b4 = '\ /'
Tic_Tac_Toe.b5 = ' X '
Tic_Tac_Toe.b6 = '/ \\'
elif player_choice == '6' and Tic_Tac_Toe.c5 == ' X ' or player_choice == '6' and Tic_Tac_Toe.c5 == '|+|':
self.already_taken()
elif player_choice == '6':
Tic_Tac_Toe.x6 = 1
Tic_Tac_Toe.c4 = '\ /'
Tic_Tac_Toe.c5 = ' X '
Tic_Tac_Toe.c6 = '/ \\'
elif player_choice == '7' and Tic_Tac_Toe.a8 == ' X ' or player_choice == '7' and Tic_Tac_Toe.a8 == '|+|':
self.already_taken()
elif player_choice == '7':
Tic_Tac_Toe.x7 = 1
Tic_Tac_Toe.a7 = '\ /'
Tic_Tac_Toe.a8 = ' X '
Tic_Tac_Toe.a9 = '/ \\'
elif player_choice == '8' and Tic_Tac_Toe.b8 == ' X ' or player_choice == '8' and Tic_Tac_Toe.b8 == '|+|':
self.already_taken()
elif player_choice == '8':
Tic_Tac_Toe.x8 = 1
Tic_Tac_Toe.b7 = '\ /'
Tic_Tac_Toe.b8 = ' X '
Tic_Tac_Toe.b9 = '/ \\'
elif player_choice == '9' and Tic_Tac_Toe.c8 == ' X ' or player_choice == '9' and Tic_Tac_Toe.c8 == '|+|':
self.already_taken()
elif player_choice == '9':
Tic_Tac_Toe.x9 = 1
Tic_Tac_Toe.c7 = '\ /'
Tic_Tac_Toe.c8 = ' X '
Tic_Tac_Toe.c9 = '/ \\'
self.pr_r()
if Tic_Tac_Toe.a2 == Tic_Tac_Toe.b2 == Tic_Tac_Toe.c2 or Tic_Tac_Toe.a5 == Tic_Tac_Toe.b5 == Tic_Tac_Toe.c5:
print('You won')
input()
quit()
elif Tic_Tac_Toe.a8 == Tic_Tac_Toe.b8 == Tic_Tac_Toe.c8 or Tic_Tac_Toe.a2 == Tic_Tac_Toe.a5 == Tic_Tac_Toe.a8:
print('You won')
input()
quit()
elif Tic_Tac_Toe.b2 == Tic_Tac_Toe.b5 == Tic_Tac_Toe.b8 or Tic_Tac_Toe.c2 == Tic_Tac_Toe.c5 == Tic_Tac_Toe.c8:
print('You won')
input()
quit()
elif Tic_Tac_Toe.a2 == Tic_Tac_Toe.b5 == Tic_Tac_Toe.c8 or Tic_Tac_Toe.a8 == Tic_Tac_Toe.b5 == Tic_Tac_Toe.c2:
print('You won')
input()
quit()
player_choice = input()
if player_choice == '1' and Tic_Tac_Toe.a2 == ' X ' or player_choice == '1' and Tic_Tac_Toe.a2 == '|+|':
self.already_taken()
elif player_choice == '1':
Tic_Tac_Toe.x1 = 1
Tic_Tac_Toe.a1 = ' - '
Tic_Tac_Toe.a2 = '|+|'
Tic_Tac_Toe.a3 = ' - '
elif player_choice == '2' and Tic_Tac_Toe.b2 == ' X ' or player_choice == '2' and Tic_Tac_Toe.b2 == '|+|':
self.already_taken()
elif player_choice == '2':
Tic_Tac_Toe.x2 = 1
Tic_Tac_Toe.b1 = ' - '
Tic_Tac_Toe.b2 = '|+|'
Tic_Tac_Toe.b3 = ' - '
elif player_choice == '3' and Tic_Tac_Toe.c2 == ' X ' or player_choice == '3' and Tic_Tac_Toe.c2 == '|+|':
self.already_taken()
elif player_choice == '3':
Tic_Tac_Toe.x3 = 1
Tic_Tac_Toe.c1 = ' - '
Tic_Tac_Toe.c2 = '|+|'
Tic_Tac_Toe.c3 = ' - '
elif player_choice == '4' and Tic_Tac_Toe.a5 == ' X ' or player_choice == '4' and Tic_Tac_Toe.a5 == '|+|':
self.already_taken()
elif player_choice == '4':
Tic_Tac_Toe.x4 = 1
Tic_Tac_Toe.a4 = ' - '
Tic_Tac_Toe.a5 = '|+|'
Tic_Tac_Toe.a6 = ' - '
elif player_choice == '5' and Tic_Tac_Toe.b5 == ' X ' or player_choice == '5' and Tic_Tac_Toe.b5 == '|+|':
self.already_taken()
elif player_choice == '5':
Tic_Tac_Toe.x5 = 1
Tic_Tac_Toe.b4 = ' - '
Tic_Tac_Toe.b5 = '|+|'
Tic_Tac_Toe.b6 = ' - '
elif player_choice == '6' and Tic_Tac_Toe.c5 == ' X ' or player_choice == '6' and Tic_Tac_Toe.c5 == '|+|':
self.already_taken()
elif player_choice == '6':
Tic_Tac_Toe.x6 = 1
Tic_Tac_Toe.c4 = ' - '
Tic_Tac_Toe.c5 = '|+|'
Tic_Tac_Toe.c6 = ' - '
elif player_choice == '7' and Tic_Tac_Toe.a8 == ' X ' or player_choice == '7' and Tic_Tac_Toe.a8 == '|+|':
self.already_taken()
elif player_choice == '7':
Tic_Tac_Toe.x7 = 1
Tic_Tac_Toe.a7 = ' - '
Tic_Tac_Toe.a8 = '|+|'
Tic_Tac_Toe.a9 = ' - '
elif player_choice == '8' and Tic_Tac_Toe.b8 == ' X ' or player_choice == '8' and Tic_Tac_Toe.b8 == '|+|':
self.already_taken()
elif player_choice == '8':
Tic_Tac_Toe.x8 = 1
Tic_Tac_Toe.b7 = ' - '
Tic_Tac_Toe.b8 = '|+|'
Tic_Tac_Toe.b9 = ' - '
elif player_choice == '9' and Tic_Tac_Toe.c8 == ' X ' or player_choice == '9' and Tic_Tac_Toe.c8 == '|+|':
self.already_taken()
elif player_choice == '9':
Tic_Tac_Toe.x9 = 1
Tic_Tac_Toe.c7 = ' - '
Tic_Tac_Toe.c8 = '|+|'
Tic_Tac_Toe.c9 = ' - '
self.pr_r()
if Tic_Tac_Toe.a2 == Tic_Tac_Toe.b2 == Tic_Tac_Toe.c2 or Tic_Tac_Toe.a5 == Tic_Tac_Toe.b5 == Tic_Tac_Toe.c5:
print('You won')
input()
quit()
elif Tic_Tac_Toe.a8 == Tic_Tac_Toe.b8 == Tic_Tac_Toe.c8 or Tic_Tac_Toe.a2 == Tic_Tac_Toe.a5 == Tic_Tac_Toe.a8:
print('You won')
input()
quit()
elif Tic_Tac_Toe.b2 == Tic_Tac_Toe.b5 == Tic_Tac_Toe.b8 or Tic_Tac_Toe.c2 == Tic_Tac_Toe.c5 == Tic_Tac_Toe.c8:
print('You won')
input()
quit()
elif Tic_Tac_Toe.a2 == Tic_Tac_Toe.b5 == Tic_Tac_Toe.c8 or Tic_Tac_Toe.a8 == Tic_Tac_Toe.b5 == Tic_Tac_Toe.c2:
print('You won')
input()
quit()
round = Tic_Tac_Toe()
round.turn_x()
round.turn_x()
round.turn_x()
round.turn_x()
round.turn_x()
round.turn_x()
round.turn_x()
round.turn_x()