VEX IQ Game of Simon

This is my version of the Game of Simon for the VEX IQ. The TouchLED’s are an awesome input/output device that is just so natural to use. Programmed in RobotC and designed to be used as a way of teaching / reinforcing the concepts of arrays, in less than 100 lines of code.

Video of it in action - https://www.youtube.com/watch?v=5ex36W1pbpE

Writeup and source code can be found here - http://www.damienkee.com/home/2015/8/12/simon-game-vex-iq-version.html

Would love to hear people’s comments on it.

Regards,
Damien Kee

www.damienkee.com

Lots of constructive things to say…

Doesn’t the game normally have different tones for different buttons? And, different colors?

Given that this is based around teaching arrays, I would suggest creating an array with the tones (0-2) and another array with LED Colors (again 0-2) so when you flash an LED, you lookup the tone and color from the same way you’re looking up the LED Port.

I would also set the number of LEDs as a defined constant so it can be changed just like the levels. (default to pressed = -1)

Those may all be natural progressions of the lesson. This seems like a very good lesson.

Well done.
Steve

This is super! Steve has a great suggestion on sound. We played Simon with a visually impaired friend. We needed to set the game up so he knew which sides the different tones came from. It was very eerie to play with him, he would watch the board intently (like we did) but it was all sound to him. He was pretty much unbeatable. So maybe has Steve suggested, this could be the next lesson, making the game for someone that’s visually impaired.

Love the IQ system on how you all have moved from “build a robot for physics or a competition” to other things.

Great suggestions Steve. My original aim was to keep it as simple as possible and make it a base for other people to build upon. Doing the different colours and tones would not complicate the code at all, but adding in the ability for arbitrary TouchLED’s would proved a bit more complex given they are all assigned a short to reference them.

I think they would make excellent followup lessons. Am thinking of bringing a 12 TouchLED version to worlds next year to see if anyone can defeat it :slight_smile:

Foster, I love the idea of a visually impaired version!

Don’t you already have the arbitrary TouchLED stuff? How about this:

#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 !!//

const int LEDCount = 3;
short LEDarray[LEDCount] = {LED0, LED1, LED2};
short LEDcolor[LEDCount] = {colorRed, colorRed, colorRed}; // colorRed
short LEDsound[LEDCount] = {noteA, noteA, noteA}; // noteA

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

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

bool check_LED(int index)
{
if (getTouchLEDValue(LEDarray[index]))
{
pressed=index; //blue to confirm
setTouchLEDColor(LEDarray[index],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(LEDarray[index])){}
setTouchLEDColor(LEDarray[index],colorNone); //Turn off the TouchLED
return true;
}

return false;

}

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()%LEDCount;
}

//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&lt;length ; i++) {
    displayCenteredBigTextLine(2, "Level: %d", level);   //display what Level you're on
    sleep(1000);
    for (int j=0; j&lt;level ; j++) {
        flash_touch(sequence[j]);
    }

    //Figure out which TouchLED has been pressed.
    for (int j=0; j&lt;level ; j++)
    {
        pressed=-1;                                        //4 means no touchLED has been pressed
        while(pressed==-1)
        {
            for(int k=0;k&lt;LEDCount ; k++)
            {
                if (check_LED(k))
                {
                    // break; //??
                }
            }
        }

        //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 -&gt; Horray!!!!
eraseDisplay();
displayCenteredBigTextLine(0, "You Rock!");
displayCenteredBigTextLine(3, "Level: %d", level--);
playSound(soundTada);
sleep(3000);

}

That would definitely work although you’ll have to add in a lot of #pragmas at the start. Haven’t had a chance to test, but does the VEX IQ baulk at having #pragma’s assigned but not physically there?

I also tossed up whether to have a function call for things like checking the TouchLED pressed and running the end game sequence. For my first time around I decided to keep everything inline

I may have to turn this into a much larger set of instructions with several steps!!!

I’m pretty sure the program will not work if you define sensors that don’t exist.

If you want to really get into dynamic sensor stuff, I think you can configure sensors from the program. That is not a “basic” program. :slight_smile:

jpearman, I loved your version at Worlds, and wanted to put together a more basic one that ‘regular’ folk like me could build :slight_smile: Do you have a schematic for how you connected the LED panel?

So, you just built it, and then made changes until it worked. No formal design. Sounds familiar.

That’s why I don’t normally post code like Damien did here. Too many people [like me] tear it apart and said “you should have done it this way…” to which I normally think [and sometime reply] well, if I had designed it the way it turned out, yes, I would have done that also…

Steve

Nothing wrong with constructive criticism, it’s the only way to improve! The real challenge with teaching coding to kids is to be able to let them have that free flowing development / iteration process while still instilling in them some fundamentals about ‘clean’ and ‘effective’ code :slight_smile:

‘Everything worked first time’ is something that us more experienced programmers can achieve on simple tasks like this, but it notoriously difficult for newcomers to master. Often we can see the problems before they arise by experience. I’ve been known to let kids head off down the wrong tangent, knowing full well they would hit a dead end, purely for the educational benefit they get from having the ‘failure’, but more importantly working around it and solving it.

Damien,

I tried to send a PM but it looks like it didn’t go through: would you be willing to let us promote this project through our blog? We’d be more than happy to showcase your skills and the work you’ve put into this, and are always on the lookout for cool projects like this.

If you’re interested, feel free to shoot me an email directly via jwatson@robomatter.com and I’ll be more than happy to connect you with our marketing team.

Great work on this regardless, it’s projects like these that make robotics so incredibly awesome!

Hi John, sorry, just saw this message. Would love to have it promoted on the blog :slight_smile: I’ll send you a direct email.