I am writing a program that will simulate a forest fire, I have moved many functions into one larger function called initSim where each should be called in order to get the required inputs from the 'user' and display the forest. When I run the program I do not get any of the questions for the user in the console, and thus the program does not display the desired output.
I do have a bunch of code commented out as it is not needed yet.
I have a screenshot of the output if that would help. But in the mean time here is my code:
/**************************************************************
** Chris Leonard ~ CpSc246 Spring 2012 ~ Forest Fire Sim **
***************************************************************
** Ask user for burn probablility, how fire will spread, **
** and start point. Burn Forest graphically, display **
** report, and allow reset of Forest. **
***************************************************************
** Adapted from www.shodor.org/interactivate/activities/Fire/ **
***************************************************************/
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 = '.';
char forest [MAX][MAX];
//bool isNeighborOnFire(forest, row, column);
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], int& probFactor);
void printReport();
void printRequired246Headings();
void updateForest(char forest [MAX][MAX]);
void wait(float seconds);
|
/**********************************************************
************************************************* M A I N *
**********************************************************/
1 2 3 4 5 6 7 8 9 10 11 12 13
|
int main()
{
int probFactor;
printRequired246Headings();
initSim(forest, probFactor);
//displayForest(forest); call it from burn simulation module and results module
//isNeighborOnFire(forest, row, column)-called from somewhere else.
cout <<"\n\n*--Normal Termination--*\n";
return;
}
|
/**********************************************************/
/******************************************* initForest ***/
// Initialize Forest with "trees"
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 if(r == row && c == column)
forest[r][c] = BURNING; //Set start point of burning tree
else
forest[r][c] = TREE; //Initailizes remainder of array
}//end for column loop
}//end for row loop
return;
}
|
/**********************************************************/
/************************************** displpayForest ***/
// Displays the Forest
1 2 3 4 5 6 7 8 9
|
void displayForest(char [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 interger between 1 & 28: ";
cin >> rowTree;
cout << "\nWhat column is your tree in? Please enter an interger 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 success of fire
1 2 3 4 5 6 7 8 9 10 11
|
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
|
/*
bool isNeighborOnFire(forest, row, 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] == onFire)
{
onFire = true;
else
dir++;
}
}
} */
|
/**********************************************************/
/************************************** voidWait ****/
1 2 3 4 5 6 7 8 9
|
//This function adds a wait time in between the forest update
//void wait(float seconds)
//{
// Stall "seconds" sec by system clock
// clock_t endwait;
// endwait = clock () + seconds * CLOCKS_PER_SEC ;
// while (clock() < endwait);
//}
|
/**********************************************************/
/**************************************** updateForest *******/
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
|
//
//void updateForest(char forest [MAX][MAX], int probFactor)
//{
// char tempForest [MAX][MAX];
// for (int r = 0; r < MAX -2; r++) //Walk through row to update
// {
// for (int c = 0; c < MAX - 2; c++) //Walk through column to update
// {
// if(isNeighborOnFire(forest, row, column))
// {
// if ((rand()%100)+1 < probFactor)
// forest [r][c] = BURNING;
// }
// else if (forest [r][c] == BURNING)
// {
// forest [r][c] = BURNEDOUT;
// }
// else
// {
// //Else case is tree burned out or empty cell
// }
//
// }//end for column loop
// }//end for row loop
//}
|
/**********************************************************/
/********************************************* initSim ****/
//
1 2 3 4 5 6 7 8 9 10
|
void initSim(char forest [MAX][MAX], int& probFactor)
{
//srand(time(NULL));
int rowTree;
int colTree;
getInput(rowTree, colTree);
getProbability(probFactor);
initForest(forest, rowTree, colTree);
displayForest(forest);
}
|
/**********************************************************
***************************** printRequired246Headings ***/
//
1 2 3 4 5 6
|
void printRequired246Headings()
{
cout<<"\n\nChris Leonard ~ CpSc246.01 ~ Spring 2012\n";
cout<<" Forest Fire"<<endl;
cout<<" Adapted from www.shodor.org/interactivate/activities/Fire/\n\n\n";
}
|