Advertisement
Rafpast

Menu code

Jul 1st, 2022
1,164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.77 KB | None | 0 0
  1. void Menu::showOptions( int& option, string option1, string option2)
  2. {
  3.     if (option > 2) {
  4.         option = option - 2;
  5.     }
  6.     else if (option < 1)
  7.     {
  8.         option = option + 2;
  9.     }
  10.  
  11.     if (option == 1)
  12.     {
  13.         say.communique("                              >>" + option1 + " << ");
  14.         say.communique("                                " + option2 + "    ");
  15.     }
  16.     else if (option == 2)
  17.     {
  18.         say.communique("                                " + option1 + "   ");
  19.         say.communique("                              >>" + option2 + " << ");
  20.     }
  21. }
  22.  
  23. void Menu::arrowSupport(int& option, string option1, string option2)
  24. {
  25.     char choice = ' ';
  26.  
  27.     listen.charInput(choice);
  28.  
  29.     switch (choice)
  30.     {
  31.     case 72:
  32.         --option;
  33.         say.clearScreen();
  34.         showOptions(option, option1, option2);
  35.         break;
  36.     case 80:
  37.         ++option;
  38.         say.clearScreen();
  39.         showOptions(option, option1, option2);
  40.         break;
  41.     default:
  42.         break;
  43.     }
  44.  
  45. }
  46.  
  47. void Menu::startScreen()
  48. {
  49.     int option = 1;
  50.     bool condition = true;
  51.     char choice = ' ', choice2 = ' ';
  52.     string firstOption = "Start Game", secondOption = "Close Game";
  53.    
  54.     title();
  55.     showOptions(option, firstOption, secondOption);
  56.  
  57.     while (condition)
  58.     {
  59.         if (listen.isEmpty())
  60.         {
  61.             listen.charInput(choice);
  62.  
  63.             if (static_cast <int>(choice) == -32)
  64.             {
  65.                 listen.charInput(choice2);
  66.  
  67.                 switch (choice2)
  68.                 {
  69.                 case 72:
  70.                     --option;
  71.                     say.clearScreen();
  72.                     title();
  73.                     showOptions(option, firstOption, secondOption);
  74.                     break;
  75.                 case 80:
  76.                     ++option;
  77.                     say.clearScreen();
  78.                     title();
  79.                     showOptions(option, firstOption, secondOption);
  80.                     break;
  81.                 default:
  82.                     break;
  83.                 }
  84.  
  85.             }
  86.             else
  87.             {
  88.                 switch (choice)
  89.                 {
  90.                 case 'w':
  91.                     option--;
  92.                     say.clearScreen();
  93.                     title();
  94.                     showOptions(option, firstOption, secondOption);
  95.                     break;
  96.                 case 's':
  97.                     option++;
  98.                     say.clearScreen();
  99.                     title();
  100.                     showOptions(option, firstOption, secondOption);
  101.                     break;
  102.                 case' ':
  103.                     if (option == 1)
  104.                     {
  105.                         if (!numberName.isVoid())
  106.                         {
  107.                             numberName.readScoreFromFile("Int");
  108.                         }
  109.  
  110.                         if (!fullName.isVoid())
  111.                         {
  112.                             fullName.readScoreFromFile("String");
  113.                         }
  114.  
  115.                         condition = false;
  116.                     } else if (option == 2)
  117.                     {
  118.                         say.clearScreen();
  119.                         exit(0);
  120.                     }
  121.                    
  122.                     break;
  123.                 default:
  124.                     break;
  125.                 }
  126.             }      
  127.         }
  128.     }
  129.  
  130.     say.clearScreen();
  131. }
  132.  
  133. void Menu::mainMenu()
  134. {
  135.     char choice = ' ';
  136.     bool condition = true;
  137.     int option = 1, whichMap = 0;
  138.     string firstOption = "Chose Map", secondOption = "Show Score";
  139.  
  140.     showOptions(option, firstOption, secondOption);
  141.  
  142.     while (condition)
  143.     {
  144.         if (listen.isEmpty())
  145.         {
  146.             listen.charInput(choice);
  147.  
  148.             if (static_cast <int>(choice) == -32)
  149.             {
  150.                 arrowSupport(option, firstOption, secondOption);
  151.             }
  152.             else
  153.             {
  154.                 switch (choice)
  155.                 {
  156.                 case 'w':
  157.                     option--;
  158.                     say.clearScreen();
  159.                     showOptions(option, firstOption, secondOption);
  160.                     break;
  161.                 case 's':
  162.                     option++;
  163.                     say.clearScreen();
  164.                     showOptions(option, firstOption, secondOption);
  165.                     break;
  166.                 case ' ':
  167.                     if (option == 1)
  168.                     {
  169.                         say.clearScreen();
  170.                         mapSelection(whichMap);
  171.                         condition = false;
  172.                     }
  173.                     else if (option == 2)
  174.                     {
  175.                         showScore();
  176.                         showOptions(option, firstOption, secondOption);
  177.                     }
  178.  
  179.                     break;
  180.                 case 27:
  181.                     say.clearScreen();
  182.                     startScreen();
  183.                     break;
  184.                 default:
  185.                     break;
  186.                 }
  187.             }
  188.         }
  189.     }
  190.     say.clearScreen();
  191. }
  192.  
  193. void Menu::mapSelection(int& option)
  194. {
  195.     string firstOption = "First Map", secondOption = "Second Map";
  196.     bool condition = true;
  197.     char choice = ' ';
  198.  
  199.     showOptions(option, firstOption, secondOption);
  200.  
  201.     while (condition)
  202.     {
  203.         if (listen.isEmpty())
  204.         {
  205.             listen.charInput(choice);
  206.  
  207.             if (static_cast <int>(choice) == -32)
  208.             {
  209.                 arrowSupport(option, firstOption, secondOption);
  210.             }
  211.             else
  212.             {
  213.                 switch (choice)
  214.                 {
  215.                 case 'w':
  216.                     option--;
  217.                     say.clearScreen();
  218.                     showOptions(option, firstOption, secondOption);
  219.                     break;
  220.                 case 's':
  221.                     option++;
  222.                     say.clearScreen();
  223.                     showOptions(option, firstOption, secondOption);
  224.                     break;
  225.                 case ' ':
  226.                     if (option == 1)
  227.                     {
  228.                         levels.game("map1.txt");
  229.                     }
  230.                     else if (option == 2)
  231.                     {
  232.                         levels.game("map2.txt");
  233.                     }
  234.  
  235.                     mapEnd();
  236.  
  237.                     showOptions(option, firstOption, secondOption);
  238.  
  239.                     break;
  240.                 case 27:
  241.                     say.clearScreen();
  242.                     condition = false;
  243.                     mainMenu();
  244.                     break;
  245.                 default:
  246.                     break;
  247.                 }
  248.             }
  249.         }
  250.     }
  251.  
  252.     say.clearScreen();
  253. }
  254.  
  255. void Menu::showScore()
  256. {
  257.     int option = 1;
  258.     char choice = ' ';
  259.     bool condition = true;
  260.     string firstOption = "Show number results", secondOption = "Show full name results";
  261.  
  262.     showOptions(option, firstOption, secondOption);
  263.  
  264.     while (condition)
  265.     {
  266.         if (listen.isEmpty())
  267.         {
  268.             listen.charInput(choice);
  269.  
  270.             if (static_cast <int>(choice) == -32)
  271.             {
  272.                 arrowSupport(option, firstOption, secondOption);
  273.             }
  274.             else
  275.             {
  276.                 switch (choice)
  277.                 {
  278.                 case 'w':
  279.                     option--;
  280.                     say.clearScreen();
  281.                     showOptions(option, firstOption, secondOption);
  282.                     break;
  283.                 case 's':
  284.                     option++;
  285.                     say.clearScreen();
  286.                     showOptions(option, firstOption, secondOption);
  287.                     break;
  288.                 case ' ':
  289.                     if (option == 1)
  290.                     {
  291.                         showResults("Id:", "Number results:", true, numberName.mapAccess(), fullName.mapAccess());
  292.                     }
  293.                     else if (option == 2)
  294.                     {
  295.                         showResults("Name:", "Full name results:", false, numberName.mapAccess(), fullName.mapAccess());
  296.                     }
  297.  
  298.                     break;
  299.                 case 27:
  300.                     condition = false;
  301.                     say.clearScreen();
  302.                     mainMenu();
  303.                     break;
  304.                 default:
  305.                     break;
  306.                 }
  307.             }
  308.         }
  309.     }
  310.  
  311.     say.clearScreen();
  312. }
  313.  
  314. void Menu::mapEnd()
  315. {
  316.     char whichOne = ' ';
  317.     bool condition = true;
  318.     string outcome = "You made " + levels.getScore();
  319.     outcome += " moves. ";
  320.  
  321.     say.communique("outcome");
  322.  
  323.     while (condition)
  324.     {
  325.         say.clearScreen();
  326.         say.communique("Would you like to be remembered by name(1) or number(2)? ");
  327.  
  328.         listen.charInput(whichOne);
  329.  
  330.         if (whichOne == '1')
  331.         {
  332.             string name;
  333.             say.clearScreen();
  334.             say.communique("Enter your name:  ");
  335.             listen.stringInput(name);
  336.             addScore(true, name, levels.getScore());
  337.             say.communique("Your result is: " + std::to_string(levels.getScore()));
  338.             fullName.saveScoreToFile(fullName.mapAccess(), "String");
  339.             say.stop(2);
  340.             condition = false;
  341.         }
  342.         else if (whichOne == '2')
  343.         {
  344.             string name = std::to_string(numberName.size() + 1);
  345.             say.clearScreen();
  346.             say.communique("Your number will be generated: ");
  347.  
  348.             addScore(false, name, levels.getScore());
  349.             say.communique("Your number is: " + name);
  350.             say.communique("Your result is: " + std::to_string(levels.getScore()));
  351.             numberName.saveScoreToFile(numberName.mapAccess(), "Int");
  352.             say.stop(2);
  353.             condition = false;
  354.         }
  355.         else
  356.         {
  357.             say.communique("Wrong choise, try again.");
  358.             say.stop(1);
  359.         }
  360.     }
  361.     say.clearScreen();
  362. }
  363.  
  364. void Menu::showResults(string name, string resultsName, bool whichOne, map<int, int> intMap, map<string, int> stringMap)
  365. {
  366.     string whatItShows;
  367.  
  368.     if (!numberName.mapAccess().empty())
  369.     {
  370.         say.communique("                              " + resultsName + "\n");
  371.  
  372.         if (whichOne)
  373.         {
  374.             for (const auto& [key, value] : intMap)
  375.             {
  376.                 whatItShows = "                              " + name + std::to_string(key);
  377.                 whatItShows += " Value: " + std::to_string(value) + "\n";
  378.  
  379.                 say.communique(whatItShows);
  380.             }
  381.         }
  382.         else
  383.         {
  384.             for (const auto& [key, value] : stringMap)
  385.             {
  386.                 whatItShows = "                              " + name + key;
  387.                 whatItShows += " Value: " + std::to_string(value) + "\n";
  388.  
  389.                 say.communique(whatItShows);
  390.             }
  391.         }
  392.     }
  393.  
  394.     say.stop(1);
  395. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement