Пример #1
0
void ItemFactory::addDoor()
{
	//check if the doors exist
	if (m_pDoors.first != nullptr || m_pDoors.second != nullptr)
		return;

	//check the empty grid
	Snake* snake = m_pSnakeMap->getSnake();
	if (!snake)
		return;
	int left = m_pSnakeMap->getMovableNumbers() - snake->getLength();

	//no empty grids left
	if (left <= 0)
		return;

	//set the door index
	int index = (int)(rand() % left + 1);
	auto mapIndex = m_pSnakeMap->getEmptyGridIndex(index);

	//create door model
	m_pDoors.first = Door::create();
	this->addChild(m_pDoors.first, 1, eID_Door1);
	m_pDoors.first->setPosition(VisibleRect::getVisibleRect().origin + VisibleRect::getHalfGridVec() + VisibleRect::getGridLength()*mapIndex);
	m_pDoors.first->setIndex(mapIndex);

	//set the door transfer direction
	auto transferDir = randomDirection();
	m_pDoors.first->setTransferDirection(transferDir);
	//actual door direction is opposite to transfer direction
	m_pDoors.first->setRotation(arcByDirection(oppositeDirection(transferDir)));

	//set the grid type
	m_pSnakeMap->setGridType(mapIndex, eType_Door);

	//create the second door
	index = (int)(rand() % left + 1);
	mapIndex = m_pSnakeMap->getEmptyGridIndex(index);
	m_pDoors.second = Door::create();
	this->addChild(m_pDoors.second, 1, eID_Door2);
	m_pDoors.second->setPosition(VisibleRect::getVisibleRect().origin + VisibleRect::getHalfGridVec() + VisibleRect::getGridLength()*mapIndex);
	m_pDoors.second->setIndex(mapIndex);

	transferDir = randomDirection();
	m_pDoors.second->setTransferDirection(transferDir);
	m_pDoors.second->setRotation(arcByDirection(oppositeDirection(transferDir)));

	m_pSnakeMap->setGridType(mapIndex, eType_Door);

	//connect the two doors
	m_pDoors.first->setOtherDoor(m_pDoors.second);
	m_pDoors.second->setOtherDoor(m_pDoors.first);
}
Пример #2
0
int solve(field f, location *robot, location to, unsigned max, move *moves) {
  //  printf("max: %d, to: %d, ls: %d,%d,%d,%d\n%s", max, (int)to, (int)robot[0], (int)robot[1], (int)robot[2], (int)robot[3], prettyPrint(f));
  if (robot[0] == to) {
    (moves+1)->d = 0;
    return 1;
  }

  if (max < 1 || lookup(robot, max))
      return 0;

  for (int i = 0; i < 4; ++i) {
    for (int d = 0; d < 4; ++d) {
      if (moves->c == i + 1 &&  moves->d == oppositeDirection(1 << d))
        continue;
      field f2 = cloneField(f);
      location from = robot[i];
      int target = doMove(f2, from, 1 << d);
      if (target >= 0) {
        (moves+1)->c = i + 1;
        (moves+1)->d = 1<<d;
        robot[i] = target;
        if (solve(f2, robot, to, max - 1, moves + 1)) {
          free(f2);
          return 1;
        }
      }
      free(f2);
      robot[i] = from;
    }
  }
  return 0;
}
Пример #3
0
void GridElement::setWall(Direction direction, bool is_solid) {
	walls[direction] = is_solid;

	// set opposite wall if it exists
	if (neighbours[direction] != NULL) {
		neighbours[direction]->walls[oppositeDirection(direction)] = is_solid;
	}
}
Пример #4
0
bool goToSon(){
    int next_x = getForwardX();
    int next_y = getForwardY();
    if( verifyWall( next_x, next_y ) ){
        return false;
    }
    goForward();
    current_x = next_x;
    current_y = next_y;
    parents_map[current_x][current_y] = oppositeDirection(current_direction);
    current_deep++;
    return true;
}
Пример #5
0
unsigned	Point::axis(Direction dir) const
{
  assert(dir >= Direction::LEFT && dir <= Direction::BOTTOMLEFT);
  const Radar	*directions[] =
    {
      &cdirection(dir),
      &cdirection(oppositeDirection(dir))
    };
  unsigned	length = 1;

  for (const Radar *direction : directions) {
    if (direction->color == _center) {
      length += direction->length;
    }
  }
  return length;
}
Пример #6
0
void ItemFactory::addDoor(eID doorID, eDirection dir, cocos2d::Vec2 pos, std::string model)
{
	if (doorID != eID_Door1 && doorID != eID_Door2)
		return;

	if (dir == eDir_None || (pos.x < 0 || pos.x >= MAPWIDTH || pos.y < 0 || pos.y >= MAPHEIGHT))
		return;

	// remove origin door
	removeDoor(doorID);

	Door** ppDoorToAdd = nullptr;
	if (doorID == eID_Door1)
		ppDoorToAdd = &m_pDoors.first;
	else if (doorID == eID_Door2)
		ppDoorToAdd = &m_pDoors.second;

	if (ppDoorToAdd)
	{
		Door* pDoorToAdd = Door::create(model);
		*ppDoorToAdd = pDoorToAdd;
		this->addChild(pDoorToAdd, 1, doorID);
		pDoorToAdd->setPosition(VisibleRect::getVisibleRect().origin + VisibleRect::getHalfGridVec() + VisibleRect::getGridLength()*pos);
		pDoorToAdd->setIndex(pos);

		//set the door transfer direction
		pDoorToAdd->setTransferDirection(dir);
		//actual door direction is opposite to transfer direction
		pDoorToAdd->setRotation(arcByDirection(oppositeDirection(dir)));

		//set the grid type
		m_pSnakeMap->setGridType(pos, eType_Door);
	}

	//connect the two doors
	if (m_pDoors.first)
		m_pDoors.first->setOtherDoor(m_pDoors.second);
	if (m_pDoors.second)
		m_pDoors.second->setOtherDoor(m_pDoors.first);
}
Пример #7
0
int AI::betterMemory()
{
    int position = 0;
    int nextPos = 0;
    int lastPos = 0;
    bool ok = false;
    Direction lastDir = AI::UP;
    bool keepPosAndDir = false;

    do
    {
        // Check if an active ship is available
        if(shipActive())
        {
            if(!keepPosAndDir)
            {
                // Get information about the last turn
                lastPos = _alreadyTried.last();
                lastDir = lastDirection();
                keepPosAndDir = false;
            }

            // Get next position
            if(lastHit())
            {
                nextPos = positionInDirection(lastDir, lastPos);
            }
            else
            {
                if(_currentHitsOnShip >= 2)
                {
                    if(!keepPosAndDir)
                        lastDir = oppositeDirection(lastDir);
                    nextPos = positionInDirection(lastDir, _shipFirstHit);
                }
                else
                {
                    if(!keepPosAndDir)
                        lastDir = nextDirection(lastDir);
                    nextPos = positionInDirection(lastDir, _shipFirstHit);
                }
            }

            // If the position is not valid, try an other one
            if(nextPos < 0)
            {
                if(_currentHitsOnShip >= 2)
                    lastDir = oppositeDirection(lastDir);
                else
                    lastDir = nextDirection(lastDir);

                lastPos = _shipFirstHit;
                keepPosAndDir = true;
                continue;
            }

            // If position was already tried, try an other one
            if(_alreadyTried.contains(nextPos))
            {
                lastDir = nextDirection(lastDir);
                lastPos = _shipFirstHit;
                keepPosAndDir = true;
                continue;
            }
            else
            {
                ok = true;
            }

            // Set the next position
            position = nextPos;
        }
        else
        {
            // If no active ship is available, pick random position
            position = simpleMemory();
            _shipSunken = false;
            ok = true;
        }

    }while(!ok);

    _alreadyTried.append(position);
    setLastDirection(lastDir);

    return position;
}