/**
 * Battleships Agent - C Implementation
 * Implements the full protocol with stateful ship placement.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

/* =============================================================================
 * State
 * ========================================================================== */

#define MAX_FIRED 200

char already_fired[MAX_FIRED][4];  /* e.g. "A10" */
int fired_count = 0;

const char* ships_to_place[] = {
    "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) */
};
const int ships_count = 8;

int current_ship_index = 0;
int preparation_done = 0;

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

int starts_with(const char* line, const char* prefix) {
    return strncmp(line, prefix, strlen(prefix)) == 0;
}

/* =============================================================================
 * Actions
 * ========================================================================== */

void send_message(const char* message) {
    printf("%s\n", message);
    fflush(stdout);
}

void place_next_ship(void) {
    if (current_ship_index < ships_count) {
        send_message(ships_to_place[current_ship_index]);
    }
}

int already_fired_contains(const char* pos) {
    for (int i = 0; i < fired_count; i++) {
        if (strcmp(already_fired[i], pos) == 0) {
            return 1;
        }
    }
    return 0;
}

void already_fired_add(const char* pos) {
    if (fired_count < MAX_FIRED) {
        strcpy(already_fired[fired_count++], pos);
    }
}

void fire_random(void) {
    char pos[4];
    char message[16];
    
    do {
        int row = rand() % 10 + 1;       /* 1-10 */
        char row_char = 'A' + row - 1;   /* A-J */
        int col = rand() % 10 + 1;       /* 1-10 */
        snprintf(pos, sizeof(pos), "%c%d", row_char, col);
    } while (already_fired_contains(pos));
    
    snprintf(message, sizeof(message), "FIRE %s", pos);
    send_message(message);
    already_fired_add(pos);
}

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

void handle_game_started(void) {
    current_ship_index = 0;
    preparation_done = 0;
    place_next_ship();
}

void handle_ok(void) {
    if (preparation_done) {
        return;
    }
    
    current_ship_index++;
    if (current_ship_index < ships_count) {
        place_next_ship();
    } else if (current_ship_index == ships_count) {
        send_message("READY");
    } else {
        preparation_done = 1;
    }
}

void handle_your_turn(void) {
    fire_random();
}

void handle_game_over(void) {
    exit(0);
}

void handle_you_won(void) {
    exit(0);
}

void handle_ko(const char* line) {
    fprintf(stderr, "Error: %s\n", line);
}

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

void process_line(const char* line) {
    if (starts_with(line, "GAME STARTED")) {
        handle_game_started();
    } else if (starts_with(line, "YOUR TURN")) {
        handle_your_turn();
    } else if (starts_with(line, "GAME OVER")) {
        handle_game_over();
    } else if (starts_with(line, "YOU WON")) {
        handle_you_won();
    } else if (starts_with(line, "OK")) {
        handle_ok();
    } else if (starts_with(line, "KO")) {
        handle_ko(line);
    }
}

int main(void) {
    char line[256];
    
    srand((unsigned int)(time(NULL) ^ getpid()));
    
    while (fgets(line, sizeof(line), stdin)) {
        /* Remove trailing newline */
        size_t len = strlen(line);
        if (len > 0 && line[len - 1] == '\n') {
            line[len - 1] = '\0';
        }
        
        process_line(line);
    }
    
    return 0;
}
