コード例 #1
0
ファイル: SampleAgent.c プロジェクト: Tener/tradgames
void play_game(void) {
    // Identify myself
    fprintf(stdout, "#name %s\n", name);
    fflush(stdout);

    // Wait for start of game
    wait_for_start();

    // Main game loop
    for (;;) {
        if (current_player == my_player) {
            // My turn
            // Check if game is over (optional)

            // Determine next move
            next_move();

            // Apply it to local state

            // Tell the world
            print_and_recv_echo(next_move_cache);

            // It is the opponents turn
            current_player = (current_player == player1) ? player2 : player1;
        } else {
            // Wait for move from other player
            // Get server's next instruction
            read_msg_and_tokenize();

            if (tokens_size == 6 && strcmp(tokens[0], "MOVE") == 0) {
                // Translate to local coordinates and update our local state

                // It is now my turn
                current_player = (current_player == player1) ? player2 : player1;
            } else if (tokens_size == 4 && strcmp(tokens[0], "FINAL") == 0 &&
                       strcmp(tokens[2], "BEATS") == 0) {
                // Game over
                if (strcmp(tokens[1], name) == 0 && strcmp(tokens[3], opp_name) == 0) {
                    fprintf(stderr, "I, %s, have won!\n", name);
                    fflush(stderr);
                } else if (strcmp(tokens[3], name) == 0&& strcmp(tokens[1], opp_name) == 0) {
                    fprintf(stderr, "I, %s, have lost.\n", name);
                    fflush(stderr);
                } else {
                    fprintf(stderr, "Did not find expected players in FINAL command.\n");
                    fprintf(stderr, "Found '%s' and '%s'.\n", tokens[1], tokens[3]);
                    fprintf(stderr, "Expected '%s' and '%s'.\n", name, opp_name);
                    fprintf(stderr, "Received message '%s'\n", message);
                    fflush(stderr);
                }
                break;
            } else {
                // Unknown command
                fprintf(stderr, "Unknown command of '%s' from the server\n", message);
                fflush(stderr);
            }
        }
    }
}
コード例 #2
0
ファイル: SampleAgent.cpp プロジェクト: fakegeek/tradgames
void Client::play_game() {
  // Identify myself
  std::cout << "#name " << name << std::endl;

  // Wait for start of game
  wait_for_start();

  // Main game loop
  for (;;) {
    if (current_player == my_player) {
      // My turn

      // Check if game is over
      /*
      if (gs.game_over()) {
        std::cerr << "I, " << name << ", have lost" << std::endl;
        switch_current_player();
        continue;
      }
      */

      // Determine next move
      const Move m = next_move();

      // Apply it locally
      // gs.apply_move(m);

      // Tell the world
      print_and_recv_echo(m);

      // It is the opponents turn
      switch_current_player();
    } else {
      // Wait for move from other player
      // Get server's next instruction
      std::string server_msg = read_msg();
      const std::vector<std::string> tokens = tokenize_msg(server_msg);

      if (tokens.size() == 5 && tokens[0] == "MOVE") {
        // Translate to local coordinates and update our local state
        // const Move m = gs.translate_to_local(tokens);
        // gs.apply_move(m);

        // It is now my turn
        switch_current_player();
      } else if (tokens.size() == 4 && tokens[0] == "FINAL" &&
                 tokens[2] == "BEATS") {
        // Game over
        if (tokens[1] == name && tokens[3] == opp_name)
          std::cerr << "I, " << name << ", have won!" << std::endl;
        else if (tokens[3] == name && tokens[1] == opp_name)
          std::cerr << "I, " << name << ", have lost." << std::endl;
        else
          std::cerr << "Did not find expected players in FINAL command.\n"
                    << "Found '" << tokens[1] << "' and '" << tokens[3] << "'. "
                    << "Expected '" << name << "' and '" << opp_name << "'.\n"
                    << "Received message '" << server_msg << "'" << std::endl;
        break;
      } else {
        // Unknown command
        std::cerr << "Unknown command of '" << server_msg << "' from the server"
                  << std::endl;
      }
    }
  }
}