Criticize my TIC TAC TOE game - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Code Review (https://python-forum.io/forum-46.html) +--- Thread: Criticize my TIC TAC TOE game (/thread-27436.html) |
Criticize my TIC TAC TOE game - modeezy23 - Jun-07-2020 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. RE: Criticize my TIC TAC TOE game - GOTO10 - Jun-07-2020 Not a bad start! A few suggestions:
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. RE: Criticize my TIC TAC TOE game - modeezy23 - Jun-07-2020 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! RE: Criticize my TIC TAC TOE game - buran - Jun-07-2020 Please, avoid ALL CAPS. |