Rafpast

Menu code 2

Jul 2nd, 2022
927
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.85 KB | None | 0 0
  1. void Menu::showOptions( int& option, string option1, string option2)
  2. {
  3.  
  4.     if(option1 != "Start Game" || option2 != "Close Game")
  5.     {
  6.         say.clearScreen();
  7.     }
  8.    
  9.  
  10.     option = (option > 2) ? (option - 2) : ((option < 1) ? option + 2 : option);
  11.  
  12.     if (option == 1)
  13.     {
  14.         say.communique("                              >> " + option1 + " << ");
  15.         say.communique("                                 " + option2 + "    ");
  16.     }
  17.     else if (option == 2)
  18.     {
  19.         say.communique("                                 " + option1 + "   ");
  20.         say.communique("                              >> " + option2 + " << ");
  21.     }
  22. }
  23.  
  24. void Menu::arrowSupport(int& option, string option1, string option2)
  25. {
  26.     char choice = ' ';
  27.  
  28.     listen.charInput(choice);
  29.  
  30.     switch (choice)
  31.     {
  32.     case 72:
  33.     case 80:
  34.         option = (choice == 72) ? --option : ((choice == 80) ? ++option : option);
  35.         showOptions(option, option1, option2);
  36.     default:
  37.         break;
  38.     }
  39. }
  40.  
  41.     /**
  42.     * @param int& state: 0 - startScreen(), 1 - mainMenu(), 2 - mapSelection(), 3 - showScore(), 4 - mapEnd()
  43.     */
  44. void  Menu::mainSelection(int& state, string firstOption, string secondOption)
  45. {
  46.     string name;
  47.     int option = 1;
  48.     char choice = 'w';
  49.  
  50.     if (state == 0)
  51.     {
  52.         title();
  53.     }
  54.  
  55.     showOptions(option, firstOption, secondOption);
  56.     bool condition = true, insideCondition = true, showCondition = true;
  57.  
  58.     while (condition)
  59.     {
  60.         if (listen.isEmpty())
  61.         {
  62.             listen.charInput(choice);
  63.  
  64.             if (static_cast <int>(choice) == -32)
  65.             {
  66.                 if (state == 0)
  67.                 {
  68.                     title();
  69.                 }
  70.  
  71.                 arrowSupport(option, firstOption, secondOption);
  72.             }
  73.             else
  74.             {
  75.                 switch (choice)
  76.                 {
  77.                 case 'w':
  78.                 case 'W':
  79.                     option--;
  80.  
  81.                     if (state == 0)
  82.                     {
  83.                         title();
  84.                     }
  85.                     else if (state == 4)
  86.                     {
  87.                         choice = showCondition ? 'w' : 27;
  88.                     }
  89.  
  90.                     showOptions(option, firstOption, secondOption);
  91.  
  92.                     break;
  93.                 case 's':
  94.                 case 'S':
  95.                     option++;
  96.  
  97.                     if (state == 0)
  98.                     {
  99.                         title();
  100.                     }
  101.                     else if (state == 4)
  102.                     {
  103.                         choice = showCondition ? 's' : 27;
  104.                     }
  105.  
  106.  
  107.                     showOptions(option, firstOption, secondOption);
  108.  
  109.                     break;
  110.                 case ' ':
  111.                     if (option == 1)
  112.                     {
  113.                         condition = false;
  114.                         say.clearScreen();
  115.  
  116.                         if (state == 1)
  117.                         {
  118.                             mapSelection();
  119.                         }
  120.                         else if (state == 2)
  121.                         {
  122.                             levels.game("map1.txt");
  123.                             mapEnd();
  124.                         }
  125.                         else if (state == 3)
  126.                         {
  127.                             showResults("Id:", "Number results:", true, numberName.mapAccess(), fullName.mapAccess());
  128.                         }
  129.                         else if (state == 4)
  130.                         {
  131.                             if (insideCondition)
  132.                             {
  133.                                 say.communique("Enter your name:  ");
  134.                                 listen.stringInput(name);
  135.                                 addScore(true, name, levels.getScore());
  136.                                 insideCondition = false;
  137.                                 say.clearScreen();
  138.                             }
  139.  
  140.                             say.communique("Your name is: " + name);
  141.                             say.communique("Your result is: " + std::to_string(levels.getScore()));
  142.                             //fullName.saveScoreToFile(fullName.mapAccess(), "String");
  143.                        
  144.                         }
  145.  
  146.                         showOptions(option, firstOption, secondOption);
  147.                     }
  148.                     else if (option == 2)
  149.                     {
  150.                         say.clearScreen();
  151.  
  152.                         if (state == 0)
  153.                         {                      
  154.                             exit(0);
  155.                         }
  156.                         else if (state == 1)
  157.                         {
  158.                             showScore();
  159.                         }
  160.                         else if (state == 2)
  161.                         {
  162.                             levels.game("map2.txt");
  163.                             mapEnd();
  164.                         }
  165.                         else if (state == 3)
  166.                         {
  167.                             showResults("Name:", "Full name results:", false, numberName.mapAccess(), fullName.mapAccess());
  168.                         }
  169.                         else if (state == 4)
  170.                         {
  171.                             if (insideCondition)
  172.                             {
  173.                                 name = std::to_string(numberName.size() + 1);
  174.                                 addScore(false, name, levels.getScore());
  175.                                 insideCondition = false;
  176.                             }
  177.  
  178.                             say.clearScreen();
  179.                             say.communique("Your number is: " + name);
  180.                             say.communique("Your result is: " + std::to_string(levels.getScore()));
  181.                             //numberName.saveScoreToFile(numberName.mapAccess(), "Int");
  182.                         }
  183.  
  184.                         showOptions(option, firstOption, secondOption);
  185.                        
  186.                     }
  187.  
  188.                     showCondition = false;
  189.  
  190.                     break;
  191.                 case 27:
  192.                    
  193.                     if (state == 1)
  194.                     {
  195.                         say.clearScreen();
  196.                         startScreen();
  197.                     }
  198.                     else if (state == 2 || state == 3 || state == 4)
  199.                     {
  200.                         say.clearScreen();
  201.                         mainMenu();
  202.                     }
  203.  
  204.                     break;
  205.                 default:
  206.                     break;
  207.                 }
  208.             }
  209.         }
  210.     }
  211.     say.clearScreen();
  212. }
  213.  
  214. void Menu::startScreen()
  215. {
  216.     int option = 0;
  217.     mainSelection(option, "Start Game", "Close Game");
  218. }
  219.  
  220. void Menu::mainMenu()
  221. {
  222.     int option = 1;
  223.     mainSelection(option, "Chose Map", "Show Score");
  224. }
  225.  
  226. void Menu::mapSelection()
  227. {
  228.     int option = 2;
  229.     mainSelection(option, "First Map", "Second Map");
  230. }
  231.  
  232. void Menu::showScore()
  233. {
  234.     int option = 3;
  235.     mainSelection(option, "Show number results", "Show full name results");
  236. }
  237.  
  238. void Menu::mapEnd()
  239. {
  240.     int option = 4;
  241.     mainSelection(option, "Enter name", "Wait for number");
  242. }
  243.  
  244. void Menu::showResults(string name, string resultsName, bool whichOne, map<int, int> intMap, map<string, int> stringMap)
  245. {
  246.     string whatItShows;
  247.  
  248.     if (!numberName.mapAccess().empty())
  249.     {
  250.         say.communique("\n                              " + resultsName + "\n");
  251.  
  252.         if (whichOne)
  253.         {
  254.             for (const auto& [key, value] : intMap)
  255.             {
  256.                 whatItShows = "                              " + name + std::to_string(key);
  257.                 whatItShows += " Value: " + std::to_string(value) + "\n";
  258.  
  259.                 say.communique(whatItShows);
  260.             }
  261.         }
  262.         else
  263.         {
  264.             for (const auto& [key, value] : stringMap)
  265.             {
  266.                 whatItShows = "                              " + name + key;
  267.                 whatItShows += " Value: " + std::to_string(value) + "\n";
  268.  
  269.                 say.communique(whatItShows);
  270.             }
  271.         }
  272.     }
  273.  
  274.     say.stop(1);
  275. }
  276.  
  277. void Menu::addScore(bool which, string name, int value)
  278. {
  279.  
  280.     if (which)
  281.     {
  282.         fullName.add(name, value);
  283.     }
  284.     else
  285.     {
  286.         numberName.add(numberName.size() + 1, value);
  287.     }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment