#!/usr/bin/python3
import RPi.GPIO as GPIO
import time
import random

leds = [ 16, 26, 13 ]
buttons = [ 12, 19, 6 ] 

wait_time = 0.3
wait_time2 = 0.5 

def setup():
    GPIO.setmode(GPIO.BCM)
    for led in leds:
        GPIO.setup(led, GPIO.OUT)
    for button in buttons:
        GPIO.setup(button, GPIO.IN)
    all_leds_off()

    
def led_on(led):
    GPIO.output(led, GPIO.HIGH)

def led_off(led):
    GPIO.output(led, GPIO.LOW)

def all_leds_off():
    for led in leds:
        led_off(led)

def button_pressed(button):
    return GPIO.input(button) == GPIO.LOW  

def main():

    setup()
    
    try:
        points = 0

        while True:

            print("Points: ", points) 
            
            wait = random.randint(30, max(40, 5000))
            time.sleep(wait/1000)

            choice = random.randint(0, 2)
            led = leds[choice]
            button = buttons[choice]
            
            other_button1 = buttons[(choice + 1) % len(buttons)]
            other_button2 = buttons[(choice - 1) % len(buttons)] 

            led_on(led)

            ts = time.clock()

            while time.clock() - ts < wait_time : 
                if button_pressed(button):
                    print(random.choice(["Great!", "Nice", "Very Good!", "You Rock!"]))
                    time.sleep(wait_time2)
                    points += 1
                    break
                elif button_pressed(other_button1) or button_pressed(other_button2):
                    print(random.choice(["Bad!", "Wrong Button", "Not Good!"]))
                    time.sleep(wait_time2)
                    if points > 0:
                        points -= 1
                    break

            led_off(led)

    finally:

        all_leds_off()
        GPIO.cleanup()

main()



