예제 #1
0
/*	function :	rollBack ()
		UNDO all actions that occured after the last leap of faith.
		in other words : rollback until you reach a leap of faith action.
		if this action reaches a (BOARD_INITIALIZED) action before reaching
		a (LEAP_OF_FAITH) action, it considers that the initial  state of
		the board is an invalid state and impossible to solve.
		in the later case, this function sets the state flag (_BAD_INITIALIZATION).
		return type :
			-	if rolled back successfully	: true.
			-	otherwise					: false.
*/
bool actionCourse::rollBack()
{

	do
	{
		if( lastAction() . act == actionRecord::BOARD_INITIALIZED)
		{
			// if reached the BOARD_INITIALIZED action ...
			// set the state flag _BAD_INITIALIZATION
			dynamic_cast<sudokuBoard*>(this)->state |= sudokuState::_BAD_INITIALIZATION;
			return false;
		}
		else
			Undo();
	}
	// keep rolling back until you reach a LEAP_OF_FAITH action.
	while( Tracker.top() != actionRecord::LEAP_OF_FAITH );

	// signal the current value as faulty and shouldn't be used for future leaps.
		Tracker.top().faultyValues.push_back(*(lastAction().subject));

		if( Tracker.top().faultyValues.size() < (unsigned)Tracker.top().subject->countPossibilities() )
		//correct the value taken by the leap
			return Correct_LeapOfFaith();

		else if(Tracker.top().subject->countPossibilities() == Tracker.top().faultyValues.size() )
			return rollBack();	//if all the possibilities are tried, then a previuos leap is the mistake, roll back another time.

		else
			return false;	// report failure.
}
예제 #2
0
void Player::takeIn(){ // take in the opponent's move, and deal with roll back
		int i, j;
		cin >> i >> j;
		if (i == -1 && j == -1) {
				rollBack();				
				cout << "Insert you move again" << endl;
				takeIn();
		}
		else {
				lastYou_ = you;				
				you[0] = i; you[1] = j;
				you[0]--; you[1]--;				
				char c;
				while (!checkLegal(you) ) {
						cout << "Are u sure that u input something illegal y/n?" << endl;						
						cin >> c;
						if (c == 'y') {
								gameOver = MAX;	
								return;
						}
						cin >> i >> j;
						you[0] = i; you[1] = j;
						you[0]--; you[1]--;
				}
				curState.update(MIN, you);				
				increaseStep();
		}
}
예제 #3
0
int OptVectorLayer::addFeatureWithDefaultValue( OptFeature& f )
{
//{zhangliye2715:api}
  if ( !isEditable() )
  {
    startEditing();
  }

  // add the fields to the QgsFeature
  QgsVectorDataProvider* provider = dataProvider();
  const QgsFieldMap fields = provider->fields();

  for ( QgsFieldMap::const_iterator it = fields.begin(); it != fields.end(); ++it )
  {
    f.addAttribute( it.key(), provider->defaultValue( it.key() ) );
  }

  int id = -1;
  id = OptVectorLayer::addFeature( f );
  if ( id != -1 )
  {
    //add points to other features to keep topology up-to-date
    int topologicalEditing = QgsProject::instance()->readNumEntry( "Digitizing", "/TopologicalEditing", 0 );

    if( topologicalEditing )
    {
      addTopologicalPoints( f.geometry() );
    }
  }
  else
  {
    return -1;
  }

  //vlayer->setModified(true);
  //commit or rollBack the change
  if ( isModified() )
  {
    commitChanges();
  }
  else
  {
    rollBack();
    return -1;
  }

  //make the layer still editable for the next adding operation
  startEditing();

  return id;
}
예제 #4
0
/*!
    \fn OptVectorLayer:: changeAttribute(QgsFeature& feature, int field, QVariant attr)
 */
