from machine import Pin, SoftI2C
from utime import sleep
from ssd1306 import SSD1306_I2C
from random import randrange
import random

LARGURA = 128
ALTURA = 64


i2c = SoftI2C(scl=Pin(9), sda=Pin(8))
i2c_2 = SoftI2C(scl=Pin(15), sda=Pin(14))
i2c_3 = SoftI2C(scl=Pin(4), sda=Pin(2))
i2c_4 = SoftI2C(scl=Pin(18), sda=Pin(20))

display1 = SSD1306_I2C(LARGURA, ALTURA, i2c)
display2 = SSD1306_I2C(LARGURA, ALTURA, i2c_2)
display3 = SSD1306_I2C(LARGURA, ALTURA, i2c_3)
display4 = SSD1306_I2C(LARGURA, ALTURA, i2c_4)
########################

botao1 = Pin(21,Pin.IN,Pin.PULL_UP)
botao2 = Pin(22,Pin.IN,Pin.PULL_UP)
botao3 = Pin(26,Pin.IN,Pin.PULL_UP)
botao4 = Pin(27,Pin.IN,Pin.PULL_UP)
botao5 = Pin(28,Pin.IN,Pin.PULL_UP)

'''variáveis'''
galinha1 = 0
galinha2 = 0
galinhas = []
escolhasPossiveis = [1, 2, 3]

spr_car = 'VOCE GANHOU'
spr_galinha1 = "COCORICO"
spr_galinha2 = "COCORICOOOOO"

'''Gerador do carro'''
carro = randrange(1, 4)

'''teste'''
sleep(0.01)
print("A porta é:", carro)
sleep(0.01)

'''Gerador das Galinhas'''
if(carro == 1):
    galinha1 = 2
    galinha2 = 3
    portas = [spr_car, spr_galinha1, spr_galinha2]
if(carro == 2):
    galinha1 = 3
    galinha2 = 1
    portas = [spr_galinha2, spr_car, spr_galinha1]
if(carro == 3):
    galinha1 = 1
    galinha2 = 2  
    portas = [spr_galinha1, spr_galinha2, spr_car ]

galinhas.append(galinha1)
galinhas.append(galinha2)

porta1 = portas[0]
porta2 = portas[1]
porta2 = portas[2]

'''teste'''

#print('as galinhas são:', galinhas)

##########################################################
################## ESCOLHA COM BUT #######################
##########################################################
##########################################################

'''Escolha do player'''
escolha = 0
#escolha = int(input("Qual porta você deseja olhar?"))
print("Qual porta você deseja olhar?")
display4.text(
    """Qual porta?""", 0, 0, 1)
display4.show()
display1.text('1', 63, 31, 1)
display1.show()
display2.text('2', 63, 31, 1)
display2.show()
display3.text('3', 63, 31, 1)
display3.show()
##########################################################

while True:       
    sleep(0.01)
    lista = 1
    count = 0

    if botao1.value() == 0:
        escolha = 1
        count += 1
        break
    if botao2.value() == 0:
        escolha = 2
        count += 1
        break
    if botao3.value() == 0:
        escolha = 3
        count += 1
        break


    escolhalista = [escolha]    
    while not count == 0: #deixa o botão pressionado sem acontecer nada
        sleep(0.01)
        count = 0
        for i in range(12):
            count += 1 - b[i].value()


##########################################################
####################### MAQUINA ##########################
##########################################################


sleep(0.01)
'''Escolha da outra porta (que contem uma galinha) pela máquina'''
ra1 = set(escolhalista) <= set(galinhas)
sleep(0.01)
sleep(0.01)
if (ra1 == True):
    sleep(0.01)
    escolhaDaMaquina = list(set(galinhas) - set(escolhalista))
    escolhaDaMaquina = escolhaDaMaquina[0]
    listaEDM =  [escolhaDaMaquina]

if(ra1 == False):
    sleep(0.01)
    '''print('as galinhas são: ', galinhas)'''
    escolhaDaMaquina = int(random.choice(galinhas))
    listaEDM = []
    listaEDM.append(escolhaDaMaquina)
    '''print(f"a escolha aleatória foi {escolhaDaMaquina}")'''
sleep(0.01)

'''teste'''

'''Dica'''
sleep(0.01)
print("===" * 15)
sleep(0.01)
print(f'Certo, a porta escolhida foi a número {escolha}. ')
sleep(0.01)
print(f'Dica: não está na porta {escolhaDaMaquina} ')
sleep(0.01)


if escolhaDaMaquina == 1:
    display1.fill(0)
    display1.text(portas [0], 0, 0, 1)
    display1.show()
