Python Forum
Criticize my TIC TAC TOE game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Criticize my TIC TAC TOE game
#1
Hi! I'm a bit new to python. I've been learning on my own for about 2-3weeks. Here is a typical TIC-TAC-TOE game that I made.
PLEASE LET ME KNOW WHAT I CAN DO TO BETTER IMPROVE MY CODING SKILLS!
IS THIS CONSIDERED GOOD CLEAN CODE?
Thanks in advance for all comments and and advice!

Also, HOW CAN I MAKE LINE 81 SHORTER/LOOK CLEANER?

def get_player():
    player = int(input('\nWhich player are you? (1 or 2): '))
    return player
    
def get_num():
    num = int(input('Enter number to take its place: '))
    return num
    
def assigns_letters(player,num,row1,row2,row3):
    if num in row3 and player == 1:
        index = row3.index(num)
        row3[index] = 'X'
    elif num in row3 and player == 2:
        index = row3.index(num)
        row3[index] = 'O'
    elif num in row2 and player == 1:
        index = row2.index(num)
        row2[index] = 'X'
    elif num in row2 and player == 2:
        index = row2.index(num)
        row2[index] = 'O'
    elif num in row1 and player == 1:
        index = row1.index(num)
        row1[index] = 'X'
    elif num in row1 and player == 2:
        index = row1.index(num)
        row1[index] = 'O'
        
def update_rows(player,num,row1,row2,row3):
    row3 = row3
    row2 = row2
    row1 = row1
    
def print_board(player,num,row1,row2,row3):
    print('|', end='')
    for i in row3:
        print(f' {i} |',end='')
    print('\n ----------')
    print('|', end='')
    for i in row2:
        print(f' {i} |',end='')
    print('\n ----------')
    print('|', end='')
    for i in row1:
        print(f' {i} |',end='')
        
def check_rows(player,num,row1,row2,row3):
    # CHECKS ROWS
    if row1 == ['X','X','X'] or row2 == ['X','X','X'] or row3 == ['X','X','X']:
        print(f'\nPlayer {player}, you WON!!')
        return True
    elif row1 == ['O','O','O'] or row2 == ['O','O','O'] or row3 == ['O','O','O']:
        print(f'\nPlayer {player}, you WON!!')
        return True
        
def check_columns(player,num,row1,row2,row3):
    # CHECK COLUMNS
    column1 = [row3[0],row2[0],row1[0]]
    column2 = [row3[1],row2[1],row1[1]]
    column3 = [row3[2],row2[2],row1[2]]
    if column1 == ['X','X','X'] or column2 == ['X','X','X'] or column2 == ['X','X','X']:
        print(f'\nPlayer {player}, you WON!!')
        return True
    elif column1 == ['O','O','O'] or column2 == ['O','O','O'] or column2 == ['O','O','O']:
        print(f'\nPlayer {player}, you WON!!')
        return True
        
def check_diagonal(player,num,row1,row2,row3):
    # CHECKS DIAGONAL
    diagonal1 = [row3[0],row2[1],row1[2]]
    diagonal2 = [row3[2],row2[1],row1[0]]
    if diagonal1 == ['X','X','X'] or diagonal2 == ['X','X','X']:
        print(f'\nPlayer {player}, you WON!!')
        return True
    elif diagonal1 == ['O','O','O'] or diagonal2 == ['O','O','O']:
        print(f'\nPlayer {player}, you WON!!')
        return True
        
def check_tie(player,num,row1,row2,row3):
    tie_game = [row3[0],row3[1],row3[2],row2[0],row2[1],row2[2],row1[0],row1[1],row1[2]]
    if (tie_game[0] == 'O' or tie_game[0] == 'X') and (tie_game[1] == 'O' or tie_game[1] == 'X') and (tie_game[2] == 'O' or tie_game[2] == 'X') and (tie_game[3] == 'O' or tie_game[3] == 'X') and (tie_game[4] == 'O' or tie_game[4] == 'X') and (tie_game[5] == 'O' or tie_game[5] == 'X') and (tie_game[6] == 'O' or tie_game[6] == 'X') and (tie_game[7] == 'O' or tie_game[7] == 'X') and (tie_game[8] == 'O' or tie_game[8] == 'X'):
        print('\nITS A TIE GAME')
        return True

row1 = [1,2,3]
row2 = [4,5,6]
row3 = [7,8,9]
print('WELCOME TO TIC-TAC-TOE!')
print('This is the layout of the board.\nENTER A NUMBER TO REPLACE IT WITH \'X\' OR \'O\'')
print_board(player,num,row1,row2,row3)
endgame = False
while endgame != True:
    player = get_player()
    while player not in range(1,3):
        print('INVALID ENTRY')
        player = get_player()
    else:
        pass
    num = get_num()
    assigns_letters(player,num,row1,row2,row3)
    update_rows(player,num,row1,row2,row3)
    print_board(player,num,row1,row2,row3)
    if check_rows(player,num,row1,row2,row3) == True:
        break
    if check_columns(player,num,row1,row2,row3) == True:
        break
    if check_diagonal(player,num,row1,row2,row3) == True:
        break
    if check_tie(player,num,row1,row2,row3) == True:
        break

EDIT:
LINE 84: player = 0, num = 0
I forgot to put this statement in. It is needed in order for the code to run properly. Also, I'm sorry for the profanity that shows when you win the game. I forgot to change it before I posted lol.
Reply
#2
Not a bad start! A few suggestions:

  1. Your update_rows() function doesn't actually do anything. You can remove it and the call on line 101 without affecting the program's behavior.
  2. Several of your function calls include parameters you aren't using. For example, why pass the player and num values to print_board() when you aren't using them within the function?
  3. Since turns always alternate in Tic-Tac-Toe, you could build that into the game instead of asking which player the user is. If Player 1 just moved, then it's Player 2's turn, and so on. With your current code, Player 1 can just go three times in a row to win.
  4. Regarding line 81, there is a much simpler way to tell if the game is a tie. If 9 turns have been completed and there is no winner, it's a tie.

You may find other things you'd like to change as well, but those few jumped out at me. Tic-Tac-Toe is a great project to write and rewrite when you are starting out, because there are a ton of different ways to achieve the desired result.
Reply
#3
These are all awesome suggestions and advice. Thank you so much for taking the time out of your day to help me out. It means a lot. I’ll definitely try implementing the alternation between players without having to ask. That’ll be fun to figure out. I didn’t realize I didn’t need the update_rows function. I could’ve sworn it made a difference but I’ll definitely try it without. As for line 81, wow, idk how I didn’t think of that. That’s definitely a much simpler way of solving that problem. Again, thanks for your reply! It is very much appreciated and I will definitely take it into consideration to further develop my programming skills!
Reply
#4
Please, avoid ALL CAPS.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020