Hello all--
Earlier, I got help here on how to instantiate objects. Koepel helped me out with the syntax.
Now I'm trying to move further to make arrays of those objects.
In my same program, code below, that creates objects of class Automobile, I now have it so that it creates an array of 3 of those Automobiles. My automobile class needs 3 parameters passed to it, so I've successfully got it instantiating an array of 3 of the Automobiles, by using this statement out in the global section of the code:
Automobile autos[3] = {Automobile(40,0,40), Automobile(5,75,5), Automobile(20,55,50)};
So I have 2 more annoying questions:
1-- The above works great, but what if I wanted to instantiate 200 Automobiles all at once? Naturally I don't want to type an ultra long list within the brackets... is there a way to use a loop to do this? I've done this in Python a long time ago using a "list comprehension" but don't know the best way to do it in Arduino C++.
2-- Then, to go further (and as far as I intend to go...) ... I'd like to set up a 2 dimensional array, not just a one-dimensional one. (My ultimate goal is to make a simple game with a grid of objects. But for this example, I guess we're imagining a 2 dimensional grid of Automobiles...)
... I know that the way to work with 2-dimensional arrays C++ is autos[3][5] ... So once I have succeeded instantiating it, I'm guessing I will say things like autos[3][5].acclerate()
... BUT, just like the question 1 above, I don't know the syntax to instantiate the 2-dimensional array to begin with (that it, without typing out the long list of nested { { },{ },{ }, ... things...
Here is my working code with only the above three-automobile instantiating I mention above...
class Automobile {
public:
Automobile(int gaslevel, int velocity, int tanksize);
void accelerate();
void halt();
void decelerate();
void filltank();
void consumegas();
int checkspeed();
int checkgas();
private:
int _gaslevel;
int _velocity;
int _tanksize;
};
// creating functions for the object... in other words object members that are METHODS
Automobile::Automobile(int gaslevel, int velocity, int tanksize) {
_gaslevel = gaslevel;
_velocity = velocity;
_tanksize = tanksize;
}
void Automobile::accelerate() {
_velocity++;
}
void Automobile::halt() {
_velocity = 0;
}
void Automobile::decelerate() {
_velocity--;
}
void Automobile::filltank() {
_gaslevel = _tanksize;
}
void Automobile::consumegas() {
_gaslevel--;
}
int Automobile::checkspeed() {
return _velocity;
}
int Automobile::checkgas() {
return _gaslevel;
}
// INSTANTIATING THE ARRAY IN THE SIMPLE WAY...
Automobile autos[3] = {Automobile(40,0,40), Automobile(5,75,5), Automobile(20,55,50)};
void setup() {
Serial.begin(9600);
Serial.println("------------------");
}
void loop() {
int r = random(1,4);
if ( r == 1) {
autos[1].accelerate();
}
else if (r== 2) {
autos[1].decelerate();
}
autos[1].consumegas();
if (autos[1].checkgas() < 1) {
autos[1].halt();
autos[1].filltank();
}
Serial.print(" velocity=");
Serial.print(autos[1].checkspeed());
Serial.print(" gaslevel=");
Serial.print(autos[1].checkgas());
Serial.println();
}
Huge thanks for any ideas --
Eric