-2

am new to c++ and am trying to create a burning forest simulator. I have been trying to call a function from the forest class but i dont know how to give it an argument, if anyone could help that would be great here is the code that i have at the moment.

using namespace std;



class ForestSetup
{
private:

    const char Tree = '^';
    const char Fire = '*';
    const char emptySpace = '.';
    const char forestBorder = '#';
    const int fireX = 10;
    const int fireY = 10;

    char forest[21][21];


public:

    void CreateForest()
    {
        // this function is to create the forest

        for (int i = 0; i < 21; i++) // this sets the value of the rows from 0 to 20
        {
            for (int j = 0; j < 21; j++) // this sets the value of the columns from 0 to 20
            {

                if (i == 0 || i == 20)
                {
                    forest[i][j] = forestBorder; // this creates the north and south of the forest border
                }

                else if (j == 0 || j == 20)
                {
                    forest[i][j] = forestBorder; // this creates the east and the west forest border
                }

                else
                {
                    forest[i][j] = Tree; // this filles the rest of the arrays with trees
                }

            }
        }

        forest[fireX][fireY] = Fire; // this sets the fire in the middle of the grid
    }

    void ShowForest(char grid[21][21])
    {
        for (int i = 0; i < 21; i++)
        {
            for (int j = 0; j < 21; j++)
            {
                cout << grid[i][j];
            }

            cout << endl;
        }
    }



};

int main(void)
{


    ForestSetup myForest;

    myForest.ShowForest();

    system("Pause");
    return 0;
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302

1 Answers1

0

Let's say you have a setOnFire function with the x and y co-ordinates of where you want to start the fire.

public:
  setOnFire(int x, int y)
  {
      // Code to flag that part of the forest on fire
  }

In your main, you would call it with

myForest.setOnFire(5,5);

Within the ForestSetup class you just need setOnFire(5,5);

This tutorials point article might help.

Neil Stoker
  • 284
  • 3
  • 16