Advertisement
Guest User

Game.cpp

a guest
May 20th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.52 KB | None | 0 0
  1. #include "Game.h"
  2.  
  3. bool Game::file_exists(string filename)
  4. {
  5.     if (ifstream(filename))
  6.     {
  7.         return true;
  8.     }
  9.     return false;
  10. }
  11.  
  12. //================================================================
  13. //===========================PUBLIC===============================
  14. //================================================================
  15.  
  16. Game::Game(Dictionary dictionary, Board board) {
  17.  
  18.     this->dictionary = dictionary;
  19.     this->board = board;
  20. }
  21.  
  22. //Shows the initial options given to the user
  23. void Game::main_menu(string dictionary_file) {
  24.  
  25.     unsigned int option;
  26.  
  27.     cout << "CROSSWORDS PUZZLE CREATOR" << endl;
  28.     cout << "==============================================\n" << endl;
  29.  
  30.     cout << "INSTRUCTIONS:" << endl;
  31.     cout << "CTRL-Z = back" << endl;
  32.  
  33.     cout << "----------------------------------------------\n" << endl;
  34.  
  35.     cout << "OPTIONS:" << endl;
  36.     cout << "1 - Create Puzzle" << endl;
  37.     cout << "2 - Resume Puzzle" << endl;
  38.     cout << "0 - Exit" << endl << endl;
  39.  
  40.     cout << "Option: ";
  41.     cin >> option;
  42.  
  43.     while (option != 0)
  44.     {
  45.  
  46.         while (
  47.             cin.fail()
  48.             || (option < 0)
  49.             || (option > 2)
  50.             )
  51.         {
  52.             cout << "Please insert a number from the options available" << endl;
  53.             cout << "1 - Create Puzzle" << endl;
  54.             cout << "2 - Resume Puzzle" << endl;
  55.             cout << "0 - Exit" << endl << endl;
  56.             cin.clear();
  57.             cin.ignore(1000, '\n');
  58.             cout << "Option: ";
  59.             cin >> option;
  60.         }
  61.  
  62.         switch (option)
  63.         {
  64.         case 1:
  65.         {
  66.             dictionary.loadDictionary(dictionary_file);
  67.             if (board.board())
  68.             {
  69.                 board.show_board();
  70.                 advanced_menu();
  71.                 board.storeBoard(dictionary_file, false, "irrelevant parameter");
  72.             }
  73.         }
  74.         break;
  75.         case 2:
  76.         {
  77.             string board_file_name = board.loadBoard();
  78.             dictionary.loadDictionary(dictionary_file);
  79.             board.show_board();
  80.             advanced_menu();
  81.             board.storeBoard(dictionary_file, true, board_file_name);
  82.         }
  83.         break;
  84.         case 0:
  85.             cout << "Shuting down..." << endl;
  86.         }
  87.  
  88.         if (option != 0) {
  89.  
  90.             cin.clear();
  91.  
  92.             cout << endl << "OPTIONS:" << endl;
  93.             cout << "1 - Create Puzzle" << endl;
  94.             cout << "2 - Resume Puzzle" << endl;
  95.             cout << "0 - Exit" << endl << endl;
  96.  
  97.             cout << "Option: ";
  98.             cin >> option;
  99.         }
  100.     }
  101. }
  102.  
  103. //================================================================
  104.  
  105. //The menu with the options that affect the board directly(insert/remove word)
  106. void Game::advanced_menu() {
  107.  
  108.     char command;
  109.  
  110.     cout << "COMMANDS:" << endl;
  111.     cout << "+ = place word in board" << endl;
  112.     cout << "- = remove word from board" << endl;
  113.     cout << "b = back to main menu" << endl << endl;
  114.  
  115.     cout << "Command: ";
  116.     cin >> command;
  117.  
  118.     while (command != 'b')
  119.     {
  120.  
  121.         while (cin.fail()
  122.             || ((command != '+')
  123.             && (command != '-')
  124.             && (command != 'b'))
  125.             )
  126.         {
  127.             cout << endl << "Invalid command input" << endl;
  128.             cout << "PLease insert one of the available commands" << endl << endl;
  129.  
  130.             cout << "COMMANDS:" << endl;
  131.             cout << "+ = place word in board" << endl;
  132.             cout << "- = remove word from board" << endl;
  133.             cout << "b = back to main menu" << endl << endl;
  134.  
  135.             cout << "Command: ";
  136.             cin.clear();
  137.             cin.ignore(1000, '\n');
  138.             cin >> command;
  139.         }
  140.  
  141.         switch (command)
  142.         {
  143.         case '+':
  144.             board.insert_word(dictionary);
  145.             break;
  146.         case '-':
  147.             board.remove_word();
  148.             break;
  149.         }
  150.  
  151.         cin.clear();
  152.  
  153.         cout << endl << "COMMANDS:" << endl;
  154.         cout << "+ = place word in board" << endl;
  155.         cout << "- = remove word from board" << endl;
  156.         cout << "b = back to main menu" << endl << endl;
  157.  
  158.         cout << "Command: ";
  159.         cin >> command;
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement