Пример #1
0
bool Robot::Moove(int counterColumn,int counterLigne,std::vector<std::vector<Box*> > matrix)
{
    //A simple algorithm, trying first to go right then down, left and up.
    bool endgame = false;
    if(x+1<counterColumn&&(!this->HasPassed((matrix.at(y).at(x+1))))&&matrix.at(y).at(x+1)->IsAvailable())
            {
            Box* c = matrix.at(y).at(x+1);

                 x = x+1;
                 endgame = c->EndGame();
                 this -> AddToList(c);
                 this-> AddToStack(c);
                 this -> AddToListMooves('R');
                 this -> AddCash();

            }

        else if(y+1<counterLigne&&(!this->HasPassed((matrix.at(y+1).at(x))))&&matrix.at(y+1).at(x)->IsAvailable())
        {
            Box* c = matrix.at(y+1).at(x);

                 y = y+1;
                 endgame = c->EndGame();
                 this -> AddToList(c);
                 this-> AddToStack(c);
                 this -> AddToListMooves('D');
                 this -> AddCash();

        }
        else if(x-1>=0&&(!this->HasPassed((matrix.at(y).at(x-1))))&&matrix.at(y).at(x-1)->IsAvailable())
        {
            Box* c = matrix.at(y).at(x-1);

                 x = x-1;
                 endgame = c->EndGame();
                 this -> AddToList(c);
                 this-> AddToStack(c);
                 this -> AddToListMooves('L');
                 this -> AddCash();

        }

        else if(y-1>=0&&(!this->HasPassed((matrix.at(y-1).at(x))))&&matrix.at(y-1).at(x)->IsAvailable())
        {
            Box* c = matrix.at(y-1).at(x);

                 y = y-1;
                 endgame = c->EndGame();
                 this -> AddToList(c);
                 this-> AddToStack(c);
                 this -> AddToListMooves('U');
                 this -> AddCash();

        }

        else //If the robot is stuck:
        {
            try{Box* lastbox = this -> Return(); //It tries to go to the last box

            Box* currentbox = new Box(this->GetY(), this ->GetX());
            this -> AddToListMooves(lastbox->Compare(currentbox));  //This method give the movement of the robot while returning
                                                                    //U, R, D, L
            x=lastbox-> GetX();  //Move the robot
            y= lastbox->GetY();

            this -> AddCash();
            }
            catch(std::string const& chaine)
    {
        cerr << chaine << endl;
       endgame = true;
        }
    }
    return endgame;
}