I am making a Forest Fire Simulation using ASCII symbols and I am suposed to get a display of one "forest" that updates with burning and burned out trees every iteration. I have to be over looking something as I cannot figure out what I am getting multiple prints of the the forest in the console. I thank you in advance for you're assistance.
Here is the complete code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <iostream>
#include <iomanip>
#include <time.h>
#include <stdio.h>
using namespace std;
const int MAX = 30;
const char TREE = '+';
const char BURNING = '#';
const char BURNEDOUT = '.';
void burnForest(char forest [MAX][MAX], int& probFactor);
void displayForest(char [MAX][MAX]);
void getInput(int& rowTree, int& colTree);
void getProbability(int& probFactor);
void initForest(char forest[MAX][MAX],int row, int column);
void initSim(char forest [MAX][MAX]);
bool isAnyTreeOnFire(char forest [MAX][MAX]);
bool isNeighborOnFire(char forest[MAX][MAX], int row, int column);
//void printReport(char forest[MAX][MAX], int probFactor, burnCycle);
void printRequired246Headings();
void updateForest(char forest [MAX][MAX]);
void wait(int seconds);
|
//************************************************* M A I N *
1 2 3 4 5 6 7 8 9 10 11 12
|
int main()
{
char forest [MAX][MAX];
printRequired246Headings(); // Name, Course Number, and Program Desctiption
initSim(forest); // Initializes Simulation by Gathering User Input
// and setting One Tree on Fire
cout <<"\n\n*--Normal Termination--*\n";
system("pause");
return 0;
}
|
/******************************************* initForest ***/
// Initialize Forest with "trees" and sets one on fire
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
void initForest(char forest [MAX][MAX], int row, int column)
{
for (int r = 0; r < MAX; r++) //Initialize row[0] and row[29]
{
for (int c = 0; c < MAX; c++) // Initialize column[0] and column[29]
{
if(r == 0 || r == (MAX - 1))
forest[r][c]= ' ';
else if(c == 0 || c == (MAX - 1))
forest[r][c] = ' ';
else
forest[r][c] = TREE; // Initailizes remainder of array
}// end for column loop
}// end for row loop
forest[row][column] = BURNING; // Set start point of burning tree
return;
}
|
/************************************** displpayForest ***/
// Displays the Forest
1 2 3 4 5 6 7 8 9
|
void displayForest(char forest [MAX][MAX])
{
for (int i = 0; i < MAX; i++)
{
cout << "\n";
for (int j = 0; j < MAX; j++)
cout << forest[i][j];
}
}
|
/********************************************* getInput ***/
// Get user input for row and column of tree to start burn
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
void getInput(int& rowTree, int& colTree)
{
// Input from user for selection of row and column for start point
cout << "This program will simulate a forest fire based on user input" << endl;
cout << "\nWhat row is your tree in? Please enter an integer between 1 & 28: ";
cin >> rowTree;
cout << "\nWhat column is your tree in? Please enter an integer between 1 & 28: ";
cin >> colTree;
// Validation loop to check if row is within 1 and 28
while (rowTree <1 || rowTree >MAX - 2)
{
cout << "\nYour tree is not in the forest. Try Again.";
cin >> rowTree;
}
// Validation loop to check if column is within 1 and 28
while (colTree <1 || colTree >MAX - 2)
{
cout << "\nYour tree is not in the forest. Try Again.";
cin >> colTree;
}
}
|
/*************************************** getProbability ***/
// Get Probability from User to determine progression of fire
1 2 3 4 5 6 7 8 9 10 11 12
|
void getProbability(int& probFactor)
{
cout << "\nPlease enter a probability for the forest fire between 1 & 100: ";
cin >> probFactor;
// Validation loop to check if probability is between 1 and 100
while (probFactor <1 || probFactor >100)
{
cout << "\nPlease re-enter a probability between 1 & 100: ";
cin >> probFactor;
}
}
|
/************************************ isNeighborOnFire ***/
// Function to determmine is neighboring tree is on fire
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
bool isNeighborOnFire(char forest[MAX][MAX], int row, int column)
{
int dir = 0; // Set direction to zero
bool onFire = false;
// Declare and initialize a Direction Array for the 8 locations around a tree
int dirAry [8][2] = {{-1,-1},{-1,0},{-1,-1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
// This while loop scans through the Direction Array to help determine
// the probability of neighboring trees being on fire
while (dir <8 && !onFire)
{
int r = row + dirAry[dir][0];
int c = column + dirAry[dir][1];
if (forest[r][c] == BURNING)
{
onFire = true;
}
else
{
dir++;
}
}
return onFire;
}
|
/******************************************** voidWait ****/
//This function adds a wait time in between the forest update
//Function modified from cplusplus.com
1 2 3 4 5 6 7 8
|
void wait(int seconds)
{
//Stall "seconds" sec by system clock
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait)
{/* do nothing */}
}
|
/**************************************** updateForest ****/
// Updates state of forest to determine state of tree, BURNING or
// BURNEDOUT and copies forest to tempForest out to forest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
void updateForest(char forest [MAX][MAX], int probFactor)
{
char tempForest [MAX][MAX];
for(int r = 0; r < MAX; r++)
for(int c = 0; c < MAX; c++)
tempForest[r][c] = forest[r][c];
for (int r = 1; r < MAX -1; r++) // Walk through row to update
{
for (int c = 1; c < MAX - 1; c++) // Walk through column to update
{
if(isNeighborOnFire(forest, r, c))
{
if ((rand()%100)+1 < probFactor && forest[r][c] == TREE)
tempForest [r][c] = BURNING;
}
if (forest [r][c] == BURNING)
{
tempForest[r][c] = BURNEDOUT;
}
}// end for column loop
}// end for row loop
for(int r = 0; r < MAX; r++)
for(int c = 0; c < MAX; c++)
forest[r][c] = tempForest[r][c];
}
|
/********************************************* initSim ****/
//Initial Simulation, seeds the random number generator, gets input from
//the user, i.e. row & column number to set the first tree on fire,
//probability factor for determining the progression of the burn, displaying
//the forest and setting it ablaze.
1 2 3 4 5 6 7 8 9 10 11
|
void initSim(char forest [MAX][MAX])
{
srand(time(NULL));
int rowTree = 0;
int colTree = 0;
int probFactor = 0;
getInput(rowTree, colTree);
getProbability(probFactor);
initForest(forest, rowTree, colTree);
burnForest(forest, probFactor);
}
|
/****************************************** burnForest ****/
// Burns forest, displays forest, updates forest, and
// checks to see if any tree is on fire
1 2 3 4 5 6 7 8 9 10 11 12
|
void burnForest(char forest [MAX][MAX], int& probFactor)
{
int count =0;
while (isAnyTreeOnFire(forest))
{
displayForest(forest);
updateForest(forest, probFactor);
count++; // counts cycles
wait(1);
}
//printReport(forest, probFactor, count);
}
|
/***************************************** printReport ****/
//
1 2 3 4
|
/*void printReport(char forest[MAX][MAX], int probFactor, int burnCycle)
{
} */
|
/************************************* isAnyTreeOnFire ****/
// Checks to see if any tree is on fire is the forest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
bool isAnyTreeOnFire(char forest [MAX][MAX])
{
bool anyOnFire = false;
int i; //Loop control variable
int j; //Loop control variable
// Runs through forest to determine trees on fire and returns forest
for ( i = 0; i < MAX; i++)
{
cout << "\n";
for ( j = 0; j < MAX; j++)
{
cout << forest[i][j];
if (forest[i][j] == BURNING)
{
anyOnFire = true;
}
}
}
return anyOnFire;
}
|
/***************************** printRequired246Headings ***/
1 2 3 4 5 6
|
void printRequired246Headings()
{
cout<<"\n\nxxxxxxxxxxxxxx ~ CpSc246.01 ~ Spring 2012\n";
cout<<" Forest Fire"<<endl;
cout<<"Adapted from www.shodor.org/interactivate/activities/Fire/\n\n\n";
}
|