# -*- coding: utf-8 -*-

from matplotlib import pyplot as plt
import random

k=9
xx=[[1,0,1,0,1,0,1,0,1]]
d=[1]

def rndB():
    return random.uniform(0.5, 0.9)
def rndL():
    return random.uniform(0.1, 0.5)

for i in range(50):
    xx.append([rndB(), rndL(),rndB(),
               rndL(),rndB(),rndL(),
               rndB(), rndL(),rndB()])
    d.append([1,0,0,0])
    xx.append([rndL(), rndB(),rndL(),
               rndB(),rndL(),rndB(),
               rndL(), rndB(),rndL()])
    d.append([0,1,0,0])
    xx.append([rndB(), rndB(),rndB(),
               rndL(),rndB(),rndL(),
               rndL(), rndB(),rndL()])
    d.append([0,0,1,0])
    xx.append([rndB(), rndL(),rndB(),
               rndB(),rndB(), rndB(),
               rndB(), rndL(), rndB()])
    d.append([0,0,0,1])
    
def pict(a):
    if a==1: return [rndB(), rndL(),rndB(),
                     rndL(),rndB(), rndL(),
                     rndB(), rndL(),rndB()]
    elif a==0:return [rndL(), rndB(),rndL(),
                     rndB(),rndL(), rndB(),
                     rndL(), rndB(),rndL()]
    elif a==2:return [rndB(), rndB(),rndB(),
                     rndL(),rndB(),rndL(),
                     rndL(), rndB(),rndL()]
    elif a==3:return [rndB(), rndL(),rndB(),
                     rndB(),rndB(), rndB(),
                     rndB(), rndL(), rndB()]

    
w1=[0]*k
w2=[0]*k
w3=[0]*k
w4=[0]*k

b1=random.uniform(-1,1)
b2=random.uniform(-1,1)
b3=random.uniform(-1,1)
b4=random.uniform(-1,1)

for j in range(k):
    w1[j]=random.uniform(-1,1)
    w2[j]=random.uniform(-1,1)
    w3[j]=random.uniform(-1,1)
    w4[j]=random.uniform(-1,1)

for i in range(len(xx)):
    x=xx[i]
    y1,y2,y3,y4=0,0,0,0
    for j in range(len(x)):
        y1+=w1[j]*x[j]
        y2+=w2[j]*x[j]
        y3+=w3[j]*x[j]
        y4+=w4[j]*x[j]

    if d == 1:
        dd = [1,0,0,0]
    elif d == 0
        dd = [0,1,0,0]
    elif d == 2
        dd = [0,0,1,0]
    elif d == 3
        dd = [0,0,0,1]
    y1+=b1
    y2+=b2
    e1=dd[0]-y1
    e2=dd[1]-y2
    y3+=b3
    y4+=b4
    e3=dd[2]-y3
    e4=dd[3]-y4

    for j in range(len(x)):
        w1[j]+=x[j]*e1/(i+1)
        w2[j]+=x[j]*e2/(i+1)
        w3[j]+=x[j]*e3/(i+1)
        w4[j]+=x[j]*e4/(i+1)

    b1+=e1/(i+1)
    b2+=e2/(i+1)
    b3+=e3/(i+1)
    b4+=e4/(i+1)

accuracy=input('Проверка точности или игра? ')

while True:
    if (accuracy == 'проверка') or (accuracy == 'точность'):
        ff = 0
        circles = 0
        circlesr = 0
        crosses = 0
        crossesr = 0
        n = 0
        nr = 0
        t = 0
        tr = 0
        for k in range(100000):
            typ = random.randint(0,3)
            xtest = pict(typ)
            
            y1=0; y2=0; y3=0; y4=0

            for j in range(len(xtest)):
                y1+=w1[j]*xtest[j]
                y2+=w2[j]*xtest[j]
                y3+=w3[j]*xtest[j]
                y4+=w4[j]*xtest[j]

            y1+=b1; y2+=b2; y3+=b3; y4+=b4
            if y1 == max(y1,y2,y3,y4): tp=0
            elif y2 == max(y1,y2,y3,y4): tp=1
            elif y3 == max(y1,y2,y3,y4): tp=1
            elif y4 == max(y1,y2,y3,y4): tp=1
    
            if tp==typ: ff+=1

            if typ==0:
                circles+=1
            elif typ==

        print(round(ff/100000,2)*100,'%','(',circles,' ноликов)','(',circlesr,' правильных)')
        print('(',crosses,' крестиков)','(',crossesr,' правильных)')
    elif accuracy in cmde: break
    else:
        ff = 0
        while not ff:
            xtest = []
            for j in range(k):
                xtest.append(float(input('Введите {}-й элемент: '.format(j+1))))

            plt.imshow([xtest[:3], xtest[3:6], xtest[6:]], interpolation='nearest')
            y1=0; y2=0

            for j in range(len(xtest)):
                y1+=w1[j]*xtest[j]
                y2+=w2[j]*xtest[j]

            y1+=b1; y2+=b2
            if y1>y2: tp='Крестик'
            else: tp='Нолик'
            print(tp)
            plt.show() 
            accuracy = input('Ещё или проверка?  ')
            if (accuracy == 'нет') or (accuracy == 'проверка') or (accuracy == 'точность'): ff = 1

print('прощай :3')
