#include #include #include #include #include #include #include "libacrosslite.h" AcrossLiteClue::AcrossLiteClue () { _number = 0; _clue = ""; } AcrossLiteClue::AcrossLiteClue (const int number, const string& clue) { _number = 0; _clue = ""; set (number, clue); } AcrossLiteClue::AcrossLiteClue (const AcrossLiteClue& src) { _number = 0; _clue = ""; *this = src; } AcrossLiteClue::~AcrossLiteClue () { } AcrossLiteClue& AcrossLiteClue::operator= (const AcrossLiteClue& rhs) { set (rhs.number(), rhs.clue()); return *this; } bool AcrossLiteClue::operator< (const AcrossLiteClue& rhs) { return (number() < rhs.number()); } bool AcrossLiteClue::operator== (const AcrossLiteClue& rhs) { return (number() == rhs.number()); } ostream& operator<< (ostream& os, const AcrossLiteClue& rhs) { os << rhs.number() << ") " << rhs.clue(); return os; } void AcrossLiteClue::set (const int number, const string& clue) { _number = number; _clue = clue; } int AcrossLiteClue::number () const { return _number; } const string& AcrossLiteClue::clue () const { return _clue; } AcrossLiteGrid::AcrossLiteGrid () { resize (0,0); } AcrossLiteGrid::AcrossLiteGrid (const int nColumns, const int nRows) { resize(nColumns, nRows); } AcrossLiteGrid::AcrossLiteGrid (const AcrossLiteGrid& src) { *this = src; } AcrossLiteGrid::~AcrossLiteGrid () { } AcrossLiteGrid& AcrossLiteGrid::operator= (const AcrossLiteGrid& rhs) { _nColumns = rhs._nColumns; _nRows = rhs._nRows; _cells = rhs._cells; return *this; } ostream& operator<< (ostream& os, const AcrossLiteGrid& rhs) { for (int r = 0; r 25) { return; } if (nRows() < 1 || nRows() > 25) { return; } // // Read the the Solution and populate the Solution grid. // for (int r=0; r") != 0) { return; } // Read 'title' line. str = _readString(fh); if (str.find("") != 0) { return; } _puzzleName = _readString(fh); // Read 'author' line. str = _readString(fh); if (str.find("<AUTHOR>") != 0) { return; } _authorName = _readString(fh); // Read 'copyright' line. str = _readString(fh); if (str.find("<COPYRIGHT>") != 0) { return; } _copyright = _readString(fh); // Read 'size' line. str = _readString(fh); if (str.find("<SIZE>") != 0) { return; } int nr = 0; int nc = 0; str = _readString(fh); (void)sscanf (str.c_str(), "%dx%d", &nr, &nc); if (nr == 0 || nc == 0) { return; } _diagram.resize(nc,nr); _solution.resize(nc,nr); _numbers.resize(nc,nr); if (nColumns() < 1 || nColumns() > 25) { return; } if (nRows() < 1 || nRows() > 25) { return; } // Read the the Solution and populate the Solution grid. str = _readString(fh); if (str.find("<GRID>") != 0) { return; } for (int r=0; r<nRows(); r++) { str = _readString(fh); for (int c=0; c<nColumns(); c++) { char letter = str[c]; _solution.setCell(c,r,letter); if (letter == '.') { _diagram.setCell(c,r, '.'); }else{ _diagram.setCell(c,r, ' '); } } } // Calculate the cell numbers. _calculateCellNumbers(); // Read the 'across' clues. str = _readString(fh); if (str.find("<ACROSS>") != 0) { return; } for (int r=0; r<nRows(); r++) { for (int c=0; c<nColumns(); c++) { int number = cellNumber (c,r); if (number == 0) continue; // Try an 'across' if (c == 0 || diagramCell(c-1,r) == '.') { string clueText = _readString(fh); if (clueText.size() == 0) { cout << "Unexepected EOF while reading clues." << endl; } AcrossLiteClue c(number, clueText); _acrossClues.push_back(c); } } } // Read the 'down' clues. str = _readString(fh); if (str.find("<DOWN>") != 0) { return; } for (int r=0; r<nRows(); r++) { for (int c=0; c<nColumns(); c++) { int number = cellNumber (c,r); if (number == 0) continue; // Try a 'down' if (r == 0 || diagramCell(c,r-1) == '.') { string clueText = _readString(fh); if (clueText.size() == 0) { cout << "Unexepected EOF while reading clues." << endl; } AcrossLiteClue c(number, clueText); _downClues.push_back(c); } } } // Close the file. int stat = ::close (fh); if (stat < 0) { return; } } void AcrossLiteTxtPuzzle::save (const string& /*filename*/) { cout << "Sorry, saving to .txt format is not implemented yet." << endl; } void AcrossLiteTxtPuzzle::copy (const AcrossLitePuzzleBase& /*rhs*/) { cout << "Sorry, copying to .txt format is not implemented yet." << endl; } void AcrossLiteTxtPuzzle::_init () { _diagram.resize(0,0); _solution.resize(0,0); _numbers.resize(0,0); _acrossClues.resize(0); _downClues.resize(0); _puzzleName = ""; _authorName = ""; _copyright = ""; } void AcrossLiteTxtPuzzle::_calculateCellNumbers() { _numbers.resize(nColumns(), nRows()); int number = 1; for (int r=0; r<nRows(); r++) { for (int c=0; c<nColumns(); c++) { if (diagramCell(c,r) == '.') continue; if (r == 0 || c == 0) { _numbers.setCell(c,r,number++); }else if (diagramCell(c,r-1) == '.') { _numbers.setCell(c,r,number++); }else if (diagramCell(c-1,r) == '.') { _numbers.setCell(c,r,number++); }else{ _numbers.setCell(c,r,0); } } } } string AcrossLiteTxtPuzzle::_readString (int fh) { string s; while (1) { char letter; int stat = ::read (fh, &letter, 1); if (stat != 1) break; if (letter == 0) break; if (letter == '\n') break; if (letter == '\t') continue; s += letter; } return s; }