I have to create a C++ program will simulate students, initially placed at rando
ID: 3571320 • Letter: I
Question
I have to create a C++ program will simulate students, initially placed at random position on campus (inside or outside the school building), wandering about the campus grounds picking up trash in an effort to gain rewards. Eventually, he/she/it will enter the school building and, after encountering the teacher, will be assigned a grade for the project based on what they collected. When all classmates have received a grade, a MOST INTELLIGENT STUDENT AWARD will be given and the winner recognized.
The classes I am required to use are:
Person class:
member variables:
- a string (either type) variable for the person’s first name.
- a string (either type) variable for the person’s last name.
- an integer variable for the object's intelligence quotient (iq).
member functions: (all public)
- a default constructor to which you can assign person's name to "" (empty string) with an IQ of 0.
- Accessors and mutators (functions for member variables).
Add one Structure for Position with two member variables: pos_x and pos_y.
Add one Structure for Dimension with two member variables: width and height.
Student class derived from Person's class
member variables:
- a string for the student's name
- an int of the location array
member functions (public):
- go_to_campus() function : "places" the classmate object in an empty cell of the campus at random
- Each student object will have “pockets” into which he/she/it puts trash objects encountered in their travels about the campus. Implement the “pockets” using an array of Trash type objects (Trash is described below). Make the max number of items a student can hold 10. Naturally, the student will also need to keep track of how many pieces of trash they have
- move() function: Student only allowed to move one cell at a time. If the student steps on a cell marked 'T' then they pick up the trash and put it in their pockets. If the student's pockets are full, then the lowest valued item in the pockets is discarded and the new item replaces it. Any bomb picked up, gets eaten and the student's IQ is diminished by 2 points. The student cannot move outside of campus, cannot move through the walls, or move through another classmate. Once all trash have been picked up, the students move to the door of the school building and encounter the teacher to sum up their total points.
Campus class
private:
int MAX = 25;
char maxSize[MAX][MAX];
int campus_size;
int building_size;
void buildCampus();
public:
Campus() // Default constructor
void buildCampus(); // Constructs the campus
// S in every cell of the school building
// D for door in the cell of the school building
// Space elsewhere
// T for trash, 10% rounded down, in blank spaces at random
The campus needs some way to keep track of how much trash is on campus at any time.
The campus's constructor will randomly place one F (for Teacher) in the school building not in the doorway
Build
Trash class
Trash will have a member variable for its name, and a member variable for its value (a positive integer).
It will have a constructor that will connect to a file of trash items (.txt file) and will pick at random an entry to instantiate the values of the trash item. format is: name(string) value(int)
// point system
// sodaCan 5
// usedGum 4
// textbook 10
// candyWrapper 1
// glass 6
// key 8
// bomb 0
// deadAnimal 9
// pen 2
// cup 3
// point system
Main:
start main
declare campus
declare student1,student2
student1.go_to_school()
student2.go_to_school()
display campus
initialize loop counter
start simulation - loop on trash gathering
student1.move()
student2.move()
display campus
increment loop counter
end simulation - end loop - all classmates scored
declare/display winner and number of moves to finish
end main
I've started on the code but I'm having trouble trying to formulate the graphics of the simulation since it was barely covered in class and the textbook.
Explanation / Answer
main.cpp
using namespace std;
#include <iostream>
#include "Person.h"
#include "Student.h"
#include "Campus.h"
#include "Trash.h"
#include <string>
#include <stdlib.h>
int main()
{
/*Trash t;
cout<<t;
t.removeTrash();
cout<<t;*/
Dimension camp, build;
camp.width=10;
build.width=5;
camp.height=8;
build.height=7;
Campus UTD(camp, build);
/*
Position pos;
pos.pos_x=5;
pos.pos_y=5;
Student John("John", "Go f urself", pos, 20);
*/
Student John("John", "Cena", 69);
Student Jane("Jane", "Eyre", 120);
John.go_to_campus(UTD);
Jane.go_to_campus(UTD);
cout<<UTD;
int moves = 0;
while(moves<35)//UTD.numTrash() != 0)
{
John.move(UTD);
Jane.move(UTD);
//cout<<UTD.numTrash()<<" ";
//cout<<John.getNumTrash()<<" "<<Jane.getNumTrash()<<" ";
cout<<UTD;
cout<<"--------------------- ";
moves++;
}
if(John.getGrade() == Jane.getGrade())
cout<<"A tie! Both students have a grade of: "<< Jane.getGrade();
else if(John.getGrade() < Jane.getGrade())
cout<<"Jane wins with a grade of: "<<Jane.getGrade();
else cout<<"John wins with a grade of: "<<John.getGrade();
}
Person.h
using namespace std;
#ifndef PERSON_H_INCLUDED
#define PERSON_H_INCLUDED
#include <iostream>
#include <string>
#include <stdlib.h>
class Person{
private:
string fname;
string lname;
int iq;
public:
Person()
{
fname = "";
lname = "";
iq = 0;
}
Person(string f, string l, int i)
{
fname = f;
lname = l;
iq = i;
}
void setfname(string f)
{
fname = f;
}
string getfname()
{
return fname;
}
void setlname(string l)
{
lname = l;
}
string getlname()
{
return lname;
}
void setIQ(int i)
{
iq = i;
}
int getIQ()
{
return iq;
}
friend ostream& operator<<(ostream& os, const Person &per)
{
os << per.fname<< " " << per.lname
<< " has iq " << per.iq;
}
};
#endif // PERSON_H_INCLUDED
Campus.h
using namespace std;
#ifndef CAMPUS_H_INCLUDED
#define CAMPUS_H_INCLUDED
#include "Person.h"
#include "Student.h"
#include "Global.h"
#include <iostream>
#include <string>
#include <stdlib.h>
#include <math.h>
class Campus{
friend class Student;
private:
char area[25][25];
Dimension bdim;
Dimension cdim;
int trash;
void buildCampus()
{
int trashAmount = (int) floor(0.05 * (cdim.width*cdim.height - bdim.width*bdim.height));
trash = trashAmount;
bool filled = false;
for(int i=0; i<cdim.height; i++)
{
for(int j=0; j<cdim.width; j++)
{
if (i==bdim.height-1 && j==bdim.width-1)
area[i][j] = 'D';
else if(i<bdim.height && j<bdim.width)
area[i][j] = 'B';
else area[i][j] = ' ';
}
}
bool isFac = false;
while(!isFac)
{
int a=(rand() % bdim.height);
int b=(rand() % bdim.width);
if(area[a][b] != 'D')
{
area[a][b] = 'F';
isFac = true;
}
}
while(!filled)
{
int x=rand() % cdim.width;
int y=rand() % cdim.height;
if(area[x][y] == ' ')
{
area[x][y] = 'T';
trashAmount--;
}
if(trashAmount == 0)
filled = true;
}
}
/*
void placeStudent(int x, int y)
{
if(area[x][y] == ' ')
{
area[x][y] = 'S';
//cout<<"placeStudent debug ";
}
}*/
public:
Campus(Dimension c, Dimension b)
{
//bool ok = (b.height < c.height && c.height < 26) && (b.width < c.width && c.width < 26);
//if(ok)
//{
bdim.height=b.height;
bdim.width=b.width;
cdim.height=c.height;
cdim.width=c.width;
for(int i=0; i<25; i++)
{
for(int j=0; j<25; j++)
{
area[i][j] = ' ';
}
}
buildCampus();
//}
}
int numTrash()
{
return trash;
}
/*
int getCampusDimension()
{
return cdim.width;
}
int getBuildingDimension()
{
return bdim.width;
}
*/
friend ostream& operator<<(ostream& os, const Campus &camp)
{
for(int i=0; i<camp.cdim.width; i++)
{
for(int j=0; j<camp.cdim.height; j++)
cout<<camp.area[i][j];
cout<<" ";
}
}
};
#endif // CAMPUS_H_INCLUDED
Global.h
#ifndef GLOBAL_H_INCLUDED
#define GLOBAL_H_INCLUDED
struct Dimension{
int width, height;
};
struct Position{
int pos_x, pos_y;
};
#endif // GLOBAL_H_INCLUDED
Student.h
using namespace std;
#ifndef STUDENT_H_INCLUDED
#define STUDENT_H_INCLUDED
#include "Campus.h"
#include "Trash.h"
#include <iostream>
#include <string>
#include <stdlib.h>
class Student: public Person{
friend class Campus;
friend class Trash;
private:
string fname;
string lname;
Position pos;
int iq;
int grade;
Trash pockets[10];
public:
Student()
{
fname = "Bob";
lname = "Doherty";
iq = rand() % 20 + 50;
pos.pos_x = 0;
pos.pos_y = 0;
for(int i=0; i<10; i++)
{
pockets[i].removeTrash();
//cout<<pockets[i];
}
}
Student(string f, string l, int q)
{
fname = f;
lname = l;
//pos.pos_x = p.pos_x;
//pos.pos_y = p.pos_y;
pos.pos_x = 0;
pos.pos_y = 0;
iq = q;
for(int i=0; i<10; i++)
{
pockets[i].removeTrash();
//cout<<pockets[i];
}
}
void go_to_campus(Campus& c)
{
bool emptySpace = false;
if(pos.pos_x == 0 && pos.pos_y == 0)
while(!emptySpace)
{
int x=rand() % c.cdim.width;
int y=rand() % c.cdim.height;
if(c.area[x][y] == ' ')
{
pos.pos_x = x;
pos.pos_y = y;
c.area[x][y] = 'S';
emptySpace = true;
}
}
else c.area[pos.pos_x][pos.pos_y];
//cout<<"go_to_campus debug";
}
void setIQ(int i)
{
iq = i;
}
int getIQ()
{
return iq;
}
void move(Campus& c)
{
if (c.numTrash() == 0)
{
/*int x=rand() % c.cdim.width;
int y=rand() % c.cdim.height;
c.area[x][y]='G';*/
c.area[pos.pos_x][pos.pos_y] = ' ';
int diffX = pos.pos_x - c.bdim.width;
int diffY = pos.pos_y - c.bdim.height;
if(diffX == diffY)
{
pos.pos_x--;
pos.pos_y--;
}
else if(diffX == 0 && diffY > 0)
pos.pos_y--;
else if(diffX == 0 && diffY < 0)
pos.pos_y++;
else if(diffX > 0 && diffY == 0)
pos.pos_x--;
else if(diffX < 0 && diffY == 0)
pos.pos_x++;
else if (diffX > 0 && diffY < 0)
{
pos.pos_x--;
pos.pos_y++;
}
else if(diffX < 0 && diffY > 0)
{
pos.pos_x++;
pos.pos_y--;
}
if(c.area[pos.pos_x][pos.pos_y] == 'D')
cout<<"AT DOOR";
c.area[pos.pos_x][pos.pos_y] = 'S';
//cout<<"move debug ";
}
else
{
c.area[pos.pos_x][pos.pos_y] = ' ';
int i = rand() % 8 + 1;
loop:
i = rand() % 8+ 1;
if(c.numTrash()>0)
switch(i)
{
case 1:
if(pos.pos_x>0)
{
pos.pos_x--;
}
else
{
goto loop;
}
break;
case 2:
if(pos.pos_y>0)
{
pos.pos_y--;
}
else
{
i = rand() % 8+ 1;
goto loop;
}
break;
case 3:
if(pos.pos_x<c.cdim.width-1)
{
pos.pos_x++;
}
else
{
i = rand() % 8+ 1;
goto loop;
}
break;
case 4:
if(pos.pos_y<c.cdim.height-1)
{
pos.pos_y++;
}
else
{
i = rand() % 8+ 1;
goto loop;
}
break;
case 5:
if(pos.pos_x>0 && pos.pos_y<c.cdim.height-1)
{
pos.pos_x--;
pos.pos_y++;
}
else
{
i = rand() % 8+ 1;
goto loop;
}
break;
case 6:
if(pos.pos_x<c.cdim.width-1 && pos.pos_y>0)
{
pos.pos_x++;
pos.pos_y--;
}
else
{
i = rand() % 8+ 1;
goto loop;
}
break;
case 7:
if(pos.pos_x<c.cdim.width-1 && pos.pos_y<c.cdim.height-1)
{
pos.pos_x++;
pos.pos_y++;
}
else
{
i = rand() % 8+ 1;
goto loop;
}
break;
case 8:
if(pos.pos_x>0 && pos.pos_y>0)
{
pos.pos_x--;
pos.pos_y--;
}
else
{
i = rand() % 8+ 1;
goto loop;
}
break;
}
if(c.area[pos.pos_x][pos.pos_y] == 'T')
{
int index = getNumTrash();
pockets[index] = Trash();
if(pockets[index].name == "bomb")
{
iq -= 2;
cout<<"BOOM! ";
pockets[index].removeTrash();
}
else speak();
c.trash--;
c.area[pos.pos_x][pos.pos_y] = 'S';
grade += pockets[index].value;
}
else if(c.area[pos.pos_x][pos.pos_y] == ' ')
c.area[pos.pos_x][pos.pos_y] = 'S';
else
{
i = rand() % 8+ 1;
goto loop;
}
}
}
int getNumTrash()
{
int num =0;
for(int i=0; i<10; i++)
if(pockets[i].name != " ")
num++;
return num;
}
int getGrade()
{
return grade;
}
void speak()
{
cout<<fname<<": I found a "<<pockets[getNumTrash()-1].name<<"! ";
}
friend ostream& operator<<(ostream& os, const Student &stu)
{
os << stu.fname << " " << stu.lname
<< " has iq " << stu.iq
<< " and is at (" << stu.pos.pos_x<< ", "<< stu.pos.pos_y<< ")";
}
};
#endif // STUDENT_H_INCLUDED
Trash.h
using namespace std;
#ifndef TRASH_H_INCLUDED
#define TRASH_H_INCLUDED
#include "Campus.h"
#include "Student.h"
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
class Trash
{
friend class Student;
friend class Campus;
private:
string name;
int value;
public:
Trash()
{
fstream file;
string input;
char temp[10];
file.open("trash.txt");
getline(file,input);
strcpy(temp,input.c_str());
int length = atoi(temp);
string tnames[length];
int tvals[length];
int looper = 0;
while(!file.eof())
{
getline(file,input,' ');
tnames[looper] = input;
getline(file,input);
strcpy(temp,input.c_str());
tvals[looper] = atoi(temp);
looper++;
}
file.close();
int index = rand() % 10;
name = tnames[index];
value = tvals[index];
//cout<<name<<value;
}
void removeTrash()
{
name = " ";
value = 0;
}
friend ostream& operator<<(ostream& os, const Trash &tr)
{
cout<<tr.name<<" "<<tr.value<<" ";
}
};
#endif // TRASH_H_INCLUDED