Ask a question from expert

Ask now

Word Puzzle Generator in C++

8 Pages2069 Words314 Views
   

Added on  2019-10-18

About This Document

This program generates a word puzzle game in C++ using unique words and letters. It allows the user to find the words in the puzzle and exit the game. The program uses a menu to generate a new puzzle or exit the game. It generates a 10x10 board with random letters and places unique words in the board in three different ways: top to bottom, left to right, and diagonal. The program also allows the user to find the words in the puzzle by entering the first and second letter cell of the selected word. Once all words are found, the game ends.

Word Puzzle Generator in C++

   Added on 2019-10-18

BookmarkShareRelated Documents
Program:#include<iostream>#include<cstdlib>#include<ctime>#include<string>#include<vector>#include<algorithm>usingnamespace std;int generateUniqueInt(){staticvector<int> generatedValues;int num;srand((unsigned)time(0));while (1){num = rand() % 20;if (find(generatedValues.begin(), generatedValues.end(), num) ==generatedValues.end()) {generatedValues.push_back(num);break;}}return num;}int main(){string words[] = { "SAMPLE", "WORD", "ABOUT", "ABOVE", "GRADE", "INDEX", "INNER","JOINT", "JAPAN", "LASER", "GRASS", "SPACE", "LAUGH", "LABEL","STAND", "START", "SPEED", "YOUTH", "STAGE", "WRITE" };char letters[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};enumMETHOD { TOP_BOTTOM, LEFT_RIGHT, DIAGONAL };char board[10][10];bool loop = true;int choice;while (loop){cout <<"Menu\n";cout <<"******\n";cout <<"1) Generate a new Word Puzzle\n";cout <<"2) Exit\n";cout <<"Enter your choice: ";cin >> choice;switch (choice){case 1:{srand((unsigned)time(0));for (int i = 0; i < 10; i++)for (int j = 0; j < 10; j++)board[i][j] = '-';staticvector<string> selectedWords;staticvector<string> selectedFirstCell;staticvector<string> selectedSecondCell;
Word Puzzle Generator in C++_1
staticvector<bool> wordFound;for (int i = 0; i < 4; i++){string selectedWord = words[generateUniqueInt()];selectedWords.push_back(selectedWord);wordFound.push_back(false);METHOD type = static_cast<METHOD>(rand() % 3);switch (type){caseTOP_BOTTOM:while (1){int selectRow = rand() % 10;int selectCol = rand() % 10;if (selectRow + selectedWord.length() - 1 < 10){int loc = 0;bool correctWord = false;for (int j = selectRow; j < (selectRow + selectedWord.length()); j++){if (board[j][selectCol] == '-'|| board[j][selectCol] == selectedWord.at(loc++))correctWord = true;else{correctWord = false;break;}}if (correctWord){loc = 0;string str = to_string(selectRow) +string(1,letters[selectCol]);selectedFirstCell.push_back(str);int j;for ( j = selectRow; j < (selectRow + selectedWord.length()); j++)if (board[j][selectCol] == '-' || board[j][selectCol] == selectedWord.at(loc))board[j][selectCol] = selectedWord.at(loc++);str = to_string(j-1) +string(1, letters[selectCol]);selectedSecondCell.push_back(str);break;}}}break;caseLEFT_RIGHT:while (1){
Word Puzzle Generator in C++_2
int selectRow = rand() % 10;int selectCol = rand() % 10;if (selectCol + selectedWord.length() - 1 < 10){int loc = 0;bool correctWord = false;for (int j = selectCol; j < (selectCol + selectedWord.length()); j++){if (board[selectRow][j] == '-'|| board[selectRow][j] == selectedWord.at(loc++))correctWord = true;else{correctWord = false;break;}}if (correctWord){loc = 0;string str = to_string(selectRow) +string(1, letters[selectCol]);selectedFirstCell.push_back(str);int j;for ( j = selectCol; j < (selectCol + selectedWord.length()); j++)if (board[selectRow][j] == '-' || board[selectRow][j] == selectedWord.at(loc))board[selectRow][j] = selectedWord.at(loc++);str = to_string(selectRow) +string(1, letters[j-1]);selectedSecondCell.push_back(str);break;}}}break;caseDIAGONAL:int count = 0;while (1){count++;if (count == 3)break;int selectRow = rand() % 10;int selectCol = rand() % 10;if (selectRow + selectedWord.length() - 1 < 10 && selectCol + selectedWord.length() - 1 < 10){int loc = 0;bool correctWord = false;
Word Puzzle Generator in C++_3

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents
Word Puzzle Generator in C++
|9
|2474
|149

Java Program to Draw Shapes - Desklib
|10
|1283
|284

PriorityThreadQueue and ListManager
|4
|670
|24

SEO Expert for Desklib
|9
|1663
|89

Memory Sequence Game - Java Program for Console Based Game
|8
|998
|259