C++ Programming: 1) Tree Class (Complete each of the following in order) Now tha
ID: 3575281 • Letter: C
Question
C++ Programming:
1) Tree Class (Complete each of the following in order)
Now that you are able to plot and draw using GraphX, you are ready to tackle the Forest Fire simulation. This is a discrete simulation that will use basic object-oriented techniques to model trees and forests and visualize the progression of the fire using graphical output.
The Tree class can represent various kinds of trees, including a null tree, i.e., a location in the forest that does not have a tree. The Tree class will have the following:
Private data members:
• type (string) - the species of the tree (e.g., "Oak", "not_a_tree")
• probCatch (double) - the probability (0.0 to 1.0) of the tree catching on fire
• status (int) - the tree's status as follows:
0 - not a tree (for a forest location without a tree)
1 - a live tree (i.e., not burning)
2 - a burning tree
• wetness (double) - a number between 1.0 and 100.0 to indicate wetness (default 1.0)
• burnTime (int) - the number of time steps for a tree to burn (default 1)
• xpos (int) - the x position of the tree within a GraphX window (default 0)
• ypos (int) - the y position of the tree within a GraphX window (default 0)
• symbol (string) - the symbol used to draw the tree within a GraphX window (e.g. “solidtriangle”)
Public member functions:
• A default constructor to set the default values ("not_a_tree", 1.0, 0, 1.0, 1, 0, 0, “solidtriangle”)
• A constructor with parameters to let the user initialize data members
• getProbCatch() - an accessor function for the probCatch data member
• setProbCatch() - a mutator function to set probCatch
• getStatus() - an accessor function for the status data member
• setStatus() - a mutator function to set status
• getWetness() - an accessor function for the wetness data member
• setWetness() - a mutator function to set wetness
• getBurnTime() - an accessor function for the burnTime data member
• setBurnTime() - a mutator function to set burnTime
Put the declaration part of the Tree class in file Tree.hpp and the implementation part in file Tree.cpp. Write a driver program to test the Tree class.
This is GraphX.hpp file:
Explanation / Answer
main.cpp
#include "GraphX.hpp"
#include "Tree.hpp"
#include "Forest.hpp"
#include "Oak.hpp"
using namespace std;
int main(){
GraphX graph;
graph.scale(800,800);
Oak oak1("oak", 1.0, 1, 1.0, 1, 200, 100, "solidcircle"), oak2("oak", 1.0, 1, 1.0, 1, 50, 100, "solidcircle"), oak3("oak", 1.0, 1, 1.0, 1, 100, 100, "solidcircle");
Tree tree("tree", 1.0, 1, 1.0, 1, 200, 200, "solidtriangle");
oak1.draw(graph);
oak2.draw(graph);
oak3.draw(graph);
tree.draw(graph);
while(1);
return 0;
}
Tree.cpp
#include "lab13_Tree.hpp"
#include "GraphX.hpp"
using namespace std;
Tree::Tree(){
type="not_a_tree";
probCatch=1.0;
status=0;
wetness=1.0;
burnTime=1;
xpos=0;
ypos=0;
symbol="solidtriangle";
}
Tree::Tree(string str, double a, int b, double c, int d, int e, int f, string str2){
type=str;
probCatch=a;
status=b;
wetness=c;
burnTime=d;
xpos=e;
ypos=f;
symbol=str2;
}
double Tree::getProbCatch(){
return probCatch;
}
void Tree::setProbCatch(double a){
probCatch=a;
}
int Tree::getStatus(){
return status;
}
void Tree::setStatus(int a){
status=a;
}
double Tree::getWetness(){
return wetness;
}
void Tree::setWetness(double a){
wetness=a;
}
int Tree::getBurntime(){
return burnTime;
}
void Tree::setBurntime(int a){
burnTime=a;
}
void Tree::draw(GraphX& graph){
graph.setpos(xpos,ypos);
graph.symbolsize(10);
graph.symbol(symbol);
if(getStatus()==0)
graph.pencolor("white");
else if(getStatus()==1)
graph.pencolor("green");
else if(getStatus()==2)
graph.pencolor("red");
else if(getStatus()==3)
graph.pencolor("blue");
graph.plot();
}
Tree.hpp
#include <string>
#include "GraphX.hpp"
#ifndef TREE
#define TREE
using namespace std;
class Tree{
public:
Tree();
Tree(string, double, int, double, int, int, int, string);
double getProbCatch();
void setProbCatch(double);
int getStatus();
void setStatus(int);
double getWetness();
void setWetness(double);
int getBurntime();
void setBurntime(int);
virtual void draw(GraphX&);
protected:
string type;
double probCatch;
int status;
double wetness;
int burnTime;
int xpos;
int ypos;
string symbol;
};
#endif
GraphX.hpp
#ifndef GRAPHX_HPP
#define GRAPHX_HPP
#include <X11/Xlib.h>
#include <string>
using namespace std;
class GraphX
{
public:
// constructors
GraphX(); // default: create a 800 pixel wide x 600 pixel high window
GraphX(int w, int h); // create a w-pixel wide x h-pixel high window
~GraphX(); // destructor: close the x-server connection and remove window
// drawing primitives
void setpos(int x, int y); // move the pen location to x,y
void setposition(int x, int y); // ^^
void setx(int x); // move the pen location to x, unchanged y
void sety(int y); // move the pen location to unchanged x, y
void plot(); // plot current symbol at pen location
void erase(); // erase current symbol at pen location
void penup() {pendn = false;} // pendown state = false
void pu(){penup();} // ^^
void up(){penup();} // ^^
void pendown(){pendn = true;} // pendown state = true
void pd(){pendown();} // ^^
void down(){pendown();} // ^^
void clear(); // fill window with background color
void clearscreen(){clear();} // ^^
void reset(){clear();} // ^^
void resetscreen(){clear();} // ^^
void delay(double secs) // delay start of next GraphX operation
{delaytime = secs;}
void screensize(int x, int y); // clear and resize the GraphX window
void screensize(int x, int y, string c) // ^^ and change color
{backgroundcolor(c); screensize(x,y);}
// appearance
void pencolor(string); // set pen color
void pensize(int); // set pen size (in pixels)
void width(int w){pensize(w);} // ^^
void penstyle(string); // solid line, dashes, dots, dash-dot
void symbol(string); // set the plot symbol
void symbolsize(int); // set the size of the plotted symbol
void backgroundcolor(string); // set background color
// scaling
void scale(int width, int height); // set pixel units for x & y
void origin(int x, int y); // set the origin to x units, y units
void xaxis( ); // draw a line through the x axis
void yaxis( ); // draw a line through the y axis
// status
bool isopen( ){return openstate;} // return state of graphics environment
bool fail(){return !openstate;} // return status of last operation
bool isdown(){return pendn;} // return pen state (down = true)
int xcor(){return xloc;} // return current pen location
int ycor(){return yloc;} // return current pen location
void position(int &x, int &y); // return current pen x,y location
void pos(int &x, int &y); // ^^
int pensize(){return psize;} // return pen size (in pixels)
int width(){return pensize();} // ^^
void getscreensize(int &x, int &y) // return the current screen size
{x = winwidth; y = winheight;}
int delay(){return delaytime;} // return delay between drawing ops
// extended drawing
void moverel(int dx, int dy); // move relative to current location
void plot(int x, int y); // combine 'move' and 'plot' operations
void plot(string); // set current symbol and plot it
void plot(int,int,string); // combine 'move' and 'plot(symbol')
// input
string input(); // return input status
char eventkey(); // return key event value
void eventxy(int &x, int &y); // return x,y (scaled appropriately)
private:
enum GXColor {BLACK=0, WHITE=1, RED=2, YELLOW=3, BLUE=4, GREEN=5, ORANGE=6,
PURPLE=7};
enum GXLine {SOLID, DOTS, DASH, LONGDASH, DOTDASH};
enum GXSym {CIRCLE, SQUARE, DIAMOND, TRIANGLE, INVTRIANGLE,
SOLIDCIRCLE, SOLIDSQUARE, SOLIDDIAMOND, SOLIDTRIANGLE,
SOLIDINVTRIANGLE};
enum GXEvent {NONE=0, KEYPRESS=1, LEFTBUTTON=2, RIGHTBUTTON=3};
bool openstate; // window is opened and OK to use
int winwidth; // width of window in pixels
int winheight; // height of window in pixels
int xloc; // current pen location (scale units)
int yloc; // current pen location (scale units)
double xscale; // pixels per x unit
double yscale; // pixels per y unit
int xorigin; // optional xorigin value (scale units)
int yorigin; // optional yorigin value (scale units)
double delaytime; // delay next operation by (in microseconds)
GXColor bgcolor; // current background color
int psize; // pen size
GXColor pcolor; // pen color
GXLine pstyle; // pen style
GXSym psymbol; // plotting symbol
int symsize; // size (in pixels) of the plotted symbol
bool pendn; // pen 'down' state (true/false)
GXEvent lastinput; // most recent keyboard/mouse event
char kval; // last keyboard event: key value
int buttonx; // last button event: x location
int buttony; // last button event: y location
//Xlib related data
Display *disp; // the display pointer
int screen; // default screen identifier
Window win; // the main window (added)
XEvent event; // event reporting structure
GC gc; // graphics context for drawing
unsigned long pixelvalues[8]; // color pixel values (mapped at start)
//Private class functions
void wait(); // delay a graphics operation
void setDefaults(); // initialize all the default values
bool openX(int, int); // create and initialize environment
void drawSymbol(GXColor); // draw current symbol
void setLineGC(GXColor); // set the Xlib GC information for a line
int mapX(int); // return pixel offset given x location
int mapY(int); // return pixel offset given y location
GXColor getColor(string); // string to GXColor conversion
GXLine getLine(string); // string to GXLine conversion
GXSym getSym(string); // string to GXSym conversion
};
#endif
Oak.cpp
#include "Tree.hpp"
#include "GraphX.hpp"
#include "lab13_Oak.hpp"
using namespace std;
Oak::Oak(){
type="Oak";
probCatch=1.0;
status=1;
wetness=1.0;
burnTime=1;
xpos=0;
ypos=0;
symbol="solidcircle";
}
Oak::Oak(string a, double b, int c, double d, int e, int f, int g, string h){
type="Oak";
probCatch=b;
status=c;
wetness=d;
burnTime=e;
xpos=f;
ypos=g;
symbol=h;
}
void Oak::draw(GraphX& gx){
gx.setpos(xpos,ypos);
gx.symbolsize(10);
gx.symbol("solidcircle");
if(getStatus()==0)
gx.pencolor("white");
else if(getStatus()==1)
gx.pencolor("green");
else if(getStatus()==2)
gx.pencolor("red");
else if(getStatus()==3)
gx.pencolor("blue");
gx.plot();
}
Oak.hpp
#include <string>
#include "GraphX.hpp"
#include "Tree.hpp"
#ifndef OAK
#define OAK
using namespace std;
class Oak:public Tree{
public:
Oak();
Oak(string a, double b, int c, double d, int e, int f, int g, string h);
void draw(GraphX& gx);
};
#endif
Forest.cpp
#include "Tree.hpp"
#include "GraphX.hpp"
#include "lab13_Forest.hpp"
#include <cstdlib>
#include <ctime>
using namespace std;
Forest::Forest(){
srand(time(0)); //remember to put this srand in constructor
for(int i=0; i<33; i++){
for(int j=0; j<33; j++){
if((i==0)||(j==0)||(i==32)||(j==32))
grid[i][j] = Tree("not_a_tree", 0.0, 0, 1.0, 1, i*25, j*25, "solidtriangle");
else if((i==16)&&(j==16))
grid[i][j] = Tree("Oak", 1.0, 2, 1.0, 1, i*25, j*25, "solidtriangle");
else
grid[i][j] = Tree("Oak", 0.80, 1, 1.0, 1, i*25, j*25, "solidtriangle");
}
}
}
void Forest::draw(GraphX& graph){
for(int i=0; i<33; i++){
for(int j=0; j<33; j++){
grid[i][j].draw(graph);
}
}
}
int Forest::nextStatus(int i, int j){
if(grid[i][j].getStatus()==0)
return 0;
else if(grid[i][j].getStatus()==1){
if(((grid[i+1][j].getStatus()==2)||(grid[i][j+1].getStatus()==2)||(grid[i-1][j].getStatus()==2)||(grid[i][j-1].getStatus()==2))
&&(((double)rand()/(RAND_MAX))<((grid[i][j].getProbCatch())/(grid[i][j].getWetness())))) //be careful not to divide int by int here. put (double) to get double rand() num.
return 2;
else
return 1;
}
else
return 0;
}
void Forest::applyNextStatus(){
Tree next_grid[33][33];
//copy grid to next_grid
for(int i=0; i<33; i++){
for(int j=0; j<33; j++){
next_grid[i][j]=grid[i][j];
}
}
for(int i=0; i<33; i++){
for(int j=0; j<33; j++){
next_grid[i][j].setStatus(nextStatus(i,j));
}
}
for(int i=0; i<33; i++){
for(int j=0; j<33; j++){
grid[i][j]=next_grid[i][j];
}
}
}
bool Forest::isBurning(){
for(int i=0; i<33; i++){
for(int j=0; j<33; j++){
if(grid[i][j].getStatus()==2)
return true;
}
}
return false;
}
void Forest::setGrid(int x, int y, double a, int b, double c, int d){
grid[x][y].setProbCatch(a);
grid[x][y].setStatus(b);
grid[x][y].setWetness(c);
grid[x][y].setBurntime(d);
}
void Forest::lightning(double lightProb){
for(int i=0; i<33; i++){
for(int j=0; j<33; j++){
if(((double)rand()/(RAND_MAX))<lightProb)
grid[i][j].setStatus(2);
}
}
}
void Forest::regrowth(double growProb){
Tree next_grid[33][33];
//copy grid to next_grid
for(int i=0; i<33; i++){
for(int j=0; j<33; j++){
next_grid[i][j]=grid[i][j];
}
}
for(int i=1; i<32; i++){
for(int j=1; j<32; j++){
if((grid[i][j].getStatus()==0)&&(((double)rand()/(RAND_MAX))<growProb))
next_grid[i][j].setStatus(3);
}
}
for(int i=0; i<33; i++){
for(int j=0; j<33; j++){
grid[i][j]=next_grid[i][j];
}
}
}
Forest.hpp
#include "Tree.hpp"
#include "GraphX.hpp"
using namespace std;
#ifndef FOREST
#define FOREST
class Forest{
public:
Forest();
void draw(GraphX&);
int nextStatus(int i, int j);
void applyNextStatus();
bool isBurning();
void setGrid(int, int, double, int, double, int);
void lightning(double lightProb);
void regrowth(double growProb);
private:
Tree grid[33][33];
};
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.