示例#1
0
int main(int argc, char ** argv)
{
    if (argc != 3)
    {
        printf("Usage: %s HOSTNAME PORT\n", argv[0]);
        return 1;
    }

    string address = argv[1];
    string port_str = argv[2];
    int port = std::stoi(port_str);
    string name = "nearest";

    try
    {
        Session session;

        printf("Connecting to %s:%d...\n", address.c_str(), port);
        session.connect(address, port);

        printf("Logging in as a player named '%s'...\n", name.c_str());
        session.login_player(name);

        printf("Waiting for WELCOME...\n");
        session.wait_for_welcome();

        printf("Waiting for GAME_STARTS...\n");
        session.wait_for_game_starts();
        int mon_id = session.player_id();

        std::vector<NeutralCell> cellules_neutres;
        TurnPlayerCell temp_cell;
        std::map<double, NeutralCell*> distances; // trie les cellules neutres par leur distance à ma cellule
        double temp_distance;
        NeutralCell temp_destination;
        Turn tour;
        Position destination;
        Actions actions;

        while(session.is_logged())
        {
            actions.clear();

            printf("Waiting for next turn...\n");
            session.wait_for_next_turn();

            /// recevoir le tour
            cellules_neutres.clear();
            distances.clear();
            cellules_neutres = session.neutral_cells();

            /// récupérer la liste de mes cellules
            for (unsigned int i=0; i<tour.pcells.size(); ++i) {
                if (tour.pcells[i].player_id == mon_id) {
                    temp_cell = tour.pcells[i];
                    /// trier les cellules neutres par distance croissante
                    for (unsigned int j=0; j<cellules_neutres.size(); ++j) {
                        if (cellules_neutres[j].remaining_turns_before_apparition == 0) {
                            // on ne prend en compte que les cellules vivantes
                            temp_distance = distance(temp_cell, cellules_neutres[j]);
                            distances[temp_distance] = &cellules_neutres[j];
                        }
                    }
                    /// récupérer les coord de la cellule neutre la plus proche
                    temp_destination = *(distances.begin())->second;
                    destination.x = temp_destination.position.x;
                    destination.y = temp_destination.position.y;

                    /// envoyer l'action
                    printf("Adding move_action (pcell_id=%d, dest=(%g,%g))\n",
                           temp_cell.pcell_id, destination.x, destination.y);
                    actions.add_move_action(temp_cell.pcell_id, destination.x, destination.y);
                }
            }

            printf("Sending actions...\n");
            session.send_actions(actions);
        }
    }
    catch (ainet16::AINetException & exception)
    {
        cout << exception.what() << endl;
        return 1;
    }

    return 0;
}
示例#2
0
int main(int argc, char ** argv)
{
    if (argc != 3)
    {
        printf("Usage: %s HOSTNAME PORT\n", argv[0]);
        return 1;
    }

    string address = argv[1];
    string port_str = argv[2];
    int port = std::stoi(port_str);
    string name = "splitter";

    try
    {
        Session session;

        printf("Connecting to %s:%d...\n", address.c_str(), port);
        session.connect(address, port);

        printf("Logging in as a player named '%s'...\n", name.c_str());
        session.login_player(name);

        printf("Waiting for WELCOME...\n");
        session.wait_for_welcome();
        Welcome welcome = session.welcome();
        GameParameters p = welcome.parameters;

        printf("Waiting for GAME_STARTS...\n");
        session.wait_for_game_starts();

        while(session.is_logged())
        {
            Actions actions;

            printf("Waiting for next turn...\n");
            session.wait_for_next_turn();

            printf("\nNew turn!\n");

            vector<NeutralCell> ncells = session.neutral_cells();

            map<int, CellData> my_cells;

            for (const TurnPlayerCell & cell : session.my_player_cells())
            {
                if (cell.player_id == session.player_id())
                {
                    CellData data;
                    data.pcell = cell;

                    if (cell.mass > p.minimum_pcell_mass * 2)
                        data.can_split = true;

                    my_cells[cell.pcell_id] = data;
                }
            }

            // If all our cells are dead, let's surrender
            if (my_cells.size() == 0)
            {
                printf("Surrender\n");
                actions.add_surrender_action();
            }
            else
            {
                int nb_pcells = my_cells.size();
                // If the maximum number of cells is not reached, let's split!

                for (auto mit : my_cells)
                {
                    CellData & data = mit.second;
                    if (data.can_split && nb_pcells < p.max_cells_per_player)
                    {
                        int pcell_id = data.pcell.pcell_id;
                        float target_x = 0;
                        float target_y = 0;
                        float mass = data.pcell.mass / 2;
                        printf("Split action: (pcell_id=%d, pos=(%g,%g), mass=%g)\n",
                               pcell_id, target_x, target_y, mass);
                        actions.add_divide_action(pcell_id, target_x, target_y, mass);
                        ++nb_pcells;
                    }
                    else if (ncells.size() > 0)
                    {
                        // nearest
                        int ncell_idx = nearest_ncell(data.pcell.position, ncells);

                        if (ncell_idx != -1)
                        {
                            NeutralCell ncell = ncells[ncell_idx];

                            int pcell_id = data.pcell.pcell_id;
                            float target_x = ncell.position.x;
                            float target_y = ncell.position.y;
                            printf("Move action: (pcell_id=%d, pos=(%g,%g))\n",
                                   pcell_id, target_x, target_y);
                            actions.add_move_action(data.pcell.pcell_id, ncell.position.x, ncell.position.y);

                            ncells.erase(ncells.begin() + ncell_idx);
                        }
                        else
                            printf("Bug !\n");
                    }
                    else
                    {
                        printf("Nothing to do...\n");
                    }
                }
            }

            printf("Sending actions...\n");
            session.send_actions(actions);
        }
    }
    catch (const ainet16::GameFinishedException & e)
    {
        cout << "Game finished!" << endl;
        cout << "Winner = " << e.winner_player_id() << endl << endl;
        std::vector<ainet16::GameEndsPlayer> players = e.players();

        cout << "Player scores:" << endl;
        for (GameEndsPlayer player : players)
            printf("  (player_id=%d, score=%ld)\n", player.player_id, player.score);
    }
    catch (ainet16::AINetException & exception)
    {
        cout << exception.what() << endl;
        return 1;
    }

    return 0;
}