Page 1 of 1

NXC memory game not working.

Posted: 05 Jan 2011, 15:05
by ant11
Hi,
I'm trying to create a memory game based on colours. I wrote this code and BricxCC compiles it but when i run it in my NXT ti shows a file error:
the code is this:

Code: Select all

task main()
{
SetSensorColorFull(IN_1);
  int random[4];
  int color[4];
  int v = 1;
  int j = 1;
  int i =1;
  int result = 0;
  
    for(v = 1; v <= 4 ; v++)
      { random[v]= Random(3) + 2;
        if  (random[v] == 2)
           { TextOut (0, LCD_LINE3, "Azul");}
           else if (random[v] == 3)
           { TextOut (0, LCD_LINE3, "Verde");}
           else if (random[v] == 4)
           { TextOut (0, LCD_LINE3, "Amarillo");}
           else
           { TextOut (0, LCD_LINE3, "Rojo");}
      }
  Wait(10000);
  
  for(;j <=4; j++)
  {
  RotateMotor(OUT_A, 100,360);
  color[j] = Sensor(IN_1)
  }
  for(; i<=4; i++)
  {   switch(color[i])
     {
     case 2:
       TextOut (0, LCD_LINE3, "Azul");
       break;
     case 3:
       TextOut (0, LCD_LINE4, "Verde");
       break;
     case 4:
       TextOut (0, LCD_LINE5, "Amarillo");
       break;
     case 5:
       TextOut (0, LCD_LINE6, "Rojo");
       break;
      }
           if (color[i] != random[i])
           { result +=1;}


  }
  

  if (result > 0)
   {TextOut (0, LCD_LINE3, "Wrong!");  }
   
  else
   {TextOut (0, LCD_LINE3, "Well done!");}
   
   
  Wait(5000);
}

Re: NXC memory game not working.

Posted: 05 Jan 2011, 15:22
by hassenplug
That type of error is often something out of range. (bad parameter)

I believe arrays in NXC are 0-based, so the statement:

int random[4];

creates four elements, numbered 0-3. Looks like changing the statements to:

int random[5];


...should solve the problem. However, keep in mind that you've created an array that is larger than what you need. That's generally not a good programming technique.

Steve

Re: NXC memory game not working.

Posted: 06 Jan 2011, 07:31
by muntoo
hassenplug wrote:...should solve the problem. However, keep in mind that you've created an array that is larger than what you need. That's generally not a good programming technique.
What he means is, use this:

Code: Select all

#define ACCESS_ELEMENT(arr,idx) arr[idx-1]

// ...

for(unsigned int idx = 1; idx <= 4; idx++)
    ACCESS_ELEMENT(random, idx) = 4; // Look here: http://xkcd.com/221/
(Mentally, of course.)

Re: NXC memory game not working.

Posted: 06 Jan 2011, 18:12
by afanofosc
My recommendation would be to change this:

Code: Select all

    for(v = 1; v <= 4 ; v++)
to this:

Code: Select all

    for(v = 0; v < 4 ; v++)
I would also replace "4" with a #define constant named ARRAY_SIZE or something like that so that you do not have what are referred to as "magic numbers" sprinkled throughout your source code. Alternatively, you could call ArrayLen instead of using a hard-coded value.

John Hansen

Re: NXC memory game not working.

Posted: 06 Jan 2011, 18:15
by afanofosc
Forgot to mention that you would also need to start i and j counting at zero instead of 1 and use < rather than <= in the for loops that iterate on i and j.

John Hansen