Пример #1
0
void AVLTree::add(string newWord, Document * document) {
    cout << "nurrr" << document->getAuthor() << endl;
    
    
    if (root == NULL) {

        root = new AVLNode;

        root->data->setWord(newWord);
        root->data->addDocument(document);
        root->height = 1;

    } else if (root != NULL) {


        if (newWord < root->data->getWord()) {

            if (root->left == NULL) {

                root->left = new AVLNode;

                root->left->data->setWord(newWord);
                root->left->data->addDocument(document );
                root->left->height = 1;
            } else {

                _add(newWord, document, root->left);

            }

        } else if (newWord > root->data->getWord()) {

            if (root->right == NULL) {

                root->right = new AVLNode;

                root->right->data->setWord(newWord);
                root->right->height = 1;
            } else {

                _add(newWord, document, root->right);
            }
        } else if (newWord == root->data->getWord()) {
           
            root->data->addDocument(document);
        }

        //check for imballance



        if ((height(root->left) - height(root->right)) < -1) {

            if (height(root->right->left) - height(root->right->right) == -1) {
                rightRight(root);
            } else if (height(root->right->left) - height(root->right->right) == 1) {
                rightLeft(root);

            }

            root->height = height(root->right) + 1;
        } else if ((height(root->left) - height(root->right)) > 1) {

            if (height(root->left->left) - height(root->left->right) == -1) {
                leftRight(root);
            } else if (height(root->right->left) - height(root->right->right) == 1) {
                leftLeft(root);
            } else {

            }


            root->height = height(root->left) + 1;
        }
    }
    updateHeight(root);
}
Пример #2
0
void AVLTree::_add(string newWord, Document * document, AVLNode *& node) {
    if (newWord < node->data->getWord()) {

        if (node->left == NULL) {

            node->left = new AVLNode;

            node->left->data->setWord(newWord);
            node->left->data->addDocument(document);
            node->left->height = 1;
        } else {

            _add(newWord, document, node->left);

        }

    } else if (newWord > node->data->getWord()) {

        if (node->right == NULL) {

            node->right = new AVLNode;

            node->right->data->setWord(newWord);

            node->right->data->addDocument(document);
            node->right->height = 1;
        } else {

            _add(newWord, document, node->right);
        }
    } else if (newWord == node->data->getWord()) {
        
        node->data->addDocument(document);
    }

    //check for imballance


    if ((height(node->left) - height(node->right)) < -1) {

        if (height(node->right->left) - height(node->right->right) == -1) {
            rightRight(node);
        } else if (height(node->right->left) - height(node->right->right) == 1) {

            rightLeft(node);



        }

        node->height = height(node->right) + 1;
    } else if ((height(node->left) - height(node->right)) > 1) {

        if (height(node->left->left) - height(node->left->right) == -1) {
            leftRight(node);
        } else if (height(node->right->left) - height(node->right->right) == 1) {
            leftLeft(node);
        }

        node->height = height(node->left) + 1;
    } else {
        if (height(node->left) > height(node->right)) {
            node->height = height(node->left) + 1;
        } else {
            node->height = height(node->right) + 1;
        }


    }
    updateHeight(node);
}
Пример #3
0
int main()
{
    //Create window, and limit frame rate
    sf::RenderWindow window (sf::VideoMode(800, 600, 32), "Game", sf::Style::Default);
    window.setFramerateLimit(60);

//------------------------TEXTURES------------------------------

    //Declare textures
    sf::Texture texture;
    sf::Texture texture1;
    sf::Texture texture2;
    //Load image
    if(!texture.loadFromFile("Sprites/main.png"))
    {
        return 1;
    }
    if(!texture1.loadFromFile("Sprites/background.png"))
    {
        return 1;
    }
    if(!texture2.loadFromFile("Sprites/house.png"))
    {
        return 1;
    }

//------------------------SPRITES--------------------------

    //Creates and places the sprites
    sf::Sprite sprite;
    sf::Sprite background;
    sf::Sprite house;
    sprite.setPosition(400, 300);
    background.setPosition(0, 0);
    house.setPosition(440, 300);

    //Loads texture into sprite
    sprite.setTexture(texture);
    background.setTexture(texture1);
    house.setTexture(texture2);

//-------------------------RECTANGLES--------------------------------

    //Declares the rectangles
    sf::IntRect front(1, 1, 18, 24);
    sf::IntRect back (20, 1, 18, 24);
    sf::IntRect left (20, 26, 18, 24);
    sf::IntRect right (1, 26, 18, 24);
    //Steps
    sf::IntRect frontLeft(39, 1, 18, 24);
    sf::IntRect frontRight(39, 26, 18, 24);
    sf::IntRect backLeft();
    sf::IntRect backRight();
    sf::IntRect leftLeft();
    sf::IntRect leftRight();
    sf::IntRect rightLeft();
    sf::IntRect rightRight();

    sf::IntRect backgroundRect (0, 0, 800, 600);

    sf::IntRect houseRect (0, 0, 17, 22);


    //Crop sprites using rectangles defined above
    sprite.setTextureRect(front);
    background.setTextureRect(backgroundRect);
    house.setTextureRect(houseRect);

//-----------------------SOUND------------------------------------------------------

    //Declare the Sound Buffer
    sf::SoundBuffer footstepsBuffer;
    sf::SoundBuffer bumpBuffer;
    //Loads the sound file
    if(!footstepsBuffer.loadFromFile("Sounds/footsteps.wav"))
    {
        return 1;
    }
    if(!bumpBuffer.loadFromFile("Sounds/bump.wav"))
    {
        return 1;
    }

    //Declare sound
    sf::Sound footsteps;
    sf::Sound bump;
    //Load Buffer into Sound
    footsteps.setBuffer(footstepsBuffer);
    bump.setBuffer(bumpBuffer);

//-------------------------------MAIN-----------------------------

    //Main window loop
    while(window.isOpen())
    {
        sf::Event event;

        //Vectors used for collision
        sf::Vector2i spritePosition(sprite.getPosition());
        sf::Vector2i backgroundPosition(background.getPosition());
        sf::Vector2i housePosition(house.getPosition());

        //Sprite Vectors
        sf::Vector2i backVector(back.width, back.height);
        sf::Vector2i frontVector(front.width, front.height);
        sf::Vector2i rightVector(right.width, right.height);
        sf::Vector2i leftVector(left.width, left.height);

        //House Vectors
        sf::Vector2i houseVector(houseRect.width, houseRect.height);

        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
            {
                window.close();
            }

            if(event.key.code == sf::Keyboard::Insert)
            {
                sf::Image screenshot = window.capture();
                screenshot.saveToFile("Screenshot.png");
            }
        }

//-----------------------------------MOVEMENT----------------------------------------

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
            //Change to stepping sprite
            Sleep(250);
            sprite.setTextureRect(back);
            sprite.move(0, -24);
            //Redeclaring the collision textures
            sf::Vector2i spritePosition(sprite.getPosition());
            sf::Vector2i housePosition(house.getPosition());
            if(collision(spritePosition, backVector, housePosition, houseVector) == true)
            {
                sprite.move(0, 24);
                bump.play();
            }
            else
            {
                footsteps.play();
            }
        }

        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
            //Change to stepping sprite
            Sleep(250);
            sprite.setTextureRect(front);
            sprite.move(0, 24);
            //Redeclaring the collision textures
            sf::Vector2i spritePosition(sprite.getPosition());
            sf::Vector2i housePosition(house.getPosition());
            if(collision(spritePosition, frontVector, housePosition, houseVector) == true)
            {
                sprite.move(0, -24);
                bump.play();
            }
            else
            {
                footsteps.play();
            }
        }

        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
            //Change to stepping sprite
            Sleep(250);
            sprite.setTextureRect(right);
            sprite.move(19, 0);
            //Redeclaring the collision textures
            sf::Vector2i spritePosition(sprite.getPosition());
            sf::Vector2i housePosition(house.getPosition());
            if(collision(spritePosition, leftVector, housePosition, houseVector) == true)
            {
                sprite.move(-19, 0);
                bump.play();
            }
            else
            {
                footsteps.play();
            }
        }

        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
            //Change to stepping sprite
            Sleep(250);
            sprite.setTextureRect(left);
            sprite.move(-19, 0);
            //Redeclaring the collision textures
            sf::Vector2i spritePosition(sprite.getPosition());
            sf::Vector2i housePosition(house.getPosition());
            if(collision(spritePosition, rightVector, housePosition, houseVector) == true)
            {
                sprite.move(19, 0);
                bump.play();
            }
            else
            {
                footsteps.play();
            }
        }

        //Draw sequence
        window.clear(); //(Red, Green, Blue, (optional) Alpha) Alpha is transperency

        //Draw....

        window.draw(background);

        window.draw(house);

        window.draw(sprite);

        window.display();

        std::cout << x << std::endl;
        std::cout << y << std::endl;
    }
    return 0;
}