Array in If Statement[Solved]

I have been working on this space invaders code for a long while, and with some help from stack overflow, I accomplished this code that works properly, but I need one more thing for this code to become an working game. I need to use an array for the aliens.

#include <LiquidCrystal.h> 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);                                        
byte Ship[8] = { //the code for the player ship                               
    B00000, 
    B00000,                                                                   
    B00001, 
    B00111, 
    B11001, 
    B00111, 
    B00001, 
    B00000 
}; 
byte Ailen[8] = {//the code for the ailens                                    
    B10001, 
    B10001, 
    B01010, 
    B01110,                                                                   
    B01110, 
    B01010, 
    B10001, 
    B10001 
}; 
byte Bullet[8] = {//the code for the bullet                                   
    B00000, 
    B00000, 
    B11111,                                                                   
    B11111, 
    B11011,                                                                   
    B11111, 
    B11111,                                                                   
    B00000                                                                    
}; 
#define lButton  13 //pin for the left button                                 
#define rButton  6  //pin for the right button                                
#define fButton  7  //pin for the firing button 
int xa = 0;//the aliens x coords 
int ya = 0;//the aliens y coords                                              
int xShip=15;//the starting x coord of the ship                               
int yShip=0;//starting y coord of the ship                                    
int yAliens[2] = {0,1};                                                       
int xAliens[12]= {0,1,2,3,4,5,6,7,8,9,10,11}; /* attempted use of the array   
                                                 which did not work but I will leave it in*/              
#define DTIME 150 //what is the delay for movement                            
void setup() {                                                                
    pinMode(lButton, INPUT);//sets up left button                             
    pinMode(rButton, INPUT);//sets up right button                            
    pinMode(fButton, INPUT);//sets up firing button                           
    lcd.begin(16, 2);//parameter of the lcd 
    lcd.createChar(0, Ship);//sets char                                       
    lcd.createChar(1, Ailen);//sets char                                      
    lcd.createChar(2, Bullet);//sets char                                     
    lcd.setCursor(xa,ya);//coords of the one ailen                            
    lcd.write((byte)1);//prints the one ailen                                 
} 

void loop() { 
    static boolean alien = true;   //is the one ailen alive?                  
    static boolean fire = false;   //is the bullet firing                     
    static byte xb = 0;             //the bullets x coords                     
    static byte yb = 0;             //the bullets y coords                     

    byte lState = digitalRead(lButton);  //pulls the state of the left button  
    byte rState = digitalRead(rButton);  //pulls the state of the right button 
    byte fState = digitalRead(fButton);  //pulls the state of the firing button                            


    /*if the bullet is moving and the alien's position is                     
     * the same as the bullets, then                                          
     * */  
    if (fire && alien && yb == ya && xb == yb) {                              
        fire = false;            //bullet stops moving                        
        alien = false;           //the alien is dead                          
        lcd.setCursor(xa, ya);   //the position of the alien                  
        lcd.print(" ");          //becomes blank                              
        tone(9,400,100);         //plays kill noise                           
    } 
    if (fire) {        //if the bullet is moving and is on the screen then    
        lcd.setCursor(xb+1,yb);//clears where bullet was 
        lcd.print(" ");//clear symbol 
        if (xb-- < 0) {//move the bullet 
            /* bullet ouside of the screen */ 
            fire = false; 
        } else { 
            lcd.setCursor(xb, yb); 
            lcd.write((byte)2);       //displays bullet 
            delay(DTIME / 2);//bullet moves twice as fast as ship 
        } 
    } 

    if (fState == HIGH && !fire) {  //if the fire button is pressed 
        fire = true;                     //starts moving the bullet 
        /* bullet starts one away from ship */ 
        xb = xShip - 1; 
        yb = yShip; 
    } 

    //if the right button is pressed and the left isnt 
    if (rState == HIGH && lState == LOW && yShip != 0) { 
        lcd.setCursor(xShip, yShip);  //ship's location 
        lcd.print(" ");               //clearing block 
        yShip = 0;                    //make the ship move to the right 
        lcd.setCursor(xShip, yShip);  //ship's location 
        lcd.write((byte)0);           //display the ship byte 
        delay(DTIME);                 //time to deter mashing 
    } 
    // if the right button is pressed and the     right isnt 
    else if (lState == HIGH && rState == LOW && yShip != 1) { 
        lcd.setCursor(xShip, yShip);  //ship's location 
        lcd.print(" ");               //clearing block 
        yShip = 1;                    //make the ship move to the right 
        lcd.setCursor(xShip, yShip);  //ship's location 
        lcd.write((byte)0);           //display the ship byte 
        delay(DTIME);                 //time to deter mashing 
    } 
    //if nothing is pressed 
    else{ 
        lcd.setCursor(xShip, yShip);//set previous coordinates for the ship*good practice 
        lcd.write((byte)0);//display the ship 
    } 
}

I was thinking that the arrays would look something like this, but I do not know the syntax to accomplish these functions.

