0

Some of the code that I have is unbearably long and drives me insane. I don't know if there were simpler ways to doing what I am doing.

long turn1;
long turn2;
long turn3;
long turn4;
long turn5;
long turn6;
long turn7; 
long turn8;
long turn9;
long turn10;
long turn11;
long turn12;
long turn13;
long turn14;
long turn15;

#################

  turn1 = random(sizeof(turns) / sizeof(char*));
  turn2 = random(sizeof(turns) / sizeof(char*));
  turn3 = random(sizeof(turns) / sizeof(char*));
  turn4 = random(sizeof(turns) / sizeof(char*));
  turn5 = random(sizeof(turns) / sizeof(char*));
  turn6 = random(sizeof(turns) / sizeof(char*));
  turn7 = random(sizeof(turns) / sizeof(char*));
  turn8 = random(sizeof(turns) / sizeof(char*));
  turn9 = random(sizeof(turns) / sizeof(char*));
  turn10 = random(sizeof(turns) / sizeof(char*));
  turn11 = random(sizeof(turns) / sizeof(char*));
  turn12 = random(sizeof(turns) / sizeof(char*));
  turn13 = random(sizeof(turns) / sizeof(char*));
  turn14 = random(sizeof(turns) / sizeof(char*));
  turn15 = random(sizeof(turns) / sizeof(char*));
#
if (turn1 == turn2 or turn2 == turn3 or turn3 == turn4 or turn4 == turn5 or turn6 == turn7 or turn7 == turn8 or turn8 == turn9 or turn9 == turn10 or turn10 == turn11 or turn11 == turn12 or turn12 == turn13 or turn13 == turn14 or turn14 == turn15) {
#
lcd.print(turns[turn1]);
lcd.print(turns[turn2]);
lcd.print(turns[turn3]);
lcd.print(turns[turn4]);
lcd.print(turns[turn5]);
lcd.setCursor(0, 2);
lcd.print(turns[turn6]);
lcd.print(turns[turn7]);
lcd.print(turns[turn8]);
lcd.print(turns[turn9]);
lcd.print(turns[turn10]);
lcd.setCursor(0, 3);
lcd.print(turns[turn11]);
lcd.print(turns[turn12]);
lcd.print(turns[turn13]);
lcd.print(turns[turn14]);
lcd.print(turns[turn15]);
1

1 Answer 1

6

Yes you can, and the solution is very simple. It's in two parts, and they make up part of the absolute fundamental building blocks not just in C, but in almost any programming language.

You can read good information on arrays here, and 'for loops' here (it's about half way down the page). Without wishing to offend, it seems like you are missing some really basic concepts and I recommend starting at the beginning of the tutorial series that the two aforementioned links are part of.

I'll just cover the basics. If you don't understand anything, refer to the tutorials. If you still don't understand then please comment, but make sure you can convince us that you've really given it a good go. We're here to help as long as you're here to learn.

To declare an array of N variables, all of which are the same type, you use:

<type> <name_of_variable> [<number_of_elements>];

or:

long turns[15];

Now you have an array of 15 longs. Ta da!

You now have to set the values of each turn. As your values are all the same (if you ignore the fact that random() ultimately leaves you with different values) you can use a for loop:

for (int x=0; x<15; x++)
{
    turns[x] = random(sizeof(turns) / sizeof(char*));
}

I won't explain how a for loop works; I gave you a link to the tutorial to save me writing it all out myself.

That covers shrinking of part 1 and 2. Part 3 requires a little more thought. That said, as you didn't complete your if statement, I don't actually know what you plan to do if any two adjacent turn elements are the same. I shall assume that you would 're-randomise' one of the duplicates. For this you can again use a for loop, starting at the beginning and running to N-1 th element, where N is the total number of elements:

for (int x=0; x<14; x++)
{
    if (turns[x] == turns[x+1])
    {
        turns[x+1] = random(sizeof(turns) / sizeof(char*));
    }
}

You need make sure that turns[x+1] is not outside of the array, which is why the for loop runs for one iteration less than there are elements.

The fourth shrinkable part is just a matter of swtiching multiple print statements into single for loops:

for (int x=0; x<NUMBER_OF_PRINTS_IN_1st_BUNCH; x++)
    lcd.print(turns[x]);
lcd.setcursor(0,2);
for (int x=WHERE_YOU_LEFT_OFF; x<NUMBER_OF_PRINTS_IN_2nd_BUNCH; x++)
    lcd.print(turns[x]);
lcd.setcursor(0,3);
for (int x=WHERE_YOU_LEFT_OFF_AGAIN; x<NUMBER_OF_REMAINING_PRINTS; x++)
    lcd.print(turns[x]);

All done.

7
  • Is it my imagination or are you off-by-one, @chaaarlie2? Your array is of length 15, so is 0-14; in the first for loop code, you say int x=0; x<16; x++ - surely that will go around from 0 to 15, not 0 to 14? Same applies to the second for loop (reduced by 1 to skip the last value). Jun 4, 2015 at 1:14
  • Alternative to the fourth part: do one loop to do all of them, then if (x==4) lcd.setcursor(0,2); if (x==9) lcd.setcursor(0,3); Jun 4, 2015 at 1:16
  • @AMADANON it's obviously my imagination, not yours. I wrote the whole answer with reference to 16 values, then when I noticed the OP didn't have "long turn0;" at the start, went back and only remembered to change the first two references. Answer updated, thanks for spotting. Jun 4, 2015 at 2:34
  • Thanks for the help. Also, for the third shrinkable, good assumption, that's actually what I am doing. On another note, is there a way to fix if two of them match? The turn values are stated as this: {"L2 ", "R2 ", "F2 ", "U2 ", "D2 ", "B2 " , "L' ", "R' ", "F' ", "U' ", "D' ", "B' ", "L ", "R ", "U ", "F ", "D ", "B "} , is there a way I could prevent something like L2 L or L2 L'? Jun 4, 2015 at 21:17
  • Also, turns[] was already declared in char *turns[], so i end up getting conflicting declaration 'long int turns [15]' Heres the chunk: char *turns[] = {"L2 ", "R2 ", "F2 ", "U2 ", "D2 ", "B2 " , "L' ", "R' ", "F' ", "U' ", "D' ", "B' ", "L ", "R ", "U ", "F ", "D ", "B "}; long turns[15]; Jun 4, 2015 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.