Untitled

 avatar
unknown
c_cpp
2 months ago
2.6 kB
2
Indexable
void Antenna::Apply(Grid* pGrid, Player* pPlayer) {
    // Get Output and Input interfaces
    Output* pOut = pGrid->GetOutput();
    Input* pIn = pGrid->GetInput();
    int x, y;
 // Ensure the antenna exists
    if (!pGrid->HasAntenna()) {
        pOut->PrintMessage("Error: No antenna exists on the grid. Click anywhere to continue...");
        pIn->GetPointClicked(x, y);
        pOut->ClearStatusBar();
        return;
    }

    // Print a message to notify the player
    pOut->PrintMessage("The antenna will decide the turn of players. Click to continue...");
    pIn->GetPointClicked(x, y); // Wait for the user to click
    pOut->ClearStatusBar();

   
    // Initialize players and distances
    Player* player1 = pGrid->GetCurrentPlayer(); // Get first player
    pGrid->AdvanceCurrentPlayer(); // Move to the second player
    Player* player2 = pGrid->GetCurrentPlayer(); // Get second player

    CellPosition player1Pos = player1->GetCell()->GetCellPosition();
    CellPosition player2Pos = player2->GetCell()->GetCellPosition();
    CellPosition antennaPos = this->GetPosition();

    // Calculate Manhattan distances for both players
    int player1Distance = (player1Pos.VCell() > antennaPos.VCell() 
                               ? player1Pos.VCell() - antennaPos.VCell() 
                               : antennaPos.VCell() - player1Pos.VCell()) +
                          (player1Pos.HCell() > antennaPos.HCell() 
                               ? player1Pos.HCell() - antennaPos.HCell() 
                               : antennaPos.HCell() - player1Pos.HCell());

    int player2Distance = (player2Pos.VCell() > antennaPos.VCell() 
                               ? player2Pos.VCell() - antennaPos.VCell() 
                               : antennaPos.VCell() - player2Pos.VCell()) +
                          (player2Pos.HCell() > antennaPos.HCell() 
                               ? player2Pos.HCell() - antennaPos.HCell() 
                               : antennaPos.HCell() - player2Pos.HCell());

    // Determine the first player
    Player* firstPlayer = nullptr;
    if (player1Distance < player2Distance || 
        (player1Distance == player2Distance && player1->GetPlayerNumber() < player2->GetPlayerNumber())) {
        firstPlayer = player1;
    } else {
        firstPlayer = player2;
    }

    // Notify the user about the first player
    pOut->PrintMessage("Player " + to_string(firstPlayer->GetPlayerNumber()) + " will play first. Click anywhere to continue...");
    pIn->GetPointClicked(x, y);
    pOut->ClearStatusBar();
}
Leave a Comment