Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Game.h"
- // Set text color
- void setcolor(unsigned int color)
- {
- HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleTextAttribute(hcon, color);
- }
- // Set text color & background
- void setcolor(unsigned int color, unsigned int background_color) {
- #define BLACK 0
- #define LIGHTGRAY 7
- #define LIGHTRED 12
- HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
- if (background_color == BLACK)
- SetConsoleTextAttribute(hCon, color);
- else
- SetConsoleTextAttribute(hCon, color | BACKGROUND_BLUE | BACKGROUND_GREEN |
- BACKGROUND_RED);
- }
- bool file_exists(string filename)
- {
- ifstream file(filename);
- if (file.is_open())
- {
- file.close();
- return true;
- }
- return false;
- }
- bool comma_line(string line)
- {
- return (any_of(line.begin(), line.end(), [](char c) {return c == ','; }));
- }
- bool pound_line(string line)
- {
- return (any_of(line.begin(), line.end(), [](char c) {return c == '#'; }));
- }
- string all_caps(string word) {
- for (size_t i = 0; i < word.size(); i++)
- word[i] = toupper(word[i]);
- return word;
- }
- pair<string, vector<string>> separate_line(string line) {
- auto colon = line.find(': ');
- string headline = line.substr(0, colon - 1);
- headline = all_caps(headline);
- line.erase(0, colon + 1);
- vector<string> synonims_vector;
- while (comma_line(line))
- {
- size_t comma = line.find(", ");
- string synonim = line.substr(0, comma);
- synonim = all_caps(synonim);
- synonims_vector.push_back(synonim);
- line.erase(0, comma + 2);
- }
- synonims_vector.push_back(line);
- return pair<string, vector<string>> (headline, synonims_vector);
- }
- void loadDictionary(string inputFile, map<string, vector<string>>& map) {
- string line;
- ifstream inFile(inputFile);
- if (inFile.is_open())
- {
- while (getline(inFile, line))
- map.insert(separate_line(line));
- inFile.close();
- }
- else cout << "Unable to open file" << endl;
- }
- void storeBoard(string dictionary_file, vector<vector<char>> m_board, map<string,string> board_words) {
- unsigned int b;
- ostringstream board_number;
- fstream board_number_file("Board Number.txt");
- board_number_file >> b;
- b++;
- board_number_file << b;
- board_number.width(3);
- char blanks = board_number.fill('0');
- board_number << b;
- string file_name = 'b' + board_number.str() + ".txt"; // este e o nome do ficheiro, e epreciso meter a stream em string, daí .srt()
- ofstream board_file;
- board_file.open(file_name);
- //STORING DATA INTO FILE
- board_file << "Dictionary File:" << endl;
- board_file << dictionary_file << endl;
- board_file << "Board Contents:" << endl;
- int line_size = m_board.size();
- int column_size = m_board[0].size();
- for (int l = 0; l < line_size; l++) // storing the contents of the board in the board_file
- {
- for (int c = 0; c < column_size; c++)
- {
- board_file << setw(3) << m_board[l][c];
- }
- board_file << endl;
- }
- board_file << "Words in board:" << endl;
- // storing all the words currently in the board into the board file along witht their positions in the board
- for (auto synonims_pair = board_words.begin(); synonims_pair != board_words.end(); synonims_pair++) //synonims pair refers to the pair
- {
- board_file << synonims_pair->second << ": " << synonims_pair->first << endl;
- }
- board_file << "End of file" << endl;
- board_file.close();
- }
- //CHECKS IF LINE HAS SPACES
- bool space_line(string line)
- {
- return (any_of(line.begin(), line.end(), [](char c) {return c == ' '; }));
- }
- void loadBoard(vector<vector<char>> &m_board, map<string, string> &board_words) {
- string file_name; //name of file containing the contents of the chosen board
- cout << "Board File?" << endl;
- cin >> file_name;
- while (cin.fail()
- || (!(file_exists(file_name)))
- || cin.eof())
- {
- if (cin.fail())
- cout << "Invalid type of input!" << endl;
- else cout << "Unable to open given file" << endl;
- cin.clear();
- cin.ignore(1000, '\n');
- cout << "Board File?" << endl;
- cin >> file_name;
- }
- if (!cin.eof()) {
- string line;
- string dictionary_file;
- ifstream board_file(file_name);
- int l = 0;
- bool file_start = false, board_start = false, board_end = false, file_end = false;
- while (getline(board_file, line))
- {
- if ((file_start) && (!(board_start)) && (line != "Board Contents:"))
- dictionary_file = line;
- else if ((board_start) && (!(board_end)) && (line != "Words in board:"))
- {
- m_board.resize(line.length() / 3);
- while (pound_line(line))
- {
- m_board[l].push_back(line[2]);
- line.erase(0, 3);
- }
- l++;
- }
- else if ((board_end) && (!(file_end)) && (line != "End of file"))
- {
- string position = line.substr(0, 3);
- line.erase(0, 5);
- string word = line;
- board_words.insert(pair<string, string>(word, position));
- }
- if (line == "Dictionary File:")
- file_start = true;
- else if (line == "Board Contents:")
- board_start = true;
- else if (line == "Words in board:")
- board_end = true;
- else if (line == "End of file")
- file_end = true;
- }
- cout << dictionary_file << endl;
- }
- }
- bool word_exists(string word, map<string, vector<string>> synonims_map) {
- if (synonims_map.find(word) == synonims_map.end())
- return false;
- return true;
- }
- //================================================================
- bool board(vector<vector<char>> &m_board) {
- unsigned int l, c;
- cout << "Board size (lines & columns) ? ";
- cin >> l >> c;
- //Testar se input é valido e nr maior que 0 e menor que 27
- while (cin.fail() || l < 1 || c < 1 || l > 26 || c > 26) {
- cout << "Insert 2 numbers larger than 0 and smaller than 27!" << endl;
- cin.clear();
- cin.ignore(1000, '\n');
- cout << "Board size (lines & columns)? ";
- cin >> l >> c;
- }
- if (l != 0) {
- vector<char> column;
- for (unsigned int i = 1; i < c + 3; i++) {
- column.push_back('.');
- }
- for (unsigned int i = 1; i < l + 3; i++) {
- m_board.push_back(column);
- }
- m_board[0][0] = '#';
- //Fills the first line with '#'
- for (unsigned int c = 1; c < m_board.size(); c++) {
- m_board[0][c] = '#';
- }
- //Fills the last line with '#'
- for (unsigned int c = 1; c < m_board.size(); c++) {
- m_board[m_board[0].size() - 1][c] = '#';
- }
- //Fills the first column with '#'
- for (unsigned int l = 1; l < m_board[0].size(); l++) {
- m_board[l][0] = '#';
- }
- //Fills the last column with '#'
- for (unsigned int l = 1; l < m_board[0].size(); l++) {
- m_board[l][m_board.size() - 1] = '#';
- }
- cout << endl;
- return true;
- }
- return false;
- }
- void show_board(vector<vector<char>> m_board) {
- //Letras minusculas e maiusculas que vao ser usadas para preencher
- int uppers = 65, lowers = 97;
- //Letras minusculas e maiusculas que vao ser usadas para preencher
- for (size_t l = 0; l < m_board.size() - 1; l++)
- {
- for (size_t c = 0; c < m_board[0].size() - 1; c++)
- {
- //First Tile
- if ((l == 0) && (c == 0))
- {
- setcolor(12);
- cout << left << setw(2) << m_board[0][0] << " ";
- }
- //First Line
- else if (l == 0)
- {
- setcolor(12);
- cout << left << setw(2) << (char)lowers++;
- }
- //First Column
- else if (c == 0)
- {
- setcolor(12);
- cout << left << setw(2) << (char)uppers++;
- }
- //Board Contents
- else
- {
- if (m_board[l][c] == '#')
- {
- setcolor(0, 7);
- cout << " ";
- setcolor(7, 0);
- cout << '#';
- }
- else
- {
- setcolor(0, 7);
- cout << right << setw(2) << m_board[l][c];
- }
- }
- }
- //Back to original color
- cout << " ";
- setcolor(7, 0);
- cout << " " << endl;
- }
- cout << endl;
- }
- bool horizontal_exception(int l, int c, vector<vector<char>> m_board, map<string, vector<string>> synonims_map) {
- int line_size = m_board.size() - 2;
- string possible_word, possible_word_head, possible_word_tail;
- if (c == 1)
- {
- for (int c_right = c; (isalpha(m_board[l][c_right])); c_right++)
- {
- possible_word.push_back(m_board[l][c_right]);
- }
- }
- else if (c == line_size)
- {
- for (int c_left = c; (isalpha(m_board[l][c_left])); c_left--)
- {
- possible_word.push_back(m_board[l][c_left]);
- }
- reverse(possible_word.begin(), possible_word.end());
- }
- else
- {
- if (!(isalpha(m_board[l][c - 1])))
- {
- for (int c_right = c + 1; (isalpha(m_board[l][c_right])); c_right++)
- {
- possible_word_tail.push_back(m_board[l][c_right]);
- }
- for (int c_left = c; (isalpha(m_board[l][c_left])); c_left--)
- {
- possible_word_head.push_back(m_board[l][c_left]);
- }
- reverse(possible_word_head.begin(), possible_word_head.end());
- possible_word = possible_word_head + possible_word_tail;
- }
- else
- {
- for (int c_right = c; (isalpha(m_board[l][c_right])); c_right++)
- {
- possible_word_tail.push_back(m_board[l][c_right]);
- }
- for (int c_left = c - 1; (isalpha(m_board[l][c_left])); c_left--)
- {
- possible_word_head.push_back(m_board[l][c_left]);
- }
- reverse(possible_word_head.begin(), possible_word_head.end());
- possible_word = possible_word_head + possible_word_tail;
- }
- }
- return (word_exists(possible_word, synonims_map));
- }
- bool vertical_exception(int l, int c, vector<vector<char>> m_board, map<string, vector<string>> synonims_map) {
- int column_size = m_board[0].size() - 2;
- string possible_word, possible_word_head, possible_word_tail;
- if (l == 1)
- {
- for (int l_down = l; (isalpha(m_board[l_down][c])); l_down++)
- {
- possible_word.push_back(m_board[l_down][c]);
- }
- }
- else if (l == column_size)
- {
- for (int l_up = l; (isalpha(m_board[l_up][c])); l_up--)
- {
- possible_word.push_back(m_board[l_up][c]);
- }
- reverse(possible_word.begin(), possible_word.end());
- }
- else
- {
- if (!(isalpha(m_board[l - 1][c])))
- {
- for (int l_down = l + 1; (isalpha(m_board[l_down][c])); l_down++)
- {
- possible_word_tail.push_back(m_board[l_down][c]);
- }
- for (int l_up = l; (isalpha(m_board[l_up][c])); l_up--)
- {
- possible_word_head.push_back(m_board[l_up][c]);
- }
- reverse(possible_word_head.begin(), possible_word_head.end());
- possible_word = possible_word_head + possible_word_tail;
- }
- else
- {
- for (int l_down = l; (isalpha(m_board[l_down][c])); l_down++)
- {
- possible_word_tail.push_back(m_board[l_down][c]);
- }
- for (int l_up = l - 1; (isalpha(m_board[l_up][c])); l_up--)
- {
- possible_word_head.push_back(m_board[l_up][c]);
- }
- reverse(possible_word_head.begin(), possible_word_head.end());
- possible_word = possible_word_head + possible_word_tail;
- }
- }
- return (word_exists(possible_word, synonims_map));
- }
- bool word_fits(string position, string word, vector<vector<char>> m_board, map<string, string> board_words, map<string, vector<string>> synonims_map) {
- char line_letter = position[0];
- char column_letter = position[1];
- char direction_letter = position[2];
- int line_size = m_board.size() - 2;
- int column_size = m_board[0].size() - 2;
- size_t initial_line = (int)line_letter - 64;
- size_t initial_column = (int)column_letter - 96;
- size_t l = initial_line;
- size_t c = initial_column;
- //VERTICALLY
- if ((direction_letter == 'V') || (direction_letter == 'v')) {
- //Checking if word limmits don't go beyond the board's lower limits
- if ((initial_line + word.length()) > line_size + 1)
- {
- //cout << "Word would go beyond the board limits" << endl;
- return false;
- }
- if (isalpha(m_board[l - 1][c])) //checking that there isnt anything before the word where the # would be
- {
- //cout << "The tile where the '#' before the word would be isn't empty" << endl;
- return false;
- }
- //Checking if word wouldn't overlap with existing letters and if there wouldn't any letters around it
- for (int i = 0; i < word.length(); l++, i++) {
- //if current board tiles where word letters would be are different from those letters...
- if ((m_board[l][c] != '.') && (m_board[l][c] != word[i]))
- {
- //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;
- return false;
- }
- //Checking if there would be other vertical words siding with our word:
- //Note: there are a few exceptions where this is alright
- //Checking left
- 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]))))
- && (!(horizontal_exception(l, c - 1, m_board, synonims_map))))
- {
- //cout << "5There would be another vertical word siding with word on the left" << endl;
- return false;
- }
- //Checking right
- 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]))))
- && (!(horizontal_exception(l, c + 1, m_board, synonims_map))))
- {
- //cout << "6There would be another vertical word siding with word on the right" << endl;
- return false;
- }
- }
- if (isalpha(m_board[l][c])) //checking that there isnt anything after the word where the # would be
- {
- //cout << "The tile where the '#' after the word would be isn't empty" << endl;
- return false;
- }
- }
- //HORIZONTALLY
- if ((direction_letter == 'H') || (direction_letter == 'h')) {
- //Checking if word limmits don't go beyond the board's right limits
- if ((initial_column + word.length()) > column_size + 1)
- {
- //cout << "Word would go beyond the board limits" << endl;
- return false;
- }
- if (isalpha(m_board[l][c - 1])) //checking that there isnt anything before the word where the # would be
- {
- //cout << "The tile where the '#' before the word would be isn't empty" << endl;
- return false;
- }
- //Checking if word wouldn't overlap with existing letters and if there wouldn't be any letters around it
- for (int i = 0; i < word.length(); c++, i++) {
- //if tiles where word letters would be are different from those letters...
- if ((m_board[l][c] != '.') && (m_board[l][c] != word[i]))
- {
- //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;
- return false;
- }
- //Checking if there would be other horizontal words siding with our word:
- //Note: there are a few exceptions where this is alright
- //Checking down
- 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]))))
- && (!(vertical_exception(l + 1, c, m_board, synonims_map))))
- {
- //cout << "5There would be another horizontal word siding with word below" << endl;
- return false;
- }
- //Checking up
- 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]))))
- && (!(vertical_exception(l - 1, c, m_board, synonims_map))))
- {
- //cout << "6There would be another horizontal word siding with word above" << endl;
- return false;
- }
- }
- if (isalpha(m_board[l][c])) //checking that there aren't any letters after the word where the # would be
- {
- //cout << "The tile where the '#' after the word would be isn't empty" << endl;
- return false;
- }
- }
- return true;
- }
- void insert_word(vector<vector<char>> &m_board, map<string, string> &board_words, map<string, vector<string>> synonims_map) {
- //Posititon
- string position;
- cout << "Position (LCD / CTRL-Z = cancel) ? ";
- cin >> position;
- int line_size = m_board.size() - 2;
- int column_size = m_board[0].size() - 2;
- //Testing if input position is valid
- while (true)
- {
- if ((position.length() == 3
- && (int)position[0] >= 65 && (int)position[0] <= 64 + line_size
- && (int)position[1] >= 97 && (int)position[1] <= 96 + column_size
- && ((position[2] == 'V' || position[2] == 'H')
- || (position[2] == 'v' || position[2] == 'h')))
- || cin.eof())
- break;
- else
- {
- cout << "Introduza duas letras (Maiuscula seguida de Minuscula) que pertencam ao quadro seguida da direcao desejada (V ou H)\n" << endl;
- cin.clear();
- cin.ignore(1000, '\n');
- cout << "Position (LCD / CTRL-Z = cancel) ? ";
- cin >> position;
- }
- }
- if (position.length() > 0) //If the user didn't change his mind
- {
- string word;
- cout << "Word to insert (CTRL-Z = cancel) ? ";
- cin >> word;
- word = all_caps(word);
- //Testing if input word is valid
- while (cin.fail()
- || (!(word_exists(word, synonims_map)))
- || (!(word_fits(position, word, m_board, board_words, synonims_map)))
- || !cin.eof())
- {
- if (cin.fail())
- {
- cout << "Invalid Input for Word!" << endl;
- cin.clear();
- cin.ignore(1000, '\n');
- cout << "Word to insert.. ? ";
- }
- else if (!(word_exists(word, synonims_map)))
- {
- cout << "Given word does not exist according to our records" << endl;
- cin.clear();
- cin.ignore(1000, '\n');
- cout << "Please insert a different word:" << endl;
- }
- else if (!(word_fits(position, word, m_board, board_words, synonims_map)))
- {
- cout << "Given word cannot be placed in position" << endl;
- cin.clear();
- cin.ignore(1000, '\n');
- cout << "Please insert a different word or alter the board so it fits:" << endl;
- }
- else break;
- cin >> word;
- word = all_caps(word);
- }
- //INSERTING WORD
- 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
- 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
- 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
- word = all_caps(word);
- size_t initial_line = (int)line_letter - 64; //Ex: Se line_letter = 'a' entao initial_line = 1
- size_t initial_column = (int)column_letter - 96; //Ex: Se column_letter = 'A' entao initial_column = 1
- size_t l = initial_line;
- size_t c = initial_column;
- // Inserts word in board vertically
- if ((direction_letter == 'V') || (direction_letter == 'v')) {
- m_board[l - 1][c] = '#'; //places a # before the word
- for (int i = 0; i < word.length(); i++, l++) {
- m_board[l][c] = word[i];
- }
- m_board[l][c] = '#'; //places a # after the word
- }
- //Inserts word in board horizontally
- if ((direction_letter == 'H') || (direction_letter == 'h')) {
- m_board[l][c - 1] = '#'; //places a # before the word
- for (int i = 0; i < word.length(); i++, c++) {
- m_board[l][c] = word[i];
- }
- m_board[l][c] = '#'; //places a # after the word
- }
- if (word.length() > 0) //If the user didn't change his mind...
- {
- board_words.insert(pair<string, string>(word, position)); //added word to the list of words in the board
- //sera que devia usar emplace em vez de insert?, esta bem feito este pair temporario?
- show_board(m_board);
- }
- }
- }
- void pound_check(vector<vector<char>> &m_board, map<string, string> board_words) {
- m_board[0][0] = '#';
- //Fills the first line with '#'
- for (int c = 1; c < m_board.size(); c++) {
- m_board[0][c] = '#';
- }
- //Fills the last line with '#'
- for (int c = 1; c < m_board.size(); c++) {
- m_board[m_board[0].size() - 1][c] = '#';
- }
- //Fills the first column with '#'
- for (int l = 1; l < m_board[0].size(); l++) {
- m_board[l][0] = '#';
- }
- //Fills the last column with '#'
- for (int l = 1; l < m_board[0].size(); l++) {
- m_board[l][m_board.size() - 1] = '#';
- }
- for (auto it = board_words.begin(); it != board_words.end(); it++) {
- string word = it->first;
- string position = it->second;
- 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
- 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
- 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
- size_t initial_line = (int)line_letter - 64; //Ex: Se line_letter = 'a' entao initial_line = 1
- size_t initial_column = (int)column_letter - 96; //Ex: Se column_letter = 'A' entao initial_column = 1
- size_t l = initial_line;
- size_t c = initial_column;
- if ((direction_letter == 'V') || (direction_letter == 'v')){
- m_board[l - 1][c] = '#';
- m_board[l + word.length()][c] = '#';
- }
- if ((direction_letter == 'H') || (direction_letter == 'h')) {
- m_board[l][c - 1] = '#';
- m_board[l][c + word.length()] = '#';
- }
- }
- }
- void remove_word(vector<vector<char>> &m_board, map<string, string> &board_words) {
- string word;
- cout << "Word to remove (CTRL-Z = cancel) ?" << endl;
- cin >> word;
- word = all_caps(word);
- //Testing if input word is on the board
- while (board_words.find(word) == board_words.end()
- || !cin.eof()
- || cin.fail())
- {
- if (cin.fail())
- {
- cout << "Invalid Input for Word!" << endl;
- cin.clear();
- cin.ignore(1000, '\n');
- cout << "Word to remove (CTRL-Z = cancel) ?" << endl;
- }
- else if (board_words.find(word) == board_words.end())
- {
- cout << "There is no such word currently in the board" << endl;
- cin.clear();
- cin.ignore(1000, '\n');
- cout << "Word to remove (CTRL-Z = cancel) ?" << endl;
- }
- else break;
- cin >> word;
- word = all_caps(word);
- }
- if (word.length() > 0) //If the user didn't change his mind...
- {
- map<string, string>::iterator it = board_words.find(word);
- string position = it->second; //tells you the position of the word to be removed
- 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
- 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
- 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
- size_t initial_line = (int)line_letter - 64; //Ex: If line_letter = 'a' then initial_line = 1
- size_t initial_column = (int)column_letter - 96; //Ex: If column_letter = 'A' then initial_column = 1
- size_t l = initial_line;
- size_t c = initial_column;
- //REMOVING WORD
- //There will be no need to later "restore" words that may have been intercepted with the word removed
- //as we are checking before removing a letter wether or not it is attached to another word
- //Removes word in board vertically
- if ((direction_letter == 'V') || (direction_letter == 'v')) {
- //Resets the (#)tile before the word
- m_board[l - 1][c] = '.';
- for (int i = 0; i < word.length(); i++, l++) {
- if ((!(isalpha(m_board[l][c - 1]))) && (!(isalpha(m_board[l][c + 1]))))
- m_board[l][c] = '.';
- }
- //Resets the (#)tile after the word
- m_board[l][c] = '.';
- }
- //Removes word in board horizontally
- if ((direction_letter == 'H') || (direction_letter == 'h')) {
- //Resets the (#)tile before the word
- m_board[l][c - 1] = '.';
- for (int i = 0; i < word.length(); i++, c++) {
- if ((!(isalpha(m_board[l - 1][c]))) && (!(isalpha(m_board[l + 1][c]))))
- m_board[l][c] = '.';
- }
- //Resets the (#)tile after the word
- m_board[l][c] = '.';
- }
- board_words.erase(board_words.find(word)); // removes word from the list of words that are currently in the board
- pound_check(m_board, board_words);
- show_board(m_board);
- }
- }
- //================================================================
- void advanced_menu(vector<vector<char>> &m_board, map<string, string> &board_words, map<string, vector<string>> synonims_map) {
- char command;
- cout << "COMMANDS:" << endl;
- cout << "+ = place word in board" << endl;
- cout << "- = remove word from board" << endl;
- cout << "? = help" << endl;
- cout << "b = back to main menu" << endl << endl;
- cout << "Command: ";
- cin >> command;
- while (command != 'b')
- {
- while (cin.fail()
- || ((command != '+')
- && (command != '-')
- && (command != '?')
- && (command != 'b'))
- )
- {
- cout << endl << "Invalid command input" << endl;
- cout << "PLease insert one of the available commands" << endl << endl;
- cout << "COMMANDS:" << endl;
- cout << "+ = place word in board" << endl;
- cout << "- = remove word from board" << endl;
- cout << "? = help" << endl;
- cout << "b = back to main menu" << endl << endl;
- cout << "Command: ";
- cin.clear();
- cin.ignore(1000, '\n');
- cin >> command;
- }
- switch (command)
- {
- case '+':
- insert_word(m_board, board_words, synonims_map);
- break;
- case '-':
- remove_word(m_board, board_words);
- break;
- }
- if (command == '?')
- break;
- cout << "COMMANDS:" << endl;
- cout << "+ = place word in board" << endl;
- cout << "- = remove word from board" << endl;
- cout << "? = help" << endl;
- cout << "b = back to main menu" << endl << endl;
- cout << "Command: ";
- cin >> command;
- }
- }
- void main_menu(string dictionary_file, vector<vector<char>> &m_board, map<string, string> &board_words, map<string, vector<string>> synonims_map) {
- unsigned int option;
- cout << "CROSSWORDS PUZZLE CREATOR" << endl;
- cout << "==============================================\n" << endl;
- cout << "INSTRUCTIONS:" << endl;
- cout << "CTRL-Z = back" << endl;
- cout << "----------------------------------------------\n" << endl;
- cout << "OPTIONS:" << endl;
- cout << "1 - Create Puzzle" << endl;
- cout << "2 - Resume Puzzle" << endl;
- cout << "0 - Exit" << endl << endl;
- cout << "Option: ";
- cin >> option;
- while (option != 0)
- {
- while (
- cin.fail()
- || (option < 0)
- || (option > 2)
- )
- {
- cout << "Please insert a number from the options available" << endl;
- cout << "1 - Create Puzzle" << endl;
- cout << "2 - Resume Puzzle" << endl;
- cout << "0 - Exit" << endl << endl;
- cin.clear();
- cin.ignore(1000, '\n');
- cout << "Option: ";
- cin >> option;
- }
- switch (option)
- {
- case 1:
- {
- loadDictionary(dictionary_file, synonims_map);
- show_board(m_board);
- if (board(m_board))
- {
- advanced_menu(m_board, board_words, synonims_map);
- storeBoard(dictionary_file, m_board, board_words);
- }
- }
- break;
- case 2:
- {
- loadBoard(m_board, board_words);
- loadDictionary(dictionary_file, synonims_map);
- advanced_menu(m_board, board_words, synonims_map);
- storeBoard(dictionary_file, m_board, board_words);
- }
- break;
- case 0:
- cout << "Shuting down..." << endl;
- }
- if (option != 0) {
- cout << "OPTIONS:" << endl;
- cout << "1 - Create Puzzle" << endl;
- cout << "2 - Resume Puzzle" << endl;
- cout << "0 - Exit" << endl << endl;
- cout << "Option: ";
- cin >> option;
- }
- }
- }
- int main() {
- /*
- map<string, string> b_words;
- 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";
- vector<vector<char>> v(10);
- for (int i = 0; i < v.size(); i++)
- {
- v[i].resize(10);
- }
- cout << "1";
- for (size_t i = 0; i < v.size(); i++)
- {
- for (size_t j = 0; j < v[0].size(); j++)
- {
- v[i][j] = '.';
- }
- }
- cout << "2";
- v[1][2] = 'A';
- v[1][3] = 'R';
- cout << "3";
- b_words.insert(pair<string, string>("AR", "AbH"));
- cout << "4";
- storeBoard(inputFile, v, b_words);
- cout << "5";
- vector<vector<char>> v;
- map<string, string> b_words;
- loadBoard(v, b_words);
- for (size_t i = 0; i < v.size(); i++)
- {
- cout << endl;
- for (size_t j = 0; j < v[0].size(); j++)
- {
- cout << setw(3) << v[i][j];
- }
- }
- for (auto i = b_words.begin(); i != b_words.end(); i++)
- {
- cout << i->first << ": " << i->second << endl;
- }
- show_board(v);
- map < string, vector<string>> dic_map;
- loadDictionary("ofile3.txt", dic_map);
- */
- if (!file_exists("Board Number.txt"))
- {
- ofstream bNumber_file;
- bNumber_file.open("Board Number.txt");
- bNumber_file << 0;
- bNumber_file.close();
- }
- Dictionary dictionary;
- Board board;
- string dictionary_file;
- cout << "Dictionary File ?" << endl;
- cin >> dictionary_file;
- while (cin.fail()
- || !file_exists(dictionary_file)
- || !cin.eof())
- {
- if (cin.fail() && !cin.eof())
- {
- cout << "Invalid input for file!" << endl;
- cin.clear();
- cin.ignore(1000, '\n');
- }
- else if (!file_exists(dictionary_file) && !cin.eof())
- {
- cout << "File does not exist" << endl;
- cin.clear();
- cin.ignore(1000, '\n');
- }
- else break;
- cout << "Dictionary File ?" << endl;
- cin >> dictionary_file;
- }
- dictionary.loadDictionary(dictionary_file);
- Game game = Game(dictionary, board);
- bool start = true;
- while (true)
- {
- if (!cin.eof() && !start)
- {
- string finish;
- cout << "Finish?" << endl;
- cin >> finish;
- }
- start = false;
- if (cin.eof())
- break;
- else game.main_menu(dictionary_file);
- }
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement