HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonModerate

Tic Tic Tic Tac Tac Tac Toe Toe Toe

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
tactictoe

Problem

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 == '|+|':

Solution

You have many problems within your code, including:

  • Meaningless variable names (why, for example, is the user input named aaa?!);



  • Repetition;



  • Inconsistent whitespace;



  • Repetition;



  • Compound statements (the call the pa() should be on a separate line); and



  • Repetition.



I'd strongly recommend reading Python's style guide and following its guidance. However, your fundamental problem is that you're trying to write the game logic using the same structures you use for presenting the outcome - you don't need to.

Instead, note that the information you actually need to hold for the logic could look something like:

board = [[None, None,  'X'],
         [ 'O', None,  'X'],
         [None, None,  'O']]


This is a single, mutable structure, which can be easily passed around through various functions. You can then print out the current state based on this, using e.g. str.format:

# template for the board display

TEMPLATE = '''
              { }|{ }|{ }
              { }|{ }|{ }
              { }|{ }|{ }
              ---+---+---
              { }|{ }|{ }
              { }|{ }|{ }
              { }|{ }|{ }
              ---+---+---
              { }|{ }|{ }
              { }|{ }|{ }
              { }|{ }|{ }
'''

# how to display a single character

X_TOP = r'\ /'
X_MID = r' X '
X_BOT = r'/ \\'

...

def display(board):
    """Format the template according to the board state."""
    ...


Note that I've used multiline strings (''') for the template, to handle multiple lines, and raw (r'') strings for the components, to avoid escaping (most of!) the backslashes.

A further step could be to create a Board class, which holds both the current state (as an instance attribute) and the display functionality (as the __str__ method). However, I think OOP might be too much at this point.

Code Snippets

board = [[None, None,  'X'],
         [ 'O', None,  'X'],
         [None, None,  'O']]
# template for the board display

TEMPLATE = '''
              { }|{ }|{ }
              { }|{ }|{ }
              { }|{ }|{ }
              ---+---+---
              { }|{ }|{ }
              { }|{ }|{ }
              { }|{ }|{ }
              ---+---+---
              { }|{ }|{ }
              { }|{ }|{ }
              { }|{ }|{ }
'''

# how to display a single character

X_TOP = r'\ /'
X_MID = r' X '
X_BOT = r'/ \\'

...

def display(board):
    """Format the template according to the board state."""
    ...

Context

StackExchange Code Review Q#107741, answer score: 19

Revisions (0)

No revisions yet.