from random import randint

def getDistance(p1,p2):
    lsq = (p2[0]-p1[0])**2
    hsq = (p2[1]-p1[1])**2
    distance = (lsq+hsq)**.5
    return(distance)

def XOR(a,b,item=True):
    if item!=True:
        return(ItemXOR(a,b,item))
    else:
        if a == b:
            return(False)
        elif a != b:
            return(True)
        
def ItemXOR(a,b,item):
    if a in item and b in item:
        return(False)
    elif a not in item and b in item:
        return(True)
    elif a in item and b not in item:
        return(True)
    elif a not in item and b not in item:
        return(False)

def TripleXOR(a,b,c,item):
    if a in item and b in item and c in item:
        return(False)
    elif XOR(a,b,item) and c not in item:
        return(True)
    elif XOR(b,c,item) and a not in item:
        return(True)
    elif XOR(a,c,item) and b not in item:
        return(True)
    else:
        return(False)

def checkDiagonals(indices):
    global square
    profile = []
    leg = 0
    for index1 in indices:
        pair1=playerOne[index1]
        for index2 in indices:
            pair2=playerOne[index2]
            dist = getDistance(pair1,pair2)
            if dist != 0:
                profile.append(dist)
                if (dist < leg) or (leg == 0):
                    leg = dist
    for i in range(len(profile)):
        profile[i] = int((profile[i]/leg)*1000)
    print(square)
    print(set(profile))
    print(set(profile)==square)
    if set(profile)==square:
        print("True")
        return(True)
    else:
        return(False)
    
def checkSquare(player):
    distances = computeDistances(player)
    for item in distances:
        distance1=item[0]
        A = item[1]
        B = item[2]
        for item2 in distances:
            if item[0] == item2[0]:
                if XOR(A,B,item2):
                    if A in item2:
                        if A == item2[1]:
                            C = item2[2]
                        elif A == item2[2]:
                            C = item2[1]
                    elif B in item2:
                        if B == item2[1]:
                            C = item2[2]
                        elif B == item2[2]:
                            C = item2[1]
                    for item3 in distances:
                        if item[0]==item3[0]:
                            if TripleXOR(A,B,C,item3):
                                print(A,B,C)
                                print(item3)
                                op = item3[1:2]
                                if A in op:
                                    if A == item3[1]:
                                        D = item3[2]
                                    elif A == item3[2]:
                                        D = item3[1]
                                    return(checkDiagonals([A,B,C,D]))
                                elif B in op:
                                    if B == item3[1]:
                                        D = item3[2]
                                    elif B == item3[2]:
                                        D = item3[1]
                                    return(checkDiagonals([A,B,C,D]))
                                elif C in op:
                                    if C == item3[1]:
                                        D = item3[2]
                                    elif C == item3[2]:
                                        D = item3[1]
                                    return(checkDiagonals([A,B,C,D]))
                                else:
                                    print("else")
                                    continue
    return(False)

def computeDistances(player):
    d = []
    done=[]
    for x in range(len(player)):
        done.append(x)
        for y in range(len(player)):
            if y in done:
                continue
            z = getDistance(player[x],player[y])
            if z != 0:
                d.append([z,x,y])
    return(d)
                
def askMove():
    move = input()
    move = [int(x) for x in move.split(',')]
    if board[move[0]][move[1]] != '_':
        print("Space Occupied")
        move = askMove()
    return(move)

done = []
square = set(
    [
        1000,
        1414
    ]
)
playerOne = [(0,0),(3,4),(0,5),(8,4)] #Rhombus
playerOne= [(0,0),(0,4),(4,4),(4,0)] #Square
playerOne = [(0,0),(0,4),(0,8),(2,3)] #Other
playerOne=[]
playerTwo=[]
board=[
    ['_','_','_','_'],
    ['_','_','_','_'],
    ['_','_','_','_'],
    ['_','_','_','_']
]
one = False
two = False
c = -5
while not one and not two and c < (n**2/2)-1:
    move = askMove()
    board[move[0]][move[1]] = 'X'
    playerOne.append((move[0],move[1]))
    for line in board:
        print(line)
    if len(playerOne) >= 4:
        computeDistances(playerOne)
        one = checkSquare(playerOne)
        if one:
            print("Player One loses")
            break
    print('========')
    seed = (randint(0,n-1),randint(0,n-1))
    while board[seed[0]][seed[1]] != '_':
        seed = (randint(0,n-1),randint(0,n-1))
    board[seed[0]][seed[1]] = 'O'
    playerTwo.append((seed[0],seed[1]))
    for line in board:
        print(line)
    if len(playerTwo) >= 4:
        computeDistances(playerTwo)
        two = checkSquare(playerOne)
        if two:
            print("Player Two wins")
            break
    c+=1
