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
|
#include <iostream>
#include <cstring>
#include "room.h"
#include "screen.h"
using namespace std;
Room::Room():wallCount(0), name(NULL)
{
} // Room()
Room::~Room()
{
if(name)
delete [] name;
} // ~Room()
ostream &operator<<(ostream &os, const char *filename)
{
ofstream outf(filename, ios::out)
outf>>
outf close();
return os;
} //overloaded << operator
void Room::add()
{
char line[80];
Screen::display();
cout << "Room name: ";
cin.getline(line, 80);
name = new char[strlen(line) + 1];
strcpy(name, line);
do
{
walls[wallCount++].add();
} while(walls[0].getStartX() != walls[wallCount - 1].getEndX()
|| walls[0].getStartY() != walls[wallCount - 1].getEndY());
placeName();
} // add()
void Room::placeName() const
{
int minX = 70, maxX = 0, minY = 70, maxY = 0;
for(int i = 0; i < wallCount; i++)
{
if(walls[i].getStartX() < minX)
minX = walls[i].getStartX();
if(walls[i].getStartX() > maxX)
maxX = walls[i].getStartX();
if(walls[i].getStartY() < minY)
minY = walls[i].getStartY();
if(walls[i].getStartY() > maxY)
maxY = walls[i].getStartY();
} // for each wall
int startX = (minX + maxX + 1) / 2 - (strlen(name) / 2);
int midY = (minY + maxY) / 2;
if(startX < 0)
startX = 0;
for(int i = 0; name[i]; i++)
Screen::setLocation(startX + i, midY, name[i]);
} // placeName()
ifstream& Room::read(ifstream &inf)
{
char line[256], *ptr;
if(!inf.getline(line, 256))
return inf;
ptr = strtok(line, ",");
name = new char[strlen(ptr) + 1];
strcpy(name, ptr);
while(walls[wallCount].read())
wallCount++;
placeName();
return inf;
} // read()
void Room::remove()
{
removeName();
for(int i = 0; i < wallCount; i++)
walls[i].remove();
if(name)
{
delete [] name;
name = NULL;
} // if room has a name
} // remove()
void Room::removeName()
{
int minX = 70, maxX = 0, minY = 70, maxY = 0;
for(int i = 0; i < wallCount; i++)
{
if(walls[i].getStartX() < minX)
minX = walls[i].getStartX();
if(walls[i].getStartX() > maxX)
maxX = walls[i].getStartX();
if(walls[i].getStartY() < minY)
minY = walls[i].getStartY();
if(walls[i].getStartY() > maxY)
maxY = walls[i].getStartY();
} // for each wall
int startX = (minX + maxX + 1) / 2 - (strlen(name) / 2);
int midY = (minY + maxY) / 2;
if(startX < 0)
startX = 0;
for(int i = 0; name[i]; i++)
Screen::remove(startX + i, midY, name[i]);
} // removeName()
void Room::clear()
{
wallCount = 0;
name = NULL;
} // setName()
ofstream& Room::write(ofstream &outf)
{
outf << name;
for(int i = 0; i < wallCount; i++)
walls[i].write(outf);
outf << endl;
return outf;
} // write()
|