I am currently building a Tictac toe game in python. I have completed the game and am now trying to make the code more efficient. for context on this question, my tic tac toe board is drawn using a dictionary and changing it with user inputs, like so:
spots = {1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7', 8: '8', 9: '9'}
I have a function to loop the game if user gives a certain input once the game is complete. that function looks like this:
def wants_to_play_again(spots):
play_again = input('Do you wanna play again?\n Press (a) to continue... \n Press any other key to exit the game: ')
if play_again == 'a':
print('\n')
return True
else:
print('\nSee you later! Thanks for playing')
return False
later in the code of the game itself, I say that if the above function returns true, then I reset the board, it looks like this:
if wants_to_play_again(spots) is True:
spots = {1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7', 8: '8', 9: '9'}
available_spots = [1, 2, 3, 4, 5, 6, 7, 8, 9]
continue
else:
break
My issue is that I reuse this block of code ^ another time in the loop of the game, if the board returns a draw. my question is how can I condense this code so that I don't have to use 5 lines of code to reset the board and available_spots elements. Could I turn it into a function that I only have to write once? Is this possible? Thank you for your help.