void Colosseum::pushAllVillains(int r, int c, int dir) { // pushes all villains at [r,c] in the given direction // Note that if r,c isn't a valid location, this will // do nothing. We could instead have checked // to see if it's in range, and skipped the loop // in that case. // (or we could do bounds checking before calling, etc) for (int k = m_nVillains-1; k >= 0; k--) { Villain* vp = m_villains[k]; if( vp->row() == r && vp->col() == c ) { vp->push( dir ); if (vp->isDead()) { delete vp; // The order of Villain pointers in the m_villains array is // irrelevant, so it's easiest to move the last pointer to // replace the one pointing to the now-deleted villain. Since // we are traversing the array from last to first, we know this // last pointer does not point to a dead villain. m_villains[k] = m_villains[m_nVillains-1]; m_nVillains--; } } } }
int Colosseum::numberOfVillainsAt(int r, int c) const { int count = 0; for (int k = 0; k < m_nVillains; k++) { Villain* vp = m_villains[k]; if (vp->row() == r && vp->col() == c) count++; } return count; }
void Colosseum::moveVillains() { // Move all villains for (int k = m_nVillains-1; k >= 0; k--) { Villain* vp = m_villains[k]; vp->move(); if (m_player != NULL && vp->row() == m_player->row() && vp->col() == m_player->col()) m_player->setDead(); } // Another turn has been taken m_turns++; }