NXC memory game not working.

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
ant11
Posts: 1
Joined: 05 Jan 2011, 14:06

NXC memory game not working.

Post 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);
}
hassenplug
Posts: 346
Joined: 27 Sep 2010, 03:05
Contact:

Re: NXC memory game not working.

Post 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
---> Link to lots of MINDSTORMS stuff under my picture --->
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: NXC memory game not working.

Post 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.)
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC memory game not working.

Post 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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC memory game not working.

Post 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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests