Advertisement
Guest User

Board.cpp

a guest
May 20th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 25.17 KB | None | 0 0
  1. #include "Board.h"
  2.  
  3. //================================================================
  4. //===========================PRIVATE==============================
  5. //================================================================
  6.  
  7.  
  8. //AUXILIARY MEMBER FUNCTIONS
  9. //================================================================
  10.  
  11. // Set text color
  12. void Board::setcolor(unsigned int color)
  13. {
  14.     HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
  15.     SetConsoleTextAttribute(hcon, color);
  16. }
  17.  
  18. // Set text color & background
  19. void Board::setcolor(unsigned int color, unsigned int background_color) {
  20.  
  21.     #define BLACK 0
  22.     #define LIGHTGRAY 7
  23.     #define LIGHTRED 12
  24.  
  25.     HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
  26.     if (background_color == BLACK)
  27.         SetConsoleTextAttribute(hCon, color);
  28.     else
  29.         SetConsoleTextAttribute(hCon, color | BACKGROUND_BLUE | BACKGROUND_GREEN |
  30.             BACKGROUND_RED);
  31. }
  32.  
  33. //Checks if a file exists/can be opened
  34. bool Board::file_exists(string filename)
  35. {
  36.     if (ifstream(filename))
  37.     {
  38.         return true;
  39.     }
  40.     return false;
  41. }
  42.  
  43. // Sets all letters in word to CAPS
  44. string Board::all_caps(string word) {
  45.  
  46.     for (size_t i = 0; i < word.size(); i++)
  47.         word[i] = toupper(word[i]);
  48.  
  49.     return word;
  50. }
  51.  
  52. //Checks if line has SPACES
  53. bool Board::pound_line(string line)
  54. {
  55.     return (any_of(line.begin(), line.end(), [](char c) {return c == '#'; }));
  56. }
  57.  
  58. //RETURNS WETHER OT NOT THE BOARD IS FULL
  59. bool Board::full_board() {
  60.  
  61.     for (vector<char> column : m_board)
  62.         for (char tile : column)
  63.         {
  64.             if (tile == '.')
  65.                 return false;
  66.         }
  67.  
  68.     return true;
  69. }
  70.  
  71.  
  72.  
  73.  
  74. //================================================================
  75. //===========================PUBLIC===============================
  76. //================================================================
  77.  
  78. //PUZZLE DATA MEMBER FUNCTIONS
  79. //================================================================
  80.  
  81. //Stores contents of puzzle in a file
  82. void Board::storeBoard(string dictionary_file, bool resuming, string board_file_name) {
  83.  
  84.     ofstream board_file;
  85.  
  86.     if (!resuming) {
  87.  
  88.         unsigned int b;
  89.         ostringstream board_number;
  90.         ifstream IN_boardNumberFile("Board Number.txt");
  91.  
  92.         IN_boardNumberFile >> b;
  93.         b++;
  94.         IN_boardNumberFile.close();
  95.  
  96.         ofstream OUT_boardNumberFile("Board Number.txt");
  97.  
  98.         OUT_boardNumberFile << b;
  99.         OUT_boardNumberFile.close();
  100.  
  101.         board_number.width(3);
  102.         char blanks = board_number.fill('0');
  103.         board_number << b;
  104.         string file_name = 'b' + board_number.str() + ".txt";
  105.  
  106.         cout << file_name << endl;
  107.  
  108.         board_file.open(file_name);
  109.     }
  110.     else board_file.open(board_file_name);
  111.  
  112.     //STORING DATA INTO FILE
  113.  
  114.     board_file << "Dictionary File:" << endl;
  115.     board_file << dictionary_file << endl;
  116.     board_file << "Board Contents:" << endl;
  117.  
  118.     int line_size = m_board[0].size();
  119.     int column_size = m_board.size();
  120.  
  121.     for (int l = 0; l < line_size; l++) // storing the contents of the board in the board_file
  122.     {
  123.         for (int c = 0; c < column_size; c++)
  124.         {
  125.             board_file << setw(3) << m_board[l][c];
  126.         }
  127.         board_file << endl;
  128.     }
  129.  
  130.     board_file << "Words in board:" << endl;
  131.  
  132.     // storing all the words currently in the board into the board file along witht their positions in the board
  133.     for (auto synonims_pair = board_words.begin(); synonims_pair != board_words.end(); synonims_pair++) //synonims pair refers to the pair
  134.     {
  135.         board_file << synonims_pair->second << ": " << synonims_pair->first << endl;
  136.     }
  137.  
  138.     board_file << "End of file" << endl;
  139.  
  140.     board_file.close();
  141.  
  142.     m_board.resize(0);
  143.     board_words.erase(board_words.begin(), board_words.end());
  144. }
  145.  
  146. //Downloads the contents of chosen puzzle
  147. string Board::loadBoard() {
  148.  
  149.     string file_name; //name of file containing the contents of the chosen board
  150.     cout << "Board File?" << endl;
  151.     cin >> file_name;
  152.  
  153.     while (cin.fail()
  154.         || (!(file_exists(file_name)))
  155.         )
  156.     {
  157.         if (cin.fail())
  158.             cout << "Invalid type of input!" << endl;
  159.         else cout << "Unable to open given file" << endl;
  160.         cin.clear();
  161.         cin.ignore(1000, '\n');
  162.         cout << "Board File?" << endl;
  163.         cin >> file_name;
  164.     }
  165.  
  166.     string line;
  167.     string dictionary_file;
  168.     ifstream board_file(file_name);
  169.     unsigned int l = 0;
  170.     bool file_start = false, board_start = false, board_end = false, file_end = false;
  171.     while (getline(board_file, line))
  172.     {
  173.  
  174.         if ((file_start) && (!(board_start)) && (line != "Board Contents:"))
  175.             dictionary_file = line;
  176.         else if ((board_start) && (!(board_end)) && (line != "Words in board:"))
  177.         {
  178.             m_board.resize(line.length() / 3);
  179.  
  180.             while(pound_line(line))
  181.             {
  182.                 m_board[l].push_back(line[2]);
  183.                 line.erase(0, 3);
  184.             }
  185.             l++;
  186.         }
  187.         else if ((board_end) && (!(file_end)) && (line != "End of file"))
  188.         {
  189.             string position = line.substr(0, 3);
  190.             line.erase(0, 5);
  191.             string word = line;
  192.             board_words.insert(pair<string, string>(word, position));
  193.         }
  194.  
  195.         if (line == "Dictionary File:")
  196.             file_start = true;
  197.         else if (line == "Board Contents:")
  198.             board_start = true;
  199.         else if (line == "Words in board:")
  200.             board_end = true;
  201.         else if (line == "End of file")
  202.             file_end = true;
  203.     }
  204.  
  205.     return file_name;
  206. }
  207.  
  208.  
  209.  
  210. //MAIN MENU MEMBER FUNCTIONS
  211. //================================================================
  212.  
  213. //Creates a new board
  214. bool Board::board() {
  215.  
  216.     unsigned int l, c;
  217.  
  218.     cout << "Board size (lines & columns) ? ";
  219.     cin >> l >> c;
  220.  
  221.     //Testar se input é valido e nr maior que 0 e menor que 27
  222.     while (cin.fail() || l < 1 || c < 1 || l > 26 || c > 26) {
  223.         cout << "Insert 2 numbers larger than 0 and smaller than 27!" << endl;
  224.         cin.clear();
  225.         cin.ignore(1000, '\n');
  226.         cout << "Board size (lines & columns)? ";
  227.         cin >> l >> c;
  228.     }
  229.  
  230.     if (l != 0) {
  231.  
  232.         vector<char> column;
  233.  
  234.         for (unsigned int i = 1; i < c + 3; i++) {
  235.  
  236.             column.push_back('.');
  237.         }
  238.  
  239.         for (unsigned int i = 1; i < l + 3; i++) {
  240.  
  241.             m_board.push_back(column);
  242.         }
  243.  
  244.         m_board[0][0] = '#';
  245.  
  246.         //Fills the first line with '#'
  247.         for (unsigned int c = 1; c < m_board.size(); c++) {
  248.  
  249.             m_board[0][c] = '#';
  250.         }
  251.  
  252.         //Fills the last line with '#'
  253.         for (unsigned int c = 1; c < m_board.size(); c++) {
  254.  
  255.             m_board[m_board[0].size() - 1][c] = '#';
  256.         }
  257.  
  258.         //Fills the first column with '#'
  259.         for (unsigned int l = 1; l < m_board[0].size(); l++) {
  260.  
  261.             m_board[l][0] = '#';
  262.         }
  263.  
  264.         //Fills the last column with '#'
  265.         for (unsigned int l = 1; l < m_board[0].size(); l++) {
  266.  
  267.             m_board[l][m_board.size() - 1] = '#';
  268.         }
  269.  
  270.         cout << endl;
  271.  
  272.         return true;
  273.  
  274.     }
  275.  
  276.     return false;
  277.  
  278. }
  279.  
  280. // Shows the board according to the data in m_board
  281. void Board::show_board() {
  282.  
  283.     //Letras minusculas e maiusculas que vao ser usadas para preencher
  284.     int uppers = 65, lowers = 97;
  285.  
  286.     //Letras minusculas e maiusculas que vao ser usadas para preencher
  287.  
  288.     for (size_t l = 0; l < m_board[0].size() - 1; l++)
  289.     {
  290.         for (size_t c = 0; c < m_board.size() - 1; c++)
  291.         {
  292.             //First Tile
  293.             if ((l == 0) && (c == 0))
  294.             {
  295.                 setcolor(12);
  296.                 cout << left << setw(2) << m_board[0][0] << " ";
  297.             }
  298.             //First Line
  299.             else if (l == 0)
  300.             {
  301.                 setcolor(12);
  302.                 cout << left << setw(2) << (char)lowers++;
  303.             }
  304.             //First Column
  305.             else if (c == 0)
  306.             {
  307.                 setcolor(12);
  308.                 cout << left << setw(2) << (char)uppers++;
  309.             }
  310.             //Board Contents
  311.             else
  312.             {
  313.                 if (m_board[l][c] == '#')
  314.                 {
  315.                     setcolor(0, 7);
  316.                     cout << " ";
  317.                     setcolor(7, 0);
  318.                     cout << '#';
  319.                 }
  320.                 else
  321.                 {
  322.                     setcolor(0, 7);
  323.                     cout << right << setw(2) << m_board[l][c];
  324.                 }
  325.             }
  326.         }
  327.         //Back to original color
  328.         setcolor(7, 0);
  329.         cout << " " << endl;
  330.     }
  331.  
  332.     cout << endl;
  333. }
  334.  
  335.  
  336.  
  337. //INSERTION AUXILIARY MEMBER FUNCTIONS
  338. //================================================================
  339.  
  340. //Finds vertical exceptions to horizontal "siding"
  341. bool Board::vertical_exception(Dictionary dictionary, int l, int c) {
  342.  
  343.     int column_size = m_board.size() - 2;
  344.     string possible_word, possible_word_head, possible_word_tail;
  345.  
  346.     if (l == 1)
  347.     {
  348.         for (int l_down = l; (isalpha(m_board[l_down][c])); l_down++)
  349.         {
  350.             possible_word.push_back(m_board[l_down][c]);
  351.         }
  352.     }
  353.     else if (l == column_size)
  354.     {
  355.         for (int l_up = l; (isalpha(m_board[l_up][c])); l_up--)
  356.         {
  357.             possible_word.push_back(m_board[l_up][c]);
  358.         }
  359.         reverse(possible_word.begin(), possible_word.end());
  360.     }
  361.     else
  362.     {
  363.         if (!(isalpha(m_board[l - 1][c])))
  364.         {
  365.             for (int l_down = l + 1; (isalpha(m_board[l_down][c])); l_down++)
  366.             {
  367.                 possible_word_tail.push_back(m_board[l_down][c]);
  368.             }
  369.  
  370.             for (int l_up = l; (isalpha(m_board[l_up][c])); l_up--)
  371.             {
  372.                 possible_word_head.push_back(m_board[l_up][c]);
  373.             }
  374.             reverse(possible_word_head.begin(), possible_word_head.end());
  375.             possible_word = possible_word_head + possible_word_tail;
  376.         }
  377.         else
  378.         {
  379.             for (int l_down = l; (isalpha(m_board[l_down][c])); l_down++)
  380.             {
  381.                 possible_word_tail.push_back(m_board[l_down][c]);
  382.             }
  383.  
  384.             for (int l_up = l - 1; (isalpha(m_board[l_up][c])); l_up--)
  385.             {
  386.                 possible_word_head.push_back(m_board[l_up][c]);
  387.             }
  388.             reverse(possible_word_head.begin(), possible_word_head.end());
  389.             possible_word = possible_word_head + possible_word_tail;
  390.         }
  391.     }
  392.  
  393.     return (dictionary.word_exists(possible_word));
  394. }
  395.  
  396. //Finds horizontal exceptions to vertical "siding"
  397. bool Board::horizontal_exception(Dictionary dictionary, int l, int c) {
  398.  
  399.     int line_size = m_board[0].size() - 2;
  400.     string possible_word, possible_word_head, possible_word_tail;
  401.  
  402.     if (c == 1)
  403.     {
  404.         for (int c_right = c; (isalpha(m_board[l][c_right])); c_right++)
  405.         {
  406.             possible_word.push_back(m_board[l][c_right]);
  407.         }
  408.     }
  409.     else if (c == line_size)
  410.     {
  411.         for (int c_left = c; (isalpha(m_board[l][c_left])); c_left--)
  412.         {
  413.             possible_word.push_back(m_board[l][c_left]);
  414.         }
  415.         reverse(possible_word.begin(), possible_word.end());
  416.     }
  417.     else
  418.     {
  419.         if (!(isalpha(m_board[l][c - 1])))
  420.         {
  421.             for (int c_right = c + 1; (isalpha(m_board[l][c_right])); c_right++)
  422.             {
  423.                 possible_word_tail.push_back(m_board[l][c_right]);
  424.             }
  425.  
  426.             for (int c_left = c; (isalpha(m_board[l][c_left])); c_left--)
  427.             {
  428.                 possible_word_head.push_back(m_board[l][c_left]);
  429.             }
  430.             reverse(possible_word_head.begin(), possible_word_head.end());
  431.             possible_word = possible_word_head + possible_word_tail;
  432.         }
  433.         else
  434.         {
  435.             for (int c_right = c; (isalpha(m_board[l][c_right])); c_right++)
  436.             {
  437.                 possible_word_tail.push_back(m_board[l][c_right]);
  438.             }
  439.  
  440.             for (int c_left = c - 1; (isalpha(m_board[l][c_left])); c_left--)
  441.             {
  442.                 possible_word_head.push_back(m_board[l][c_left]);
  443.             }
  444.             reverse(possible_word_head.begin(), possible_word_head.end());
  445.             possible_word = possible_word_head + possible_word_tail;
  446.         }
  447.     }
  448.  
  449.     return (dictionary.word_exists(possible_word));
  450. }
  451.  
  452. //Returns wether or not word can be placed in position
  453. bool Board::word_fits(Dictionary dictionary, string position, string word) {
  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[0].size() - 2;
  460.     int column_size = m_board.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(dictionary, l, c - 1))))
  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(dictionary, l, c + 1))))
  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(dictionary, l + 1, c))))
  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(dictionary, l - 1, c))))
  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. //Helps the user by showing him/her a list of words that can be added taking into account the current contents of the board
  575. void Board::help(Dictionary dictionary, string position) {
  576.  
  577.     cout << "AVAILABLE COMMANDS:" << endl;
  578.     cout << "+ = place word in board" << endl;
  579.     cout << "- = remove word from board" << endl;
  580.     cout << "CTRL-Z = back to main menu" << endl;
  581.     cout << endl;
  582.  
  583.     //Max of 3 words suggested
  584.     unsigned int suggestion_counter = 0;
  585.  
  586.     cout << "SOME POSSIBILITIES FOR " << position << ":" << endl;
  587.     cout << endl;
  588.  
  589.     for (auto it = dictionary.synonims_map.begin(); it != dictionary.synonims_map.end(); it++) {
  590.  
  591.         if ((board_words.find(it->first) == board_words.end()) && word_fits(dictionary, position, it->first)) {
  592.  
  593.             cout << "(" << suggestion_counter << ") " << it->first << endl;
  594.             suggestion_counter++;
  595.         }
  596.         if (suggestion_counter == 3)
  597.             break;
  598.     }
  599.     cout << endl;
  600. }
  601.  
  602.  
  603.  
  604. //REMOVAL AUXILIARY MEMBER FUNCTIONS
  605. //================================================================
  606.  
  607. void Board::pound_check() {
  608.  
  609.     m_board[0][0] = '#';
  610.  
  611.     //Fills the first line with '#'
  612.     for (int c = 1; c < m_board[0].size(); c++) {
  613.  
  614.         m_board[0][c] = '#';
  615.     }
  616.  
  617.     //Fills the last line with '#'
  618.     for (int c = 1; c < m_board[0].size(); c++) {
  619.  
  620.         m_board[m_board.size() - 1][c] = '#';
  621.     }
  622.  
  623.     //Fills the first column with '#'
  624.     for (int l = 1; l < m_board.size(); l++) {
  625.  
  626.         m_board[l][0] = '#';
  627.     }
  628.  
  629.     //Fills the last column with '#'
  630.     for (int l = 1; l < m_board.size(); l++) {
  631.  
  632.         m_board[l][m_board[0].size() - 1] = '#';
  633.     }
  634.  
  635.     for (auto it = board_words.begin(); it != board_words.end(); it++) {
  636.  
  637.         string word = it->first;
  638.         string position = it->second;
  639.  
  640.         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
  641.         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
  642.         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
  643.  
  644.         size_t initial_line = (int)line_letter - 64;           //Ex: Se line_letter = 'a' entao initial_line = 1
  645.         size_t initial_column = (int)column_letter - 96;       //Ex: Se column_letter = 'A' entao initial_column = 1
  646.         size_t l = initial_line;
  647.         size_t c = initial_column;
  648.  
  649.         if ((direction_letter == 'V') || (direction_letter == 'v')) {
  650.  
  651.             m_board[l - 1][c] = '#';
  652.             m_board[l + word.length()][c] = '#';
  653.         }
  654.         if ((direction_letter == 'H') || (direction_letter == 'h')) {
  655.  
  656.             m_board[l][c - 1] = '#';
  657.             m_board[l][c + word.length()] = '#';
  658.         }
  659.     }
  660. }
  661.  
  662.  
  663.  
  664. //ADVANCED MENU MEMBER FUNCTIONS
  665. //================================================================
  666.  
  667. //Inserts a word in board
  668. void Board::insert_word(Dictionary dictionary)
  669. {
  670.  
  671.     //Posititon
  672.     string position;
  673.     cout << "Position (LCD / CTRL-Z = cancel): ";
  674.     cin >> position;
  675.  
  676.     int line_size = m_board[0].size() - 2;
  677.     int column_size = m_board.size() - 2;
  678.  
  679.     //Testing if input position is valid
  680.     while (true)
  681.     {
  682.         if ((position.length() == 3
  683.             && (int)position[0] >= 65 && (int)position[0] <= 64 + line_size
  684.             && (int)position[1] >= 97 && (int)position[1] <= 96 + column_size
  685.             && ((position[2] == 'V' || position[2] == 'H')
  686.                 || (position[2] == 'v' || position[2] == 'h')))
  687.  
  688.             || cin.eof())
  689.  
  690.             break;
  691.  
  692.         else
  693.         {
  694.             cout << "Introduza duas letras (Maiuscula seguida de Minuscula) que pertencam ao quadro seguida da direcao desejada (V ou H)\n" << endl;
  695.             cin.clear();
  696.             cin.ignore(1000, '\n');
  697.             cout << "Position (LCD / CTRL-Z = cancel) ? ";
  698.             cin >> position;
  699.         }
  700.     }
  701.  
  702.     if (position.length() > 0) //If the user didn't change his mind
  703.     {
  704.         string word;
  705.         cout << "Word to insert (CTRL-Z = cancel, ? = help): ";
  706.         cin >> word;
  707.  
  708.         word = all_caps(word);
  709.  
  710.         //Testing if input word is valid
  711.  
  712.         while (cin.fail()
  713.             || !(dictionary.word_exists(word))
  714.             || !(word_fits(dictionary, position, word))
  715.             || board_words.find(word) != board_words.end()
  716.             || word != "?"
  717.             || !cin.eof())
  718.         {
  719.             if (cin.fail() && !cin.eof())
  720.             {
  721.                 cout << "Invalid Input for Word!" << endl;
  722.                 cin.clear();
  723.                 cin.ignore(1000, '\n');
  724.                 cout << "Word to insert (CTRL-Z = cancel, ? = help): ";
  725.             }
  726.             else if (word == "?")
  727.             {
  728.                 help(dictionary, position);
  729.                 cin.clear();
  730.                 cin.ignore(1000, '\n');
  731.                 cout << "Word to insert (CTRL-Z = cancel, ? = help): ";
  732.             }
  733.             else if (!(dictionary.word_exists(word)) && !cin.eof())
  734.             {
  735.                 cout << "Given word does not exist according to our records" << endl;
  736.                 cin.clear();
  737.                 cin.ignore(1000, '\n');
  738.                 cout << "Please insert a different word: ";
  739.             }
  740.             else if (!(word_fits(dictionary, position, word)) && !cin.eof())
  741.             {
  742.                 cout << "Given word cannot be placed in position" << endl;
  743.                 cin.clear();
  744.                 cin.ignore(1000, '\n');
  745.                 cout << "Please insert a different word or alter the board so it fits: ";
  746.             }
  747.             else if (board_words.find(word) != board_words.end()) {
  748.  
  749.                 cout << "Given word is already on board" << endl;
  750.                 cin.clear();
  751.                 cin.ignore(1000, '\n');
  752.                 cout << "Please insert a different word or alter the board so it fits: ";
  753.             }
  754.             else break;
  755.  
  756.             cin >> word;
  757.             word = all_caps(word);
  758.         }
  759.  
  760.         if (!cin.eof()) {
  761.  
  762.             //INSERTING WORD
  763.  
  764.             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
  765.             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
  766.             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
  767.  
  768.             word = all_caps(word);
  769.             size_t initial_line = (int)line_letter - 64;           //Ex: Se line_letter = 'a' entao initial_line = 1
  770.             size_t initial_column = (int)column_letter - 96;       //Ex: Se column_letter = 'A' entao initial_column = 1
  771.             size_t l = initial_line;
  772.             size_t c = initial_column;
  773.  
  774.             // Inserts word in board vertically
  775.             if ((direction_letter == 'V') || (direction_letter == 'v')) {
  776.  
  777.                 m_board[l - 1][c] = '#'; //places a # before the word
  778.  
  779.                 for (int i = 0; i < word.length(); i++, l++) {
  780.  
  781.                     m_board[l][c] = word[i];
  782.                 }
  783.  
  784.                 m_board[l][c] = '#'; //places a # after the word
  785.             }
  786.  
  787.             //Inserts word in board horizontally
  788.             if ((direction_letter == 'H') || (direction_letter == 'h')) {
  789.  
  790.  
  791.                 m_board[l][c - 1] = '#'; //places a # before the word
  792.  
  793.                 for (int i = 0; i < word.length(); i++, c++) {
  794.  
  795.                     m_board[l][c] = word[i];
  796.                 }
  797.  
  798.                 m_board[l][c] = '#'; //places a # after the word
  799.             }
  800.  
  801.             if (word.length() > 0) //If the user didn't change his mind...
  802.             {
  803.                 board_words.insert(pair<string, string>(word, position)); //added word to the list of words in the board
  804.                                                                           //sera que devia usar emplace em vez de insert?, esta bem feito este pair temporario?
  805.  
  806.                 show_board();
  807.             }
  808.         }
  809.     }
  810. }
  811.  
  812. //Removes a word from board
  813. void Board::remove_word() {
  814.  
  815.     string word;
  816.     cout << "Word to remove (CTRL-Z = cancel) ?" << endl;
  817.     cin >> word;
  818.  
  819.     word = all_caps(word);
  820.  
  821.     //Testing if input word is on the board
  822.     while (board_words.find(word) == board_words.end()
  823.         || !cin.eof()
  824.         || cin.fail())
  825.     {
  826.         if (cin.eof())
  827.         {
  828.             break;
  829.         }
  830.         else if (cin.fail())
  831.         {
  832.             cout << "Invalid Input for Word!" << endl;
  833.             cin.clear();
  834.             cin.ignore(1000, '\n');
  835.             cout << "Word to remove (CTRL-Z = cancel) ?" << endl;
  836.         }
  837.         else if (board_words.find(word) == board_words.end())
  838.         {
  839.             cout << "There is no such word currently in the board" << endl;
  840.             cin.clear();
  841.             cin.ignore(1000, '\n');
  842.             cout << "Word to remove (CTRL-Z = cancel) ?" << endl;
  843.         }
  844.         else break;
  845.  
  846.         cin >> word;
  847.         word = all_caps(word);
  848.     }
  849.  
  850.     if (word.length() > 0) //If the user didn't change his mind...
  851.     {
  852.         map<string, string>::iterator it = board_words.find(word);
  853.         string position = it->second; //tells you the position of the word to be removed
  854.  
  855.         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
  856.         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
  857.         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
  858.  
  859.         size_t initial_line = (int)line_letter - 64;           //Ex: If line_letter = 'a' then initial_line = 1
  860.         size_t initial_column = (int)column_letter - 96;       //Ex: If column_letter = 'A' then initial_column = 1
  861.         size_t l = initial_line;
  862.         size_t c = initial_column;
  863.  
  864.         //REMOVING WORD
  865.  
  866.         //There will be no need to later "restore" words that may have been intercepted with the word removed
  867.         //as we are checking before removing a letter wether or not it is attached to another word
  868.  
  869.         //Removes word in board vertically
  870.         if ((direction_letter == 'V') || (direction_letter == 'v')) {
  871.  
  872.             //Resets the (#)tile before the word
  873.             m_board[l - 1][c] = '.';
  874.  
  875.             for (int i = 0; i < word.length(); i++, l++) {
  876.  
  877.                 if ((!(isalpha(m_board[l][c - 1]))) && (!(isalpha(m_board[l][c + 1]))))
  878.                     m_board[l][c] = '.';
  879.             }
  880.  
  881.             //Resets the (#)tile after the word
  882.             m_board[l][c] = '.';
  883.         }
  884.  
  885.         //Removes word in board horizontally
  886.         if ((direction_letter == 'H') || (direction_letter == 'h')) {
  887.  
  888.             //Resets the (#)tile before the word
  889.             m_board[l][c - 1] = '.';
  890.  
  891.             for (int i = 0; i < word.length(); i++, c++) {
  892.  
  893.                 if ((!(isalpha(m_board[l - 1][c]))) && (!(isalpha(m_board[l + 1][c]))))
  894.                     m_board[l][c] = '.';
  895.             }
  896.  
  897.             //Resets the (#)tile after the word
  898.             m_board[l][c] = '.';
  899.         }
  900.  
  901.         board_words.erase(board_words.find(word)); // removes word from the list of words that are currently in the board
  902.  
  903.         pound_check();
  904.  
  905.         show_board();
  906.     }
  907. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement