SHARE
TWEET

Untitled

a guest Nov 18th, 2019 97 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <sstream>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <netdb.h>
  10. #include <utility>
  11. #include <chrono>
  12. #include "rapidjson/document.h"
  13. #include "rapidjson/filereadstream.h"
  14.  
  15. #include <iostream>
  16. #include <fstream>
  17. #include <vector>
  18.  
  19. using namespace std;
  20. using namespace rapidjson;
  21.  
  22. vector<vector<int>> gmap;
  23. /*store the map:
  24.  * 0: land
  25.  * -1: forest
  26.  * -2: trap
  27.  * -3: swamp
  28.  * >0: gold */
  29. int playerId, height, width, maxE, maxT, playerNumbers, startX, startY;
  30.  
  31. void getInfor(string json);
  32. void updateData(string json);
  33.  
  34. ///-------------ORE_CODE--------------------
  35. #define FOR(i, a, b)            for (int i = a; i <= (int) b; ++i)
  36. #define REP(i, a, b)            for (int i = a; i <  (int) b; ++i)
  37. const int N = 30;
  38. void myCode() {
  39.     Value &array_player = d["players"];
  40.     for (int i = 0; i < array_player.Size(); i++) {
  41.         int id = array_player[i]["playerId"].GetInt();
  42.         int x = array_player[i]["posx"].GetInt();
  43.         int y = array_player[i]["posy"].GetInt();
  44.         int gold = array_player[i]["score"].GetInt();
  45.         int energy = array_player[i]["energy"].GetInt64();
  46.         int state = array_player[i]["state"].GetInt64();
  47.  
  48.         if (id != playerId) continue;
  49.  
  50.         Dijkstra_Full();
  51.     }
  52. }
  53. ///-------------((:))-----------------------
  54.  
  55. int main(int argc, char *argv[])
  56. {
  57.     char* peerHost = "localhost";
  58.     short peerPort = 4000;
  59.  
  60.     if (argc == 3){
  61.         peerHost = argv[1];
  62.         peerPort = stoi(argv[2]);
  63.     }else if(argc == 2){
  64.         peerPort = (short) atoi(argv[1]);
  65.     }
  66.  
  67.     // Create socket
  68.     int s0 = socket(AF_INET, SOCK_STREAM, 0);
  69.     if (s0 < 0)
  70.     {
  71.         std::cerr << "Error: " << strerror(errno) << std::endl;
  72.         exit(1);
  73.     }
  74.  
  75.     // Fill in server IP address
  76.     struct sockaddr_in server;
  77.     int serverAddrLen;
  78.     bzero( &server, sizeof( server ) );
  79.  
  80.     // Resolve server address (convert from symbolic name to IP number)
  81.     struct hostent *host = gethostbyname(peerHost);
  82.     if (host == NULL)
  83.     {
  84.         std::cerr << "Error: " << strerror(errno) << std::endl;
  85.         exit(1);
  86.     }
  87.  
  88.     server.sin_family = AF_INET;
  89.     server.sin_port = htons(peerPort);
  90.    
  91.     // Write resolved IP address of a server to the address structure
  92.     memmove(&(server.sin_addr.s_addr), host->h_addr_list[0], 4);
  93.  
  94.     // Connect to the remote server
  95.     int res = connect(s0, (struct sockaddr*) &server, sizeof(server));
  96.     if (res < 0)
  97.     {
  98.         std::cerr << "Error: " << strerror(errno) << std::endl;
  99.         exit(1);
  100.     }
  101.  
  102.     std::cout << "Connected. Reading a server message" << std::endl;
  103.  
  104.     int maxSize = 40000;
  105.     char buffer[maxSize];
  106.     res = read(s0, buffer, maxSize);
  107.     if (res < 0)
  108.     {
  109.         std::cerr << "Error: " << strerror(errno) << std::endl;
  110.         exit(1);
  111.     }
  112.  
  113.     std::cout << "Received: " << buffer;
  114.  
  115.     string raw_json(buffer);
  116.     getInfor(raw_json);
  117.  
  118.     ////0: go left, 1: go right, 2: go up, 3: go down, 4: free, 5: craft
  119.     string command[] = {"0", "1", "2", "3", "4", "5"};
  120.     int count = 0;
  121.     int currentE = maxE;
  122.     int idx = 0;
  123.     for (int i=0; i<maxT; i++)
  124.     {
  125.         if (currentE < 5)
  126.     {
  127.            idx = 4;//free to get more energy
  128.        currentE += maxE/4;
  129.     }
  130.     else
  131.     {
  132.         cout << "Count:" << count << endl;
  133.         if (count%4==0) idx=1;//goright
  134.         else if (count%4==1) idx=3;//godown
  135.         else if (count%4==2) idx=0;//goleft
  136.         else idx=2;//goup
  137.         count++;
  138.         currentE -= 4;
  139.     }
  140.     cout << "send command:" << command[idx] << endl;
  141.         //send command to server
  142.     write(s0, command[idx].c_str(), 1);
  143.         //recv data from server
  144.     char buffer[maxSize];
  145.         int res = read(s0, buffer, maxSize);
  146.     cout << "res:" << res << endl;
  147.     string json(buffer);
  148.  
  149.     cout << "received data: "<< json << endl;
  150.         updateData(json);
  151.  
  152.     //please insert your process code
  153.     myCode();    
  154.     ////
  155.     /////
  156.     //////  
  157.     }
  158.     close(s0);
  159.     return 0;
  160. }
  161.  
  162. void updateData(string json)
  163. {
  164.     //process data
  165.     Document d;
  166.    
  167.     ParseResult result = d.Parse(json.c_str());
  168.         if (!result) {
  169.             cout << "JSON parse error: " << result.Offset() << endl;
  170.             return;
  171.         }
  172.  
  173.         //update data for player
  174.         Value &array_player = d["players"];
  175.         cout << "playerSize:" << array_player.Size() << endl;
  176.     for (int i = 0; i < array_player.Size(); i++)
  177.         {
  178.             int id = array_player[i]["playerId"].GetInt();
  179.             int x = array_player[i]["posx"].GetInt();
  180.             int y = array_player[i]["posy"].GetInt();
  181.             int gold = array_player[i]["score"].GetInt();
  182.             int energy = array_player[i]["energy"].GetInt64();
  183.             int state = array_player[i]["state"].GetInt64();
  184.  
  185.             cout << "player " << id << " at (" << y << ", " << x << "), state: "<< state << ", gold: " << gold << ", energy: " << energy << endl;
  186.         }
  187.  
  188.         //update the golds
  189.         Value &array_gold = d["golds"];
  190.         for (int i = 0; i < array_gold.Size(); i++)
  191.         {
  192.             int x = array_gold[i]["posx"].GetInt();
  193.             int y = array_gold[i]["posy"].GetInt();
  194.             int amount = array_gold[i]["amount"].GetInt();
  195.             gmap[y][x] = amount;
  196.         }
  197.  
  198. }
  199.  
  200. void getInfor(string raw_json){
  201.     StringStream s(raw_json.c_str());
  202.     Document d;
  203.     d.ParseStream(s);
  204.  
  205.     //start game infor
  206.     playerId = d["playerId"].GetInt();
  207.     width = d["gameinfo"]["width"].GetInt();
  208.     height = d["gameinfo"]["height"].GetInt();
  209.     maxT = d["gameinfo"]["steps"].GetInt();
  210.     playerNumbers = d["gameinfo"]["numberOfPlayers"].GetInt();
  211.     startY = d["posy"].GetInt();
  212.     startX = d["posx"].GetInt();
  213.     maxE = d["energy"].GetInt();
  214.  
  215.     //init map with all lands
  216.     for (int i=0; i<height; i++)
  217.         gmap.push_back(vector<int>(width, 0));
  218.  
  219.     //init map
  220.     Value &array_obstacles = d["gameinfo"]["obstacles"];
  221.     for (int i = 0; i < array_obstacles.Size(); i++)
  222.     {
  223.         int x = array_obstacles[i]["posx"].GetInt();
  224.         int y = array_obstacles[i]["posy"].GetInt();
  225.         int type = array_obstacles[i]["type"].GetInt();
  226.         gmap[y][x] = -type;
  227.     }
  228.  
  229.     // init data for gold
  230.     Value &array_gold = d["gameinfo"]["golds"];
  231.     for (int i = 0; i < array_gold.Size(); i++)
  232.     {
  233.         int x = array_gold[i]["posx"].GetInt();
  234.         int y = array_gold[i]["posy"].GetInt();
  235.         int gold = array_gold[i]["amount"].GetInt();
  236.         gmap[y][x] = gold;
  237.     }
  238.     cout << "width, height:" << width << " " << height << endl;
  239.     cout << "maxE, maxT: " << maxE << " " << maxT << endl;
  240.     cout << "numOfPlayer, startPos:" << playerNumbers << "(" << startX << "," << startY << ")" << endl;
  241.     // dump the map
  242.     for (int i=0; i<height; i++)
  243.     {
  244.         for (int j=0; j<width; j++)
  245.         cout << gmap[i][j] << " ";
  246.     cout << endl;
  247.     }
  248. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top