Ask a question to Desklib · AI bot

Ask NowBETA

Word Puzzle Generator in C++

9 Pages2474 Words149 Views
   

Added on  2019-09-21

About This Document

This program generates a word puzzle game in C++ using randomization and user input to find the words. It includes a menu, puzzle board, word list, and word finding functionality. The user can choose to generate a new puzzle or exit the program. The puzzle board is randomly generated with letters and the words are selected from a list. The words are placed on the board in a random direction (top to bottom, left to right, or diagonal) and the user must input the first and last letter cell of the word to find it. The program keeps track of which words have been found and exits when all words have been found or the user chooses to quit.
BookmarkShareRelated Documents
Program#include<stdio.h>#include<stdlib.h>#include<ctime>#include<string>#include<vector>#include<algorithm>#define_CRT_SECURE_NO_WARNINGSusingnamespace 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(){char* 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){printf("Menu\n");printf("******\n");printf("1) Generate a new Word Puzzle\n");printf("2) Exit\n");printf("Enter your choice: ");scanf_s("%d",&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<char*> selectedWords;staticvector<char*> selectedFirstCell;
Word Puzzle Generator in C++_1
staticvector<char*> selectedSecondCell;staticvector<bool> wordFound;for (int i = 0; i < 4; i++){char* 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 + strlen(selectedWord)- 1 < 10){int loc = 0;bool correctWord = false;for (int j = selectRow; j < (selectRow + strlen(selectedWord)); j++){if (board[j][selectCol] == '-'|| board[j][selectCol] == selectedWord[loc++])correctWord = true;else{correctWord = false;break;}}if (correctWord){loc = 0;char *str= (char *)malloc(5);str[0] = '\0';_itoa_s(selectRow,str, sizeofstr,10);char temp[2] = { letters[selectCol],'\0' };strcat_s(str, 5, temp );selectedFirstCell.push_back(str);int j;for ( j = selectRow; j < (selectRow + strlen(selectedWord)); j++)if (board[j][selectCol] == '-' || board[j][selectCol] == selectedWord[loc])board[j][selectCol] = selectedWord[loc++];
Word Puzzle Generator in C++_2
char *str1=(char *) malloc(5);str1[0] = '\0';_itoa_s(j - 1, str1, sizeofstr1, 10);char temp1[2] = { letters[selectCol],'\0' };strcat_s(str1, 5, temp1);selectedSecondCell.push_back(str1);break;}}}break;caseLEFT_RIGHT:while (1){int selectRow = rand() % 10;int selectCol = rand() % 10;if (selectCol + strlen(selectedWord) - 1 < 10){int loc = 0;bool correctWord = false;for (int j = selectCol; j < (selectCol + strlen(selectedWord)); j++){if (board[selectRow][j] == '-'|| board[selectRow][j] == selectedWord[loc++])correctWord = true;else{correctWord = false;break;}}if (correctWord){loc = 0;char *str=(char *) malloc(5);str[0] = '\0';_itoa_s(selectRow, str, sizeofstr, 10);char temp[2] = { letters[selectCol],'\0' };strcat_s(str, 5, temp);selectedFirstCell.push_back(str);int j;for ( j = selectCol; j < (selectCol + strlen(selectedWord)); j++)
Word Puzzle Generator in C++_3

Found this document preview useful?

Related Documents
Word Puzzle Generator in C++
|8
|2069
|314