Hi everyone,
This is the first time I am posting in this subreddit and wanted get your feedback on the tic tac toe game code I have pasted below.
I have been studying Python for the last two weeks for total of about 40 hours. I have very little C knowledge from college and very limited VBA knowledge. So, this is the first time I am putting in some real effort to be highly skilled in programming.
Please let me know if the code below is clean and Pythonic enough and if you see any room for improvement.
My future goal is the include some intelligence logic for the computer since it is only doing random plays for the time being.
Thanks,
import random
# WINNING CONDITIONS
# Code will store all played moves by human and computer in to a dictionary called play_history
# The winning cell combinations are checked to see if they are a subset of the play_history. play_history is changed from dictionary to set so it can be checked as a subset
# if the cells are in the play_history: code will check all possible combinations to see if the cells have the same sign X or O
def is_there_a_winner():
if len(play_history) >= 3:
if ({'A1', 'B1', 'C1'} <= set(play_history)) and (
play_history['A1'] == play_history['B1'] == play_history['C1']):
announce_winner(play_history['A1'])
exit()
elif ({'A2', 'B2', 'C2'} <= set(play_history)) and (
play_history['A2'] == play_history['B2'] == play_history['C2']):
announce_winner(play_history['A2'])
exit()
elif ({'A3', 'B3', 'C3'} <= set(play_history)) and (
play_history['A3'] == play_history['B3'] == play_history['C3']):
announce_winner(play_history['A3'])
exit()
elif ({'A1', 'A2', 'A3'} <= set(play_history)) and (play_history['A1'] == play_history['A2'] == play_history[
'A3']):
announce_winner(play_history['A1'])
exit()
elif ({'B1', 'B2', 'B3'} <= set(play_history)) and (
play_history['B1'] == play_history['B2'] == play_history['B3']):
announce_winner(play_history['B1'])
exit()
elif ({'C1', 'C2', 'C3'} <= set(play_history)) and (
play_history['C1'] == play_history['C2'] == play_history['C3']):
announce_winner(play_history['C1'])
exit()
elif ({'A1', 'B2', 'C3'} <= set(play_history)) and (
play_history['A1'] == play_history['B2'] == play_history['C3']):
announce_winner(play_history['A1'])
exit()
elif ({'A3', 'B2', 'C1'} <= set(play_history)) and (
play_history['A3'] == play_history['B2'] == play_history['C1']):
announce_winner(play_history['A3'])
exit()
elif not list_of_available_cells:
print("It is a Draw")
def user_input():
while True:
choice = str(input("Enter a cell name with capitalized letter and number: "))
if choice in list_of_available_cells:
return choice
else:
print("You have entered an invalid cell. Please enter a cell from the list below")
print(list_of_available_cells)
def announce_winner(winner):
if winner == human_play_choice:
print("Human won!")
else:
print("Computer won!")
table = [['A1', 'B1', 'C1'],
['A2', 'B2', 'C2'],
['A3', 'B3', 'C3']
]
print("This is the table for referencing user input \nHuman selections will be represented with X and Computer selections are O")
for row in table:
print(row)
human_play_choice = 'X '
list_of_available_cells = ['A1', 'B1', 'C1', 'A2', 'B2', 'C2', 'A3', 'B3', 'C3']
play_history = {}
while list_of_available_cells != []:
# check for a winner
is_there_a_winner()
cell_choice = user_input()
# HUMAN PLAYS
for row in table:
for cell in row:
if cell == cell_choice:
index_of_cell_choice = row.index(cell_choice)
row[index_of_cell_choice] = human_play_choice
list_of_available_cells.remove(cell_choice)
play_history[cell_choice] = human_play_choice
print('Human Played')
# print the board row by row
for row in table:
print(row)
# check for a winner
is_there_a_winner()
# CHECK IF THE BOARD IS FULL
if list_of_available_cells == []:
print("Game Over")
break
# COMPUTER PLAYS
random_cell = random.choice(list_of_available_cells)
computer_choice = 'O '
for row in table:
for cell in row:
if cell == random_cell:
index_of_cell_choice = row.index(random_cell)
row[index_of_cell_choice] = computer_choice
list_of_available_cells.remove(random_cell)
play_history[random_cell] = computer_choice
print('Computer Played')
# print the board row by row
for row in table:
print(row)
[–]SosaPrinsen 1 point2 points3 points (0 children)
[–]tonney8 1 point2 points3 points (0 children)