nitrodog96

Elevator

Oct 11th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <stdlib.h>
  5.  
  6. //r means roof floor
  7. int curFloor = 1;
  8. std::string dir;
  9. int calls = 0;
  10. int carrying = 0;
  11. std::string callList [10];
  12. std::string newCallInput;
  13. int destList[10];
  14.  
  15. void printElevator(int floor, bool moving, bool goingUp)
  16. {
  17.     std::string firstRow = "      ";
  18.     if(moving && goingUp)
  19.     {
  20.         firstRow += "^ ";
  21.     }
  22.     else
  23.     {
  24.         firstRow += "  ";
  25.     }
  26.     firstRow += "1 2 3 4 5";
  27.     std::cout << firstRow << std::endl;
  28.  
  29.     std::string secondRow = "        ";
  30.     for(int i = 0; i < 5; i++)
  31.     {
  32.         if(floor == i + 1)
  33.         {
  34.             secondRow += "^ ";
  35.         }
  36.         else if(floor == i + 6)
  37.         {
  38.             secondRow += "v ";
  39.         }
  40.         else
  41.         {
  42.             secondRow += "  ";
  43.         }
  44.     }
  45.     std::cout << secondRow << std::endl;
  46.  
  47.     std::string thirdRow = "      ";
  48.     if(moving && !goingUp)
  49.     {
  50.         thirdRow += "v ";
  51.     }
  52.     else
  53.     {
  54.         thirdRow += "  ";
  55.     }
  56.     thirdRow += "6 7 8 9 R";
  57.     std::cout << thirdRow << std::endl;
  58.  
  59.     std::cout << "     .---------------." << std::endl;
  60.     for(int j = 0; j < 15; j++)
  61.     {
  62.         std::cout << "     ||      |      ||" << std::endl;
  63.     }
  64.     std::cout << "     '---------------'" << std::endl;
  65.  
  66. }
  67.  
  68. std::string inputToArr(int callFloor, bool dirUp)
  69. {
  70.     std::string out;
  71.     if(curFloor == 10)
  72.     {
  73.         out += "R";
  74.     }
  75.     else
  76.     {
  77.         out += std::to_string(callFloor);
  78.     }
  79.  
  80.     out += "";
  81.  
  82.     if(dirUp)
  83.     {
  84.         out += "U";
  85.     }
  86.     else
  87.     {
  88.         out += "D";
  89.     }
  90.     return out;
  91. }
  92.  
  93. std::string lowerCaser(std::string input)
  94. {
  95.     std::string output;
  96.     char c;
  97.     for(unsigned i = 0; i < input.length(); i++)
  98.     {
  99.         c = input[i];
  100.         output += (char) tolower(c);
  101.     }
  102.     return output;
  103. }
  104.  
  105. int newInput()
  106. {
  107.     std::cout << "Would you like to add a new call for the elevator? (Y/N)" << std::endl;
  108.     std::cin >> newCallInput;
  109.     newCallInput = lowerCaser(newCallInput);
  110.     while(newCallInput != "n" && newCallInput != "y")
  111.     {
  112.         std::cout << "SPEAK CLEARLY." << std::endl;
  113.         std::cout << "Would you like to add a new call for the elevator? (Y/N)" << std::endl;
  114.         std::cin >> newCallInput;
  115.         newCallInput = lowerCaser(newCallInput);
  116.     }
  117.  
  118.     if(newCallInput == "n" && calls == 0 && carrying == 0 && curFloor == 1)
  119.     {
  120.         std::cout << "Alright. Guess we're done here then.";
  121.         return 0;
  122.     }
  123.     else if(newCallInput == "y")
  124.     {
  125.         std::string callFloorIn;
  126.         std::string dirIn;
  127.         int floor;
  128.         bool dirUp;
  129.  
  130.         std::cout << "Input the floor to call from, from 1-10." << std::endl;
  131.         std::cin >> callFloorIn;
  132.         while (callFloorIn != "1" && callFloorIn != "2" && callFloorIn != "3" && callFloorIn != "4" &&
  133.                callFloorIn != "5" && callFloorIn != "6" && callFloorIn != "7" && callFloorIn != "8" &&
  134.                callFloorIn != "9" && callFloorIn != "10") {
  135.             std::cout << "SPEAK CLEARLY." << std::endl;
  136.             std::cout << "Input the floor to call from, from 1-10." << std::endl;
  137.             std::cin >> callFloorIn;
  138.         }
  139.         floor = std::stoi(callFloorIn);
  140.  
  141.         std::cout << "Are you going up (U) or down (D)?" << std::endl;
  142.         std::cin >> dirIn;
  143.         dirIn = lowerCaser(dirIn);
  144.         while(dirIn != "u" && dirIn != "d")
  145.         {
  146.             std::cout << "You stubbed your finger on the input panel. Try again." << std::endl;
  147.             std::cout << "Are you going up (U) or down (D)?" << std::endl;
  148.             std::cin >> dirIn;
  149.             dirIn = lowerCaser(dirIn);
  150.         }
  151.  
  152.         dirUp = dirIn == "u";
  153.         callList[calls] = inputToArr(floor, dirUp);
  154.         calls++;
  155.  
  156.         return 1;
  157.     }
  158.     else
  159.     {
  160.         return 1;
  161.     }
  162. }
  163.  
  164. void pickUp()
  165. {
  166.     bool realPickUp = false;
  167.     std::string firstCall = callList[0];
  168.     if(calls == 1 && firstCall[1] == dir[0] && ((firstCall[0] == 'R' && curFloor == 10) || (firstCall[0] - 48 == curFloor)))
  169.     {
  170.         realPickUp = true;
  171.     }
  172.     else
  173.     {
  174.         for(int i = 0; i < 10; i++)
  175.         {
  176.             std::string cur = callList[i];
  177.             if(cur[0] == 'R' && curFloor == 10)
  178.             {
  179.                 realPickUp = true;
  180.             }
  181.             if(cur[0] - 48 == curFloor && cur[1] == dir[0])
  182.             {
  183.                 realPickUp = true;
  184.             }
  185.         }
  186.     }
  187.  
  188.     if(realPickUp)
  189.     {
  190.         std::string numInput;
  191.         int num;
  192.  
  193.         std::cout << "How many people are getting on at this floor?" << std::endl;
  194.         std::cin >> numInput;
  195.  
  196.         while(numInput != "1" && numInput != "2" && numInput != "3" && numInput != "4" && numInput != "5" && numInput != "6" && numInput != "7" && numInput != "8" && numInput != "9" && numInput != "10")
  197.         {
  198.             std::cout << "You appear to have found Schrodinger's elevator passenger. Please try again." << std::endl;
  199.             std::cout << "How many people are getting on at this floor?" << std::endl;
  200.             std::cin >> numInput;
  201.         }
  202.         num = std::stoi(numInput);
  203.  
  204.         if(carrying + num > 10)
  205.         {
  206.             num = 10 - carrying;
  207.             std::cout << "Unfortunately the elevator currently has too many passengers. Only the first " + std::to_string(num) + " passengers can get on." << std::endl;
  208.         }
  209.         for(int i = 0; i < num; i++)
  210.         {
  211.             std::string destFloorIn;
  212.             int destFloor;
  213.             std::string output = "What floor is the " + std::to_string(i + 1);
  214.             if(i + 1 == 1)
  215.             {
  216.                 output += "st";
  217.             }
  218.             else if(i + 1 == 2)
  219.             {
  220.                 output += "nd";
  221.             }
  222.             else if(i + 1 == 3)
  223.             {
  224.                 output += "rd";
  225.             }
  226.             else
  227.             {
  228.                 output += "th";
  229.             }
  230.             output += " passenger going to? (Numbers 1-9, or R for the roof floor)";
  231.             std::cout << output << std::endl;
  232.             std::cin >> destFloorIn;
  233.  
  234.             while(destFloorIn != "R" && destFloorIn != "r" && destFloorIn != "1" && destFloorIn != "2" && destFloorIn != "3" && destFloorIn != "4" && destFloorIn != "5" && destFloorIn != "6" && destFloorIn != "7" && destFloorIn != "8" && destFloorIn != "9")
  235.             {
  236.                 std::cout << "I'm sorry, but you haven't exactly floored me with your response. Please try again." << std::endl;
  237.                 std::cout << output << std::endl;
  238.                 std::cin >> destFloorIn;
  239.             }
  240.  
  241.             if(destFloorIn == "R" || destFloorIn == "r")
  242.             {
  243.                 destFloor = 10;
  244.             }
  245.             else
  246.             {
  247.                 destFloor = std::stoi(destFloorIn);
  248.             }
  249.  
  250.             destList[carrying + i] = destFloor;
  251.             carrying++;
  252.         }
  253.  
  254.         calls--;
  255.     }
  256. }
  257.  
  258. void dropOff()
  259. {
  260.     for(int i = 0; i < 10; i++)
  261.     {
  262.         if(destList[i] == curFloor)
  263.         {
  264.             destList[i] = 0;
  265.             carrying--;
  266.         }
  267.     }
  268.  
  269.     for(int i = 0; i < 10; i++)
  270.     {
  271.         if(destList[i] == 0)
  272.         {
  273.             for(int j = i + 1; j < 10; j++)
  274.             {
  275.                 if(destList[j] != 0)
  276.                 {
  277.                     destList[i] = destList[j];
  278.                     destList[j] = 0;
  279.                 }
  280.             }
  281.         }
  282.     }
  283. }
  284.  
  285. void moveFloor(bool goingUp)
  286. {
  287.     if(goingUp)
  288.     {
  289.         dir = "U";
  290.         curFloor++;
  291.         if(curFloor == 10)
  292.         {
  293.             dir = "N";
  294.         }
  295.     }
  296.     else
  297.     {
  298.         dir = "D";
  299.         curFloor--;
  300.         if(curFloor == 1)
  301.         {
  302.             dir = "N";
  303.         }
  304.     }
  305.     printElevator(curFloor, dir != "N", dir == "U");
  306.     dropOff();
  307.     pickUp();
  308. }
  309.  
  310. void bringDown()
  311. {
  312.     for(int i = 0; i < 10; i++)
  313.     {
  314.         if(callList[i].empty())
  315.         {
  316.             for(int j = i + 1; j < 10; j++)
  317.             {
  318.                 if(!callList[j].empty())
  319.                 {
  320.                     callList[i] = callList[j];
  321.                     callList[j] = "";
  322.                 }
  323.             }
  324.         }
  325.  
  326.         if(destList[i] == 0)
  327.         {
  328.             for(int j = i + 1; j < 10; j++)
  329.             {
  330.                 if(destList[j] != 0)
  331.                 {
  332.                     destList[i] = destList[j];
  333.                     destList[j] = 0;
  334.                 }
  335.             }
  336.         }
  337.     }
  338.  
  339. }
  340.  
  341. int main()
  342. {
  343.     dir = "N";
  344.     printElevator(1, false, false);
  345.  
  346.     while(true){
  347.         int rtn = newInput();
  348.         if(rtn == 0)
  349.         {
  350.             std::cout << "Have a nice day!" << std::endl;
  351.             return 0;
  352.         }
  353.  
  354.         bringDown();
  355.  
  356.         std::cout << "Carrying: " + std::to_string(carrying) + " Calls: " + std::to_string(calls) << std::endl;
  357.  
  358.         //elevator procedure
  359.         if(carrying == 0)
  360.         {
  361.             if(calls == 1)
  362.             {
  363.                 //go to it
  364.                 std::string call = callList[0];
  365.                 int callFloor;
  366.                 if(call[0] == 'R')
  367.                 {
  368.                     callFloor = 10;
  369.                 }
  370.                 else
  371.                 {
  372.                     callFloor = call[0] - 48;
  373.                 }
  374.                 if(callFloor > curFloor)
  375.                 {
  376.                     moveFloor(true);
  377.                 }
  378.                 else if(callFloor < curFloor)
  379.                 {
  380.                     moveFloor(false);
  381.                 }
  382.                 else
  383.                 {
  384.                     pickUp();
  385.                 }
  386.             }
  387.             else if(calls > 0)
  388.             {
  389.                 //go to the nearest call in our direction
  390.                 int target;
  391.                 int targetDist = 10;
  392.                 for(int i = 0; i < 10; i++)
  393.                 {
  394.                     std::string cur = callList[i];
  395.                     if(!cur.empty())
  396.                     {
  397.                         if(cur[0] - 48 > curFloor && cur[1] == 'U' && (cur[0] - 48) - curFloor < targetDist)
  398.                         {
  399.                             target = cur[0] - 48;
  400.                         }
  401.                         if(cur[0] - 48 < curFloor && cur[1] == 'D' && curFloor - (cur[0] - 48) < targetDist)
  402.                         {
  403.                             target = cur[0] - 48;
  404.                         }
  405.                     }
  406.                 }
  407.  
  408.                 if(target == curFloor)
  409.                 {
  410.                     pickUp();
  411.                 }
  412.                 else
  413.                 {
  414.                     moveFloor(target > curFloor);
  415.                 }
  416.             }
  417.             else
  418.             {
  419.                 if(curFloor > 1)
  420.                 {
  421.                     moveFloor(false);
  422.                 }
  423.                 else
  424.                 {
  425.                     dir = "N";
  426.                 }
  427.             }
  428.         }
  429.         else
  430.         {
  431.             //figure out direction
  432.             int target;
  433.             int closeDist = 10;
  434.             bool inSameDir = false;
  435.  
  436.             for(int i = 0; i < 10; i++)
  437.             {
  438.                 int curDest = destList[i];
  439.                 if(curDest > curFloor && dir == "U" && curDest - curFloor < closeDist)
  440.                 {
  441.                     inSameDir = true;
  442.                     target = curDest;
  443.                     closeDist = curDest - curFloor;
  444.                 }
  445.                 if(curDest < curFloor && dir == "D" && curFloor - curDest < closeDist)
  446.                 {
  447.                     inSameDir = true;
  448.                     target = curDest;
  449.                     closeDist = curFloor - curDest;
  450.                 }
  451.             }
  452.  
  453.             if(!inSameDir)
  454.             {
  455.                 for(int i = 0; i < 10; i++)
  456.                 {
  457.                     int curDest = destList[i];
  458.                     if(abs(curDest - curFloor) < closeDist)
  459.                     {
  460.                         target = curDest;
  461.                     }
  462.                 }
  463.             }
  464.  
  465.             if(target < curFloor)
  466.             {
  467.                 dir = "D";
  468.             }
  469.             else if(target > curFloor)
  470.             {
  471.                 dir = "U";
  472.             }
  473.  
  474.             //transport them
  475.             moveFloor(dir == "U");
  476.  
  477.             //pick people up if they're on the way and going the same direction
  478.             for(int i = 0; i < 10; i++)
  479.             {
  480.                 std::string cur = callList[i];
  481.                 int callFloor;
  482.                 if(cur[0] == 'R')
  483.                 {
  484.                     callFloor = 10;
  485.                 }
  486.                 else
  487.                 {
  488.                     callFloor = cur[0] - 48;
  489.                 }
  490.                 if(callFloor == curFloor)
  491.                 {
  492.                     if(cur[1] == dir[0])
  493.                     {
  494.                         pickUp();
  495.                     }
  496.                 }
  497.             }
  498.         }
  499.     }
  500. }
Advertisement
Add Comment
Please, Sign In to add comment