I am a beginner programmer and would like some feedback on this Tic Tac Toe game I made on Python. Any feedback would be appreciated, as I really want to get better at programming and pick up good habits. Here is my code. Something I would add is try
statements for invalid user input, but this is not important as I would like feedback on how to optimise this code. I would also love feedback on the check_if_win()
function as I believe there might be a better way of doing this. Thank you!
"""Tic Tac Toe"""
__author__ = "Enrique Gonzalez Aretos"
EMPTY = " "
PLAYER_1 = "X"
PLAYER_2 = "O"
# Create Board
def create_board():
"""
Function which creates an empty game board
:return: <list<list<str>>> Returns the game board
"""
return [
[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY]
]
# Show board
def show_board(board):
"""
Function which prints the board for the user
:param board: <list<list<str>>> The Tic Tac Toe board
:return: <str> A clean-looking version of the board
"""
for x in board:
# Find better way to print this
print("|".join(x))
# Place X on spot
def place_player(board, active_player, row, column):
"""
Function which places the player into the board
:param board:
:param active_player:
:param row:
:param column:
:return:
"""
if board[row-1][column-1] == EMPTY:
board[row-1][column-1] = active_player
# Check if someone has won
def check_if_win(board, active_player):
"""
Function which checks if the active player has won
:param board: <list<list<str>>> Tic Tac Toe Board
:param active_player: <str> The current active player
:return: <bool> Return True or False
"""
# Making lists which will store the diagonals and columns of baord
diagonal1 = []
diagonal2 = []
column1 = []
column2 = []
column3 = []
# Diagonal counter is used to index the different values in each row
diagonal_counter = 1
for row in board:
# When each row is checked, the different indexes are organised
# into the column and diagonal lists
column1.append(row[0])
column2.append(row[1])
column3.append(row[2])
diagonal1.append(row[diagonal_counter-1])
diagonal2.append(row[-diagonal_counter])
diagonal_counter += 1
if "".join(row) == active_player * 3:
return True
# If statement which checks if any list is full of the active
# player's symbol
if "".join(column1) == active_player * 3 or "".join(column2) ==
active_player * 3 or "".join(column3) == active_player * 3:
return True
elif "".join(diagonal1) == active_player * 3 or "".join(diagonal2)
== active_player * 3:
return True
else:
return False
def swap_player(active_player):
if active_player == PLAYER_1:
return PLAYER_2
else:
return PLAYER_1
def main():
# Creates board and assigns starting player
board = create_board()
active_player = PLAYER_1
while True:
show_board(board)
# Ask for player input
row = int(input("\nIt is {}'s turn play. Choose a row (1-3):
".format(active_player)))
column = int(input("Choose a column: "))
place_player(board, active_player, row, column)
# Checks if player has won
if check_if_win(board, active_player) is True:
show_board(board)
print("\n{} won!".format(active_player))
break
active_player = swap_player(active_player)
if __name__ == "__main__":
main()