I am trying to learn Python 3.6 with the help of a book. Until now I have learned the basics of functions, conditions and printing etc. No loops yet - so they need to come from recursion.
To test the current stuff I wrote this basic TicTacToe - and would like to know what could be better.
def print_game(*args):
formatstring = " A B C\n" + "-" *10 + "\n1 {}|{}|{}\n" + "-" *10 + "\n2 {}|{}|{}\n" + "-" *10 + "\n3 {}|{}|{}"
print(formatstring.format(*args))
def ask_player(round):
prompt_player1 = "Player one>> "
prompt_player2 = "Player two>> "
if round % 2 == 0:
current_choice = input(prompt_player2)
current_player = 2
else:
current_choice = input(prompt_player1)
current_player = 1
if current_choice != "A1" and current_choice != "B1" and current_choice != "C1" and current_choice != "A2" and current_choice != "B2" and current_choice != "C2" and current_choice != "A3" and current_choice != "B3" and current_choice != "C3":
print("Please use Field identifier like A1/B3 ...")
ask_player(round)
else:
return current_choice, current_player
def do_the_draw(game,field,player):
valid = 1
if player == 1:
char='X'
if player == 2:
char='O'
if field == 'A1' and game[0]==' ':
game[0] = char
elif field == 'B1' and game[1]==' ':
game[1] = char
elif field == 'C1' and game[2]==' ':
game[2] = char
elif field == 'A2' and game[3]==' ':
game[3] = char
elif field == 'B2' and game[4]==' ':
game[4] = char
elif field == 'C2' and game[5]==' ':
game[5] = char
elif field == 'A3' and game[6]==' ':
game[6] = char
elif field == 'B3' and game[7]==' ':
game[7] = char
elif field == 'C3' and game[8]==' ':
game[8] = char
else:
print(f"Invalid Move field {field} is already used - Choose another one")
valid = 0
print_game(*game)
return game,valid
def evaluate(s):
result = False #game continues
# Horizontal lines
if s[0]+s[1]+s[2] == 'XXX' or s[0]+s[1]+s[2] == 'OOO':
result = True
if s[3]+s[4]+s[5] == 'XXX' or s[3]+s[4]+s[5] == 'OOO':
result = True
if s[6]+s[7]+s[8] == 'XXX' or s[6]+s[7]+s[8] == 'OOO' :
result = True
# Vertical lines
if s[0]+s[3]+s[6] == 'XXX' or s[0]+s[3]+s[6] == 'OOO':
result = True
if s[1]+s[4]+s[7] == 'XXX' or s[1]+s[4]+s[7] == 'OOO':
result = True
if s[2]+s[5]+s[8] == 'XXX' or s[2]+s[5]+s[8] == 'OOO' :
result = True
# Diagonal lines
if s[0]+s[4]+s[8] == 'XXX' or s[1]+s[4]+s[7] == 'OOO':
result = True
if s[2]+s[4]+s[6] == 'XXX' or s[2]+s[5]+s[8] == 'OOO' :
result = True
return result
def main(draws,status):
user_input, player = ask_player(draws)
status,valid = do_the_draw(status,user_input,player)
if valid == 0:
return main(draws,status)
draws -=1
if evaluate(status):
draws = False
print(f"We have a winner Player: {player}")
if draws:
return main(draws,status)
elif input("try again?")=='y':
print("#" *100)
print("Here we go again\n\n")
draws = 9
status = " , , , , , , , , "
status = status.split(',')
print_game(*status)
return main(draws,status)
# Print the initial Game pattern
themaingame=" , , , , , , , , "
# cause i was not able to figure out how to initialise lists ... we split
themaingame = themaingame.split(',')
# Initial Print of the Board
print_game(*themaingame)
#Calling the Main Method with the maximum number of draws (9) and the prepared List
main(9,themaingame)