Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <float.h>
  6. #include <limits.h>
  7. using namespace std;
  8.  
  9. const int WIDTH = 7;
  10. const int HEIGHT = 7;
  11. const int MAX_PLAYERS = 2;
  12.  
  13.  
  14. enum class MoveType
  15. {
  16.     Push = 0,
  17.     Move = 1,
  18.     Pass = -1 // non existant in input, so just - 1
  19. };
  20.  
  21. enum Direction
  22. {
  23.     Up = 1 << 3,
  24.     Right = 1 << 2,
  25.     Down = 1 << 1,
  26.     Left = 1 << 0
  27. };
  28.  
  29.  
  30. struct vec2
  31. {
  32.     int x;
  33.     int y;
  34.  
  35.     bool insideMap()
  36.     {
  37.         return x >= 0 && y >= 0 && x <= WIDTH - 1 && y <= HEIGHT - 1;
  38.     }
  39.     vec2 operator-(vec2 b)
  40.     {
  41.         return vec2{ x - b.x, y - b.y };
  42.     }
  43.     vec2 operator+(vec2 b)
  44.     {
  45.         return vec2{ x + b.x, y + b.y };
  46.     }
  47.     void operator+=(vec2 b)
  48.     {
  49.         x += b.x;
  50.         y += b.y;
  51.     }
  52.     void operator-=(vec2 b)
  53.     {
  54.         x -= b.x;
  55.         y -= b.y;
  56.     }
  57.  
  58.     bool operator==(vec2 b)
  59.     {
  60.         return x == b.x && y == b.y;
  61.     }
  62. };
  63.  
  64. struct Move
  65. {
  66.     vec2 vel;
  67.     Direction from;
  68.     Direction to;
  69.  
  70. };
  71. //vec2 UP = { 0, -1 }, DOWN = { 0, 1 }, LEFT = { -1, 0 }, RIGHT = { 1, 0 };
  72. Move
  73. UP = { {0, -1}, Direction::Down, Direction::Up },
  74. DOWN = { {0, 1}, Direction::Up, Direction::Down },
  75. LEFT = { {-1, 0}, Direction::Right, Direction::Left },
  76. RIGHT = { {1, 0}, Direction::Left, Direction::Right };
  77.  
  78. Move availableMoves[4] = { UP, DOWN, LEFT, RIGHT };
  79.  
  80.  
  81.  
  82.  
  83. struct Grid
  84. {
  85.     int type; // binary type
  86.     vec2 pos;
  87.     int item; // 0 if mine, 1 if enemy, -1 if none
  88. };
  89.  
  90. struct Player
  91. {
  92.     int type;
  93.     vec2 pos;
  94.     int questN;
  95.     bool holdingItem; // in hand
  96. };
  97.  
  98. struct Item
  99. {
  100.     string name;
  101.     vec2 pos;
  102.     int playerId;
  103. };
  104.  
  105. struct Quest
  106. {
  107.     string name;
  108.     int playerId;
  109. };
  110.  
  111.  
  112.  
  113.  
  114. struct State
  115. {
  116.     MoveType moveType;
  117.     Grid grid[HEIGHT][WIDTH];
  118.     Player players[MAX_PLAYERS];
  119.  
  120.     vector<Item> items;
  121.     vector<Quest> quests;
  122.  
  123.     void clear()
  124.     {
  125.         items.clear();
  126.         quests.clear();
  127.     }
  128.     bool availableCell(vec2 fromPos, vec2 toPos, Move move)
  129.     {
  130.         return (grid[fromPos.y][fromPos.x].type & move.to) && (grid[toPos.y][toPos.x].type & move.from);
  131.     }
  132.     vector<vec2> getAvailableMoves(vec2 pos)
  133.     {
  134.         vector<vec2> moves;
  135.  
  136.         for (int i = 0; i < 4; i++)
  137.         {
  138.             vec2 futurePos = pos + availableMoves[i].vel;
  139.             if (!futurePos.insideMap()) continue;
  140.  
  141.             if (availableCell(pos, futurePos, availableMoves[i]))
  142.             {
  143.                 moves.push_back({ availableMoves[i].vel });
  144.             }
  145.         }
  146.  
  147.         return moves;
  148.     }
  149. };
  150.  
  151.  
  152.  
  153.  
  154. struct Action
  155. {
  156.     MoveType moveType;
  157.     int id = -1; // if id == -1 then moveType is move
  158.     vector<Direction> directionMoves;
  159.  
  160.     void clear()
  161.     {
  162.         directionMoves.clear();
  163.     }
  164.     string getString(int index)
  165.     {
  166.         switch (directionMoves[index])
  167.         {
  168.         case Up:
  169.             return "UP";
  170.         case Down:
  171.             return "DOWN";
  172.         case Left:
  173.             return "LEFT";
  174.         case Right:
  175.             return "RIGHT";
  176.         }
  177.  
  178.         return "NULL";
  179.     }
  180. };
  181. struct Agent
  182. {
  183.     State state;
  184.     Action bestAction;
  185.     float bestScore = -FLT_MAX;
  186.  
  187.     void read()
  188.     {
  189.         state.clear();
  190.  
  191.  
  192.         // game loop
  193.         int turnType;
  194.         cin >> turnType; cin.ignore();
  195.         state.moveType = (MoveType)turnType;
  196.  
  197.         for (int i = 0; i < 7; i++) {
  198.             for (int j = 0; j < 7; j++) {
  199.                 string tile;
  200.                 cin >> tile; cin.ignore();
  201.  
  202.                 int binaryNum = 0;
  203.                 for (int k = 3; k >= 0; k--)
  204.                 {
  205.                     if (tile[k] == '1')
  206.                     {
  207.                         binaryNum |= 1 << k;
  208.                     }
  209.                 }
  210.  
  211.  
  212.                 state.grid[j][i].pos.x = j;
  213.                 state.grid[j][i].pos.y = i;
  214.                 state.grid[j][i].type = binaryNum;
  215.                 state.grid[j][i].item = -1;
  216.  
  217.             }
  218.         }
  219.         for (int i = 0; i < 2; i++) {
  220.             int numPlayerCards; // the total number of quests for a player (hidden and revealed)
  221.             int playerX;
  222.             int playerY;
  223.             string playerTile;
  224.             cin >> numPlayerCards >> playerX >> playerY >> playerTile; cin.ignore();
  225.             Player & player = state.players[i];
  226.  
  227.             player.pos.x = playerX;
  228.             player.pos.y = playerY;
  229.             player.questN = numPlayerCards;
  230.  
  231.             int binaryNum = 0;
  232.             for (int k = 3; k >= 0; k--)
  233.             {
  234.                 if (playerTile[k] == '1')
  235.                 {
  236.                     binaryNum |= 1 << k;
  237.                 }
  238.             }
  239.  
  240.             player.type = binaryNum;
  241.             player.holdingItem = false;
  242.  
  243.         }
  244.  
  245.         int numItems; // the total number of items available on board and on player tiles
  246.         cin >> numItems; cin.ignore();
  247.         for (int i = 0; i < numItems; i++) {
  248.             string itemName;
  249.             int itemX;
  250.             int itemY;
  251.             int itemPlayerId;
  252.             cin >> itemName >> itemX >> itemY >> itemPlayerId; cin.ignore();
  253.             state.items.push_back({ itemName, vec2{itemX, itemY}, itemPlayerId });
  254.  
  255.             state.grid[itemY][itemX].item = itemPlayerId;
  256.             if (itemX == -1 && itemPlayerId == 0)
  257.             {
  258.                 state.players[0].holdingItem = true;
  259.             }
  260.         }
  261.  
  262.  
  263.  
  264.         int numQuests; // the total number of revealed quests for both players
  265.         cin >> numQuests; cin.ignore();
  266.         for (int i = 0; i < numQuests; i++) {
  267.             string questItemName;
  268.             int questPlayerId;
  269.             cin >> questItemName >> questPlayerId; cin.ignore();
  270.             state.quests.push_back({ questItemName, questPlayerId });
  271.         }
  272.  
  273.     }
  274.  
  275.     void think()
  276.     {
  277.         bestScore = -FLT_MAX;
  278.  
  279.         // heuristic stuff
  280.         if (state.moveType == MoveType::Move)
  281.         {
  282.             auto moves = state.getAvailableMoves(state.players[0].pos);
  283.  
  284.             for (auto move : moves)
  285.             {
  286.  
  287.             }
  288.  
  289.             // for now just pass
  290.             bestAction.moveType = MoveType::Pass;
  291.  
  292.         }
  293.         else
  294.         {
  295.             bestAction.moveType = MoveType::Push;
  296.  
  297.             if (state.players[0].holdingItem)
  298.             {
  299.                 Player & player = state.players[0];
  300.                 if (player.pos.x == 0)
  301.                 {
  302.                     bestAction.clear();
  303.                     bestAction.directionMoves.push_back({ Left });
  304.                     bestAction.id = player.pos.y;
  305.                     return;
  306.                 }
  307.                 else if (player.pos.x == 6)
  308.                 {
  309.                     bestAction.clear();
  310.                     bestAction.directionMoves.push_back({ Right });
  311.                     bestAction.id = player.pos.y;
  312.                     return;
  313.                 }
  314.                 else if (player.pos.y == 0)
  315.                 {
  316.                     bestAction.clear();
  317.                     bestAction.directionMoves.push_back({ Up });
  318.                     bestAction.id = player.pos.x;
  319.                     return;
  320.                 }
  321.                 else if (player.pos.y == 6)
  322.                 {
  323.                     bestAction.clear();
  324.                     bestAction.directionMoves.push_back({ Down });
  325.                     bestAction.id = player.pos.x;
  326.                     return;
  327.                 }
  328.                
  329.             }
  330.  
  331.             // find best push
  332.             for (auto & item : state.items)
  333.             {
  334.                 bool quested = false;
  335.  
  336.                 if (item.playerId != 0 || item.pos.x < 0) continue;
  337.  
  338.                 for (auto & quest : state.quests)
  339.                 {
  340.                     if (quest.name == item.name && quest.playerId == 0)
  341.                     {
  342.                         quested = true;
  343.                         break;
  344.                     }
  345.                 }
  346.  
  347.                 if (!quested) continue;
  348.  
  349.                 float avrg = (float)WIDTH / 2.0f;
  350.  
  351.                 // LEFT RIGHT
  352.                 if (item.pos.x < avrg)
  353.                 {
  354.                     int d = -item.pos.x;
  355.                     if (d > bestScore)
  356.                     {
  357.                         bestScore = d;
  358.                         bestAction.id = item.pos.y;
  359.                         bestAction.clear();
  360.                         bestAction.directionMoves.push_back(Left);
  361.                     }
  362.  
  363.                 }
  364.                 else
  365.                 {
  366.                     int d = -WIDTH + 1 + item.pos.x;
  367.                     if (d > bestScore)
  368.                     {
  369.                         bestScore = d;
  370.                         bestAction.id = item.pos.y;
  371.                         bestAction.clear();
  372.                         bestAction.directionMoves.push_back(Right);
  373.                     }
  374.                 }
  375.  
  376.                 if (item.pos.y < avrg)
  377.                 {
  378.                     int d = -item.pos.y;
  379.                     if (d > bestScore)
  380.                     {
  381.                         bestScore = d;
  382.                         bestAction.id = item.pos.x;
  383.                         bestAction.clear();
  384.                         bestAction.directionMoves.push_back(Up);
  385.                     }
  386.  
  387.                 }
  388.                 else
  389.                 {
  390.                     int d = -HEIGHT + 1 + item.pos.y;
  391.                     if (d > bestScore)
  392.                     {
  393.                         bestScore = d;
  394.                         bestAction.id = item.pos.x;
  395.                         bestAction.clear();
  396.                         bestAction.directionMoves.push_back(Down);
  397.                     }
  398.                 }
  399.  
  400.             }
  401.  
  402.  
  403.  
  404.         }
  405.  
  406.     }
  407.  
  408.     void output()
  409.     {
  410.         if (bestAction.moveType == MoveType::Push)
  411.         {
  412.             cout << "PUSH " << bestAction.id << " " << bestAction.getString(0) << endl;
  413.  
  414.         }
  415.         else
  416.         {
  417.             cout << "PASS" << endl;
  418.         }
  419.  
  420.     }
  421.  
  422.  
  423.  
  424. };
  425. int main()
  426. {
  427.     Agent agent;
  428.  
  429.     while (1)
  430.     {
  431.         agent.read();
  432.         agent.think();
  433.         agent.output();
  434.     }
  435.  
  436.     return 0;
  437.  
  438. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement