Exemplo n.º 1
0
void Recognizer::parseFile(const QString & infileName, const QString & outfileName)
{
    QFile inFile(infileName);
    if (!inFile.open(QIODevice::ReadOnly | QIODevice::Text))
        return;

    QFile outFile(outfileName);
    if (!outfileName.isEmpty() && !outFile.open(QIODevice::WriteOnly | QIODevice::Text))
        return;

    // use textstream instead of direct file reading for prevent
    // problems with different RETURN symbol on different platforms
    // textstream always returns trimmed string
    QTextStream in(&inFile);
    QTextStream out(&outFile);

    QStringList lineList;
    QString line;

    do {
        line = in.readLine();
        lineList.append(line);

        // 4 lines read so starting to process them
        if (lineList.count() == 4) {

            QString accountNumber = decodeValue(lineList);
            ErrorCode code = checkSum(accountNumber);

            // correct errors if possible
            QStringList possibleVariants;
            if (code == ILL || code == ERR) {
                code = errorCorrection(lineList, &possibleVariants);
            }

            QString formattedString;

            // was value corrected or or it's iginal
            if (possibleVariants.size() == 1)
                formattedString.append(possibleVariants[0]);
            else
                formattedString.append(accountNumber);

            // status
            if (code == AMB) {
                formattedString += "  " + errorToString(code) + "  [\'" + possibleVariants.join("\',\'") + "\']";
            } else
                formattedString += "  " + errorToString(code);


            if (outfileName.isEmpty())
                std::cout << formattedString.toStdString() << std::endl;
            else
                out << formattedString << "\n";

            lineList.clear();
            possibleVariants.clear();
        }
    } while(!line.isNull());

    inFile.close();
    if (outFile.isOpen())
        outFile.close();
}
Exemplo n.º 2
0
void generate()
   {
//--------------------------------------------------------------
//initialize
//--------------------------------------------------------------
   int x;
   int y;
   int neighbors[MAXELEVVAL];
   int a;
   int b;
   int tempElevation;
   int nextElevation;
   int special;

   mapCornerMaxG = totalMapSizeG/2;
   mapMiddleMaxG = mapCornerMaxG - 1;

   for (y = 0; y <= mapCornerMaxG; ++y) //initialise map to UNASSIGNED tiles
      for (x = 0; x <= mapCornerMaxG; ++x)
         {
         mapCorner[x][y] = UNASSIGNED;
         mapMiddle[x][y] = UNASSIGNED;
         }
   if (advancedMirrorOK_G)
      mirrorTypeG = pickFrom4(NORMALMIRROR,1, XOFFSETMIRROR,2, YOFFSETMIRROR,2, MAXDISTMIRROR, 4);
   else
      mirrorTypeG = NORMALMIRROR;
   getSpecials(); //get start and pools

//--------------------------------------------------------------
// loop through each square
//--------------------------------------------------------------
   mapCorner[0][0] = pickFrom3(LOW,1, MEDIUM,(terrainSeedFlagG < 9), HIGH,(terrainSeedFlagG < 8)); //seed
//   mapCorner[0][0] = LOW;//HIGH;//MEDIUM;//
   for (y = 0; y <= mapCornerMaxG; ++y) //fill in the rest of the random map
      for (x = 0; x <= mapCornerMaxG; ++x)
         {
         special = mapMiddle[x][y]; //water wouldn't have been assigned yet, so must be pool, start, or UNASSIGNED

//--------------------------------------------------------------
// check neighbors
//--------------------------------------------------------------
         if ((mapCorner[x][y] != UNASSIGNED)&&(mapCorner[x][y] != LOW_OR_WATER))
            nextElevation = mapCorner[x][y]; //already defined because of a special or (0,0), so no change
         else
            {
            neighbors[HIGH] = 0;
            neighbors[MEDIUM] = 0;
            neighbors[LOW] = 0;
            neighbors[WATER] = 0;

            if (x > 0)
               {
               a = mapCorner[x-1][y];
               if((y > 1)&&(mapMiddle[x-1][y-2] == WATER))
                  ++neighbors[WATER];
               if (y > 0)
                  neighbors[mapCorner[x-1][y-1]] += 3;
               }
            else
               a = mapCorner[x][y-1];
//               {
//               a = mapCorner[mapCornerMaxG][y-1];
//               if((y > 1)&&(mapMiddle[mapCornerMaxG][y-2] == WATER))
//                  ++neighbors[WATER];
//               }
            neighbors[a] += 3;
            if (y > 0)
               {
               b = mapCorner[x][y-1];
               neighbors[b] += 3;
               if (x < mapCornerMaxG)
                  {
                  ++neighbors[mapCorner[x+1][y-1]]; //so this value can be ignored when choosing water
                  if ((special != UNASSIGNED)&&(x < mapCornerMaxG-1))
                     ++neighbors[mapCorner[x+2][y-1]];
//                  else
//                     ++neighbors[mapCorner[0][y-1]];
                  }
//               else
//                  ++neighbors[mapCorner[0][y-1]];
               if((x > 1)&&(mapMiddle[x-2][y-1] == WATER))
                  ++neighbors[WATER];
               }
            else
               b = mapCorner[x-1][y]; // for probability equations for edges

//--------------------------------------------------------------
// pick new elevation
//--------------------------------------------------------------
            //neighbors                  possible new elevation
            //HIGH or HIGH with MEDIUM   HIGH or MEDIUM
            //MEDIUM only                HIGH, MEDIUM or LOW
            //LOW or WATER only          MEDIUM, LOW or WATER
            //MEDIUM with LOW or WATER   MEDIUM or LOW, possible WATER if no MEDIUM left, down, or down-left
            //HIGH with LOW or WATER     MEDIUM
#define HIGH_AMT 105 //default elevation distributions
#define MEDIUM_AMT (100+waterAmountG)
#define LOW_AMT (105+3*waterAmountG)
#define WATER_AMT 15*waterAmountG

            if (neighbors[LOW])
               if (neighbors[HIGH]) //HIGH with LOW or WATER
                  nextElevation = MEDIUM;
               else if (neighbors[MEDIUM] >= 3) //MEDIUM with LOW or WATER
                  if (a != b)
                     nextElevation = pickFrom2(LOW,LOW_AMT, MEDIUM,MEDIUM_AMT);
                  else if (a == LOW)
                     nextElevation = pickFrom2(LOW,100*LOW_AMT, MEDIUM,MEDIUM_AMT*cliffAmountG);
                  else
                     nextElevation = pickFrom2(LOW,LOW_AMT*cliffAmountG, MEDIUM,100*MEDIUM_AMT);
               else //LOW or WATER only, possibly MEDIUM down-right
                  if (neighbors[WATER] == 1)
                     nextElevation = pickFrom3(WATER,100*WATER_AMT, LOW,100*LOW_AMT, MEDIUM,MEDIUM_AMT*cliffAmountG);
                  else if (neighbors[WATER] == 0)
                     nextElevation = pickFrom3(WATER,WATER_AMT*cliffAmountG, LOW,100*LOW_AMT, MEDIUM,MEDIUM_AMT*cliffAmountG);
                  else
                     nextElevation = pickFrom3(WATER,10000*WATER_AMT, LOW,LOW_AMT*100*cliffAmountG, MEDIUM,MEDIUM_AMT*cliffAmountG*cliffAmountG);
            else
               if (neighbors[HIGH]) //HIGH or HIGH with MEDIUM
                  if (a != b)
                     nextElevation = pickFrom2(MEDIUM,MEDIUM_AMT,HIGH,HIGH_AMT);
                  else if (a == HIGH)
                     nextElevation = pickFrom2(MEDIUM,MEDIUM_AMT*cliffAmountG,HIGH,100*HIGH_AMT);
                  else
                     nextElevation = pickFrom2(MEDIUM,100*MEDIUM_AMT,HIGH,HIGH_AMT*cliffAmountG);
               else //MEDIUM only
                  nextElevation = pickFrom3(LOW,LOW_AMT*cliffAmountG,MEDIUM,200*MEDIUM_AMT, HIGH,HIGH_AMT*cliffAmountG);

//--------------------------------------------------------------
// set elevation
//--------------------------------------------------------------
            if ((mapCorner[x][y] == LOW_OR_WATER)&&(nextElevation != WATER))
//bottom and left edges of a special on LOW ground there may only be LOW or WATER
               nextElevation = LOW;

            if (nextElevation == WATER)
               {
               if ((x != 0)&&(y != 0)&&(mapMiddle[x-1][y-1] == UNASSIGNED))
                  mapMiddle[x-1][y-1] = WATER; //set WATER
               nextElevation = LOW;
               }
            }


         mapCorner[x][y] = nextElevation; //set elevation

         if (special != UNASSIGNED) //if special, make flat spot (don't worry about going over map edge, will go into mirrored part)
            {
            tempElevation = nextElevation;
            if(tempElevation == LOW)
               tempElevation = LOW_OR_WATER; //allow for water on left and bottom edges
            mapCorner[x+1][y+1] = nextElevation;
            mapCorner[x+1][y] = tempElevation;
            mapCorner[x][y+1] = tempElevation;
            }
         }

   if (islandsFlagG) //replace borders with water, errorCorrection() finishes it.
      {
      int edgeWaterA = (int)(islandsFlagG * totalMapSizeG / 16 + 0.5);
      int edgeWaterB = mapMiddleMaxG-(int)(islandsFlagG * totalMapSizeG / 16);
      for (y = 0; y <= mapCornerMaxG; ++y)
         {
         for (x = 0; x < edgeWaterA; ++x)
            {
            mapCorner[x][y] = LOW;
            mapMiddle[x][y] = WATER;
            }
         if(mapCorner[edgeWaterA+1][y] == HIGH)
            mapCorner[edgeWaterA][y] = MEDIUM;

         for (x = mapMiddleMaxG; x > edgeWaterB; --x)
            {
            mapCorner[x+1][y] = LOW;
            mapMiddle[x][y] = WATER;
            }
         if(mapCorner[edgeWaterB][y] == HIGH)
            mapCorner[edgeWaterB+1][y] = MEDIUM;
         }

      for (x = edgeWaterA; x <= edgeWaterB+1; ++x)
         {
         for (y = 0; y < edgeWaterA; ++y)
            {
            mapCorner[x][y] = LOW;
            mapMiddle[x][y] = WATER;
            }
         if(mapCorner[x][edgeWaterA+1] == HIGH)
            mapCorner[x][edgeWaterA] = MEDIUM;

         for (y = mapMiddleMaxG; y > edgeWaterB; --y)
            {
            mapCorner[x][y+1] = LOW;
            mapMiddle[x][y] = WATER;
            }
         if(mapCorner[x][edgeWaterB] == HIGH)
            mapCorner[x][edgeWaterB+1] = MEDIUM;
         }
      if (islandsFlagG == 2) //add tiny islands to help bridge wide channels
         {
         int j;
         for (int i = 0; i < totalMapSizeG / 16; ++i)
            {
            x = (int)(totalMapSizeG / 16 - .5);//SpiffRand((int)(totalMapSizeG / 16 - .5), (int)(totalMapSizeG / 8 - 3));
            y = SpiffRand(x, totalMapSizeG/2 - 1 - x);//(int)(totalMapSizeG / 16 - .5), totalMapSizeG/2-(int)(totalMapSizeG / 16 - .5));
            if (SpiffRand(0,1))
               x = totalMapSizeG / 2 - 1 - x;
            if (SpiffRand(0,1))
               {
               mapMiddle[x][y] = UNASSIGNED;
               for (j = 0; j < 4; ++j)
                  mapMiddle[x + SpiffRand(-1,1)][y + SpiffRand(-1,1)] = UNASSIGNED;
               }
            else
               {
               mapMiddle[y][x] = UNASSIGNED;
               for (j = 0; j < 4; ++j)
                  mapMiddle[y + SpiffRand(-1,1)][x + SpiffRand(-1,1)] = UNASSIGNED;
               }
            }
         }
      }

   mirrorMap();
//   stats();
   errorCorrection();
   }