#TICTACTOE V 1.0

import time

#Variables5
global bd
bd = [[0,0,0],[0,0,0],[0,0,0]]
global filled_fields
filled_fields = 0
global player
global playershow
playershow = 1
player = 1
global play
play = 1


#FUNCTIONS
#--------------------
def inputs():
    global playershow
    global bd
    print("It's the turn of player", playershow)
    m = input("Where do you want to put your mark? ")
    if len(m) > 1:
        sp = m[0]
        zl = m[1]
        z = int(zl) - 1
        s = ord(sp) - 65
        if bd[z][s] > 0:
            print("""
THAT FIELD IS ALREADY TAKEN, CHOOSE DIFFERENT ONE!
""")
        else:
            inputremap(z,s)
    else:
        print("""
        PLEASE TYPE IN A LEGIT COORDINATE:
        FORM: Coordinate Y Coordinate X --> Ex. A1/A2
        """)
    
def inputremap(z, s):
    global player
    global playershow
    global bd
    bd[z][s] = player
    if playershow == 1:
        playershow = 2
        player = 7
    else:
        playershow = 1
        player = 1
    global filled_fields
    filled_fields +=  1
    full(filled_fields)
    

def winning():
    global play
    global bd
    x = 0
    y = 0
    global go
    go = 1
    count = 0
    print("""
checking for winner
""")
    while go == 1:
        if sum(bd[x]) == 3:
            go = 0
            play = 0
            print("The Winner is player 1")
        elif sum(bd[x]) == 21:
            go = 0
            play = 0
            print("The Winner is player 2")
        elif bd[0][y] + bd[1][y] + bd[2][y] == 3:
            go = 0
            play = 0
            print("The Winner is player 1")
        elif bd[0][y] + bd[1][y] + bd[2][y] == 21:
            go = 0
            play = 0
            print("The Winner is player 2")
        else:
            x += 1
            y += 1
            if count >= 2:
                go = 0
                print("""no winner yet
""")
            else:
                count += 1

def full(filled_field):
    global bd
    if filled_field == 9:
        print("draw")
        global play
        play = 0
    else:
        winning()


#BOARD DRAWING
def board_draw():
    print("\n")
    xx = 0
    yy = 0
    global bd
    while yy < 3:
        while xx < 3:
            if bd[xx][yy] == 0:
                print("      ", end = " ")
            elif bd[xx][yy] == 1:
                print("  X  ", end = " ")
            elif bd[xx][yy] == 7:
                print("  O  ", end = " ")
            else:
                print(""" CRITICAL ERROR""")
            if xx < 2:
                print("|", end = " ")
            else:
                print(" ", end = " ")
            xx += 1
        print(" ")
        xx = 0
        if yy < 2:
            print("----------------------")
        else:
            print(" ")
        yy += 1
            
        


#MAIN CYCLE
print(50*("\n"))
      
print("""

        ------------------------------------------------------




                            TIC TAC TOE
                            ===========
                                  V 1.0
                            V 2021.06.18      
                        
                        
                        
by Janis Hutz
""")
time.sleep(2)
print("starting...")
time.sleep(3)
print("\n\n\n")
goahead = 1
print("""
                 |       |
            ----------------
                 |       |
            ----------------
                 |       |
                """)
while goahead == 1:
    while play == 1:
        inputs()
        board_draw()
    time.sleep(2)
    print("""
REPLAY?
""")
    i = input("""Choose: (y/n)""")
    if i == "y":
        goahead = 1
        play = 1
    else:
        goahead = 0
print("""

    GOOD BYE!

""")