Please help any advice will be greatly appreciated!


#1

So my team and I have been trying to get a version of wack-a-mole but with vex equipment. We got everything done except the keeping score and we’re gonna have LED 5 be the one keeping the score. We are gonna try to do that when the LED lights up then you press the button at the same time and when you get it on time LED flashes once. BUT we don’t know how to have the score to continue to add up.

int value;
task main()
{
	while(1==1)
	{
		value = rand() % 4 + 1;

		if (value == 1)
		{
			turnLEDOn(GreenLED1);
			waitInMilliseconds(300);
			if(SensorValue[Button1]==1)
			{
				turnLEDOn(GreenLED5);
				wait(2);
				turnLEDOff(GreenLED5);
			}
			waitInMilliseconds(300);
			turnLEDOff(GreenLED1);
			waitInMilliseconds(600);
		}
		if (value == 2)
		{
			turnLEDOn(GreenLED2);
			waitInMilliseconds(600);
			turnLEDOff(GreenLED2);
			waitInMilliseconds(600);
		}
		if (value == 3)
		{
			turnLEDOn(GreenLED3);
			waitInMilliseconds(600);
			turnLEDOff(GreenLED3);
			waitInMilliseconds(600);
		}
		if (value == 4)
		{
			turnLEDOn(GreenLED4);
			waitInMilliseconds(600);
			turnLEDOff(GreenLED4);
			waitInMilliseconds(600);
				}
			}
		}

#2

Don’t have time to solve the problem, job interview in a 90 minutes, but:

while(true) { } is more readable.

if(value == 1) can be written as if(value), in which case value will be cast to a bool.

The definition of false in C is 0, and true is defined as any non-zero value.

Or in assembly terms, it is basically just the zero flag that is used to check for true/false.

I can’t really tell what you’re describing. Maybe some pictures and a better description would help.


#3

I believe this is what you’re looking for:

What you’re doing with if statements could have been done with an if-else block; however it should be done with a switch statement. Also, when using repetitive code, such as the whack-a-mole blink led code, it is best to create a dynamic method to reduce the typing and create a better structured code infrastructure.

int value, score;

tSensors SCORE_LED = GreenLED5;

//Blinks LED and checks if button is pressed
//creating a "Whack-A-Mole" effect
void blinkLED(tSensors led) {
	
	int lightDuration = 600;
	
	//Turn on led for 300ms
	turnLEDOn(led);
	waitInMilliseconds(lightDuration / 2);
	
	//While LED is on, check if button is pressed
	//if button is pressed, blink LED #5
	if(SensorValue[Button1]==1) {
		turnLEDOn(SCORE_LED);
		wait(2);
		turnLEDOff(SCORE_LED);
		
		//Add to score
		score ++;
		
	}
	
	//wait 300 ms before cutting mole LED off
	//then pause thread for 600ms to wait for next round
	waitInMilliseconds(lightDuration / 2);
	turnLEDOff(led);
	waitInMilliseconds(lightDuration);
}


//Flashes score with the score LED
void flashScore() {
	
	//use for loop to flash LED as many times
	//as the score that the player has
	for (int i = 0; i < score; i ++) {
	
		//blink score LED for 150ms
		turnLEDOn(SCORE_LED);
		waitInMilliseconds(150);
		turnLEDOff(SCORE_LED);
				
	}
	
}


task main() {
	
	while (1) {
		
		//Random led value
		value = rand() % 4 + 1;
		
		//Switch case to led that was selected
		switch (value) {
		
		case 1:
			blinkLED(GreenLED1);
			break;
		
		case 2:
			blinkLED(GreenLED2);
			break;
		
		case 3:
			blinkLED(GreenLED3);
			break;
		
		case 4:
			blinkLED(GreenLED4);
			break;
		
		}
		
		//flash player score, then wait 1 second for next loop
		flashScore();
		wait(1);
		
	}
	
	
}