With c++ You will create a program that uses a Critter class to move around a Gr
ID: 3690702 • Letter: W
Question
With c++
You will create a program that uses a Critter class to move around a Grid, which is also a class.
The Critter and the Grid classes will be in separate files.
The Critter class will have a data member to count the number of moves made. It will also need data members to hold the current x and y coordinates. It will have a member function that randomly moves it one space in one of 4 directions. You will create both the source and header files for the class.
The Grid class has a 2D array data member. This will be an array of type character. It will have an updateLocation() function that will be used by the Critter class to pass the new location of the Critter to be displayed. For the array it will use a space character, ‘ ‘ for empty and a single ‘C’ for the location of the Critter.
The Grid will have one Critter that moves around. The starting location will be random.
The Critter will move around until it would exit the gird. In that case it is squashed. The program should terminate normally and tell the user how many steps the Critter took.
After every move the grid will be displayed to the user.
You will create a program that prompts the user for the number of rows and columns. It must be at least 1 x 1. It will create the Grid and Critter, run the simulation, display the grid after each move, and inform the user when the Critter gets squashed showing the number of moves made.
Explanation / Answer
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
void initialGrid(int,int);
void updateLocation(int,int);
char grid[10][10]; //array declaration
int rows,columns;
int total=0;
class Grid //Grid class
{
public:
void readData()
{
cout<<"Enter number of rows:";:";/*display Enter number of rows:*/
cin>>rows;
cout<<"Enter number of columns:";/*display Enter number of columns:*/
cin>>columns;
}
void initialGrid(int x,int y)
{
int a,b;
cout<<"Initial Random Grid:";
if(x<1||y<1)
{
cout<<"1X1 minimum";
}
else
{
for(a=1;a<=rows;a++) //for loop starting
{
for(b=1;b<=rows;b++)
{
if(a==x&&b==y)
{
grid[a][b]='C';
}
else
grid[][]='';
}
}
}
for(a=1;a<=rows;a++)
{
for(b=1;b<=columns;b++)
cout<<grid[a][b];
cout<<" ";
}
}
void updateLocation(int x,int y)
{
int a,b;
if(x<1||y<1)
{
cout<<"1X1 minimum";
}
else
{
for(a=1;a<=rows;a++)
{
for(b=1;b<=columns;b++)
{
if(a==x&&b==y)
{
grid[a][b]='C';
total++;
}
else
grid[][]='';
}
}
}
for(a=1;a<=rows;a++)
{
for(b=1;b<=columns;b++)
cout<<grid[a][b];
cout<<" ";
}
}
};
class Critter //critter class
{
public:
int x,y;
void randomMove()
{
Grid g;
x=rand()%rows+1;
y=rand()%columns+1;
g.initialGrid(x,y);
cout<<"First updated Grid: ";
g.updateLocation(1,2);
cout<<"Second updated Grid: ";
g.updateLocation(0,3);
cout<<" Third updated Grid: ";
g.updateLocation(2,1);
}
void moves()
{
cout<<" Total number of moves:"<<total<<" ";/*display as Total number of moves: */
}
};
int main()
{
Critter c;
Grid g;
g.readData();
c.randomMoves();
c.moves();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.