logo

Word Puzzle Game

   

Added on  2019-10-18

8 Pages2069 Words314 Views
 | 
 | 
 | 
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 Game_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 Game_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 Game_3

End of preview

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

Related Documents