/**
 * Battleships Agent - C++ Implementation
 * Implements the full protocol with stateful ship placement.
 */
#include <iostream>
#include <string>
#include <vector>
#include <unordered_set>
#include <random>
#include <cstdlib>

// =============================================================================
// State
// =============================================================================

std::unordered_set<std::string> alreadyFired;

const std::vector<std::string> shipsToPlace = {
    "PLACE C A1 N",   // Carrier (5 cells)
    "PLACE B C1 N",   // Battleship 1 (4 cells)
    "PLACE B E1 N",   // Battleship 2 (4 cells)
    "PLACE D G1 N",   // Destroyer 1 (3 cells)
    "PLACE D I1 N",   // Destroyer 2 (3 cells)
    "PLACE P A7 D",   // Patrol 1 (2 cells)
    "PLACE P A9 D",   // Patrol 2 (2 cells)
    "PLACE P H8 N",   // Patrol 3 (2 cells)
};

int currentShipIndex = 0;
bool preparationDone = false;

// Random number generator
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dist(1, 10);

// =============================================================================
// Command Detection
// =============================================================================

bool startsWith(const std::string& line, const std::string& prefix) {
    return line.rfind(prefix, 0) == 0;
}

// =============================================================================
// Actions
// =============================================================================

void send(const std::string& message) {
    std::cout << message << std::endl;
    std::cout.flush();
}

void placeNextShip() {
    if (currentShipIndex < static_cast<int>(shipsToPlace.size())) {
        send(shipsToPlace[currentShipIndex]);
    }
}

void fireRandom() {
    std::string pos;
    do {
        int row = dist(gen);              // 1-10
        char rowChar = 'A' + row - 1;     // A-J
        int column = dist(gen);           // 1-10
        pos = rowChar + std::to_string(column);
    } while (alreadyFired.count(pos));
    
    send("FIRE " + pos);
    alreadyFired.insert(pos);
}

// =============================================================================
// Command Handlers
// =============================================================================

void handleGameStarted() {
    currentShipIndex = 0;
    preparationDone = false;
    placeNextShip();
}

void handleOk() {
    if (preparationDone) {
        return;
    }
    
    currentShipIndex++;
    if (currentShipIndex < static_cast<int>(shipsToPlace.size())) {
        placeNextShip();
    } else if (currentShipIndex == static_cast<int>(shipsToPlace.size())) {
        send("READY");
    } else {
        preparationDone = true;
    }
}

void handleYourTurn() {
    fireRandom();
}

void handleGameOver() {
    std::exit(0);
}

void handleYouWon() {
    std::exit(0);
}

void handleKo(const std::string& line) {
    std::cerr << "Error: " << line << std::endl;
}

// =============================================================================
// Main Loop
// =============================================================================

void processLine(const std::string& line) {
    if (startsWith(line, "GAME STARTED")) {
        handleGameStarted();
    } else if (startsWith(line, "YOUR TURN")) {
        handleYourTurn();
    } else if (startsWith(line, "GAME OVER")) {
        handleGameOver();
    } else if (startsWith(line, "YOU WON")) {
        handleYouWon();
    } else if (startsWith(line, "OK")) {
        handleOk();
    } else if (startsWith(line, "KO")) {
        handleKo(line);
    }
}

int main() {
    std::string line;
    while (std::getline(std::cin, line)) {
        processLine(line);
    }
    return 0;
}