if escolhaDaMaquina == 2:
    display2.fill(0)
    display2.text(portas [1], 0, 0, 1)
    display2.show()
if escolhaDaMaquina == 3:
    display3.fill(0)
    display3.text(portas [2], 0, 0, 1)
    display3.show()

##########################################################
############# PRECISA ABRIR A PORTA ######################
##########################################################

'''Gerador de troca'''
sleep(0.01)
troca0 = list(set(escolhasPossiveis) - set(listaEDM))
sleep(0.01)
troca = list(set(troca0) - set(escolhalista))
sleep(0.01)
troca = troca[0]
sleep(0.01)
'''teste'''
'''print('a variável troca, é ', troca)'''


'''Troca?'''
pTroca = False

sleep(0.01)
print("===" * 15)
sleep(0.01)
print(f"Deseja trocar a porta escolhida ({escolha}) ")
sleep(0.01)
print(f"pela porta de número ({troca})? ")
sleep(0.01)
print("===" * 15)
sleep(0.01)
print("Para TROCAR, escolha o botão VERMELHO.")
print("Para NÃO TROCAR, pressione o VERDE")
sleep(0.01)
print("===" * 15)
sleep(0.01)


while True:
    sleep(0.01)
    lista = 1
    count = 0
    if botao4.value() == 0:
        pTroca = True
        count += 1
        break
    if botao5.value() == 0:
        pTroca = False
        count += 1
        break
   
    while not count == 0: #deixa o botão pressionado sem acontecer nada
        sleep(0.01)
        count = 0
        for i in range(12):
            count += 1 - b[i].value()



if(pTroca == True):
    escolha = troca
    '''print("passou por aqui")
    print(f'carro = {carro}')
    print(f'escolha = {escolha}')'''
    if(escolha == carro):
        print(f"Parabéns, você optou pela porta de número {carro}, e ganhou.")
    else:
        print(f"poxa, você optou pela porta {troca} e perdeu. A porta correta era a de número {carro}")
    

    sleep(0.01)
    display1.fill(0)
    display1.text(portas [0], 0, 0, 1)
    display1.show()
    sleep(0.01)
    display2.fill(0)
    display2.text(portas [1], 0, 0, 1)
    display2.show()
    sleep(0.01)
    display3.fill(0)
    display3.text(portas [2], 0, 0, 1)
    display3.show()
    sleep(5)

    ##########################################################
    ############# PRECISA ABRIR AS PORTAS RESTANTES ##########
    ##########################################################  

if(pTroca == False):
    '''print("passou por aqui")
    print(f'carro = {carro}')
    print(f'escolha = {escolha}')'''
    if(escolha == carro):
        print(f"Parabéns, você optou pela porta de número {carro}, e ganhou.")
    else:
        print(f"poxa, você optou pela porta {escolha} e perdeu. A porta correta era a de número {troca}")
    
    sleep(0.01)
    display1.fill(0)
    display1.text(portas [0], 0, 0, 1)
    display1.show()
    sleep(0.01)
    display2.fill(0)
    display2.text(portas [1], 0, 0, 1)
    display2.show()
    sleep(0.01)
    display3.fill(0)
    display3.text(portas [2], 0, 0, 1)
    display3.show()
    sleep(5)
    ##########################################################
    ############# PRECISA ABRIR AS PORTAS RESTANTES ##########
    ########################################################## 

sleep(5)

print(carro)
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT
pico:GP0
pico:GP1
pico:GND.1
pico:GP2
pico:GP3
pico:GP4
pico:GP5
pico:GND.2
pico:GP6
pico:GP7
pico:GP8
pico:GP9
pico:GND.3
pico:GP10
pico:GP11
pico:GP12
pico:GP13
pico:GND.4
pico:GP14
pico:GP15
pico:GP16
pico:GP17
pico:GND.5
pico:GP18
pico:GP19
pico:GP20
pico:GP21
pico:GND.6
pico:GP22
pico:RUN
pico:GP26
pico:GP27
pico:GND.7
pico:GP28
pico:ADC_VREF
pico:3V3
pico:3V3_EN
pico:GND.8
pico:VSYS
pico:VBUS
oled1:GND
oled1:VCC
oled1:SCL
oled1:SDA
oled2:GND
oled2:VCC
oled2:SCL
oled2:SDA
oled3:GND
oled3:VCC
oled3:SCL
oled3:SDA
oled4:GND
oled4:VCC
oled4:SCL
oled4:SDA
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r
btn4:1.l
btn4:2.l
btn4:1.r
btn4:2.r
btn5:1.l
btn5:2.l
btn5:1.r
btn5:2.r