void Game::play(bool verbose) { while(getNumResources() > 0 && getNumPieces() > 1) round(); }
// play game until over void Game::play(bool verbose) { __verbose = verbose; round(); if(getNumPieces()>1 && getNumResources() > 0) { play(verbose); } }
void Game::play(bool verbose) { __verbose = verbose; __status = PLAYING; round(); if (getNumResources() > 0) { if (getNumPieces() > 0) { play(verbose); } } }
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++; }
void Game::play(bool verbose) { __status = PLAYING; if(verbose) { __verbose = verbose; while(getNumResources()) { std::cout << *this << std::endl; round(); } __status = OVER; std::cout << *this << std::endl; } else { while(getNumResources()) round(); __status = OVER; } }
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++; }
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; }
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++; }
// 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; } }
// getters unsigned int Game::getNumPieces() const { return getNumAgents() +getNumResources(); }
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++; }
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++; }