Exemplo n.º 1
0
int doTurn(char guess, char **answer) {
   int c;
   match("\n");
   printBoard(0);
   match("used:      \"");
   int i = 0;
   do {
      c = getchar();
      if (c == EOF) {
         _terminate(13);
      }
      used[i++] = c;
   } while (c != '"');
   used[--i] = 0;
   match("\navailable: \"");

   i = 0;
   do {
      c = getchar();
      if (c == EOF) {
         _terminate(14);
      }
      avail[i++] = c;
   } while (c != '"');
   avail[--i] = 0;

   match("\n\ncurrent: ");

   i = 0;
   do {
      c = getchar();
      if (c == EOF) {
         _terminate(15);
      }
      current[i++] = c;
   } while (c != '\n');
   current[--i] = 0;

   // int len;
   match("\nYour guess: ");

   checked_transmit(1, &guess, 1);
   checked_transmit(1, "\n", 1);

   return makeGuess(answer);
   
}
Exemplo n.º 2
0
void Converter::convertSingleDocQuick( const string_t& fileName )
{
	tDocumentsSp docs = word()->getDocuments();
    tDocumentSp  doc  = docs->open(toUtf16(getInputAbsPath(fileName)));
    if (!doc) {
        logError(logger(), "Error while opening document: " + fileName);
        return;
    }

    tCharMappingSp cm;
    string_t       fontName, newFontName;
    wstring_t      text, textUnicode, docAsText;
    int            c = 0;

	tSelectionSp s = word()->getSelection();
    int pos = 0;
    int totalCharsQty = s->getStoryLength();

    do {
        s->setStart(pos);
        s->setEnd(pos + 1);
        s->selectCurrentFont();
        fontName = s->getFont()->getName();

        if ( canSkipFont(fontName) ) {
            //s->getFont()->haveCommonAttributes();
            pos = s->getEnd();
            docAsText += s->getText();
            std::cout << "\r" << percentageStr(pos, totalCharsQty - 1);
            continue;
        }

        text = s->getText();
        if ( fontName.empty() ) {
            saveSelection(s);
            fontName = makeGuess(s);            
            restoreSelection(s);

            /// if after all we have empty font name, log about that event
            /// and go forward
            if (fontName.empty()) {
                logError(logger(), "EMPTY FONT NAME: Investigate");
                pos = s->getEnd();
                docAsText += text;
                std::cout << "\r" << percentageStr(pos, totalCharsQty - 1);
                continue;
            }
        }

        /// use mapping
        textUnicode.clear();
        cm = getCM(fontName);
        if (cm) {
            bool spacingOnly = cm->doConversion(text, textUnicode, fontName);
            newFontName = getFontSubstitution(cm, fontName);
            //tFontSp fontDup = s->getFont()->duplicate();
            s->setText(textUnicode);
            //s->getFont()->haveCommonAttributes();
            s->getFont()->setName(newFontName);
            //s->setFont(fontDup);
        }

        /// extract text from the document as well
        docAsText += textUnicode;
        pos = s->getEnd();

        std::cout << "\r" << percentageStr(pos, totalCharsQty - 1);
    } while ( pos < totalCharsQty - 1 );


    /// -------------------------------------------///
    /// now save result in the appropriate folder  ///
    string_t outputDir = getOutputAbsPath(fileName);
    Poco::File(outputDir).createDirectories();
    Poco::Path p(fileName);
    doc->saveAs( outputDir + p.getBaseName() + " QUICK." + p.getExtension() );
    doc->close();

    if ( config_->getBool("app.saveAlsoAsUTF8", false) )
        writeFileAsBinary( outputDir + p.getBaseName() + " UTF8 QUICK.txt", toUtf8(docAsText));
}
Exemplo n.º 3
0
// The recursive solver will solve any valid board. 
// Returns TRUE If the board is solved.
// Returns FALSE if the board is not solved and all appropriate guesses have been exhausted.
bool Sudoku::recursiveSolver( /*int depth*/ )
{
	Sudoku original = *this;
	Sudoku child;
	Square saved;
	int guess = 0;
	bool validGuess;

	// Solve the board as much as possible deterministically.
	deterministic();

	// Save a copy of the board so that changes made from invalid
	// guesses can be reverted to the original parent.
	Sudoku parent = *this;

//	// Display the depth and current board state.
//	cout << "depth: " << depth << endl;
//	print();

	// If the board is invalid return false.
	if( !isSolvable() )
	{
		return false;
	}

	// If the board is solved return true.
	if( isSolved() )
	{
		return true;
	}

	// If the board is still valid but not solved we need to make guesses.
	else
	{
		int iter = 0;
		do
		{
			// Make a guess of the lowest possible value in the first empty cell
			// Save the cell where the guess is.
			saved = makeGuess();

			// If we have exhausted the guesses in the first empty cell then
			// exit since this is no longer a valid board. 
			if( saved.row == -1 )
			{
				break;
			}

			// Save the value of the guess made in the "Square saved" cell.
			guess = cell[saved.row][saved.col];

			// The recursiveSolver() will return whether the board is solved after making that guess.
			validGuess = recursiveSolver( /*depth + 1*/ );

			// If it solved it return true.
			if( validGuess )
			{
				return true;
			}

			// If the board was not solved then the guess was not valid.
			// Revert the board to the parent.
			// Remove guess from the possible values in the saved cell.
			// Update parent so it remembers the guesses we have already tried.
			else
			{
				*this = parent;
				possible[saved.row][saved.col][guess] = false;
				parent = *this;
			}
		}while( !validGuess && saved.row != -1 && iter++ < 9 );
		// Some redundancies in the while loop exit criteria
		// Do this while the last guess was bad and there are still
		// more guesses to make.
		// Also never loop more than 9 guesses. ( redundant, should never catch )
	}

	// If we pass all of that then the board had all the guesses exhausted in a particular cell.
	// This means we have a cell with no possible values. 
	// Revert to the parent board.
	// Exit and pass back up to the calling function.
	// If this was called recursively then the next level down will make the next appropriate guess in 
	// the last cell where a guess was made.
	*this = original;
	return false;
}