示例#1
0
	//
	// e x t r a c t S t r i n g
	//
	bool DinoLineBuffer::extractString(const DinoLineBufferPosition &startPosition,
									   const DinoLineBufferPosition &endPosition,
									   char *targetString){

		// StartPosition invalid, probably because the line of the startPosition
		// has already been overwritten, i.e. the string is too long
		if (!isValidPosition(startPosition))
		{
			ogdf::strcpy(targetString, DinoLineBuffer::c_maxStringLength, "String too long!");
			return false;
		}

		// EndPosition must be valid
		OGDF_ASSERT(isValidPosition(endPosition))
		
		// Remember original currentPosition 
		DinoLineBufferPosition originalCurrentPosition = getCurrentPosition();

		// Begin at startPosition
		setCurrentPosition(startPosition);

		// Copy characters to tempString
		int targetStringIndex = 0;
		while (getCurrentPosition() != endPosition)
		{

			// Check if eof
			OGDF_ASSERT(getCurrentCharacter() != EOF)

			// Put character into targetString
			targetString[targetStringIndex] = getCurrentCharacter();
			++targetStringIndex;

			// String too long
			if (targetStringIndex >= DinoLineBuffer::c_maxStringLength - 1){

				ogdf::strcpy(targetString, DinoLineBuffer::c_maxStringLength, "String too long!");

				// Set back the original current position
				setCurrentPosition(originalCurrentPosition);

				return false;

			}

			// Move to next character
			moveToNextCharacter();

		} // Copy characters to tempString

		// Set back the original current position
		setCurrentPosition(originalCurrentPosition);

		// Terminate string
		targetString[targetStringIndex] = '\0';

		return true;

	} // extractString
