Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <ctime>
  6.  
  7. using namespace std;
  8.  
  9. struct Tile
  10. {
  11.     bool up;
  12.     bool right;
  13.     bool down;
  14.     bool left;
  15.     int x; // just for save points, the save points cordinate
  16.     int y; // just for save points, the save points cordinate
  17.     int k; // just for save points, how many steps at that save point
  18.    
  19.     vector<string> path;
  20. };
  21.  
  22. struct Player
  23. {
  24.     int num_player_quests;
  25.     int x;
  26.     int y;
  27.     Tile player_tile;
  28.    
  29. };
  30.  
  31. struct Item
  32. {
  33.     string name;
  34.     int x;
  35.     int y;
  36.     int player_id;
  37. };
  38.  
  39. struct Quest
  40. {
  41.     string name;
  42.    
  43.     int x;
  44.     int y;
  45. };
  46.  
  47. bool can_up(Tile me, vector<vector<Tile> > line) // can it go one step up
  48. {
  49.     if(me.y == 0)return false;
  50.     else if(line[me.y][me.x].up == true && line[me.y - 1][me.x ].down == true)return true;
  51.     else return false;
  52. }
  53.  
  54. bool can_right(Tile me, vector<vector<Tile> > line) // can it go one step right
  55. {
  56.     if(me.x == 6)return false;
  57.     else if(line[me.y][me.x].right == true && line[me.y][me.x + 1].left == true)return true;
  58.     else return false;
  59. }
  60.  
  61. bool can_down(Tile me, vector<vector<Tile> > line) // can it go one step down
  62. {
  63.     if(me.y == 6)return false;
  64.     else if(line[me.y][me.x].down == true && line[me.y + 1][me.x].up == true)return true;
  65.     else return false;
  66. }
  67.  
  68. bool can_left(Tile me, vector<vector<Tile> > line) //  can it go one step left
  69. {
  70.     if(me.x == 0)return false;
  71.     else if(line[me.y][me.x].left == true && line[me.y][me.x - 1].right == true)return true;
  72.     else return false;
  73. }
  74. int countVertexes(vector<vector<Tile> > line, Tile me)
  75. {
  76.     int n = 0;
  77.     if(can_up(me, line)) n++;
  78.     if(can_down(me, line)) n++;
  79.     if(can_left(me, line)) n++;
  80.     if(can_right(me, line)) n++;
  81.     return n;
  82. }
  83.  
  84. bool find_available_path(vector<vector<Tile> > &line, Tile item, Player me)
  85. {
  86.     vector<Tile*> save_points;
  87.    
  88.     int k = 0; // number of steps, limit is 20
  89.     int save_index = -1; // which save point is which
  90.     Tile copy_me; // the one that moves around to look if there is an available path
  91.    
  92.     copy_me.x = me.x;
  93.     copy_me.y = me.y;
  94.    
  95.     cerr << me.x << " " << me.y  << endl;
  96.     cerr << endl;
  97.    
  98.     while((copy_me.x != item.x || copy_me.y != item.y) && k < 20)
  99.     {
  100.        
  101.         cerr<<"X Y : " << copy_me.x << " " << copy_me.y << endl;
  102.         if(copy_me.x == item.x && copy_me.y == item.y)return true;
  103.    
  104.         if(can_up(copy_me, line) == true)
  105.         {
  106.             if(countVertexes(line, copy_me) > 1)
  107.             {                    
  108.                 line[copy_me.y][copy_me.x].k = k;
  109.                 save_points.push_back(&line[copy_me.y][copy_me.x]);
  110.             }
  111.            
  112.             line[copy_me.y][copy_me.x].up = false;
  113.            
  114.             Tile current_pos = copy_me;
  115.            
  116.             copy_me.x = copy_me.x;
  117.             copy_me.y = copy_me.y - 1;
  118.             k++;
  119.             line[copy_me.y][copy_me.x].down = false;
  120.            
  121.             line[copy_me.y][copy_me.x].path =  line[current_pos.y][current_pos.x].path;
  122.             line[copy_me.y][copy_me.x].path.push_back({"UP"});
  123.            
  124.         }
  125.         else if(can_right(copy_me, line) == true)
  126.         {
  127.            if(countVertexes(line, copy_me) > 1)
  128.             {                    
  129.                 line[copy_me.y][copy_me.x].k = k;
  130.                 save_points.push_back(&line[copy_me.y][copy_me.x]);
  131.                
  132.             }
  133.             line[copy_me.y][copy_me.x].right = false;
  134.            
  135.             Tile current_pos = copy_me;
  136.        
  137.             copy_me.x = copy_me.x + 1;
  138.             copy_me. y = copy_me.y;
  139.             k++;
  140.             line[copy_me.y][copy_me.x].left = false;
  141.            
  142.             line[copy_me.y][copy_me.x].path =  line[current_pos.y][current_pos.x].path;
  143.             line[copy_me.y][copy_me.x].path.push_back({"RIGHT"});
  144.  
  145.         }
  146.         else if(can_down(copy_me, line) == true)
  147.         {
  148.             if(countVertexes(line, copy_me) > 1)
  149.             {                    
  150.                 line[copy_me.y][copy_me.x].k = k;
  151.                 save_points.push_back(&line[copy_me.y][copy_me.x]);
  152.             }
  153.             line[copy_me.y][copy_me.x].down = false;
  154.            
  155.             Tile current_pos = copy_me;
  156.            
  157.             copy_me.x = copy_me.x;
  158.             copy_me.y = copy_me.y+1;
  159.             k++;
  160.             line[copy_me.y][copy_me.x].up = false;
  161.            
  162.             line[copy_me.y][copy_me.x].path =  line[current_pos.y][current_pos.x].path;
  163.             line[copy_me.y][copy_me.x].path.push_back({"DOWN"});
  164.         }
  165.         else if(can_left(copy_me, line) == true)
  166.         {
  167.             if(countVertexes(line, copy_me) > 1)
  168.             {                    
  169.                 line[copy_me.y][copy_me.x].k = k;
  170.                 save_points.push_back(&line[copy_me.y][copy_me.x]);
  171.             }
  172.             line[copy_me.y][copy_me.x].left = false;
  173.            
  174.             Tile current_pos = copy_me;
  175.              
  176.             copy_me.x = copy_me.x-1;
  177.             copy_me.y = copy_me.y;
  178.             k++;
  179.             line[copy_me.y][copy_me.x].right = false;
  180.            
  181.             line[copy_me.y][copy_me.x].path =  line[current_pos.y][current_pos.x].path;
  182.             line[copy_me.y][copy_me.x].path.push_back({"LEFT"});
  183.  
  184.         }
  185.         else if(can_up(copy_me, line) == false && can_right(copy_me, line) == false && can_down(copy_me, line) == false && can_left(copy_me, line) == false)
  186.         {
  187.            
  188.             if(save_points.size() > 0)
  189.             {
  190.                 while(save_points.size() > 0 && countVertexes(line, *save_points[save_points.size() - 1])<= 0)
  191.                 {
  192.                     save_points.erase(save_points.end() - 1);
  193.                 }
  194.                 if(save_points.size() == 0) return false;
  195.                 copy_me = *save_points[save_points.size() - 1];
  196.                 k = save_points[save_points.size() - 1]->k;
  197.             }
  198.             else return false;
  199.            
  200.         }
  201.         if(copy_me.x == item.x && copy_me.y == item.y)return true;
  202.     }
  203.     return false;
  204.    
  205. }
  206.  
  207.  
  208. void push_turn (vector<vector<Tile> > line, Player me, Item my_item)
  209. {
  210.     if(me.x != 0 && me.x != 6 && me.y != 0 && me.y != 6)
  211.     {
  212.         if(me.x < 3)cout << "PUSH " << me.y << " LEFT" << endl;
  213.         else cout << "PUSH " << me.y << " RIGHT" << endl;
  214.     }
  215.     else if(my_item.x != -1 && my_item.x != -2)
  216.     {
  217.         if(my_item.x != me.x)
  218.         {
  219.             if(my_item.y < 3)cout << "PUSH " << my_item.x << " UP" << endl;
  220.             else cout << "PUSH " << my_item.x << " DOWN" << endl;
  221.         }
  222.         else
  223.         {
  224.             if(my_item.x < 3)cout << "PUSH " << my_item.y << " LEFT" << endl;
  225.             else cout << "PUSH " << my_item.y << " RIGHT" << endl;
  226.         }
  227.     }
  228.     else if(my_item.x == -1)
  229.     {
  230.         if(me.x == 0)cout << "PUSH " << me.y << " LEFT" << endl;
  231.         else if(me.x == 6)cout << "PUSH " << me.y << " RIGHT" << endl;
  232.         else if(me.y == 0)cout << "PUSH " << me.x << " UP" << endl;
  233.         else cout << "PUSH " << me.x << " DOWN" << endl;
  234.     }
  235.     else cout << "PUSH " << 3 << " LEFT";
  236. }
  237.  
  238. int main()
  239. {
  240.     //srand(time(0));
  241.     // game loop
  242.     while (1)
  243.     {
  244.        
  245.         vector<vector<Tile> > line;
  246.        
  247.         Player me;
  248.         Player enemy;
  249.        
  250.         vector<Item> my_items;
  251.         vector<Item> enemy_items;
  252.        
  253.         vector<Quest> my_quests;
  254.         vector<Quest> enemy_quests;
  255.        
  256.         line.resize(7);
  257.        
  258.         int turn_type;
  259.         cin >> turn_type; cin.ignore();
  260.        
  261.         for (int i = 0; i < line.size(); i++) // ivedu duomenis zaidimo lentos
  262.         {
  263.             for (int j = 0; j < 7; j++)
  264.             {
  265.                 string tile;
  266.                 cin >> tile; cin.ignore();
  267.                 line[i].push_back({tile[0] - '0', tile[1] - '0', tile[2] - '0', tile[3] - '0'});
  268.                 line[i][j].x = j;
  269.                 line[i][j].y = i;
  270.                 line[i][j].path.clear();
  271.             }
  272.         }
  273.        
  274.         string a;string b;Tile A;Tile B;// cancer tik duomenu uzsaugojimui
  275.        
  276.         cin >>  me.num_player_quests >> me.x >> me.y >> a; cin.ignore(); // ivedu duomenis apie save
  277.         A.up = a[0] - '0'; A.right = a[1] - '0'; A.down = a[2] - '0'; A.left = a[3] - '0';
  278.         me.player_tile = A;
  279.        
  280.         cin >>  enemy.num_player_quests >> enemy.x >> enemy.y >> b; cin.ignore(); // ivedu duomenis apie priesa
  281.         B.up = b[0] - '0'; B.right = b[1] - '0'; B.down = b[2] - '0'; B.left = b[3] - '0';
  282.         enemy.player_tile = B;
  283.        
  284.         int num_of_items;
  285.         cin >> num_of_items; cin.ignore();// the total number of items available on board and on player tiles
  286.        
  287.         for (int i = 0; i < num_of_items; i++)
  288.         {
  289.             Item A;
  290.             cin >> A.name >> A.x >> A.y >> A.player_id; cin.ignore();
  291.             if(A.player_id == 0)my_items.push_back(A);
  292.             else enemy_items.push_back(A);
  293.         }
  294.         int num_of_quests;
  295.         cin >> num_of_quests; cin.ignore(); // the total number of revealed quests for both players
  296.         for (int i = 0; i < num_of_quests; i++)
  297.         {
  298.            
  299.             Quest A;
  300.             int id;
  301.             cin >> A.name >> id; cin.ignore();
  302.             if(id == 0)
  303.             {
  304.                 my_quests.push_back(A);
  305.                 for(int j = 0; j < my_items.size(); j++)
  306.                 {
  307.                     if(my_items[j].name == my_quests[i].name)
  308.                     {
  309.                         my_quests[i].x = my_items[j].x;
  310.                         my_quests[i].y = my_items[j].y;
  311.                        
  312.                     }
  313.                 }
  314.             }
  315.             else enemy_quests.push_back(A);
  316.            
  317.         }
  318.        // cerr << "how many items: " << my_items.size() << endl;
  319.         //bool canReach = false;
  320.         //if(my_quests.size() > 0 && my_quests[0].y > 0)
  321.         //{
  322.             //cerr << "quest 0  x y : " << my_quests[0].x << " " << my_quests[0].y << endl;
  323.           // canReach =  find_available_path(line, line[my_quests[0].y][my_quests[0].x], me) ;
  324.            //cerr << "can it reach or not:  " << canReach << endl;
  325.          
  326.         //}
  327.         if(turn_type == 0) // push
  328.         {
  329.             bool shook = false; // for breaking cycle
  330.             for(int i = 0; i < my_items.size(); i++)
  331.             {
  332.                 for(int j = 0; j < my_quests.size(); j++)
  333.                 {
  334.                     if(my_items[i].name == my_quests[j].name)
  335.                     {
  336.                         push_turn(line, me, my_items[i]);
  337.                         shook = true;
  338.                         break;
  339.                     }
  340.                 }    
  341.                 if(shook)break;
  342.             }
  343.             if(shook == false)cout << "PUSH " << 3 << " LEFT" << endl;
  344.         }
  345.            
  346.         if(turn_type == 1) // move
  347.         {
  348.           //&& my_quests[0].y > 0
  349.           //for(int )
  350.             if(my_quests.size() > 0)
  351.             {
  352.                 Tile copyLines[7][7];
  353.                 for(int i = 0; i < 7; i++)
  354.                 {
  355.                     for(int j = 0; j < 7; j++)
  356.                     {
  357.                         copyLines[i][j] = line[i][j];
  358.                     }
  359.                 }
  360.                
  361.                 bool canReach = false;
  362.                 for(int i = 0; i < my_quests.size(); i++)  
  363.                 {
  364.                     if(my_quests[i].x < 0) continue;
  365.                    
  366.                     for(int i1 = 0; i1 < 7; i1++)
  367.                         {
  368.                             for(int j = 0; j < 7; j++)
  369.                             {
  370.                                 line[i1][j] = copyLines[i1][j];
  371.                             }
  372.                         }
  373.                     cerr << "does it work hmmm xdd " << endl;
  374.                     canReach = find_available_path(line, line[my_quests[i].y][my_quests[i].x], me);
  375.                     if(my_quests[i].x > 0 && canReach)
  376.                     {    
  377.                          cout << "MOVE ";
  378.                          for(int j = 0; j < line[my_quests[i].y][my_quests[i].x].path.size(); j++)
  379.                          {
  380.                              if(j == line[my_quests[i].y][my_quests[i].x].path.size() - 1) cout << line[my_quests[i].y][my_quests[i].x].path[j] << endl;
  381.                              else cout << line[my_quests[i].y][my_quests[i].x].path[j] << " ";
  382.                          }
  383.                          break; // later i will not break but check if i can move elsewhere
  384.                     }
  385.                    
  386.                 }
  387.             if(!canReach)
  388.             {
  389.                 cout << "PASS" << endl;
  390.             }
  391.                
  392.             }
  393.             else cout << "PASS" << endl;
  394.            
  395.            
  396.            
  397.         }
  398.          
  399.     }
  400.        
  401. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement