The document is a code snippet that defines classes for different shapes (O, I, L, S, X, U) used in a program that places blocks in different letter shapes on a grid. The code provides constructors for each shape that initialize the x and y coordinate arrays for each shape. It also includes functions to get the name and size of each shape, as well as a function to check if two shapes overlap.
I am writing a program that places bolcks on a grid the the sape of .pdf
1. I am writing a program that places bolcks on a grid the the sape of different letters. It can also
move the blocks and checks if they overlap. I have most of the test cases figured out except for
four. Can someone help me out?
Thanks in advance.
output:
input:
output:
input:
output:
I CAN ONLY MODIFY ONE FILE SHAPE.CPP
#ifndef SHAPE_CPP
#define SHAPE_CPP
#include
#include
#include "Shape.h"
using namespace std;
// Constructor for O
O::O(int posx, int posy)
{
x = new int[1];
y = new int[1];
x[0] = posx;
y[0] = posy;
}
char O::name() const
{
return 'O';
}
int O::size() const
{
return 1;
}
// Constructor for I
I::I(int posx, int posy)
{
2. x = new int[2];
y = new int[2];
x[0] = x[1] = posx;
y[0] = posy;
y[1] = posy+1;
}
char I::name() const
{
return 'I';
}
int I::size() const
{
return 2;
}
// Constructor for L
L::L(int posx, int posy)
{
x = new int[3];
y = new int[3];
x[0] = x[2] = posx;
y[0] = y[1] = posy;
x[1] = posx+1;
y[2] = posy+1;
}
char L::name() const
{
return 'L';
}
int L::size() const
{
return 3;
}
// Constructor for S
S::S(int posx, int posy)
{
3. x = new int[4];
y = new int[4];
x[0] = posx;
x[1] = posx+1;
x[2] = posx+1;
x[3] = posx+2;
y[0] = y[1] = posy;
y[2] = y[3] = posy+1;
}
char S::name() const
{
return 'S';
}
int S::size() const
{
return 4;
}
/*
Constructor for X
the constructor initialises the cell co-ordinates
X is spread across 5 cells
*
* * *
*
The Numbering of cells is done from bottom-top and left to right
(x[0], y[0]) is the position of bottom most cell
(x[1], y[1]) is the position of left most cell of the 2nd row from bottom
(x[2], y[2]) is the position of the middle cell in the 2nd row from bottom
....
*/
X::X(int posx, int posy)
{
x = new int[5];
y = new int[5];
x[0] = x[2] = x[4] =posx;
x[1] = posx-1;
4. x[3] = posx+1;
y[0] = posy;
y[1] = y[2] = y[3] = posy+1;
y[4] = posy+2;
}
/*
This function returns the name of type of object i.e it is a X, or L, or U ...
*/
char X::name() const
{
return 'X';
}
int X::size() const
{
return 5;
}
// Constructor for U
U::U(int posx, int posy)
{
x = new int[7];
y = new int[7];
x[0] = x[3] = x[5] = posx;
x[1] = posx+1;
x[2] = x[4] = x[6] = posx+2;
y[0] = y[1] = y[2] = posy;
y[3] = y[4] = posy+1;
y[5] = y[6] = posy+2;
}
char U::name() const
{
return 'U';
}
int U::size() const
{
return 7;
}
5. void Shape::print() const
{
//get the size of the object i.e no of cells across which this is spread
int sz = size();
// get the type of object i.e O, L, X...
char n = name();
cout< > st;
int sz1 = t.size();
int sz2 = size();
for(int i = 0 ; i < sz1 ; i++)
{
st.insert(make_pair(t.x[i], t.y[i]));
}
for(int i = 0 ; i < sz2 ; i++)
{
if(st.find(make_pair(x[i], y[i]))!= st.end())
return true;
}
return false;
}
/*
This function creates a new object of type ch
*/
Shape * Shape::makeShape(char ch, int posx, int posy)
{
if(ch == 'O')
return new O(posx, posy);
else if(ch == 'I')
return new I(posx, posy);
else if(ch == 'L')
return new L(posx, posy);
else if(ch == 'S')
return new S(posx, posy);
else if(ch == 'X')
return new X(posx, posy);
else if(ch == 'U')
6. return new U(posx, posy);
else
throw std::invalid_argument("invalid type:");
}
// base class virtual destructor
Shape::~Shape()
{
delete [] x;
delete [] y;
}
#endif
//END OF FILE
//
// testShape.cpp
//
#include "Shape.h"
#include
#include
using namespace std;
int main()
{
Shape *t1, *t2;
char ch;
int x,y;
try
{
cin >> ch >> x >> y;
t1 = Shape::makeShape(ch,x,y);
t1->print();
cin >> ch >> x >> y;
t2 = Shape::makeShape(ch,x,y);
t2->print();
t2->move(1,-1);
t2->print();
if ( t1->overlap(*t2) )
cout << "overlap" << endl;
7. else
cout << "no overlap" << endl;
delete t1;
delete t2;
}
catch ( invalid_argument &exc )
{
cout << exc.what() << ": " << ch << " " << x << " " << y << endl;
}
}
//
// Shape.h
//
#ifndef SHAPE_H
#define SHAPE_H
class Shape
{
public:
virtual ~Shape(void);
virtual char name(void) const = 0;
virtual int size(void) const = 0;
void print(void) const;
void move (int dx, int dy);
bool overlap(const Shape &t) const;
static Shape *makeShape(char ch,int posx,int posy);
protected:
int *x, *y;
};
class O: public Shape
{
public:
O(int posx, int posy);
virtual char name(void) const;
virtual int size(void) const;
};
class I: public Shape
8. {
public:
I(int posx, int posy);
virtual char name(void) const;
virtual int size(void) const;
};
class L: public Shape
{
public:
L(int posx, int posy);
virtual char name(void) const;
virtual int size(void) const;
};
class S: public Shape
{
public:
S(int posx, int posy);
virtual char name(void) const;
virtual int size(void) const;
};
class X: public Shape
{
public:
X(int posx, int posy);
virtual char name(void) const;
virtual int size(void) const;
};
class U: public Shape
{
public:
U(int posx, int posy);
virtual char name(void) const;
virtual int size(void) const;
};
#endif
9. Solution
#ifndef SHAPE_CPP
#define SHAPE_CPP
#include
#include
#include "Shape.h"
using namespace std;
// Constructor for O
O::O(int posx, int posy)
{
x = new int[1];
y = new int[1];
x[0] = posx;
y[0] = posy;
}
char O::name() const
{
return 'O';
}
int O::size() const
{
return 1;
}
// Constructor for I
I::I(int posx, int posy)
{
x = new int[2];
y = new int[2];
x[0] = x[1] = posx;
y[0] = posy;
y[1] = posy+1;
}
char I::name() const
{
return 'I';
10. }
int I::size() const
{
return 2;
}
// Constructor for L
L::L(int posx, int posy)
{
x = new int[3];
y = new int[3];
x[0] = x[2] = posx;
y[0] = y[1] = posy;
x[1] = posx+1;
y[2] = posy+1;
}
char L::name() const
{
return 'L';
}
int L::size() const
{
return 3;
}
// Constructor for S
S::S(int posx, int posy)
{
x = new int[4];
y = new int[4];
x[0] = posx;
x[1] = posx+1;
x[2] = posx+1;
x[3] = posx+2;
y[0] = y[1] = posy;
y[2] = y[3] = posy+1;
}
11. char S::name() const
{
return 'S';
}
int S::size() const
{
return 4;
}
/*
Constructor for X
the constructor initialises the cell co-ordinates
X is spread across 5 cells
*
* * *
*
The Numbering of cells is done from bottom-top and left to right
(x[0], y[0]) is the position of bottom most cell
(x[1], y[1]) is the position of left most cell of the 2nd row from bottom
(x[2], y[2]) is the position of the middle cell in the 2nd row from bottom
....
*/
X::X(int posx, int posy)
{
x = new int[5];
y = new int[5];
x[0] = x[2] = x[4] =posx;
x[1] = posx-1;
x[3] = posx+1;
y[0] = posy;
y[1] = y[2] = y[3] = posy+1;
y[4] = posy+2;
}
/*
This function returns the name of type of object i.e it is a X, or L, or U ...
*/
char X::name() const
12. {
return 'X';
}
int X::size() const
{
return 5;
}
// Constructor for U
U::U(int posx, int posy)
{
x = new int[7];
y = new int[7];
x[0] = x[3] = x[5] = posx;
x[1] = posx+1;
x[2] = x[4] = x[6] = posx+2;
y[0] = y[1] = y[2] = posy;
y[3] = y[4] = posy+1;
y[5] = y[6] = posy+2;
}
char U::name() const
{
return 'U';
}
int U::size() const
{
return 7;
}
void Shape::print() const
{
//get the size of the object i.e no of cells across which this is spread
int sz = size();
// get the type of object i.e O, L, X...
char n = name();
cout< > st;
int sz1 = t.size();
int sz2 = size();
13. for(int i = 0 ; i < sz1 ; i++)
{
st.insert(make_pair(t.x[i], t.y[i]));
}
for(int i = 0 ; i < sz2 ; i++)
{
if(st.find(make_pair(x[i], y[i]))!= st.end())
return true;
}
return false;
}
/*
This function creates a new object of type ch
*/
Shape * Shape::makeShape(char ch, int posx, int posy)
{
if(ch == 'O')
return new O(posx, posy);
else if(ch == 'I')
return new I(posx, posy);
else if(ch == 'L')
return new L(posx, posy);
else if(ch == 'S')
return new S(posx, posy);
else if(ch == 'X')
return new X(posx, posy);
else if(ch == 'U')
return new U(posx, posy);
else
throw std::invalid_argument("invalid type:");
}
// base class virtual destructor
Shape::~Shape()
{
delete [] x;
delete [] y;