bool MapCollision::smallStep(float &x, float &y, float step_x, float step_y, int movement_type, int collide_type) {
	if (isValidPosition(x + step_x, y + step_y, movement_type, collide_type)) {
		x += step_x;
		y += step_y;
		assert(isValidPosition(x,y,movement_type, collide_type));
		return true;
	}
	else {
		return false;
	}
}
示例#3
0
文件: state.cpp 项目: amreis/ia-chess
// Moves a piece from <f> to <t>. If any position is invalid or there is a friend
// there, returns false since the movement can't be made.
bool State::move(ii f, ii t)
{
	if (!isValidPosition(f) || !isValidPosition(t) || f == t)
		return false;
	if (foundFriend(t))
		return false;

	board[t.first*8 + t.second] = board[f.first*8 + f.second];
	board[f.first*8 + f.second] = '.';
	#ifdef DEBUG
	assert(f != t);
	#endif
	this->lastMove = make_pair(f,t);
	return true;
}
示例#4
0
/*!
*/
void GridItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if (!m_ballSelected) {
        event->ignore();
        return;
    }

    GridPos pt;
    fromViewToGridCoordinate(event->pos(), pt);

    if (isValidPosition(pt) && isFreePos(pt) && (pt != m_beginPos)) {
        m_pathTracker.clear();

        QVector<GridPos>& path = m_pathTracker.path();
        bool found = PathFinder::instance()->execute(this, m_beginPos, pt, path);

        if (found && (path.count() >= 2)) {
            int n = path.count();
            for (int i = 0; i < n-1; ++i) {
                QPoint pt1;
                fromGridToCenteredCoordinate(path.at(i), pt1);

                QPoint pt2;
                fromGridToCenteredCoordinate(path.at(i+1), pt2);

                m_pathTracker.addLine(pt1, pt2);
            }

            update(); // repaint the grid
        }
    }
}
bool MapCollision::smallStepForcedSlideAlongGrid(float &x, float &y, float step_x, float step_y, int movement_type, int collide_type) {
	if (isValidPosition(x + step_x, y, movement_type, collide_type)) { // slide along wall
		if (step_x == 0) return true;
		x += step_x;
		assert(isValidPosition(x,y,movement_type, collide_type));
	}
	else if (isValidPosition(x, y + step_y, movement_type, collide_type)) {
		if (step_y == 0) return true;
		y += step_y;
		assert(isValidPosition(x,y,movement_type, collide_type));
	}
	else {
		return false;
	}
	return true;
}
示例#6
0
/*!
* Displays the ball item in a given location onto the grid.
*/
void GridItem::showBall(BallItem *ball, int row, int col, bool updateInternalStruct)
{
    if (!ball) {
        qDebug() << "GridItem::showBall(...): pointer to the ball item is NULL";
        return;
    }

    Q_ASSERT(isValidPosition(row, col));

    ball->setCoordinates(row, col);
    if (!ball->isVisible()) {
        ball->setVisible(true);
    }

    if (updateInternalStruct) {
        m_balls[row][col] = ball;

        // available to used
        BallItemsProvider::instance()->fromAvailableToUsed(ball->coordinates());
        //
    }

    QPoint pt;
    fromGridToCenteredCoordinate(row, col, pt);

    ball->setPos(pt.x(), pt.y());
}
void ConcreteProblem::getSolutionStepsAroundCell(const CellPtr cellPtr, AbstractSolutionStepPtrs& solutionStepPtrs) const{
    integer row1 = cellPtr->row;
    integer column1 = cellPtr->column;
    SolutionStepPoolPtr solutionStepPoolPtr = SolutionStepPool::getInstance();
    if (isValidPosition(row1, column1 + 1)) {
        solutionStepPtrs.push_back(solutionStepPoolPtr->getSolutionStepPtr(row1, column1, HORIZONTAL));
    }
    if (isValidPosition(row1, column1 - 1)) {
        solutionStepPtrs.push_back(solutionStepPoolPtr->getSolutionStepPtr(row1, column1 - 1, HORIZONTAL));
    }
    if (isValidPosition(row1 + 1, column1)) {
        solutionStepPtrs.push_back(solutionStepPoolPtr->getSolutionStepPtr(row1, column1, VERTICAL));
    }
    if (isValidPosition(row1 - 1, column1)) {
        solutionStepPtrs.push_back(solutionStepPoolPtr->getSolutionStepPtr(row1 - 1, column1, VERTICAL));
    }
}
const Piece* ChessBoardModel::cell(QPoint p) const
{
    if(!isValidPosition(p))
    {
        return NULL;
    }
    return cells_[p.x() + p.y() * 8];
}
示例#9
0
/*!
*/
void GridItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    if (!m_ballSelected) {
        event->ignore();
        return;
    }

    GridPos pt;
    fromViewToGridCoordinate(event->pos(), pt);

    if (m_beginPos == pt) {
        m_ballSelected = false;
        selectBall(pt, false);

        // clear the path tracker and repaint the grid
        m_pathTracker.clear();
        update();
        //
    } else if (isValidPosition(pt) && isFreePos(pt)) {
        QVector<GridPos>& path = m_pathTracker.path();
        if (!path.isEmpty()) {
            // have we moved onto a square occupied by a hint ball ? if we have then a new set of the hint balls
            // needs to be generated.
            BallItem *ball = ballAt(path.back());
            bool enforceHintBalls = (ball && ball->isHint());
            //

            moveBall(ballAt(m_beginPos), path);

            // unselect the moving ball
            ball = ballAt(path.back());
            ball->select(false);
            m_ballSelected = false;

            // searches for the lines of the same ball items that have the same colours.
            checkLines(ball);

            // do we have more available positions ? if we do then proceed with a new set of ball items.
            if (!BallItemsProvider::instance()->availableIndexes().isEmpty()) {
                // get the next set of balls
                const QList<BallItem*> &currentBalls = BallItemsProvider::instance()->nextBalls(enforceHintBalls);
                // searches for the lines of the same ball items that have the same colours.
                checkLines(currentBalls);
            } 
            
            // even more available positions ? if not then quit or reset the game.
            if (BallItemsProvider::instance()->availableIndexes().isEmpty()){
                if (promptForGameEnd()) {
                    QCoreApplication::quit();
                } else {
                    MainWidget::instance()->boardView()->reset();
                } 
            }
        }
    }
}
示例#10
0
bool MapBlock::isValidPositionParent(v3s16 p)
{
	if(isValidPosition(p))
	{
		return true;
	}
	else{
		return m_parent->isValidPosition(getPosRelative() + p);
	}
}
示例#11
0
文件: state.cpp 项目: amreis/ia-chess
// Function that determines if there is a FOE in some position of the board.
bool State::foundFoe (ii pos) const
{
	if (!isValidPosition(pos)) return false;
	if ((isupper(board[pos.first*8 + pos.second]) && ourTeam == BLACK)
		|| (islower(board[pos.first*8+pos.second]) && ourTeam == WHITE))
		return true;
	else
		return false;

}
示例#12
0
	//
	// s e t C u r r e n t P o s i t i o n
	//
	bool DinoLineBuffer::setCurrentPosition(const DinoLineBufferPosition &newPosition){

		// Given positon is not valid
		if (!isValidPosition(newPosition))
		{
			return false;
		}

		m_currentPosition = newPosition;

		return true;

	} // setCurrentPosition
示例#13
0
MapNode MapBlock::getNodeParent(v3s16 p)
{
	if(isValidPosition(p) == false)
	{
		return m_parent->getNode(getPosRelative() + p);
	}
	else
	{
		if(data == NULL)
			throw InvalidPositionException();
		return data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X];
	}
}
示例#14
0
文件: ebook.cpp 项目: klauer/qolibri
QList <CandItem> EbMenu::topMenu()
{

    EB_Position pos = menu();
    if (!isValidPosition(pos)) {
        QList<CandItem> i;
        return i;
    }

    QString t;
    return candidate(pos, &t);

}
示例#15
0
void MapBlock::setNodeParent(v3s16 p, MapNode & n)
{
	if(isValidPosition(p) == false)
	{
		m_parent->setNode(getPosRelative() + p, n);
	}
	else
	{
		if(data == NULL)
			throw InvalidPositionException();
		data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X] = n;
	}
}
示例#16
0
MapNode MapBlock::getNodeParent(v3s16 p, bool *is_valid_position)
{
	if (isValidPosition(p) == false)
		return m_parent->getNodeNoEx(getPosRelative() + p, is_valid_position);

	if (!data) {
		if (is_valid_position)
			*is_valid_position = false;
		return MapNode(CONTENT_IGNORE);
	}
	if (is_valid_position)
		*is_valid_position = true;
	return data[p.Z * zstride + p.Y * ystride + p.X];
}
示例#17
0
MapNode MapBlock::getNodeParent(v3s16 p, bool *is_valid_position)
{
    if (isValidPosition(p) == false)
        return m_parent->getNodeNoEx(getPosRelative() + p, is_valid_position);

    if (data == NULL) {
        if (is_valid_position)
            *is_valid_position = false;
        return MapNode(CONTENT_IGNORE);
    }
    if (is_valid_position)
        *is_valid_position = true;
    return data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X];
}
示例#18
0
bool MapCollision::smallStepForcedSlide(float &x, float &y, float step_x, float step_y, int movement_type, int collide_type) {
	// is there a singular obstacle or corner we can step around?
	// only works if we are moving straight
	const float epsilon = 0.01f;
	if (step_x != 0) {
		assert(step_y == 0);
		float dy = y - floorf(y);

		if (isValidTile(int(x), int(y) + 1, movement_type, collide_type)
				&& isValidTile(int(x) + sgn(step_x), int(y) + 1, movement_type, collide_type)
				&& dy > 0.5) {
			y += std::min(1 - dy + epsilon, float(fabs(step_x)));
		}
		else if (isValidTile(int(x), int(y) - 1, movement_type, collide_type)
				 && isValidTile(int(x) + sgn(step_x), int(y) - 1, movement_type, collide_type)
				 && dy < 0.5) {
			y -= std::min(dy + epsilon, float(fabs(step_x)));
		}
		else {
			return false;
		}
		assert(isValidPosition(x,y,movement_type, collide_type));
	}
	else if (step_y != 0) {
		assert(step_x == 0);
		float dx = x - floorf(x);

		if (isValidTile(int(x) + 1, int(y), movement_type, collide_type)
				&& isValidTile(int(x) + 1, int(y) + sgn(step_y), movement_type, collide_type)
				&& dx > 0.5) {
			x += std::min(1 - dx + epsilon, float(fabs(step_y)));
		}
		else if (isValidTile(int(x) - 1, int(y), movement_type, collide_type)
				 && isValidTile(int(x) - 1, int(y) + sgn(step_y), movement_type, collide_type)
				 && dx < 0.5) {
			x -= std::min(dx + epsilon, float(fabs(step_y)));
		}
		else {
			return false;
		}
	}
	else {
		assert(false);
	}
	return true;
}
示例#19
0
/**
 * Given a target, trys to return one of the 8+ adjacent tiles
 * Returns the retargeted position on success, returns the original position on failure
 */
FPoint MapCollision::getRandomNeighbor(const Point& target, int range, bool ignore_blocked) {
	FPoint new_target(target);
	std::vector<FPoint> valid_tiles;

	for (int i=-range; i<=range; i++) {
		for (int j=-range; j<=range; j++) {
			if (i == 0 && j == 0) continue; // skip the middle tile
			new_target.x = static_cast<float>(target.x + i) + 0.5f;
			new_target.y = static_cast<float>(target.y + j) + 0.5f;
			if (isValidPosition(new_target.x, new_target.y, MOVE_NORMAL, COLLIDE_NORMAL) || ignore_blocked)
				valid_tiles.push_back(new_target);
		}
	}

	if (!valid_tiles.empty())
		return valid_tiles[rand() % valid_tiles.size()];
	else
		return FPoint(target);
}
示例#20
0
/**
 * Does not have the "slide" submovement that move() features
 * Line can be arbitrary angles.
 */
bool MapCollision::lineCheck(const float& x1, const float& y1, const float& x2, const float& y2, int check_type, int movement_type) {
	float x = x1;
	float y = y1;
	float dx = static_cast<float>(fabs(x2 - x1));
	float dy = static_cast<float>(fabs(y2 - y1));
	float step_x;
	float step_y;
	int steps = static_cast<int>(std::max(dx, dy));


	if (dx > dy) {
		step_x = 1;
		step_y = dy / dx;
	}
	else {
		step_y = 1;
		step_x = dx / dy;
	}
	// fix signs
	if (x1 > x2) step_x = -step_x;
	if (y1 > y2) step_y = -step_y;


	if (check_type == CHECK_SIGHT) {
		for (int i=0; i<steps; i++) {
			x += step_x;
			y += step_y;
			if (isWall(x, y))
				return false;
		}
	}
	else if (check_type == CHECK_MOVEMENT) {
		for (int i=0; i<steps; i++) {
			x += step_x;
			y += step_y;
			if (!isValidPosition(x, y, movement_type, COLLIDE_NORMAL))
				return false;
		}
	}

	return true;
}
示例#21
0
void solve( int nQueensSize, solution_list_t *solutions, int *numberOfSolutions, solution_t workingSolution, int currentColumn) {
    int row = 0;
    
    if(currentColumn == nQueensSize) {
        copyToSolutions(solutions, numberOfSolutions, workingSolution, nQueensSize);
        return;
    }

    for(row = 0; row < nQueensSize; row++) {
        workingSolution[currentColumn] = row;
        
        if(isValidPosition(workingSolution, currentColumn)) {
 
            solve(nQueensSize, solutions, numberOfSolutions, workingSolution, currentColumn+1);
            
        }
    }
    
    return;
}
示例#22
0
MapNode MapBlock::getNodeParentNoEx(v3s16 p)
{
	if(isValidPosition(p) == false)
	{
		try{
			return m_parent->getNode(getPosRelative() + p);
		}
		catch(InvalidPositionException &e)
		{
			return MapNode(CONTENT_IGNORE);
		}
	}
	else
	{
		if(data == NULL)
		{
			return MapNode(CONTENT_IGNORE);
		}
		return data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X];
	}
}
示例#23
0
MapNode MapBlock::getNodeParent(v3s16 p, bool *is_valid_position)
{
	if (isValidPosition(p) == false)
		return m_parent->getNodeTry(getPosRelative() + p);

	if (data == NULL) {
		if (is_valid_position)
			*is_valid_position = false;
		return MapNode(CONTENT_IGNORE);
	}
	auto lock = try_lock_shared_rec();
	if (!lock->owns_lock()) {
		if (is_valid_position)
			*is_valid_position = false;
		return MapNode(CONTENT_IGNORE);
	}

	if (is_valid_position)
		*is_valid_position = true;
	return data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X];
}
示例#24
0
文件: test.cpp 项目: Jetqvvf/minetest
		virtual void setNode(v3s16 p, MapNode & n)
		{
			if(isValidPosition(p) == false)
				throw InvalidPositionException();
		};
示例#25
0
文件: test.cpp 项目: Jetqvvf/minetest
		virtual MapNode getNode(v3s16 p)
		{
			if(isValidPosition(p) == false)
				throw InvalidPositionException();
			return node;
		}
void VisibleSelection::validatePositionsIfNeeded()
{
    if (!isValidPosition(m_base) || !isValidPosition(m_extent) || !isValidPosition(m_start) || !isValidPosition(m_end))
        validate();
}
示例#27
0
int isValidMove(Pilot *p,Pilot *np,Map *m)
{
   if(!isValidPosition(np,m)) return 0;
   if(m->cell[p->pos->x][p->pos->y]=='~' && vectNorm(np->speed)>1) return 0;
   return 1;
}