Пример #1
0
void UnLoadSolutionRebus(Puzzle * puz)
{
    for (Square * square = puz->GetGrid().First();
         square != NULL;
         square = square->Next())
    {
        square->SetSolutionRebus(string_t(1, char_t(square->GetPlainSolution())));
    }
}
Пример #2
0
bool LoadSolutionRebus(Puzzle * puz,
                       const std::string & table,
                       const std::string & grid)
{
    // NB: In the grid rebus section (GRBS), the index is 1 greater than the
    // index in the rebus table section (RTBL).

    if (grid.size() !=  puz->GetGrid().GetWidth() * puz->GetGrid().GetHeight())
        return false;

    // Read the rebus table (RTBL)
    // Format: index ':' string ';'
    //   - Index is a number, padded to two digits with a space if needed.
    std::map<unsigned char, std::string> rebusTable;

    std::istringstream tstream(table);
    istream_wrapper table_stream(tstream);

    for (;;)
    {
        std::string key;

        // Read the index
        try {
            key = table_stream.ReadString(':');
        }
        catch(std::ios::failure &) {
            break;
        }

        int index = atoi(key.c_str());
        if (index == 0 && key != " 0")
            return false;

        // The index value in the rebus-table section is 1 less than the
        // index in the grid-rebus, so we need add 1 here.
        ++index;

        std::string value = table_stream.ReadString(';');

        rebusTable[static_cast<unsigned char>(index)] = value;
    }
    if (! table_stream.CheckEof())
        return false;


    // Set the grid rebus solution
    std::istringstream gstream(grid);
    istream_wrapper grid_stream(gstream);

    for (Square * square = puz->GetGrid().First();
         square != NULL;
         square = square->Next())
    {
        const unsigned char index = grid_stream.ReadChar();
        if (index > 0)
        {
            // Look for this index in the rebus table
            std::map<unsigned char, std::string>::const_iterator it;
            it = rebusTable.find(index);
            if (it == rebusTable.end())
                return false;

            // Don't overwrite the plain solution
            square->SetSolutionRebus(decode_puz(it->second));
        }
    }
    if (! grid_stream.CheckEof())
        return false;
    return true;
}