int yAliens[2] = {0,1};                                                       
int xAliens[12]= {0,1,2,3,4,5,6,7,8,9,10,11};
 //this should compare all of the intergers of the array to the bullets coords
    if (fire && alien && yb == yAliens[] && xb == xAliens[]) {                              
        fire = false;            //bullet stops moving                        
        alien = false;           //the alien is dead                          
        //here I need to find the numbers of the array that are true with the function
        lcd.setCursor(yAliens[], xAliens[];   //this is where they would go                  
        lcd.print(" ");          //becomes blank                              
        tone(9,400,100);         //plays kill noise

You'll have to use a for loop to check against each alien. You can't do it all in one if statement.

Again, and I know this has been mentioned before, some time working with a simpler code to learn how to work with arrays would do you a lot of good. Once you understand how it works and the syntax then come back to space invaders.

What are xAliens and yAliens supposed to represent? If they are the x and y locations of the “aliens”, then the arrays should be the same size, shouldn’t they?

You can’t compare a value to an array. You have to iterate through the array and determine if the value is in the array.

bool hitAlien = false;
for(byte a=0; a<ALIEN_COUNT; a++)
{
   if(xb == xAliens[a] && yb = yAliens[a])
   {
       hitAlien = true;
       break;
   }
}

if(hitAlien)
{
   // do something
}

Of course, it might be important to know which alien you hit so you can remove that alien’s position from the arrays.

There isn't the same amount of values in the arrays because the LCD is 2x12...
unless I have this...

int xAilens[24] = {0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,};                                                       
int yAilens[24]= {0,1,2,3,4,5,6,7,8,9,10,11,0,1,2,3,4,5,6,7,8,9,10,11};

So one number corresponds to both coords of the array. Thanks

if(val<=24){
      lcd.setCursor(xAliens[val],yAliens[val]);
      lcd.write((byte)1);
}

I made this code to print all of the Aliens, and it works, but how do I put the all of the xAliens and yAliens values in the loop to see if the bullet hits one of them?

PaulS:
What are xAliens and yAliens supposed to represent? If they are the x and y locations of the “aliens”, then the arrays should be the same size, shouldn’t they?

You can’t compare a value to an array. You have to iterate through the array and determine if the value is in the array.

bool hitAlien = false;

for(byte a=0; a<ALIEN_COUNT; a++)
{
  if(xb == xAliens[a] && yb = yAliens[a])
  {
      hitAlien = true;
      break;
  }
}

if(hitAlien)
{
  // do something
}




Of course, it might be important to know which alien you hit so you can remove that alien's position from the arrays.

I reread your post and it makes sense, thanks

Of course, the right way to do this would be to create an Alien class, to encapsulate the alien "behavior", and simply the rest of the program...

Regards,
Ray L.

That would probably be true. How does one drop an specific value from an class?

wwb00:
That would probably be true. How does one drop an specific value from an class?

classInstance.memberName;

where classInstance is the name of the specific instance and memberName is the name of the specific member.

Google “C++ class” there’s a lot to it.

I don’t know that I’d be bringing classes into this. Firstly, it just going to confuse you further if arrays were blowing your mind. And secondly, I don’t know what behavior of the aliens there is to encapsulate. They’re either there or they aren’t. They don’t move independently or do anything on their own.

How I would approach this would be to have one boolean array that holds true or false value for each alien whether it needs to be displayed or is already dead. I would only track the position of one alien and calculate the positions of the others from it.

Pseudocode something like this:

for i = 0 to number of aliens{
     if the alien is already dead -- continue
     calculate position of current alien from position of the top left alien
     compare to the position of the missile
     if it is a hit, update the boolean array to show that one as dead
}

update the position of the top left alien
for i = 0 to number of aliens {
     if the alien is still alive, calculate its position relative to top left alien and display it
}

update the position of any missile on the screen

rest of the code to move the ship and fire the missiles
if(val<=24){
      lcd.setCursor(xAliens[val],yAliens[val]);

There is no “xAliens[24]”

Oops.

Delta_G:

classInstance.memberName;

where classInstance is the name of the specific instance and memberName is the name of the specific member.

Google "C++ class" there's a lot to it.

I don't know that I'd be bringing classes into this. Firstly, it just going to confuse you further if arrays were blowing your mind. And secondly, I don't know what behavior of the aliens there is to encapsulate. They're either there or they aren't. They don't move independently or do anything on their own.

How I would approach this would be to have one boolean array that holds true or false value for each alien whether it needs to be displayed or is already dead. I would only track the position of one alien and calculate the positions of the others from it.

Pseudocode something like this:

for i = 0 to number of aliens{

if the alien is already dead -- continue
    calculate position of current alien from position of the top left alien
    compare to the position of the missile
    if it is a hit, update the boolean array to show that one as dead
}

update the position of the top left alien
for i = 0 to number of aliens {
    if the alien is still alive, calculate its position relative to top left alien and display it
}

update the position of any missile on the screen

rest of the code to move the ship and fire the missiles

I am so sorry. I meant how does one drop a value from an array. Sorry!

AWOL:

if(val<=24){

lcd.setCursor(xAliens[val],yAliens[val]);



There is no "xAliens[24]"

Oops.

I am fairly certain there is an xAliens 24.

#define ALIEN_COUNT 24
int yAliens[ALIEN_COUNT] = {0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1};                                                       
int xAliens[ALIEN_COUNT] = {0,1,2,3,4,5,6,7,8,9,10,11,0,1,2,3,4,5,6,7,8,9,10,11};[code]

No, there isn't. If there are 24 elements in an array then they would be numbered 0 through 23. There would be no element 24.

And you don't drop an element out of an array. That's why we keep telling you to make an array of booleans. Each element will be either true or false and tells you the state of one of the aliens. So if alienState[6] == true then that alien needs to be displayed. If it gets hit, change it to false So when you check next time you see that alienState[6] is false meaning don't display that one.

Delta_G:
No, there isn’t. If there are 24 elements in an array then they would be numbered 0 through 23. There would be no element 24.

And you don’t drop an element out of an array. That’s why we keep telling you to make an array of booleans. Each element will be either true or false and tells you the state of one of the aliens. So if alienState[6] == true then that alien needs to be displayed. If it gets hit, change it to false So when you check next time you see that alienState[6] is false meaning don’t display that one.

Just letting you know that I solved the error. hitAlien started as true, which made fire always false. And thank you all for the help concerning the array, which is below. Is this code what you meant with the boolean array?

for(byte a=0; a<=24; a++){
      if(fire&&xb== xAliens[a] && yb == yAliens[a] &&AlienState[a]==true){
       hitAlien = true;
       a=arraynumber;
       break;
      }
      if(a>=25){
        a=0;
      }
   } 
   if (hitAlien) {                              
        fire = false;            //bullet stops moving                                                 
        lcd.setCursor(xb, yb);   //the position of the alien                  
        lcd.print(" ");          //becomes blank                              
        lcd.setCursor(xb+1,yb);
        lcd.print(" ");
        tone(9,400,200);
        AlienState[arraynumber]=false;
        hitAlien=false;
         
           
    }

Did you read my comments in the other threads?

Esp the ones re. your approach to these arrays.

Have ONE position, and then the aliens are OFFSETS from that position.

Then you won't need these redundant arrays that have values that are the same as thier indicies (I ask again: can you see why this is wasteful?).

Direct question: are you going to try to understand what I'm suggesting (and perhaps reply?) or shall concentrate elsewhere?

1:1:
Did you read my comments in the other threads?

Esp the ones re. your approach to these arrays.

Have ONE position, and then the aliens are OFFSETS from that position.

Then you won’t need these redundant arrays that have values that are the same as their indices (I ask again: can you see why this is wasteful?).

Direct question: are you going to try to understand what I’m suggesting (and perhaps reply?) or shall concentrate elsewhere?

I am trying to understand how to use these arrays. I do see the advantages of having the aliens as offsets of an certain position, but I do not need them to move. I am trying to learn and to apply what I have learned so far from arduino to make an game from scratch. It may not be the most efficient or practical way, but I am trying to learn the arduino. I started nine months ago and since have done nine projects from the official arduino book, and a few that suited needs that I need be met.

Does this make any sense to you?

Then you won’t need these redundant arrays that have values that are the same as their indices

Heck, does it make sense to anyone? someone, please ? :drooling_face:

What you need is what grumpy_mike suggested - I think in one of the first replies to your initial thread - an array of bools, representing the life of the alien.

The position is defined in code, referenced relative from one position, >>no need for an absolute system in an array<<

Starting with them not moving is a good idea, but implementing a traditional moving group of aliens should be relatively trivial once you’ve set it up as I suggest.

This project has a quite neat way to use the byte type as an effective ‘array’ of bools - you’d learn bit-wise operations in the process and get closer to the embedded er, ‘way of life’ in doing so… But we’re not there yet.

Again - and it would appear others agree (see reply #1) - that you’d complete this sooner, if you took a step back and learned the basics.

Hope I don’t sound to negative, I really wish you success ! But you’re trying to run before you can crawl. As I’ve mentioned, looks like you’re both motivated and a fast learner, so it shouldn’t be that hard to play catch up. Trick is, that part is up to you - expecting us to carry you over the bumps isn’t going to help you get there. :o

The issues you are having aren’t arduino specific by the way, so if a tutorial at arduino doesn’t help you, try any other C tutorial, eventually you’ll find one that rings true with your learning style.

for(byte a=0; a<=24; a++){

I’ll repeat - there is no element 24 in a 24 element array.

What AWOL is saying is that your code should either be:

for(byte a=0; a<24; a++){

or

for(byte a=0; a<=23; a++){

This is a classic ‘fence post error’ that people go through when they first start coding (and occasionally beyond…)