if not one and not two:
    print("Draw")
1,1
['_', '_', '_', '_']
['_', 'X', '_', '_']
['_', '_', '_', '_']
['_', '_', '_', '_']
========
['_', '_', '_', '_']
['_', 'X', '_', '_']
['_', 'O', '_', '_']
['_', '_', '_', '_']
0,0
['X', '_', '_', '_']
['_', 'X', '_', '_']
['_', 'O', '_', '_']
['_', '_', '_', '_']
========
['X', 'O', '_', '_']
['_', 'X', '_', '_']
['_', 'O', '_', '_']
['_', '_', '_', '_']
1,0
['X', 'O', '_', '_']
['X', 'X', '_', '_']
['_', 'O', '_', '_']
['_', '_', '_', '_']
========
['X', 'O', '_', '_']
['X', 'X', 'O', '_']
['_', 'O', '_', '_']
['_', '_', '_', '_']
2,0
['X', 'O', '_', '_']
['X', 'X', 'O', '_']
['X', 'O', '_', '_']
['_', '_', '_', '_']
========
['X', 'O', '_', '_']
['X', 'X', 'O', '_']
['X', 'O', '_', '_']
['_', '_', 'O', '_']
3,0
['X', 'O', '_', '_']
['X', 'X', 'O', '_']
['X', 'O', '_', '_']
['X', '_', 'O', '_']
========
['X', 'O', '_', '_']
['X', 'X', 'O', '_']
['X', 'O', 'O', '_']
['X', '_', 'O', '_']
3,1
['X', 'O', '_', '_']
['X', 'X', 'O', '_']
['X', 'O', 'O', '_']
['X', 'X', 'O', '_']
========
['X', 'O', '_', '_']
['X', 'X', 'O', '_']
['X', 'O', 'O', 'O']
['X', 'X', 'O', '_']

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-177-ee3c18c56ba9> in <module>()
     11 c = -5
     12 while not one and not two and c < (n**2/2)-1:
---> 13     move = askMove()
     14     board[move[0]][move[1]] = 'X'
     15     playerOne.append((move[0],move[1]))

<ipython-input-176-c819d55bc988> in askMove()
    118 def askMove():
    119     move = input()
--> 120     move = [int(x) for x in move.split(',')]
    121     if board[move[0]][move[1]] != '_':
    122         print("Space Occupied")

<ipython-input-176-c819d55bc988> in <listcomp>(.0)
    118 def askMove():
    119     move = input()
--> 120     move = [int(x) for x in move.split(',')]
    121     if board[move[0]][move[1]] != '_':
    122         print("Space Occupied")

ValueError: invalid literal for int() with base 10: ''
board=[
    ['_','_','_','_'],
    ['_','_','_','_'],
    ['_','_','_','_'],
    ['_','_','_','_']
]
def askMove():
    move = input()
    move = [int(x) for x in move.split(',')]
    if board[move[0]][move[1]] != '_':
        print("Space Occupied")
        move = askMove()
    else:
        return(move)
    
move = askMove()
board[move[0]][move[1]] = 'X'
playerOne.append((move[0],move[1]))
for line in board:
    print(line)
print('========')
seed = (randint(0,n-1),randint(0,n-1))
while board[seed[0]][seed[1]] != '_':
    seed = (randint(0,n-1),randint(0,n-1))
board[seed[0]][seed[1]] = 'O'
playerTwo.append((seed[0],seed[1]))
for line in board:
    print(line)
2,3
['_', '_', '_', '_']
['_', 'X', '_', '_']
['_', '_', '_', 'X']
['_', '_', 'O', '_']
========
['_', '_', '_', 'O']
['_', 'X', '_', '_']
['_', '_', '_', 'X']
['_', '_', 'O', '_']
checkSquare(playerTwo)
0 2 3
[1.4142135623730951, 1, 2]
else
0 2 3
[1.4142135623730951, 2, 5]
{1000, 1414}
{1000, 2000, 2236, 1414}
False
False
scoreboard=[
    [0,0,0,0],
    [0,0,0,0],
    [0,0,0,0],
    [0,0,0,0]
]
playerTwo
[(2, 1), (0, 1), (1, 2), (3, 2), (2, 2), (2, 3)]
seed = (randint(0,n-1),randint(0,n-1))
n=4
seed
(2, 0)
seed = (randint(0,n-1),randint(0,n-1))
while board[seed[0]][seed[1]] != '_':
    seed = (randint(0,n-1),randint(0,n-1))
board[seed[0]][seed[1]] = 'O'
for line in board:
    print(line)
['X', '_', '_', '_']
['_', 'X', 'O', '_']
['O', '_', 'X', 'O']
['_', '_', 'O', '_']
c
2