1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
#include <sstream>
#include <memory>
using namespace std;
typedef vector<char> vchar;
typedef vector<int> vint;
class Shape
{
protected:
int instructionList[10];
int rowPoint;
int colPoint;
int colAddress;
int seed;
vector<vchar> box;
vector<vint> unoccupiedSpots;
public:
virtual void fixShape() {};
Shape();
virtual ~Shape();
void setInstruction();
void setBox();
void randRow(int &r, int &seed, vector<vint> vect);
void randCol(int &c, int &add, int n, vector<vint> vect);
void TEST();
bool boundaryCheck(int shapeHeight, int shapeLength, int randH, int randL);
};
Shape::Shape() : seed(0)
{}
Shape::~Shape()
{}
void Shape::setInstruction() //Instruction List[i]:
{ // 1. Length of Box [0]
int temp; // 2. Height of Box [1]
ifstream inFile; // 3. Maximum number of attempts [2]
inFile.open("Batch.txt"); // 4. Number of Squares [3]
// 5. Length of Square [4]
if (!inFile) // 6. Number of Rectangles [5]
{ // 7. Length of Rectangle [6]
cerr << "Unable to open file."; // 8. Height of Rectangle [7]
exit(1); // 9. Number of Triangles [8]
} // 10. Height of Triangle [9]
for(int i=0;i<10;i++)
inFile >>instructionList[i];
inFile.close();
}
void Shape::randRow(int &r, int &seed, vector<vint> vect)
{
srand(seed++);
r=rand()%vect.size();
}
void Shape::randCol(int &c, int &add, int n, vector<vint> vect)
{
srand(1);
add=rand()%vect[n].size();
c=vect[n][add];
}
void Shape::setBox()
{
vchar tempchar;
vint tempint;
for(int i=0;i<instructionList[0];i++)
{
tempchar.push_back('.');
}
for(int j=0;j<instructionList[1];j++)
{
box.push_back(tempchar);
}
for(int j=0;j<instructionList[0];j++)
{
tempint.push_back(j);
}
for(int i=0;i<instructionList[1];i++)
{
unoccupiedSpots.push_back(tempint);
}
}
bool Shape::boundaryCheck(int shapeHeight, int shapeLength, int randH, int randL)
{
if(box[shapeHeight+randH][shapeLength+randL]=='.') //check if shape will be out of bound by its bottom rightmost point
{
return true;
}
else
{
int a=shapeHeight+randH;
unoccupiedSpots[a].erase(unoccupiedSpots[a].begin()+shapeLength+randL);
return false;
}
}
void Shape::TEST()
{
for(int i=0;i<box.size();i++)
{
for(int j=0;j<box[i].size();j++)
{
cout<<box[i][j];
}
cout<<endl;
}
}
class Square : public Shape
{
public:
void fixShape()
{
cout<<"Square here\n";
for(int i=0;i<3;i++)
{
randRow(rowPoint, seed, unoccupiedSpots);
randCol(colPoint, colAddress, rowPoint, unoccupiedSpots);
cout<<"Program stops here";
if(boundaryCheck(3,3,rowPoint,colAddress))
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
box[rowPoint+i][colPoint+j]='A';
unoccupiedSpots[rowPoint+i].erase(unoccupiedSpots[rowPoint+i].begin()+colAddress);
}
}
}
}
};
int main()
{
Square b;
Shape a;
Shape *Shape1=&b;
Shape1->setInstruction();
Shape1->setBox();
Shape1->fixShape();
Shape1->TEST();
return 0;
}
|