The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 2 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Working on code and got stuck on the last two part 4 and 5.
Â
// 4) Test and show overlap
// a) Using the existing function "isOverlapping(Rect const &)", test to see
// if userRect collides with any other Rect objects. If userRect is
// overlapping, draw it with '+' instead '#'.
// b) Create a Rect * pointer that points to the address if the Rect object
// that userRect collides with. It should point at NULL if userRect is
// colliding with no other Rect objects.
// c) Print to the screen the width and height of a Rect object that userRect
// collides with. If no collision is happening, print "no collision"
// instead.
// 5) Array of objects
// a) Replace the Rect objects rect0 and rect1 with an array of 2 Rect
// objects, "rect[2]".
// b) Make sure you replace every remaining "rect0" with "rect[0]", and every
// "rect1" with "rect[1]".
// c) Increase the size of the "rect" array to 5. Make sure all 5 Rect
// objects are randomized, drawn to the screen, and tested for collision.
// d) If you have not already done so, replace
// duplicate-code-using-array-elements with a for-loop. For example:
// If you have:
// rect[0].draw('0');
// rect[1].draw('1');
// rect[2].draw('2');
// rect[3].draw('3');
// rect[4].draw('4');
// Replace it with:
// for(int i = 0; i // {
// rect[i].draw('0'+i);
// }
// Do this where objects are randomized, drawn, and tested for collision
Â
The complete code is as follows:
#define NOMINMAX // prevent Windows API from conflicting with "min" and "max"
#include // C-style output. printf(char*,...), putchar(int)
#include // SetConsoleCursorPosition(HANDLE,COORD)
#include // _getch()
#includeÂ
/**
* moves the console cursor to the given x/y coordinate
* 0, 0 is the upper-left hand coordinate. Standard consoles are 80x24.
* @param x
* @param y
*/
void moveCursor(int x, int y)
{
COORD c = {x,y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
struct Vec2
{
short x, y;
Vec2() : x(0), y(0) { }
Vec2(int x, int y) : x(x), y(y) { }
void operator+=(Vec2 v)
{
x += v.x;
y += v.y;
}
};
class Rect
{
Vec2 min, max;
public:
void setMin(Vec2 const &min)Â
{
this->min.x = min.x;
this->min.y = min.y;
}
void setMax(Vec2 const &max)Â
{
this->max.x = max.x;
this->max.y = max.y;
}
Rect(int minx, int miny, int maxx, int maxy)
:min(minx,miny),max(maxx,maxy)
{}
Rect(){}
void draw(const char letter) const
{
for(int row = min.y; row {
for(int col = min.x; col {
if(row >= 0 && col >= 0)
{
moveCursor(col, row);
putchar(letter);
}
}
}
}
bool isOverlapping(Rect const & r) const
{
return !( min.x >= r.max.x || max.x || min.y >= r.max.y || max.y }
void translate(Vec2 const & delta)
{
min += delta;
max += delta;
}
void setRandom(Rect & r)Â
{
int pos_x, pos_y, height, width;
pos_x = rand() % 51;
pos_y = rand() % 21;
height = 2 + rand() % 11;
width = 2 + rand() % 11;
r.min.x = pos_x - width;
r.min.y = pos_y - height;
r.max.x = pos_x + width;
r.max.y = pos_y + height;
}
void setRandomByPointer(Rect * r)Â
{
int pos_x,pos_y,height, width;
pos_x = rand() % 51;
pos_y = rand() % 21;
height = 2 + rand() % 11;
width = 2 + rand() % 11;
r->min.x = pos_x - width;
r->min.y = pos_y - height;
r->max.x = pos_x + width;
r->max.y = pos_y + height;
}
};
int main()
{
// initialization
Rect * userRect;
userRect = new Rect (7, 5, 10, 9);
Rect rect0;
Rect rect1;
rect0.setRandom(rect1);
rect1.setRandomByPointer(&rect0);
int userInput;
do
{
// draw
rect0.draw('0');
rect1.draw('1');
moveCursor(0, 0);// re-print instructions
printf("move with 'w', 'a', 's', and 'd'");
for (int i = 0; i {
if (userRect->isOverlapping(rect[i]))
{
userRect->draw('+');
}
else userRect->draw('#');
}
// user input
userInput = _getch();
// update
Vec2 move;
switch(userInput)
{
case 'w':move = Vec2( 0,-1);break;
case 'a':move = Vec2(-1, 0);break;
case 's':move = Vec2( 0,+1);break;
case 'd':move = Vec2(+1, 0);break;
}
userRect->draw(' ');// un-draw before moving
userRect->translate(move);
}while(userInput != 27); // escape key
delete userRect;
return 0;
}