#pragma config(Sensor, port1,  LED0,           sensorVexIQ_LED)
#pragma config(Sensor, port2,  LED1,           sensorVexIQ_LED)
#pragma config(Sensor, port3,  LED2,           sensorVexIQ_LED)
//#pragma config(Sensor, port4,  LED4,           sensorVexIQ_LED)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
short LEDarray[3] = {LED0, LED1, LED2};

const int length=8;
int level = 1;
int pressed = 4;  //keeps track of which button was pressed (4= no button pressed)
int sequence[length];

void flash_touch(int index) {
	setTouchLEDColor(LEDarray[index], colorRed);
	playNote(noteA,octave2,20);
	sleep(100);
	setTouchLEDColor(LEDarray[index], colorNone);
	sleep(200);
}

task main()
{
	srand(nSysTime);  // generate seed for rand() from current system time
	eraseDisplay();
	//Load up the sequence with a random number.  rand() gives a number between 0 and 32,767.
	//By doing a modulo function with 3 (%3) it will divide the random number by 3 and keep the remainder
	//This will give a final number of either 0,1 or 2
	for(int x=0 ; x<length; x++)
	{
		sequence[x] = rand()%3;
	}

	//Light up the TouchLED's in the correct order based on the sequence and the Level you are currently on
	for(int i=0 ; i<length ; i++) {
		displayCenteredBigTextLine(2, "Level: %d", level);   //display what Level you're on
		sleep(1000);
		for (int j=0; j<level ; j++) {
			flash_touch(sequence[j]);
		}

		//Figure out which TouchLED has been pressed.
		for (int j=0; j<level ; j++)
		{
			pressed=4;										//4 means no touchLED has been pressed
			while(pressed==4)
			{
				if(getTouchLEDValue(LED0)){							//If TouchLED0 is pressed, then set 'pressed' to 0 and light it up
					pressed=0;														//blue to confirm
					setTouchLEDColor(LED0,colorBlue);
					//While the button is still being pushed (ie. Player hasn't taken their finger off)
					//don't do any commands. (hence the empty set of {} brackets
					while(getTouchLEDValue(LED0)){}
					setTouchLEDColor(LED0,colorNone);			//Turn off the TouchLED
				}else if(getTouchLEDValue(LED1)){
					pressed=1;
					setTouchLEDColor(LED1,colorBlue);
					while(getTouchLEDValue(LED1)){}
					setTouchLEDColor(LED1,colorNone);
				}else	if(getTouchLEDValue(LED2)){
					pressed=2;
					setTouchLEDColor(LED2,colorBlue);
					while(getTouchLEDValue(LED2)){}
					setTouchLEDColor(LED2,colorNone);
				}
			}

			//If the TouchLED that was pressed is the same as the sequence then that's awesome!
			if(pressed==sequence[j])
			{
				playNote(noteF, octave2, 10);
				sleep(200);
			} else 								// if it's not the same, change the display
			{
				eraseDisplay();
				displayCenteredBigTextLine(0, "Game Over!");
				displayCenteredBigTextLine(3, "Level: %d", level);
				playSound(soundWrongWay);
				playSound(soundWrongWay);
				playSound(soundWrongWay);
				sleep(3000);
				stopAllTasks();
			}
		}

		level++; //increase the level which will add another TouchLED to the sequence.
	}
	//If you make it through the whole sequence -> Horray!!!!
	eraseDisplay();
	displayCenteredBigTextLine(0, "You Rock!");
	displayCenteredBigTextLine(3, "Level: %d", level--);
	playSound(soundTada);
	sleep(3000);
}
