Advertisement
Guest User

Project.cpp

a guest
May 20th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 29.27 KB | None | 0 0
  1. #include "Game.h"
  2.  
  3. // Set text color
  4. void setcolor(unsigned int color)
  5. {
  6.     HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
  7.     SetConsoleTextAttribute(hcon, color);
  8. }
  9.  
  10. // Set text color & background
  11. void setcolor(unsigned int color, unsigned int background_color) {
  12.  
  13. #define BLACK 0
  14. #define LIGHTGRAY 7
  15. #define LIGHTRED 12
  16.  
  17.     HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
  18.     if (background_color == BLACK)
  19.         SetConsoleTextAttribute(hCon, color);
  20.     else
  21.         SetConsoleTextAttribute(hCon, color | BACKGROUND_BLUE | BACKGROUND_GREEN |
  22.             BACKGROUND_RED);
  23. }
  24.  
  25. bool file_exists(string filename)
  26. {
  27.     ifstream file(filename);
  28.  
  29.     if (file.is_open())
  30.     {
  31.         file.close();
  32.         return true;
  33.     }
  34.     return false;
  35. }
  36.  
  37. bool comma_line(string line)
  38. {
  39.     return (any_of(line.begin(), line.end(), [](char c) {return c == ','; }));
  40. }
  41.  
  42. bool pound_line(string line)
  43. {
  44.     return (any_of(line.begin(), line.end(), [](char c) {return c == '#'; }));
  45. }
  46.  
  47.  
  48. string all_caps(string word) {
  49.  
  50.     for (size_t i = 0; i < word.size(); i++)
  51.         word[i] = toupper(word[i]);
  52.     return word;
  53. }
  54.  
  55. pair<string, vector<string>> separate_line(string line) {
  56.  
  57.     auto colon = line.find(': ');
  58.     string headline = line.substr(0, colon - 1);
  59.     headline = all_caps(headline);
  60.     line.erase(0, colon + 1);
  61.     vector<string> synonims_vector;
  62.  
  63.     while (comma_line(line))
  64.     {
  65.         size_t comma = line.find(", ");
  66.         string synonim = line.substr(0, comma);
  67.         synonim = all_caps(synonim);
  68.         synonims_vector.push_back(synonim);
  69.         line.erase(0, comma + 2);
  70.     }
  71.  
  72.     synonims_vector.push_back(line);
  73.  
  74.     return pair<string, vector<string>> (headline, synonims_vector);
  75. }
  76.  
  77. void loadDictionary(string inputFile, map<string, vector<string>>& map) {
  78.  
  79.     string line;
  80.     ifstream inFile(inputFile);
  81.  
  82.     if (inFile.is_open())
  83.     {
  84.         while (getline(inFile, line))
  85.             map.insert(separate_line(line));
  86.         inFile.close();
  87.     }
  88.     else cout << "Unable to open file" << endl;
  89.  
  90. }
  91.  
  92. void storeBoard(string dictionary_file, vector<vector<char>> m_board, map<string,string> board_words) {
  93.  
  94.     unsigned int b;
  95.     ostringstream board_number;
  96.     fstream board_number_file("Board Number.txt");
  97.  
  98.     board_number_file >> b;
  99.     b++;
  100.     board_number_file << b;
  101.  
  102.     board_number.width(3);
  103.     char blanks = board_number.fill('0');
  104.     board_number << b;
  105.     string file_name = 'b' + board_number.str() + ".txt"; // este e o nome do ficheiro, e epreciso meter a stream em string, daí .srt()
  106.  
  107.     ofstream board_file;
  108.     board_file.open(file_name);
  109.  
  110.     //STORING DATA INTO FILE
  111.  
  112.     board_file << "Dictionary File:" << endl;
  113.     board_file << dictionary_file << endl;
  114.     board_file << "Board Contents:" << endl;
  115.  
  116.     int line_size = m_board.size();
  117.     int column_size = m_board[0].size();
  118.  
  119.     for (int l = 0; l < line_size; l++) // storing the contents of the board in the board_file
  120.     {
  121.         for (int c = 0; c < column_size; c++)
  122.         {
  123.             board_file << setw(3) << m_board[l][c];
  124.         }
  125.         board_file << endl;
  126.     }
  127.  
  128.     board_file << "Words in board:" << endl;
  129.  
  130.     // storing all the words currently in the board into the board file along witht their positions in the board
  131.     for (auto synonims_pair = board_words.begin(); synonims_pair != board_words.end(); synonims_pair++) //synonims pair refers to the pair
  132.     {
  133.         board_file << synonims_pair->second << ": " << synonims_pair->first << endl;
  134.     }
  135.  
  136.     board_file << "End of file" << endl;
  137.  
  138.     board_file.close();
  139.  
  140. }
  141.  
  142. //CHECKS IF LINE HAS SPACES
  143. bool space_line(string line)
  144. {
  145.     return (any_of(line.begin(), line.end(), [](char c) {return c == ' '; }));
  146. }
  147.  
  148. void loadBoard(vector<vector<char>> &m_board, map<string, string> &board_words) {
  149.  
  150.     string file_name; //name of file containing the contents of the chosen board
  151.     cout << "Board File?" << endl;
  152.     cin >> file_name;
  153.  
  154.     while (cin.fail()
  155.         || (!(file_exists(file_name)))
  156.         || cin.eof())
  157.     {
  158.         if (cin.fail())
  159.             cout << "Invalid type of input!" << endl;
  160.         else cout << "Unable to open given file" << endl;
  161.         cin.clear();
  162.         cin.ignore(1000, '\n');
  163.         cout << "Board File?" << endl;
  164.         cin >> file_name;
  165.     }
  166.  
  167.     if (!cin.eof()) {
  168.  
  169.         string line;
  170.         string dictionary_file;
  171.         ifstream board_file(file_name);
  172.         int l = 0;
  173.         bool file_start = false, board_start = false, board_end = false, file_end = false;
  174.         while (getline(board_file, line))
  175.         {
  176.  
  177.             if ((file_start) && (!(board_start)) && (line != "Board Contents:"))
  178.                 dictionary_file = line;
  179.             else if ((board_start) && (!(board_end)) && (line != "Words in board:"))
  180.             {
  181.                 m_board.resize(line.length() / 3);
  182.  
  183.                 while (pound_line(line))
  184.                 {
  185.                     m_board[l].push_back(line[2]);
  186.                     line.erase(0, 3);
  187.                 }
  188.                 l++;
  189.             }
  190.             else if ((board_end) && (!(file_end)) && (line != "End of file"))
  191.             {
  192.                 string position = line.substr(0, 3);
  193.                 line.erase(0, 5);
  194.                 string word = line;
  195.                 board_words.insert(pair<string, string>(word, position));
  196.             }
  197.  
  198.             if (line == "Dictionary File:")
  199.                 file_start = true;
  200.             else if (line == "Board Contents:")
  201.                 board_start = true;
  202.             else if (line == "Words in board:")
  203.                 board_end = true;
  204.             else if (line == "End of file")
  205.                 file_end = true;
  206.         }
  207.  
  208.         cout << dictionary_file << endl;
  209.  
  210.     }
  211. }
  212.  
  213. bool word_exists(string word, map<string, vector<string>> synonims_map) {
  214.  
  215.     if (synonims_map.find(word) == synonims_map.end())
  216.         return false;
  217.  
  218.     return true;
  219. }
  220.  
  221. //================================================================
  222.  
  223. bool board(vector<vector<char>> &m_board) {
  224.  
  225.     unsigned int l, c;
  226.  
  227.     cout << "Board size (lines & columns) ? ";
  228.     cin >> l >> c;
  229.  
  230.     //Testar se input é valido e nr maior que 0 e menor que 27
  231.     while (cin.fail() || l < 1 || c < 1 || l > 26 || c > 26) {
  232.         cout << "Insert 2 numbers larger than 0 and smaller than 27!" << endl;
  233.         cin.clear();
  234.         cin.ignore(1000, '\n');
  235.         cout << "Board size (lines & columns)? ";
  236.         cin >> l >> c;
  237.     }
  238.  
  239.     if (l != 0) {
  240.  
  241.         vector<char> column;
  242.  
  243.         for (unsigned int i = 1; i < c + 3; i++) {
  244.  
  245.             column.push_back('.');
  246.         }
  247.  
  248.         for (unsigned int i = 1; i < l + 3; i++) {
  249.  
  250.             m_board.push_back(column);
  251.         }
  252.  
  253.         m_board[0][0] = '#';
  254.  
  255.         //Fills the first line with '#'
  256.         for (unsigned int c = 1; c < m_board.size(); c++) {
  257.  
  258.             m_board[0][c] = '#';
  259.         }
  260.  
  261.         //Fills the last line with '#'
  262.         for (unsigned int c = 1; c < m_board.size(); c++) {
  263.  
  264.             m_board[m_board[0].size() - 1][c] = '#';
  265.         }
  266.  
  267.         //Fills the first column with '#'
  268.         for (unsigned int l = 1; l < m_board[0].size(); l++) {
  269.  
  270.             m_board[l][0] = '#';
  271.         }
  272.  
  273.         //Fills the last column with '#'
  274.         for (unsigned int l = 1; l < m_board[0].size(); l++) {
  275.  
  276.             m_board[l][m_board.size() - 1] = '#';
  277.         }
  278.  
  279.         cout << endl;
  280.  
  281.         return true;
  282.  
  283.     }
  284.  
  285.     return false;
  286. }
  287.  
  288. void show_board(vector<vector<char>> m_board) {
  289.  
  290.     //Letras minusculas e maiusculas que vao ser usadas para preencher
  291.     int uppers = 65, lowers = 97;
  292.  
  293.     //Letras minusculas e maiusculas que vao ser usadas para preencher
  294.  
  295.     for (size_t l = 0; l < m_board.size() - 1; l++)
  296.     {
  297.         for (size_t c = 0; c < m_board[0].size() - 1; c++)
  298.         {
  299.             //First Tile
  300.             if ((l == 0) && (c == 0))
  301.             {
  302.                 setcolor(12);
  303.                 cout << left << setw(2) << m_board[0][0] << " ";
  304.             }
  305.             //First Line
  306.             else if (l == 0)
  307.             {
  308.                 setcolor(12);
  309.                 cout << left << setw(2) << (char)lowers++;
  310.             }
  311.             //First Column
  312.             else if (c == 0)
  313.             {
  314.                 setcolor(12);
  315.                 cout << left << setw(2) << (char)uppers++;
  316.             }
  317.             //Board Contents
  318.             else
  319.             {
  320.                 if (m_board[l][c] == '#')
  321.                 {
  322.                     setcolor(0, 7);
  323.                     cout << " ";
  324.                     setcolor(7, 0);
  325.                     cout << '#';
  326.                 }
  327.                 else
  328.                 {
  329.                     setcolor(0, 7);
  330.                     cout << right << setw(2) << m_board[l][c];
  331.                 }
  332.             }
  333.         }
  334.         //Back to original color
  335.         cout << " ";
  336.         setcolor(7, 0);
  337.         cout << " " << endl;
  338.     }
  339.  
  340.     cout << endl;
  341. }
  342.  
  343. bool horizontal_exception(int l, int c, vector<vector<char>> m_board, map<string, vector<string>> synonims_map) {
  344.  
  345.     int line_size = m_board.size() - 2;
  346.     string possible_word, possible_word_head, possible_word_tail;
  347.  
  348.     if (c == 1)
  349.     {
  350.         for (int c_right = c; (isalpha(m_board[l][c_right])); c_right++)
  351.         {
  352.             possible_word.push_back(m_board[l][c_right]);
  353.         }
  354.     }
  355.     else if (c == line_size)
  356.     {
  357.         for (int c_left = c; (isalpha(m_board[l][c_left])); c_left--)
  358.         {
  359.             possible_word.push_back(m_board[l][c_left]);
  360.         }
  361.         reverse(possible_word.begin(), possible_word.end());
  362.     }
  363.     else
  364.     {
  365.         if (!(isalpha(m_board[l][c - 1])))
  366.         {
  367.             for (int c_right = c + 1; (isalpha(m_board[l][c_right])); c_right++)
  368.             {
  369.                 possible_word_tail.push_back(m_board[l][c_right]);
  370.             }
  371.  
  372.             for (int c_left = c; (isalpha(m_board[l][c_left])); c_left--)
  373.             {
  374.                 possible_word_head.push_back(m_board[l][c_left]);
  375.             }
  376.             reverse(possible_word_head.begin(), possible_word_head.end());
  377.             possible_word = possible_word_head + possible_word_tail;
  378.         }
  379.         else
  380.         {
  381.             for (int c_right = c; (isalpha(m_board[l][c_right])); c_right++)
  382.             {
  383.                 possible_word_tail.push_back(m_board[l][c_right]);
  384.             }
  385.  
  386.             for (int c_left = c - 1; (isalpha(m_board[l][c_left])); c_left--)
  387.             {
  388.                 possible_word_head.push_back(m_board[l][c_left]);
  389.             }
  390.             reverse(possible_word_head.begin(), possible_word_head.end());
  391.             possible_word = possible_word_head + possible_word_tail;
  392.         }
  393.     }
  394.  
  395.     return (word_exists(possible_word, synonims_map));
  396. }
  397.  
  398. bool vertical_exception(int l, int c, vector<vector<char>> m_board, map<string, vector<string>> synonims_map) {
  399.  
  400.     int column_size = m_board[0].size() - 2;
  401.     string possible_word, possible_word_head, possible_word_tail;
  402.  
  403.     if (l == 1)
  404.     {
  405.         for (int l_down = l; (isalpha(m_board[l_down][c])); l_down++)
  406.         {
  407.             possible_word.push_back(m_board[l_down][c]);
  408.         }
  409.     }
  410.     else if (l == column_size)
  411.     {
  412.         for (int l_up = l; (isalpha(m_board[l_up][c])); l_up--)
  413.         {
  414.             possible_word.push_back(m_board[l_up][c]);
  415.         }
  416.         reverse(possible_word.begin(), possible_word.end());
  417.     }
  418.     else
  419.     {
  420.         if (!(isalpha(m_board[l - 1][c])))
  421.         {
  422.             for (int l_down = l + 1; (isalpha(m_board[l_down][c])); l_down++)
  423.             {
  424.                 possible_word_tail.push_back(m_board[l_down][c]);
  425.             }
  426.  
  427.             for (int l_up = l; (isalpha(m_board[l_up][c])); l_up--)
  428.             {
  429.                 possible_word_head.push_back(m_board[l_up][c]);
  430.             }
  431.             reverse(possible_word_head.begin(), possible_word_head.end());
  432.             possible_word = possible_word_head + possible_word_tail;
  433.         }
  434.         else
  435.         {
  436.             for (int l_down = l; (isalpha(m_board[l_down][c])); l_down++)
  437.             {
  438.                 possible_word_tail.push_back(m_board[l_down][c]);
  439.             }
  440.  
  441.             for (int l_up = l - 1; (isalpha(m_board[l_up][c])); l_up--)
  442.             {
  443.                 possible_word_head.push_back(m_board[l_up][c]);
  444.             }
  445.             reverse(possible_word_head.begin(), possible_word_head.end());
  446.             possible_word = possible_word_head + possible_word_tail;
  447.         }
  448.     }
  449.  
  450.     return (word_exists(possible_word, synonims_map));
  451. }
  452.  
  453. bool word_fits(string position, string word, vector<vector<char>> m_board, map<string, string> board_words, map<string, vector<string>> synonims_map) {
  454.  
  455.     char line_letter = position[0];
  456.     char column_letter = position[1];
  457.     char direction_letter = position[2];
  458.  
  459.     int line_size = m_board.size() - 2;
  460.     int column_size = m_board[0].size() - 2;
  461.  
  462.     size_t initial_line = (int)line_letter - 64;
  463.     size_t initial_column = (int)column_letter - 96;
  464.     size_t l = initial_line;
  465.     size_t c = initial_column;
  466.  
  467.     //VERTICALLY
  468.     if ((direction_letter == 'V') || (direction_letter == 'v')) {
  469.  
  470.         //Checking if word limmits don't go beyond the board's lower limits
  471.         if ((initial_line + word.length()) > line_size + 1)
  472.         {
  473.             //cout << "Word would go beyond the board limits" << endl;
  474.             return false;
  475.         }
  476.  
  477.         if (isalpha(m_board[l - 1][c])) //checking that there isnt anything before the word where the # would be
  478.         {
  479.             //cout << "The tile where the '#' before the word would be isn't empty" << endl;
  480.             return false;
  481.         }
  482.  
  483.         //Checking if word wouldn't overlap with existing letters and if there wouldn't any letters around it
  484.         for (int i = 0; i < word.length(); l++, i++) {
  485.  
  486.             //if current board tiles where word letters would be are different from those letters...
  487.             if ((m_board[l][c] != '.') && (m_board[l][c] != word[i]))
  488.             {
  489.                 //cout << "There would be an error overlap(different characters for the same tile) of word wih the current tiles of where word would be in the board" << endl;
  490.                 return false;
  491.             }
  492.  
  493.             //Checking if there would be other vertical words siding with our word:
  494.             //Note: there are a few exceptions where this is alright
  495.  
  496.             //Checking left
  497.             if ((((isalpha(m_board[l - 1][c - 1])) && (isalpha(m_board[l][c - 1]))) || ((isalpha(m_board[l][c - 1])) && (isalpha(m_board[l + 1][c - 1]))))
  498.                 && (!(horizontal_exception(l, c - 1, m_board, synonims_map))))
  499.             {
  500.                 //cout << "5There would be another vertical word siding with word on the left" << endl;
  501.                 return false;
  502.             }
  503.             //Checking right
  504.             if ((((isalpha(m_board[l - 1][c + 1])) && (isalpha(m_board[l][c + 1]))) || ((isalpha(m_board[l][c + 1])) && (isalpha(m_board[l + 1][c + 1]))))
  505.                 && (!(horizontal_exception(l, c + 1, m_board, synonims_map))))
  506.             {
  507.                 //cout << "6There would be another vertical word siding with word on the right" << endl;
  508.                 return false;
  509.             }
  510.         }
  511.  
  512.             if (isalpha(m_board[l][c])) //checking that there isnt anything after the word where the # would be
  513.             {
  514.                 //cout << "The tile where the '#' after the word would be isn't empty" << endl;
  515.                 return false;
  516.             }
  517.     }
  518.  
  519.     //HORIZONTALLY
  520.     if ((direction_letter == 'H') || (direction_letter == 'h')) {
  521.  
  522.         //Checking if word limmits don't go beyond the board's right limits
  523.         if ((initial_column + word.length()) > column_size + 1)
  524.         {
  525.             //cout << "Word would go beyond the board limits" << endl;
  526.             return false;
  527.         }
  528.  
  529.             if (isalpha(m_board[l][c - 1])) //checking that there isnt anything before the word where the # would be
  530.             {
  531.                 //cout << "The tile where the '#' before the word would be isn't empty" << endl;
  532.                 return false;
  533.             }
  534.  
  535.         //Checking if word wouldn't overlap with existing letters and if there wouldn't be any letters around it
  536.         for (int i = 0; i < word.length(); c++, i++) {
  537.  
  538.             //if tiles where word letters would be are different from those letters...
  539.             if ((m_board[l][c] != '.') && (m_board[l][c] != word[i]))
  540.             {
  541.                 //cout << "There would be an error overlap(different characters for the same tile) of word wih the current tiles of where word would be in the board" << endl;
  542.                 return false;
  543.             }
  544.  
  545.             //Checking if there would be other horizontal words siding with our word:
  546.             //Note: there are a few exceptions where this is alright
  547.  
  548.             //Checking down
  549.             if (((isalpha(m_board[l + 1][c - 1])) && (isalpha(m_board[l + 1][c])) || ((isalpha(m_board[l + 1][c])) && (isalpha(m_board[l + 1][c + 1]))))
  550.                 && (!(vertical_exception(l + 1, c, m_board, synonims_map))))
  551.             {
  552.                 //cout << "5There would be another horizontal word siding with word below" << endl;
  553.                 return false;
  554.             }
  555.             //Checking up
  556.             if ((((isalpha(m_board[l - 1][c - 1])) && (isalpha(m_board[l - 1][c]))) || ((isalpha(m_board[l - 1][c])) && (isalpha(m_board[l - 1][c + 1]))))
  557.                 && (!(vertical_exception(l - 1, c, m_board, synonims_map))))
  558.             {
  559.                 //cout << "6There would be another horizontal word siding with word above" << endl;
  560.                 return false;
  561.             }
  562.         }
  563.  
  564.             if (isalpha(m_board[l][c])) //checking that there aren't any letters after the word where the # would be
  565.             {
  566.                 //cout << "The tile where the '#' after the word would be isn't empty" << endl;
  567.                 return false;
  568.             }
  569.     }
  570.  
  571.     return true;
  572. }
  573.  
  574. void insert_word(vector<vector<char>> &m_board, map<string, string> &board_words, map<string, vector<string>> synonims_map) {
  575.  
  576.     //Posititon
  577.     string position;
  578.     cout << "Position (LCD / CTRL-Z = cancel) ? ";
  579.     cin >> position;
  580.  
  581.     int line_size = m_board.size() - 2;
  582.     int column_size = m_board[0].size() - 2;
  583.  
  584.     //Testing if input position is valid
  585.     while (true)
  586.     {
  587.         if ((position.length() == 3
  588.             && (int)position[0] >= 65 && (int)position[0] <= 64 + line_size
  589.             && (int)position[1] >= 97 && (int)position[1] <= 96 + column_size
  590.             && ((position[2] == 'V' || position[2] == 'H')
  591.                 || (position[2] == 'v' || position[2] == 'h')))
  592.  
  593.             || cin.eof())
  594.  
  595.             break;
  596.  
  597.         else
  598.         {
  599.             cout << "Introduza duas letras (Maiuscula seguida de Minuscula) que pertencam ao quadro seguida da direcao desejada (V ou H)\n" << endl;
  600.             cin.clear();
  601.             cin.ignore(1000, '\n');
  602.             cout << "Position (LCD / CTRL-Z = cancel) ? ";
  603.             cin >> position;
  604.         }
  605.     }
  606.  
  607.     if (position.length() > 0) //If the user didn't change his mind
  608.     {
  609.         string word;
  610.         cout << "Word to insert (CTRL-Z = cancel) ? ";
  611.         cin >> word;
  612.  
  613.         word = all_caps(word);
  614.  
  615.         //Testing if input word is valid
  616.  
  617.         while (cin.fail()
  618.             || (!(word_exists(word, synonims_map)))
  619.             || (!(word_fits(position, word, m_board, board_words, synonims_map)))
  620.             || !cin.eof())
  621.         {
  622.             if (cin.fail())
  623.             {
  624.                 cout << "Invalid Input for Word!" << endl;
  625.                 cin.clear();
  626.                 cin.ignore(1000, '\n');
  627.                 cout << "Word to insert.. ? ";
  628.             }
  629.             else if (!(word_exists(word, synonims_map)))
  630.             {
  631.                 cout << "Given word does not exist according to our records" << endl;
  632.                 cin.clear();
  633.                 cin.ignore(1000, '\n');
  634.                 cout << "Please insert a different word:" << endl;
  635.             }
  636.             else if (!(word_fits(position, word, m_board, board_words, synonims_map)))
  637.             {
  638.                 cout << "Given word cannot be placed in position" << endl;
  639.                 cin.clear();
  640.                 cin.ignore(1000, '\n');
  641.                 cout << "Please insert a different word or alter the board so it fits:" << endl;
  642.             }
  643.             else break;
  644.  
  645.             cin >> word;
  646.             word = all_caps(word);
  647.         }
  648.  
  649.         //INSERTING WORD
  650.  
  651.         char line_letter = position[0];      //line_letter represents the line where the first letter of the word the user wants to insert in the board will be
  652.         char column_letter = position[1];    //column_letter represents the column where the first letter of the word the user wants to insert in the board will be
  653.         char direction_letter = position[2]; //direction_letter represents the direction in which the word the user wants to insert in  the board will be writen
  654.  
  655.         word = all_caps(word);
  656.         size_t initial_line = (int)line_letter - 64;           //Ex: Se line_letter = 'a' entao initial_line = 1
  657.         size_t initial_column = (int)column_letter - 96;       //Ex: Se column_letter = 'A' entao initial_column = 1
  658.         size_t l = initial_line;
  659.         size_t c = initial_column;
  660.  
  661.         // Inserts word in board vertically
  662.         if ((direction_letter == 'V') || (direction_letter == 'v')) {
  663.  
  664.             m_board[l - 1][c] = '#'; //places a # before the word
  665.  
  666.             for (int i = 0; i < word.length(); i++, l++) {
  667.  
  668.                 m_board[l][c] = word[i];
  669.             }
  670.  
  671.             m_board[l][c] = '#'; //places a # after the word
  672.         }
  673.  
  674.         //Inserts word in board horizontally
  675.         if ((direction_letter == 'H') || (direction_letter == 'h')) {
  676.  
  677.  
  678.             m_board[l][c - 1] = '#'; //places a # before the word
  679.  
  680.             for (int i = 0; i < word.length(); i++, c++) {
  681.  
  682.                 m_board[l][c] = word[i];
  683.             }
  684.  
  685.             m_board[l][c] = '#'; //places a # after the word
  686.         }
  687.  
  688.         if (word.length() > 0) //If the user didn't change his mind...
  689.         {
  690.             board_words.insert(pair<string, string>(word, position)); //added word to the list of words in the board
  691.                                                                       //sera que devia usar emplace em vez de insert?, esta bem feito este pair temporario?
  692.  
  693.             show_board(m_board);
  694.         }
  695.     }
  696. }
  697.  
  698. void pound_check(vector<vector<char>> &m_board, map<string, string> board_words) {
  699.  
  700.     m_board[0][0] = '#';
  701.  
  702.     //Fills the first line with '#'
  703.     for (int c = 1; c < m_board.size(); c++) {
  704.  
  705.         m_board[0][c] = '#';
  706.     }
  707.  
  708.     //Fills the last line with '#'
  709.     for (int c = 1; c < m_board.size(); c++) {
  710.  
  711.         m_board[m_board[0].size() - 1][c] = '#';
  712.     }
  713.  
  714.     //Fills the first column with '#'
  715.     for (int l = 1; l < m_board[0].size(); l++) {
  716.  
  717.         m_board[l][0] = '#';
  718.     }
  719.  
  720.     //Fills the last column with '#'
  721.     for (int l = 1; l < m_board[0].size(); l++) {
  722.  
  723.         m_board[l][m_board.size() - 1] = '#';
  724.     }
  725.  
  726.     for (auto it = board_words.begin(); it != board_words.end(); it++) {
  727.  
  728.         string word = it->first;
  729.         string position = it->second;
  730.  
  731.         char line_letter = position[0];      //line_letter represents the line where the first letter of the word the user wants to insert in the board will be
  732.         char column_letter = position[1];    //column_letter represents the column where the first letter of the word the user wants to insert in the board will be
  733.         char direction_letter = position[2]; //direction_letter represents the direction in which the word the user wants to insert in  the board will be writen
  734.  
  735.         size_t initial_line = (int)line_letter - 64;           //Ex: Se line_letter = 'a' entao initial_line = 1
  736.         size_t initial_column = (int)column_letter - 96;       //Ex: Se column_letter = 'A' entao initial_column = 1
  737.         size_t l = initial_line;
  738.         size_t c = initial_column;
  739.  
  740.         if ((direction_letter == 'V') || (direction_letter == 'v')){
  741.  
  742.             m_board[l - 1][c] = '#';
  743.             m_board[l + word.length()][c] = '#';
  744.         }
  745.         if ((direction_letter == 'H') || (direction_letter == 'h')) {
  746.  
  747.             m_board[l][c - 1] = '#';
  748.             m_board[l][c + word.length()] = '#';
  749.         }
  750.     }
  751. }
  752.  
  753. void remove_word(vector<vector<char>> &m_board, map<string, string> &board_words) {
  754.  
  755.     string word;
  756.     cout << "Word to remove (CTRL-Z = cancel) ?" << endl;
  757.     cin >> word;
  758.  
  759.     word = all_caps(word);
  760.  
  761.     //Testing if input word is on the board
  762.     while (board_words.find(word) == board_words.end()
  763.         || !cin.eof()
  764.         || cin.fail())
  765.     {
  766.         if (cin.fail())
  767.         {
  768.             cout << "Invalid Input for Word!" << endl;
  769.             cin.clear();
  770.             cin.ignore(1000, '\n');
  771.             cout << "Word to remove (CTRL-Z = cancel) ?" << endl;
  772.         }
  773.         else if (board_words.find(word) == board_words.end())
  774.         {
  775.             cout << "There is no such word currently in the board" << endl;
  776.             cin.clear();
  777.             cin.ignore(1000, '\n');
  778.             cout << "Word to remove (CTRL-Z = cancel) ?" << endl;
  779.         }
  780.         else break;
  781.  
  782.         cin >> word;
  783.         word = all_caps(word);
  784.     }
  785.  
  786.     if (word.length() > 0) //If the user didn't change his mind...
  787.     {
  788.         map<string, string>::iterator it = board_words.find(word);
  789.         string position = it->second; //tells you the position of the word to be removed
  790.  
  791.         char line_letter = position[0];      //line_letter represents the line where the first letter of the word the user wants to insert in the board will be
  792.         char column_letter = position[1];    //column_letter represents the column where the first letter of the word the user wants to insert in the board will be
  793.         char direction_letter = position[2]; //direction_letter represents the direction in which the word the user wants to insert in  the board will be writen
  794.  
  795.         size_t initial_line = (int)line_letter - 64;           //Ex: If line_letter = 'a' then initial_line = 1
  796.         size_t initial_column = (int)column_letter - 96;       //Ex: If column_letter = 'A' then initial_column = 1
  797.         size_t l = initial_line;
  798.         size_t c = initial_column;
  799.  
  800.         //REMOVING WORD
  801.  
  802.         //There will be no need to later "restore" words that may have been intercepted with the word removed
  803.         //as we are checking before removing a letter wether or not it is attached to another word
  804.  
  805.         //Removes word in board vertically
  806.         if ((direction_letter == 'V') || (direction_letter == 'v')) {
  807.  
  808.             //Resets the (#)tile before the word
  809.             m_board[l - 1][c] = '.';
  810.  
  811.             for (int i = 0; i < word.length(); i++, l++) {
  812.  
  813.                 if ((!(isalpha(m_board[l][c - 1]))) && (!(isalpha(m_board[l][c + 1]))))
  814.                     m_board[l][c] = '.';
  815.             }
  816.  
  817.             //Resets the (#)tile after the word
  818.             m_board[l][c] = '.';
  819.         }
  820.  
  821.         //Removes word in board horizontally
  822.         if ((direction_letter == 'H') || (direction_letter == 'h')) {
  823.  
  824.             //Resets the (#)tile before the word
  825.             m_board[l][c - 1] = '.';
  826.  
  827.             for (int i = 0; i < word.length(); i++, c++) {
  828.  
  829.                 if ((!(isalpha(m_board[l - 1][c]))) && (!(isalpha(m_board[l + 1][c]))))
  830.                     m_board[l][c] = '.';
  831.             }
  832.  
  833.             //Resets the (#)tile after the word
  834.             m_board[l][c] = '.';
  835.         }
  836.  
  837.         board_words.erase(board_words.find(word)); // removes word from the list of words that are currently in the board
  838.  
  839.         pound_check(m_board, board_words);
  840.  
  841.         show_board(m_board);
  842.     }
  843. }
  844.  
  845. //================================================================
  846.  
  847. void advanced_menu(vector<vector<char>> &m_board, map<string, string> &board_words, map<string, vector<string>> synonims_map) {
  848.  
  849.     char command;
  850.  
  851.     cout << "COMMANDS:" << endl;
  852.     cout << "+ = place word in board" << endl;
  853.     cout << "- = remove word from board" << endl;
  854.     cout << "? = help" << endl;
  855.     cout << "b = back to main menu" << endl << endl;
  856.  
  857.     cout << "Command: ";
  858.     cin >> command;
  859.  
  860.     while (command != 'b')
  861.     {
  862.  
  863.         while (cin.fail()
  864.             || ((command != '+')
  865.                 && (command != '-')
  866.                 && (command != '?')
  867.                 && (command != 'b'))
  868.             )
  869.         {
  870.             cout << endl << "Invalid command input" << endl;
  871.             cout << "PLease insert one of the available commands" << endl << endl;
  872.  
  873.             cout << "COMMANDS:" << endl;
  874.             cout << "+ = place word in board" << endl;
  875.             cout << "- = remove word from board" << endl;
  876.             cout << "? = help" << endl;
  877.             cout << "b = back to main menu" << endl << endl;
  878.  
  879.             cout << "Command: ";
  880.             cin.clear();
  881.             cin.ignore(1000, '\n');
  882.             cin >> command;
  883.         }
  884.  
  885.         switch (command)
  886.         {
  887.         case '+':
  888.             insert_word(m_board, board_words, synonims_map);
  889.             break;
  890.         case '-':
  891.             remove_word(m_board, board_words);
  892.             break;
  893.         }
  894.  
  895.         if (command == '?')
  896.             break;
  897.  
  898.         cout << "COMMANDS:" << endl;
  899.         cout << "+ = place word in board" << endl;
  900.         cout << "- = remove word from board" << endl;
  901.         cout << "? = help" << endl;
  902.         cout << "b = back to main menu" << endl << endl;
  903.  
  904.         cout << "Command: ";
  905.         cin >> command;
  906.     }
  907. }
  908.  
  909. void main_menu(string dictionary_file, vector<vector<char>> &m_board, map<string, string> &board_words, map<string, vector<string>> synonims_map) {
  910.  
  911.     unsigned int option;
  912.  
  913.     cout << "CROSSWORDS PUZZLE CREATOR" << endl;
  914.     cout << "==============================================\n" << endl;
  915.  
  916.     cout << "INSTRUCTIONS:" << endl;
  917.     cout << "CTRL-Z = back" << endl;
  918.  
  919.     cout << "----------------------------------------------\n" << endl;
  920.  
  921.     cout << "OPTIONS:" << endl;
  922.     cout << "1 - Create Puzzle" << endl;
  923.     cout << "2 - Resume Puzzle" << endl;
  924.     cout << "0 - Exit" << endl << endl;
  925.  
  926.     cout << "Option: ";
  927.     cin >> option;
  928.  
  929.     while (option != 0)
  930.     {
  931.  
  932.         while (
  933.             cin.fail()
  934.             || (option < 0)
  935.             || (option > 2)
  936.             )
  937.         {
  938.             cout << "Please insert a number from the options available" << endl;
  939.             cout << "1 - Create Puzzle" << endl;
  940.             cout << "2 - Resume Puzzle" << endl;
  941.             cout << "0 - Exit" << endl << endl;
  942.             cin.clear();
  943.             cin.ignore(1000, '\n');
  944.             cout << "Option: ";
  945.             cin >> option;
  946.         }
  947.  
  948.         switch (option)
  949.         {
  950.         case 1:
  951.         {
  952.             loadDictionary(dictionary_file, synonims_map);
  953.             show_board(m_board);
  954.             if (board(m_board))
  955.             {
  956.                 advanced_menu(m_board, board_words, synonims_map);
  957.                 storeBoard(dictionary_file, m_board, board_words);
  958.             }
  959.         }
  960.         break;
  961.         case 2:
  962.         {
  963.             loadBoard(m_board, board_words);
  964.             loadDictionary(dictionary_file, synonims_map);
  965.             advanced_menu(m_board, board_words, synonims_map);
  966.             storeBoard(dictionary_file, m_board, board_words);
  967.         }
  968.         break;
  969.         case 0:
  970.             cout << "Shuting down..." << endl;
  971.         }
  972.  
  973.         if (option != 0) {
  974.             cout << "OPTIONS:" << endl;
  975.             cout << "1 - Create Puzzle" << endl;
  976.             cout << "2 - Resume Puzzle" << endl;
  977.             cout << "0 - Exit" << endl << endl;
  978.  
  979.             cout << "Option: ";
  980.             cin >> option;
  981.         }
  982.     }
  983. }
  984.  
  985. int main() {
  986.  
  987.     /*
  988.     map<string, string> b_words;
  989.     string word = "Zero", inputFile = "ofile3.txt", line = "Abandon: leave, forsake, desert, renounce, cease, relinquish, discontinue, castoff, resign, retire, quit, forego, forswear, depart from, vacate, surrender, abjure, repudiate";
  990.     vector<vector<char>> v(10);
  991.  
  992.     for (int i = 0; i < v.size(); i++)
  993.     {
  994.         v[i].resize(10);
  995.     }
  996.  
  997.     cout << "1";
  998.  
  999.     for (size_t i = 0; i < v.size(); i++)
  1000.     {
  1001.         for (size_t j = 0; j < v[0].size(); j++)
  1002.         {
  1003.             v[i][j] = '.';
  1004.         }
  1005.     }
  1006.  
  1007.     cout << "2";
  1008.  
  1009.     v[1][2] = 'A';
  1010.     v[1][3] = 'R';
  1011.  
  1012.     cout << "3";
  1013.  
  1014.     b_words.insert(pair<string, string>("AR", "AbH"));
  1015.  
  1016.     cout << "4";
  1017.  
  1018.     storeBoard(inputFile, v, b_words);
  1019.  
  1020.     cout << "5";
  1021.    
  1022.  
  1023.    
  1024.     vector<vector<char>> v;
  1025.     map<string, string> b_words;
  1026.  
  1027.     loadBoard(v, b_words);
  1028.  
  1029.  
  1030.     for (size_t i = 0; i < v.size(); i++)
  1031.     {
  1032.         cout << endl;
  1033.         for (size_t j = 0; j < v[0].size(); j++)
  1034.         {
  1035.             cout << setw(3) << v[i][j];
  1036.         }
  1037.     }
  1038.  
  1039.     for (auto i = b_words.begin(); i != b_words.end(); i++)
  1040.     {
  1041.         cout << i->first << ": " << i->second << endl;
  1042.     }
  1043.  
  1044.     show_board(v);
  1045.  
  1046.     map < string, vector<string>> dic_map;
  1047.     loadDictionary("ofile3.txt", dic_map);
  1048.     */
  1049.  
  1050.  
  1051.  
  1052.    
  1053.     if (!file_exists("Board Number.txt"))
  1054.     {
  1055.         ofstream bNumber_file;
  1056.         bNumber_file.open("Board Number.txt");
  1057.         bNumber_file << 0;
  1058.         bNumber_file.close();
  1059.     }
  1060.  
  1061.     Dictionary dictionary;
  1062.     Board board;
  1063.     string dictionary_file;
  1064.     cout << "Dictionary File ?" << endl;
  1065.     cin >> dictionary_file;
  1066.  
  1067.     while (cin.fail()
  1068.         || !file_exists(dictionary_file)
  1069.         || !cin.eof())
  1070.     {
  1071.         if (cin.fail() && !cin.eof())
  1072.         {
  1073.             cout << "Invalid input for file!" << endl;
  1074.             cin.clear();
  1075.             cin.ignore(1000, '\n');
  1076.         }
  1077.         else if (!file_exists(dictionary_file) && !cin.eof())
  1078.         {
  1079.             cout << "File does not exist" << endl;
  1080.             cin.clear();
  1081.             cin.ignore(1000, '\n');
  1082.         }
  1083.         else break;
  1084.  
  1085.         cout << "Dictionary File ?" << endl;
  1086.         cin >> dictionary_file;
  1087.     }
  1088.  
  1089.     dictionary.loadDictionary(dictionary_file);
  1090.  
  1091.     Game game = Game(dictionary, board);
  1092.     bool start = true;
  1093.  
  1094.     while (true)
  1095.     {
  1096.         if (!cin.eof() && !start)
  1097.         {
  1098.         string finish;
  1099.         cout << "Finish?" << endl;
  1100.         cin >> finish;
  1101.         }
  1102.  
  1103.         start = false;
  1104.  
  1105.         if (cin.eof())
  1106.             break;
  1107.         else game.main_menu(dictionary_file);
  1108.     }
  1109.  
  1110.  
  1111.     system("pause");
  1112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement