0

I am working on a C++ project which is about simulating bees in an ecosystem. We had to code an environment represented via a vector of seeds outside of the class :

Now we must make sure that each of these seeds transmits its nature (Kind : Rock, Water, Grass) to the cell of the one-dimensional array cells_ whose position it occupies. Clearly, if the seed has for position (x,y) and for nature k then the cell at position (x,y) must take the value k, except if it is a water cell (water cells do not change their nature, cannot be covered).

here are the attributs in private (among others) :


    enum class Kind : short { Grass, Water, Rock };
    std::vector<Kind>cells_;
    struct Seed { sf::Vector2i position_;}; 
std::vector<Seed> seeds_; 
int nbCells_; // represents the number of cells per line
    // uniform() is a method that attributes random coordinates for a seed 


class World {
//...
            
    private:
    int nbCells_;
    float cell_size_;

//and here is the body of the method we have to code 
    
    for (unsigned int i(0); i < nbCells_-1; ++i ) { 
           position_ =  uniform(0, nbCells_-1); }
           if (cells_[i] == position_) {
               switch Kind::i {
                   case Kind::Rock:
                   .cells_[i] = position_;
                   break;

                   case Kind::Grass:
                   .cells_[i] = position_;
                   break;

 
};

but I'm struggling with the syntax ... any ideas ? thanks !

sparklight
  • 11
  • 1
  • 1
    `switch Kind::i` should be `switch (i)`, and `.cells_[i]` should be `cells_[i]`. Any [decent C++ book](https://stackoverflow.com/questions/388242/) should have covered this. Though, your code is confusing, because if `cells_[i] == position_` is true then why are you assigning `position_` back to `cells_[i]`? That is effectively a no-op. In any case, I'm also thinking that `nbCells_-1` should probably be `nbCells_`, but you didn't show how `nbCells_` is initialized, or what `uniform()` does. And what does `seeds_` have to do with this code? Please provide a [mcve]. – Remy Lebeau Apr 07 '22 at 22:15
  • 1
    As you fix each issue, it may be helpful to create small programs alongside this one to help you test syntax. – Camwin Apr 07 '22 at 22:16
  • Please do not add important details in comments. As you can see, the code is unreadable, and sometimes even stripped (especially of things in `<>`). [edit] your question if you need to add something else. – Yksisarvinen Apr 07 '22 at 22:17
  • @RemyLebeau I would like to compare the coordinates of a seed like this in the condition : if (position_.cells_[i] == ...) to check if it is at the same index and with the same type of the compared cell – sparklight Apr 07 '22 at 22:24
  • 1
    @sparklight again, there is no way we can correlate how `Seed` fits in with the rest of the code you have shown. But `position_.cells_[i]` can't possibly be valid. What is `position_` in that case? If it is a `Seed`, then its `position_` doesn't have a `cells_` member. What does `uniform()` return? A `Seed`, or something else? Again, you need to *clean up* the code shown, actually show us what you are trying to accomplish, don't just give us *pieces* of code. – Remy Lebeau Apr 07 '22 at 22:36

0 Answers0