Overview
This is my attempt at a Tic Tac Toe game. I need some criticism and some ideas on what I could do better, because I know somethings need to be cleaned up.
Quick Notes
I also couldn't figure ways to make the dictionary checks less cluttered and more cleaner(I am noob, pardon this ugly code)
I did this without any form of help
This was made and tested on Juipiter Notebook
Code
#Tic Tac Toe: Attempt 2
from IPython.display import clear_output
#for printing the gameboard
def game_board(dictionary):
print(dictionary['part1'])
print(dictionary['part2'])
print(dictionary['part3'])
#for creating the gameboard
def create_game_board():
gboard = {'part1':[' ',' ',' '], 'part2':[' ',' ',' '], 'part3':[' ',' ',' ']}
return gboard
#player specific charachters
def replacement_value(val):
if val == 'X':
val = 'O'
elif val == 'O':
val ='X'
return val
#for choosing which player represents what charachter
def player_value():
choice_check = False
player_dict = {'X':'', 'O':''}
while choice_check == False:
choice = input('Player 1: Are you playing first[Y or N]? ')
if choice.upper() == 'Y' or choice.upper() == 'N':
if choice.upper() == 'Y':
player_dict['X'] = 'player1'
player_dict['O'] = 'player2'
print('Player 1 goes first')
choice_check = True
elif choice.upper() == 'N':
player_dict['O'] = 'player1'
player_dict['X'] = 'player2'
print('Player 2 goes first')
choice_check = True
else:
print('Please type in Y or N')
return player_dict
#for choosing what part of the board should be replaced
def input_logic():
play_location = 'Not'
in_index = False
while play_location.isdigit() == False or in_index == False:
play_location = input('Where do you want to play[Between 1 - 9]: ')
if play_location.isdigit() == True:
if int(play_location) in list(range(1,10)):
if int(play_location) > 6:
if int(play_location) == 7:
if gboard['part3'][0] == ' ':
in_index = True
else:
print('That position is already taken')
elif int(play_location) == 8:
if gboard['part3'][1] == ' ':
in_index = True
else:
print('That position is already taken')
elif int(play_location) == 9:
if gboard['part3'][2] == ' ':
in_index = True
else:
print('That position is already taken')
elif int(play_location) > 3:
if int(play_location) == 4:
if gboard['part2'][0] == ' ':
in_index = True
else:
print('That position is already taken')
elif int(play_location) == 5:
if gboard['part2'][1] == ' ':
in_index = True
else:
print('That position is already taken')
elif int(play_location) == 6:
if gboard['part2'][2] == ' ':
in_index = True
else:
print('That position is already taken')
elif int(play_location) > 0:
if int(play_location) == 1:
if gboard['part1'][0] == ' ':
in_index = True
else:
print('That position is already taken')
elif int(play_location) == 2:
if gboard['part1'][1] == ' ':
in_index = True
else:
print('That position is already taken')
elif int(play_location) == 3:
if gboard['part1'][2] == ' ':
in_index = True
else:
print('That position is already taken')
else:
print('Please make sure your number is between 1 - 9')
else:
print('Your input was not a number')
return int(play_location)
#for inputing charachters in the board
def board_input_logic(gboard, play_location, val):
if play_location > 6:
if play_location == 7:
gboard['part3'][0] = val
elif play_location == 8:
gboard['part3'][1] = val
elif play_location == 9:
gboard['part3'][2] = val
elif play_location > 3:
if play_location == 4:
gboard['part2'][0] = val
elif play_location == 5:
gboard['part2'][1] = val
elif play_location == 6:
gboard['part2'][2] = val
elif play_location > 0:
if play_location == 1:
gboard['part1'][0] = val
elif play_location == 2:
gboard['part1'][1] = val
elif play_location == 3:
gboard['part1'][2] = val
#Better Optimization: Use for loops
#checks for winner
def win_check(gboard,player_dict):
player_list = list(player_dict.values())
if gboard['part1'][0] == 'X' and gboard['part1'][1] == 'X' and gboard['part1'][2] == 'X' or gboard['part2'][0] == 'X' and gboard['part2'][1] == 'X' and gboard['part2'][2] == 'X' or gboard['part3'][0] == 'X' and gboard['part3'][1] == 'X' and gboard['part3'][2] == 'X' or gboard['part1'][0] == 'X' and gboard['part2'][0] == 'X' and gboard['part3'][0] == 'X' or gboard['part1'][1] == 'X' and gboard['part2'][1] == 'X' and gboard['part3'][1] == 'X' or gboard['part1'][2] == 'X' and gboard['part2'][2] == 'X' and gboard['part3'][2] == 'X' or gboard['part1'][0] == 'X' and gboard['part2'][1] == 'X' and gboard['part3'][2] == 'X' or gboard['part1'][2] == 'X' and gboard['part2'][1] == 'X' and gboard['part3'][0] == 'X':
print(f'{player_list[0].capitalize()} is the winner!')
return False
elif gboard['part1'][0] == 'O' and gboard['part1'][1] == 'O' and gboard['part1'][2] == 'O' or gboard['part2'][0] == 'O' and gboard['part2'][1] == 'O' and gboard['part2'][2] == 'O' or gboard['part3'][0] == 'O' and gboard['part3'][1] == 'O' and gboard['part3'][2] == 'O' or gboard['part1'][0] == 'O' and gboard['part2'][0] == 'O' and gboard['part3'][0] == 'O' or gboard['part1'][1] == 'O' and gboard['part2'][1] == 'O' and gboard['part3'][1] == 'O' or gboard['part1'][2] == 'O' and gboard['part2'][2] == 'O' and gboard['part3'][2] == 'O' or gboard['part1'][0] == 'O' and gboard['part2'][1] == 'O' and gboard['part3'][2] == 'O' or gboard['part1'][2] == 'O' and gboard['part2'][1] == 'O' and gboard['part3'][0] == 'O':
print(f'{player_list[1].capitalize()} is the winner!')
return False
elif gboard['part1'][0] != ' ' and gboard['part1'][1] != ' ' and gboard['part1'][2] != ' ' and gboard['part2'][0] != ' ' and gboard['part2'][1] != ' ' and gboard['part2'][2] != ' ' and gboard['part3'][0] != ' ' and gboard['part3'][1] != ' ' and gboard['part3'][2] != ' ':
print("It's a tie")
return False
else:
return True
#couldnt figure out how to make code more compact, for-loops maybe?
#Checks if game should be continued
def continue_game():
running = True
ans_list = ['Y', 'N']
while running:
cont = input('Would you like to continue[Y or N]? ')
if cont.upper() in ans_list:
if cont.upper() == 'Y':
return True
else:
return False
else:
print('Please type in "Y" or "N"')
#game loop
#variables
gboard = create_game_board()
player_dict = player_value()
game_loop = True
val = 'O'
running = True
#enclosing loop for repeatability
while running == True:
#actual loop for game
while game_loop == True:
val = replacement_value(val)
play_location = input_logic()
game_board(gboard)
board_input_logic(gboard,play_location,val)
clear_output()
game_board(gboard)
game_loop = win_check(gboard, player_dict)
#deciding factor for repeating game
game_loop = continue_game()
if game_loop == True:
gboard = create_game_board()
player_dict = player_value()
val = 'O'
continue
#breaking out from game(AKA - End)
else:
clear_output()
print('Thanks for playing!')
break
Any improvements/better ideas are much appreciated