Пример #1
0
/**
 * Saves the board to a file
 *
 * @param fname
 *  Name of file to save to
 * @return
 *  True if board is successfully saved
 */
bool Board::Export(const string & fname) const {
    FileIO file;

    FILEIO_TABLE table;
    for (int i = 0; i < 9; ++i) {
        FILEIO_ROW row;
        for (int j = 0; j < 9; ++j) {
            char s[2]; // Because it needs to be added as a char*
            s[0] = m_board[j][i] + '0';
            s[1] = '\0';
            row.push_back(s); // Fill a row
        }
        table.push_back(row); // Add the row to the table w're going to save
    }

    file.SetFileName(fname); // Prepare FileIO object for saving.
    file.SetTable(table);

    return file.Save(); // Save returns true if all is successful.
}
Пример #2
0
/**
 * Loads a saved board from a file
 *
 * @param fname
 *  Name of the file to load
 * @return
 *  True if saved board is found and successfully loaded
 */
bool Board::Import(const string & fname) {
    FileIO file;

    file.SetFileName(fname);
    file.Open();
    FILEIO_TABLE table = file.GetTable();

    if (table.size() == 9) {
        FILEIO_TABLE::iterator tableIt = table.begin();
        FILEIO_ROW::iterator rowIt = tableIt->begin();

        for (int i = 0; i < 9; ++i) {
            for (int j = 0; j < 9; ++j) { // Read a row from the table
                m_board[j][i] = atoi(rowIt->c_str());
                rowIt++;
            }
            tableIt++;
            rowIt = tableIt->begin();
        }
        return true;
    }
    else
        return false;
}