Example #1
0
    void Game::round()
    {
         __status = PLAYING;
         for(int i = 0; i < __grid.size(); ++i)
             if(__grid[i] != NULL && !(__grid[i]->getTurned()))
             {
                 __grid[i]->setTurned(true);
                 ActionType ac = __grid[i]->takeTurn(getSurroundings(__grid[i]->getPosition()));

                 if(ac == STAY)
                     continue;

                 Position moved = move(__grid[i]->getPosition(), ac);
                 int j = moved.x * __width + moved.y;
                 if(__grid[j] != NULL)
                 {
                     __grid[i]->operator*(*__grid[j]);
                     if(__grid[i]->isViable())
                     {
                          delete __grid[j];
                          __grid[j] = __grid[i];
                          __grid[i]->setPosition(moved);
                          __grid[i] = NULL;
                     }
                     else if(__grid[j]->isViable())
                     {
                         delete __grid[i];
                         __grid[i] = NULL;
                     }
                     else
                     {
                          delete __grid[i];
                          delete __grid[j];
                          __grid[i] = NULL;
                          __grid[j] = NULL;
                     }
                 }
                 else
                 {
                      __grid[i]->setPosition(moved);
                      __grid[j] = __grid[i];
                      __grid[i] = NULL;
                 }
             }

         for(int i = 0; i < __grid.size(); ++i)
            if(__grid[i] != NULL)
            {
                __grid[i]->setTurned(false);
                __grid[i]->age();
                if(!__grid[i]->isViable())
                {
                    delete __grid[i];
                    __grid[i] = NULL;
                }
            }
         __round++;
    }
 void Game::round() {
     //using the set example algorithm from the read me
     std::set<Piece*> board;
     Position pos, Newpos;
     ActionType action;
     Piece* pptr;
     int location;
     for (auto it = __grid.begin(); it < __grid.end(); ++it) {
         if (*it != nullptr) {
             board.insert(board.end(),*it);
             (*it)->setTurned(false);                                            //resets the pieces so that they can take a turn this will reset after each round
         }
     }
     for (auto it = board.begin(); it != board.end(); ++it) {
         if ((*it)->isViable()&& (*it)->getTurned()) {
             (*it)->age();
             (*it)->setTurned(true);
             pos = (*it)->getPosition();                                          //will pass a pieces position or location in
             action = (*it)->takeTurn(getSurroundings(pos));                     //piece takes a turn for each
             Newpos = this->move(pos, action);                                   //passes the new location into the new variabale
             location = Newpos.y + (Newpos.x * __width);
             pptr = __grid[location];
             if(pos.x != Newpos.x && pos.y != Newpos.y){                         //checks if new pos and the current pos arent n the same location
                 if (pptr != nullptr) {                                                  //this happens if the piece wants to move to annother spot hwere there is another piece in it soit will challnege or cosume if its a resource
                     (*(*it)* *pptr);                                            //this means that the pieces interact and we will pass some if statmnts
                  if ((*it)->isViable()) {                                    //if it didn't get consumed we pass it set new position
                     (*it)->setPosition(Newpos);
                     __grid[Newpos.y + (Newpos.x * __width)] = *it;                //will change the grid and set equal to it and also set it equal to nullptr
                     __grid[pos.y + (pos.x * __width)] = nullptr;
                 }
                 else {                                                      //when a resource gets consumed you set that location = to null ptr
                     __grid[pos.y + (pos.x * __width)] = nullptr;
                 }
             }
             }
             else {
                 __grid[Newpos.y + (Newpos.x * __width)] = *it;              //else to the other staments if the position is emoyty
             }
         }
     }
     for (int i = 0; i < __grid.size(); ++i) {
         //if (!__grid[i]->isViable() && __grid[i] != nullptr) {
         if(__grid[i] != nullptr && !__grid[i]->isViable()){
             delete __grid[i];
             __grid[i] = nullptr;
         }
     }
     if (getNumResources() == 0)
         this->__status = OVER;
     //we need to increment the round after it is over
     __round++;
 }
 bool Game::isLegal(const ActionType &ac, const Position &pos) const {
     //exceptiond
     if (pos.x < 0 || pos.x >= __height || pos.y < 0 || pos.y >= __width)
         throw OutOfBoundsEx(__width, __height, pos.x, pos.y);
     Surroundings surround;
     surround = getSurroundings(pos);        //surround will be qual to the surroudings recieved from whats passed into th function pos
     bool isLeg = false;     //or maybe false IDK
     //todo switch statments or if statments where we will make each case in tha surrounds either true or false
     //PASS action type into the switch
     switch(ac){
         case N:
             if (surround.array[1] != INACCESSIBLE)
                 isLeg = true;
             break;
         case NE:
             if (surround.array[2] != INACCESSIBLE)
                 isLeg = true;
             break;
         case NW:
             if (surround.array[0] != INACCESSIBLE)
                 isLeg = true;
             break;
         case E:
             if (surround.array[5] != INACCESSIBLE)
                 isLeg = true;
             break;
         case W:
             if (surround.array[3] != INACCESSIBLE)
                 isLeg = true;
             break;
         case SE:
             if (surround.array[8] != INACCESSIBLE)
                 isLeg = true;
             break;
         case SW:
             if (surround.array[6] != INACCESSIBLE)
                 isLeg = true;
             break;
         case S:
             if (surround.array[7] != INACCESSIBLE)
                 isLeg = true;
             break;
         case STAY:
             isLeg = true;
             break;
     }
     //each case will be for what direction it moves in the switch statement
     //todo////
     return isLeg;                 //if its true or false
 }
Example #4
0
    bool Game::isLegal(const ActionType &ac, const Position &pos) const{
        Surroundings surround = getSurroundings(pos);
        ActionType moves[9] =  {NW,N,NE,W,STAY,E,SW,S,SE};
        int direction;
        bool legal;
        for (int i = 0; i < 9; ++i) {
            if(moves[i] == ac) {
                direction = i;
                break;
            }
        }
        legal = surround.array[direction] != INACCESSIBLE;

        return legal;
    }
Example #5
0
    void Game::round() {
        std::set<Piece*> pc;
        __status = PLAYING;

        for(int i = 0; i < __grid.size(); i++) {
            if(__grid[i] != nullptr){
                if(!(__grid[i]->isViable())){
                    __grid[i] = nullptr;
                }
            }
        }

        for (auto it = __grid.begin(); it != __grid.end(); ++it) {
            if(*it != nullptr){
                pc.insert(pc.end(), *it);
                (*it)->setTurned(false);
            }
        }

        for (auto it = pc.begin(); it != pc.end(); ++it) {
            Position p1 = (*it)->getPosition();
            ActionType a = (*it)->takeTurn(getSurroundings(p1));
            Position p2 = move(p1, a);

            if (!(*it)->getTurned()) {
                (*it)->age();
                (*it)->setTurned(true);

                if(p1.x != p2.x || p1.y != p2.y){
                        if((*it)->getPosition().x != p1.x || (*it)->getPosition().y != p1.y){
                            __grid[p2.y + (p2.x * __width)] = (*it);
                            __grid[p1.y + (p1.x * __width)] = __grid[p2.y + (p2.x * __width)];
                        }
                    else{
                        (*it)->setPosition(p2);
                        __grid[p2.y + (p2.x * __width)] = (*it);
                        __grid[p1.y + (p1.x * __width)] = nullptr;
                    }
                }
            }
        }
        __round++;
    }
Example #6
0
 bool Game::isLegal(const ActionType &ac, const Position &pos) const
 {
     Surroundings s = getSurroundings(pos);
     int direction;
     switch (ac)
     {
         case N:
             direction = 1;
             break;
         case NE:
             direction = 2;
             break;
         case NW:
             direction = 0;
             break;
         case E:
             direction = 5;
             break;
         case W:
             direction = 3;
             break;
         case SE:
             direction = 8;
             break;
         case SW:
             direction = 6;
             break;
         case S:
             direction = 7;
             break;
         case STAY:
             direction = 4;
     }
     if (s.array[direction] != INACCESSIBLE)
         return true;
     return false;
 }
Example #7
0
void Game::round()
    {
       
        __status = PLAYING;
        ActionType ac;
        Position pos;
        
        for(int i =0; i<__grid.size(); ++i)
        {
            
            if(__grid[i]!= nullptr && ! __grid[i]->getTurned())
            {
                __grid[i]->setTurned(true);
                
                //Moving pieces and getting the surroundings during each round.
                ac = __grid[i]->takeTurn(getSurroundings(__grid[i]->getPosition()));
                
                pos = move(__grid[i]->getPosition(), ac);
                int place = pos.x * __width + pos.y;
            
                //If theres a piece on the grid check to see if its a valid piece, then move te piece to the correct pos.
                // If piece is valid, delete it then replace it with the other piece.
                if (__grid[place] != nullptr)
                {
                    __grid[i]->operator*(*(__grid[place]));
                    
                    if (__grid[i]->isViable())
                    {

                        delete __grid[place];
                        __grid[place] = __grid[i];
                        __grid[i]->setPosition(pos);
                        __grid[i] = nullptr;
                    }
                    else if (__grid[place]->isViable())
                    {
        
                        delete __grid[i];
                        __grid[i] = nullptr;
                     
                    }
                    else
                    {
                        delete __grid[place];
                        delete __grid[i];
                        __grid[place]=nullptr;
                        __grid[i]=nullptr;
                    }
                }
                
            else
                {
                __grid[i]->setPosition(pos);
                __grid[place] = __grid[i];
                __grid[i] = nullptr;
                    
                }
            }
        }
        
        //Aging pieces and deleting pieces on the grid for end of round.
        for(int i = 0; i < __grid.size(); ++i)
            if(__grid[i] != NULL)
            {
                __grid[i]->setTurned(false);
                __grid[i]->age();
                
                if(!__grid[i]->isViable())
                {
                    delete __grid[i];
                    __grid[i] = NULL;
                }
            }
        __round++;
   
    }
Example #8
0
    void Game::round()   // play a single round
    {
		std::set<Piece*> pieces;
		for (auto it = __grid.begin(); it != __grid.end(); ++it)
		{
			if (*it)
			{
				pieces.insert(pieces.end(), *it);
				(*it)->setTurned(false);
			}
		}

		// Take turns
		for (auto it = pieces.begin(); it != pieces.end(); ++it)
		{
			if (!(*it)->getTurned())
			{
				(*it)->setTurned(true);
				(*it)->age();

				ActionType ac = (*it)->takeTurn(getSurroundings((*it)->getPosition()));
				Position pos0 = (*it)->getPosition();
				Position pos1 = move(pos0, ac);

				if (pos0.x != pos1.x || pos0.y != pos1.y)
				{
					Piece *p = __grid[pos1.y + (pos1.x * __width)];
					if (p)
					{
						(*(*it)) * (*p);
						if ((*it)->getPosition().x != pos0.x || (*it)->getPosition().y != pos0.y)
						{
							// piece moved
							__grid[pos1.y + (pos1.x * __width)] = (*it);
							__grid[pos0.y + (pos0.x * __width)] = p;
						}
					}
					else
					{
						// empty move
						(*it)->setPosition(pos1);
						__grid[pos1.y + (pos1.x * __width)] = (*it);
						__grid[pos0.y + (pos0.x * __width)] = nullptr;
					}
				}
			}
		}

		// Update positions and delete unviable first
		for (unsigned int i = 0; i < __grid.size(); ++i)
			if (__grid[i] && !(__grid[i]->isViable()))
			{
				delete __grid[i];
				__grid[i] = nullptr;
			}

		// Check game over
		if (getNumResources() <= 0)
			__status = Status::OVER;

		__round++;
    }
Example #9
0
    void Game::round(){
        
        if(__round == 0 && __verbose){
            __status = PLAYING;
            cout << endl << *this;
        }
        for (int i = 0; i < __grid.size(); ++i) {
            if(__grid[i]!= nullptr){
                if(__grid[i]->isViable()) {
                    if (!__grid[i]->getTurned()) {
                        Agent * agent = dynamic_cast<Agent*>(__grid[i]);
                        if(agent) {
                            __grid[i]->setTurned(true);
                            Position currentPos = __grid[i]->getPosition();
                            Surroundings surround = getSurroundings(currentPos);
                            ActionType action = __grid[i]->takeTurn(surround);
                            if (action != STAY) {
                                Position newPos = move(currentPos, action);
                                int newPosIndx = (newPos.x * __width + newPos.y);
                                (*__grid[i]) * (*__grid[newPosIndx]);
                                if(!__grid[i]->isViable()){
                                    delete __grid[i];
                                    __grid[i]= nullptr;
                                }
                                else {
                                    __grid[i]->setPosition(newPos);
                                    if (__grid[newPosIndx] != nullptr) {
                                        delete __grid[newPosIndx];
                                        __grid[newPosIndx] = __grid[i];
                                        __grid[i] = nullptr;
                                    }
                                    else {
                                        __grid[newPosIndx] = __grid[i];
                                        __grid[i] = nullptr;
                                    }
                                }
                                if(!__grid[newPosIndx]->isViable()){
                                    delete __grid[newPosIndx];
                                    __grid[newPosIndx]= nullptr;
                                }
                            }
                        }
                    }
                }
            }
        }
        for (int j = 0; j < __grid.size(); ++j) {
            if(__grid[j] != nullptr) {
                if (!__grid[j]->isViable()) {
                    delete __grid[j];
                    __grid[j] = nullptr;
                }
                else {
                    __grid[j]->setTurned(false);
                    __grid[j]->age();
                }
            }
        }
        if(getNumPieces()< 2 || getNumResources() < 1)
            __status = OVER;
        ++__round; //

        if(__verbose)
            cout << endl << *this;

    }
Example #10
0
    void Game::round()
    {
        if (__verbose && __round == 0)
            __status = Status::PLAYING;

        for (int count = 0; count < __grid.size(); ++count)
        {
            if (__grid[count] != nullptr && !__grid[count]->getTurned() && __grid[count]->isViable())
            {

                Agent *a = dynamic_cast<Agent *>(__grid[count]);

                if (a)
                {
                    __grid[count]->setTurned(true);

                    Position p = __grid[count]->getPosition();
                    Surroundings s = getSurroundings(p);
                    ActionType act = __grid[count]->takeTurn(s);


                    if (act != STAY)
                    {
                        Position newp = move(p, act);
                        int i = (newp.x * __width + newp.y);

                        (*__grid[count]) * (*__grid[i]);


                        if (!__grid[count]->isViable())
                        {
                            delete __grid[count];
                            __grid[count] = nullptr;
                        }

                        else
                        {
                            __grid[count]->setPosition(newp);

                            if (__grid[i] == nullptr)
                            {
                                __grid[i] = __grid[count];
                                __grid[count] = nullptr;
                            }

                            else
                            {
                                delete __grid[i];
                                __grid[i] = __grid[count];
                                __grid[count] = nullptr;

                            }
                        }

                        if (!__grid[i]->isViable())
                        {
                            delete __grid[i];
                            __grid[i] = nullptr;
                        }
                    }
                }
            }
        }

        for (int i = 0; i < __grid.size(); ++i)
        {
            if (__grid[i] != nullptr)
            {
                if (__grid[i]->isViable())
                {
                    __grid[i]->setTurned(false);
                    __grid[i]->age();

                }

                else
                {
                    delete __grid[i];
                    __grid[i] = nullptr;
                }
            }
        }

        if (getNumResources() < 1)
        {
            __status = Status::OVER;
        }

        __round++;
    }
Example #11
0
    // play a single round
    void Game::round() {

        if(__round == 0 && __verbose) {
            __status = PLAYING;
            std::cout << std::endl << *this;
        }
        for (int count = 0; count < __grid.size(); ++count) {
            if(__grid[count]!= nullptr) {
                if(__grid[count]->isViable()) {
                    if (! __grid[count]->getTurned()) {
                        Agent * agent = dynamic_cast<Agent*>(__grid[count]);
                        if(agent) {
                            __grid[count]->setTurned(true);
                            Position currPosition = __grid[count]->getPosition();
                            Surroundings s = getSurroundings(currPosition);
                            ActionType aT = __grid[count]->takeTurn(s);

                            if (aT != STAY) {
                                Position newPosition = move(currPosition, aT);
                                int newPosIndex = (newPosition.x * __width + newPosition.y);
                                (*__grid[count]) * (*__grid[newPosIndex]);

                                if(! __grid[count]->isViable())
                                {
                                    delete __grid[count];
                                    __grid[count]= nullptr;
                                }

                                else
                                {
                                    __grid[count]->setPosition(newPosition);

                                    if (__grid[newPosIndex] != nullptr)
                                    {
                                        delete __grid[newPosIndex];
                                        __grid[newPosIndex] = __grid[count];
                                        __grid[count] = nullptr;
                                    }

                                    else
                                    {
                                        __grid[newPosIndex] = __grid[count];
                                        __grid[count] = nullptr;
                                    }
                                }

                                if(! __grid[newPosIndex]->isViable())
                                {
                                    delete __grid[newPosIndex];
                                    __grid[newPosIndex]= nullptr;
                                }
                            }
                        }
                    }
                }
            }
        }
        for (int i = 0; i < __grid.size(); i++) {
            if(__grid[i] != nullptr) {
                if (!__grid[i]->isViable()) {
                    delete __grid[i];
                    __grid[i] = nullptr;
                }
                else
                {
                    __grid[i]->setTurned(false);
                    __grid[i]->age();
                }
            }
        }
        if(getNumPieces()< 2 || getNumResources() < 1)
        {
            __status = OVER;
        }
        ++__round;

        if(__verbose)
        {
            std::cout << std::endl << *this;
        }

    }
Example #12
0
    void Game::round() {   // play a single round
        //__status = PLAYING;
        //std::cout << "Round started" << std::endl;
        std::set<Piece*> pieces;
        for (auto it = __grid.begin(); it != __grid.end(); ++it) {
            if (*it) {
                pieces.insert(pieces.end(), *it);
                //pieces.insert(*it);
                (*it)->setTurned(false);
            }
        }

        // Take turns
        for (auto it = pieces.begin(); it != pieces.end(); ++it) {
            if (!(*it)->getTurned()) {
                (*it)->setTurned(true);
                (*it)->age();
                ActionType ac = (*it)->takeTurn(getSurroundings((*it)->getPosition()));
                //std::cout << "------- Game::round -------" << std::endl;
                //std::cout << "Action: " << ac << std::endl;
                Position pos0 = (*it)->getPosition();
                //std::cout << "Pos0: " << pos0.x << "x" << pos0.y << std::endl;
                Position pos1 = move(pos0, ac);
                //std::cout << "Pos1: " << pos1.x << "x" << pos1.y << std::endl;
                if (pos0.x != pos1.x || pos0.y != pos1.y) {
                    Piece *p = __grid[pos1.y + (pos1.x * __width)];
                    if (p) {
                        (*(*it)) * (*p);
                        if ((*it)->getPosition().x != pos0.x || (*it)->getPosition().y != pos0.y) {
                            // piece moved
                            __grid[pos1.y + (pos1.x * __width)] = (*it);
                            __grid[pos0.y + (pos0.x * __width)] = p;
                        }
                    } else {
                        // empty move
                        (*it)->setPosition(pos1);
                        __grid[pos1.y + (pos1.x * __width)] = (*it);
                        __grid[pos0.y + (pos0.x * __width)] = nullptr;
                        //std::cout << "position updated of piece" << std::endl;
                    }
                }
            }
        }

        // Update positions and delete
        // Delete invalid first
        for (unsigned int i = 0; i < __grid.size(); ++i) {
            //if (__grid[i]) std::cout << "Piece viable: " << __grid[i]->isViable() << std::endl;
            if (__grid[i] && !(__grid[i]->isViable())) {
                delete __grid[i];
                __grid[i] = nullptr;
            }
            //if (__grid[i]) __grid[i]->age();
        }

        // Update positions of remaining
        /*for (unsigned int i = 0; i < __grid.size(); ++i) {
            Piece *currentPiece = __grid[i];
            if (currentPiece) {
                Position pos = currentPiece->getPosition();
                if (__grid[pos.y + (pos.x * __width)] != currentPiece) {
                    __grid[pos.y + (pos.x * __width)] = currentPiece;
                    __grid[i] = nullptr;
                    //std::cout << "place in __grid changed of a piece" << std::endl;
                }
            }
            i++;
        }*/

        // Check game over
        if (getNumResources() <= 0) {
            __status = Status::OVER;
        }
        __round++;
    }
Example #13
0
	void Game::round()
	{
		std::set<Piece*> gamePieces;
		for (auto reset = __grid.begin(); reset != __grid.end(); ++reset)
		{
			if (*reset)
			{
				gamePieces.insert(gamePieces.end(), *reset);
			
				(*reset)->setTurned(false);
			}
		}

	
		for (auto turn = gamePieces.begin(); turn != gamePieces.end(); ++turn) 
		{
			if (!(*turn)->getTurned()) 
			{
				(*turn)->setTurned(true);
				(*turn)->age();
				ActionType ac = (*turn)->takeTurn(getSurroundings((*turn)->getPosition()));
				Position pBefore = (*turn)->getPosition();
			
				Position pAfter = move(pBefore, ac);
			
				if (pBefore.x != pAfter.x || pBefore.y != pAfter.y) 
				{
					Piece *p = __grid[pAfter.y + (pAfter.x * __width)];
					if (p) 
					{
						(*(*turn)) * (*p);
						if ((*turn)->getPosition().x != pBefore.x || (*turn)->getPosition().y != pBefore.y) 
						{
						
							__grid[pAfter.y + (pAfter.x * __width)] = (*turn);
							__grid[pBefore.y + (pBefore.x * __width)] = p;
						}
					}
					else 
					{
						
						(*turn)->setPosition(pAfter);
						__grid[pAfter.y + (pAfter.x * __width)] = (*turn);
						__grid[pBefore.y + (pBefore.x * __width)] = nullptr;
						
					}
				}
			}
		}

		for (unsigned int i = 0; i < __grid.size(); ++i) 
		{
			
			if (__grid[i] && !(__grid[i]->isViable())) 
			{
				delete __grid[i];
				__grid[i] = nullptr;
			}
		
		}

		if (getNumResources() <= 0) 
		{
			__status = Status::OVER;
		}
		__round++;
	}