I just joined here. I have been doing python for a couple of weeks and I would like for some experienced programmers to review my code. I made this for a school project and I am proud of it. The code below works well but I would like to know what things I can improve on. Thank you for taking the time to review this.
main.py
main_grid = [['+' for col in range(3)] for row in range(3)]
player_one = {'key': 'P1', 'name': None, 'symbol': 'X', 'pos': []}
player_two = {'key': 'P2', 'name': None, 'symbol': 'O', 'pos': []}
player_list = [player_one, player_two]
def title_screen():
print(' '*15, ' _____ _____ _____')
print(' '*15, '|__ __|__ __| ____|')
print(' '*15, ' | | |_| |_| |___')
print(' '*15, ' _|_|_|_____|_____|')
print(' '*15, '|__ __| ___ | ____|')
print(' '*15, ' | | | | | | |___ ')
print(' '*15, '__|_|_|_|_|_|_____|')
print(' '*15, '|__ __| ___ | ____|')
print(' '*15, ' | | | |_| | ____|')
print(' '*15, ' |_| |_____|_____|')
def info_screen():
print('-'*51)
print('2021 L.A | A python school project. ')
print()
print("This game is a python version of 'TIC TAC TOE'.")
print('I hope you enjoy! :]')
print()
def intro_screen():
print('RULES:')
print('Rows and columns can only have inputs from [0-2].')
print('Players can only enter value once unless value is')
print('not valid.')
print("Players can't put their symbol in already occupied")
print('cells.')
print('Players win if their symbol is placed 3 in a row')
print('horizontally, vertically or diagonally. Anything')
print('else does not count as a win.')
print('If the grid is filled, the game results as a tie.')
print()
def start_screen():
title_screen()
info_screen()
intro_screen()
def get_name():
for player_type in player_list:
print(f"Enter [{player_type['key']}] name:")
player_type['name'] = input(f"[{player_type['key']}] > ")
print()
def get_input(player_type, desc):
print(desc)
user_input = input(f"[{player_type['key']}] {player_type['name']} > ")
print()
return user_input
def get_point(player_type, desc):
"""Makes user get a point value which could be a row or col."""
point = -1
while point not in range(3):
try:
point = int(get_input(player_type, desc))
except ValueError:
print('The entered value must be an integer.')
print('Please try again.')
print()
continue
if point in range(3):
return point
elif point not in range(3):
print('The entered value must be in range between 0-2.')
print('Please try again.')
print()
continue
else:
print('Unknown error')
print('Please try again.')
print()
continue
def check_pos(pos):
"""Check whether the pos value is in a player's coords list."""
if pos in player_one['pos'] or pos in player_two['pos']:
print('The entered coordinate is occupied by a player.')
print('Please try again.')
print()
return False
else:
return True
def update_grid(pos):
"""Updates grid using player coords."""
for player_type in player_list:
for prev_pos in player_type['pos']:
main_grid[prev_pos[0]][prev_pos[1]] = player_type['symbol']
def print_stats(player_type, turn_count):
"""Prints current game stats."""
print('-'*51)
print(f"CURRENT TURNS | {turn_count}/9")
print(f"CURRENT PLAYER | {player_type['key']}")
print(f"CURRENT SYMBOL | {player_type['symbol']}")
def print_grid():
"""Prints the grid"""
print()
for row in range(3):
print(' '*21, end='')
for col in range(3):
print(main_grid[row][col], end=' ')
print()
print()
def check_grid(player_type, pos):
"""End game when symbol is three in a row or grid is filled. Continues if not three in a row or not filled."""
row = pos[0]
col = pos[1]
symbol = player_type['symbol']
win_msg = f"[{player_type['key']}] {player_type['name']} has won the game."
tie_msg = 'The game has resulted a tie.'
if symbol == main_grid[row][0] == main_grid[row][1] == main_grid[row][2]:
print_result(win_msg)
return True
elif symbol == main_grid[0][col] == main_grid[1][col] == main_grid[2][col]:
print_result(win_msg)
return True
elif row == col and symbol == main_grid[0][0] == main_grid[1][1] == main_grid[2][2]:
print_result(win_msg)
return True
elif row + col == 2 and symbol == main_grid[0][2] == main_grid[1][1] == main_grid[2][0]:
print_result(win_msg)
return True
elif '+' not in main_grid[0] and '+' not in main_grid[1] and '+' not in main_grid[2]:
print_result(tie_msg)
return True
else:
return False
def print_result(msg):
"""Prints end result of game"""
print('-'*51)
print('RESULT:')
print_grid()
print(msg)
exit_result()
def setup_pos(player_type):
"""Sets up position value
Gets each point value.
Checks each point value
Checks position value
Adds position value to player dictionary
"""
valid_pos = False
while valid_pos is False:
row = get_point(player_type, 'Select a row:')
col = get_point(player_type, 'Select a column:')
new_pos = (row, col)
valid_pos = check_pos(new_pos)
if valid_pos is True:
player_type['pos'].append(new_pos)
return new_pos
def exit_result():
"""Choice to exit result screen."""
print()
print(f"Press any key to exit.")
user_input = input('> ')
exit(0)
def setup_game():
turn_count = 1
game_finish = False
while game_finish is False:
for player_type in player_list:
print_stats(player_type, turn_count)
print_grid()
new_pos = setup_pos(player_type)
update_grid(new_pos)
game_finish = check_grid(player_type, new_pos)
turn_count += 1
if game_finish is True: # If player won, break for loop on player's turn.
break
def main():
start_screen()
get_name()
setup_game()
if __name__ == '__main__':
main()