bool OptVectorLayer:: changeAttribute(QgsFeature& feature, int field, QVariant attr)
{
  if ( !isEditable() )
  {
    startEditing();
  }
  if ( isModified() )
  {
    commitChanges();
  }

  QgsChangedAttributesMap attributeChanges;
  QgsAttributeMap newAttMap;
  newAttMap.insert(field, attr );
	attributeChanges.insert(feature.id(), newAttMap);
//{zhangliye2715} ;api changed
//   commitAttributeChanges(QgsAttributeIds(),
//           QgsNewAttributesMap(),
//           attributeChanges);
	dataProvider()->changeAttributeValues(attributeChanges);

  //commit or rollBack the change
  if ( isModified() )
  {
    commitChanges();
  }
  else
  {
    rollBack();
    return false;
  }

  //make the layer still editable for the next adding operation
  startEditing();

  return true;
}
예제 #5
0
void TargetManagement::findTargets ()
{
	int ylow;
	if (discovered -> size () == 0)
	{
		ylow = initialTarget ();

		for (unsigned int j = 0; j < objects -> size (); j++)
		{
			discovered -> push_back (0);
		}
	}
	else
	{
		ylow = furtherTarget ();
	}
	int corner = 0;
	if (ylow != -1)
	{
		targets -> push_back (ylow);
		discovered -> at (ylow) = 2;

		// c1-c2-c3-c4 Atvirksciai
		int count = 0;
		int currentTarget = targets -> back ();
		lastCorners.push(vector<int>());
		lastCorners.top().push_back(currentTarget);
		lastCorners.top().push_back (0);
		lastCorners.top().push_back (0);
		Point *currentPoint = corners -> at (info->at(currentTarget)) -> at (corner);
		lastPoints.push(currentPoint);
		while (count < 4)
		{
			//cout << discovered -> at (ylow) << endl;
			vector<Point*> *recCorners = corners -> at (info->at(currentTarget));
			list<int> *g = &(graph -> graph -> at (currentTarget));
			vector<Point*> *points = &(graph -> connections -> at (currentTarget));
			if (corner == 0) // Goes to 3
			{
				Point *nextCorner = recCorners -> at (3);
				currentPoint = findNewCorner (recCorners, g, points, currentPoint, nextCorner);

				if (foundNewTarget(currentTarget))
				{
					currentTarget = lastCorners.top()[0];
					corner = setCorner (currentPoint);
					count = 0;
					lastCorners.top().push_back (corner);
					lastCorners.top().push_back(count);
				}
				else if (reachedEnd (currentPoint, count))
				{
					corner = 3;
					count++;
					lastCorners.top()[1] = corner;
					lastCorners.top()[2] = count;
					lastPoints.top() = currentPoint;
				}
				else
				{
					corner = -1;
				}
			}
			else if (corner == 1) // Goes to 0
			{
				Point *nextCorner = recCorners -> at (0);
				currentPoint = findNewCorner (recCorners, g, points, currentPoint, nextCorner);


				if (foundNewTarget(currentTarget))
				{
					currentTarget = lastCorners.top()[0];
					corner = setCorner (currentPoint);
					count = 0;
					lastCorners.top().push_back (corner);
					lastCorners.top().push_back(count);
				}
				else if (reachedEnd (currentPoint, count))
				{
					corner = 0;
					count++;
					lastCorners.top()[1] = corner;
					lastCorners.top()[2] = count;
					lastPoints.top() = currentPoint;
				}
				else
				{
					corner = -1;
				}
			}
			else if (corner == 2) // Goes to 1
			{
				Point *nextCorner = recCorners -> at (1);
				currentPoint = findNewCorner (recCorners, g, points, currentPoint, nextCorner);


				if (foundNewTarget(currentTarget))
				{
					currentTarget = lastCorners.top()[0];
					corner = setCorner (currentPoint);
					count = 0;
					lastCorners.top().push_back (corner);
					lastCorners.top().push_back(count);
				}
				else if (reachedEnd (currentPoint, count))
				{
					corner = 1;
					count++;
					lastCorners.top()[1] = corner;
					lastCorners.top()[2] = count;
					lastPoints.top() = currentPoint;
				}
				else
				{
					corner = -1;
				}
			}
			else if (corner == 3)				   // Goes to 2
			{
				Point *nextCorner = recCorners -> at (2);
				currentPoint = findNewCorner (recCorners, g, points, currentPoint, nextCorner);

				if (foundNewTarget(currentTarget))
				{
					currentTarget = lastCorners.top()[0];
					corner = setCorner (currentPoint);
					count = 0;
					lastCorners.top().push_back (corner);
					lastCorners.top().push_back(count);
				}
				else if (reachedEnd (currentPoint, count))
				{
					corner = 2;
					count++;
					lastCorners.top()[1] = corner;
					lastCorners.top()[2] = count;
					lastPoints.top() = currentPoint;
				}
				else
				{
					corner = -1;
				}
			}
			else
			{
				lastPoints.pop ();
				Point *nextCorner = lastPoints.top ();
				lastPoints.push (currentPoint);
				currentPoint = findNewCorner (recCorners, g, points, currentPoint, nextCorner);

				if (foundNewTarget(currentTarget))
				{
					currentTarget = lastCorners.top()[0];
					corner = setCorner (currentPoint);
					count = 0;
					lastCorners.top().push_back (corner);
					lastCorners.top().push_back(count);
				}
				else
				{
					count++;
					lastCorners.top()[2] = count;
					lastPoints.top() = currentPoint;
				}
			}

			if (count == 4 && traceConnection ())
			{
				// Roll back searching.
				if (rollBack ())
				{
					currentTarget = lastCorners.top()[0];
					corner = lastCorners.top()[1];
					count = lastCorners.top()[2];
					currentPoint = lastPoints.top();
					//lastPoints.pop ();
				}
			}
		}
		clearStack ();
		findTargets ();
	}
}