// remove lower line in game field // return true if there are still tiles that can be dropped down // in contest const bool GameField::removeLowerLine(const bool contest) { #ifdef DEBUG std::cout << "GameField::removeLowerLine(" << contest << ")" << std::endl; #endif bool ok = false; // create array with lower line ScoredTileArray tArray; ScoredTile sTile( FieldPos(0, 0), 10, ScoredTile::FIELDDIRECTION_RIGHT, m_field[0][0], 0 ); tArray.push_back(sTile); // remove tiles removeTiles(tArray); // let all existing tiles fall to the ground ok = fallTilesToGround(contest); // and fill with new ones fillEmptyTiles(); // clear array again tArray.clear(); return ok; }
// add points from removed tiles void Player::addPoints(const ScoredTileArray& tArray) { // delete the positions stored in the array for ( std::vector<ScoredTile>::const_iterator it = tArray.begin(); it < tArray.end(); it++ ) { addPoints(*it); } }
// search for equal entries and removes them on the set // return true if something has been removed void GameField::removeTiles(const ScoredTileArray& tArray) { // delete the positions stored in the array for ( std::vector<ScoredTile>::const_iterator it = tArray.begin(); it < tArray.end(); it++ ) { // std::cout << "GameField::removeTiles() " // << (*it).getPos().x() << " " << (*it).getPos().y() << std::endl; removeTiles(*it); } }
// remove tiles and fill the game fields until only maximum // two same tiles are next to each other void GameField::cascade(void) { // print(); ScoredTileArray tArray; while ( findSameTiles(tArray) ) { // remove tiles removeTiles(tArray); // first let all existing tiles fall to the ground fallTilesToGround(false); // and fill with new ones fillEmptyTiles(); // clear array again tArray.clear(); } }
// search for equal tiles in columns // return true if something has been found const bool GameField::findSameTilesInColumns(ScoredTileArray& tArray) const { bool found = false; // we will go through each column for ( unsigned int i = 0; i < FIELD_WIDTH; i++ ) { // reset length unsigned int length = 0; unsigned int bombValue = 0; for ( unsigned int j = 0; j < FIELD_HEIGHT; j++ ) { if ( 0 == j || m_field[i][j] == m_field[i][j-1] ) { // same tile as before or new start tile length++; // check if bomb and add bomb value if ( m_field[i][j].isBomb() ) { bombValue += m_field[i][j].getBombValue(); } // if the last tile in this column // we must check the length if ( FIELD_HEIGHT-1 == j && length >= 3 ) { // only store tiles if length is >= 3 ScoredTile sTile( FieldPos(i, j-length+1), length, ScoredTile::FIELDDIRECTION_UP, m_field[i][j], bombValue ); tArray.push_back(sTile); found = true; //std::cout << "GameField::findSameTilesInColumns1() " //<< i << " " //<< j-length+1 << " " //<< length << " " //<< (char)m_field[i][j].getType() << " " //<< bombValue //<< std::endl; } } else { // new tile here if ( length >= 3 ) { // only store tiles if length is >= 3 ScoredTile sTile( FieldPos(i, j-length), length, ScoredTile::FIELDDIRECTION_UP, m_field[i][j-1], bombValue ); tArray.push_back(sTile); found = true; //std::cout << "GameField::findSameTilesInColumns2() " //<< i << " " //<< j-length << " " //<< length << " " //<< (char)m_field[i][j-1].getType() << " " //<< bombValue //<< std::endl; } // set new length length = 1; bombValue = 0; // check if bomb and add bomb value if ( m_field[i][j].isBomb() ) { bombValue += m_field[i][j].getBombValue(); } } } } return found; }
// remove same tiles on field and give points to player const bool Game::cascade(const bool contest) { ScoredTileArray tArray; bool ok = true; // flag if first loop bool first = true; while ( m_field.findSameTiles(tArray) ) { #ifdef DEBUG // print game field if (!first) { std::cout << "Game::cascade() info: Intermediate game field" << std::endl; m_field.print(); } #endif // remove tiles m_field.removeTiles(tArray); // add points for player m_player[m_currentPlayer].addPoints(tArray); // calculate damage for opponent unsigned int damage = m_player[m_currentPlayer].getOpponentDamage(); #ifdef DEBUG if ( damage > 0 ) { std::cout << "Game::cascade() info: opponent looses " << damage << " life." << std::endl; } #endif m_player[(m_currentPlayer+1)%2].looseLife( damage ); // first let all existing tiles fall to the ground if ( !m_field.fallTilesToGround(contest) ) { if ( !contest ) { // we need some random tiles m_field.fillEmptyTiles(); } else { // we do not have any more tiles for contest ok = false; break; } } // clear array again tArray.clear(); first = false; } return ok; }