Question: I have this c++ program that does creates a word search from a text file of words. This program works however i want to know
I have this c++ program that does creates a word search from a text file of words. This program works however i want to know how to turn this program into a game.
the program should allow you to enter in the coordinates of the first letter and the coordinates of the last letter of the word in the word search.
it should also show a list that updates once you enter in the correct location of the word.
if possible also make it so that you have a limited amount of tries before you lose.
this program needs to only use c++.
Word search code
================================================
#include
using namespace std;
int main()
{
ifstream file ("list.txt");
string a, word[10];
int i = 0, l = 0,c;
srand(time(NULL));
if (!file) {
cout << "Unable to open file";
exit(1); // terminate with error
}
while (!file.eof())
{
file >> a;
if (l < a.length()) //to find the longest word length
l = a.length();
word[i++] = a;
}
i--;
file.close();
int size = l + 1; //grid size wil be length of longest word + 1
char** grid = new char*[size];
for (int j = 0; j < size; j++) {
grid[j] = new char[size];
}
//char grid[size][size];
//int gridc[size][size] = { 0 }; //gridc is used in order to track the letters which are crossed by other word. so that the same letter is not written and when deallocated the letter is not removed which might affect other word
int** gridc = new int*[size];
for (int j = 0; j < size; j++) {
gridc[j] = new int[size];
//gridc[j] = { 0 };
}
for (int x = 0; x < size; x++)
for (int y = 0; y < size; y++) {
grid[x][y] = '0';
gridc[x][y] = 0;
}
//placing the words in the grid word by word
for (int k = 0; k <= i; k++)
{
int length = word[k].length();
int j = rand() % 3;
switch (j) {
//horizontal
case 0:
{
int r = rand() % size; //to find the row to start the word
int c = rand() % (size - length); // this is find the column to start the word
for (int m = 0; m < length; m++)
{
//if cell is empty then allocate letter
if (grid[r][c] == '/0')
grid[r][c++] = word[k][m];
//if the cell contains the same letter of the word already then update gridc to track that letter belongs to some other word also
else if (grid[r][c] == word[k][m])
{
gridc[r][c] = 1;
c++;
}
else
{
//to deallocate the allocated letters
for (int n = m; n >= 0; n--)
{
if (r == 0 || c == 0)
break;
if (gridc[r][--c] == 0)
grid[r][c] = '0';
}
k--; //going back to the same word and processing
break;
}
}
break;
}
//vertical
case 1:
{
int r = rand() % (size - length); //to find the row to start the word
int c = rand() % size; // this is find the column to start the word
for (int m = 0; m < length; m++)
{
//if cell is empty then allocate letter
if (grid[r][c] == '0')
grid[r++][c] = word[k][m];
//if the cell contains the same letter of the word already then update gridc to track that letter belongs to some other word also
else if (grid[r][c] == word[k][m])
{
gridc[r][c] = 1;
r++;
}
else
{
//to deallocate the allocated letters
for (int n = m; n >= 0; n--)
{
if (r == 0 || c == 0)
break;
if (gridc[--r][c] == 0)
grid[r][c] = '0';
}
k--;//going back to the same word and processing
break;
}
}
break;
}
//diagonal
case 2:
{
int r = rand() % (size - length); //to find the row to start the word
int c = rand() % (size - length); // this is find the column to start the word
for (int m = 0; m < length; m++)
{
//if cell is empty then allocate letter
if (grid[r][c] == '0')
grid[r++][c++] = word[k][m];
//if the cell contains the same letter of the word already then update gridc to track that letter belongs to some other word also
else if (grid[r][c] == word[k][m])
{
gridc[r][c] = 1;
r++;
c++;
}
else
{
//to deallocate the allocated letters
for (int n = m; n >= 0; n--)
{
if (r == 0 || c == 0)
break;
if (gridc[--r][--c] == 0)
grid[r][c] = '0';
}
k--;//going back to the same word and processing
break;
}
}
break;
}
}
}
//to fill the empty cells with random letters
srand(time(NULL));
for (int x = 0; x < size; x++)
for (int y = 0; y < size; y++)
{
if (grid[x][y] == '0')
{
int val = rand() % 26;
val += 97;
grid[x][y] = val;
}
}
cout << "Printing grid ";
//to print the grid puzzle
for (int x = 0; x < size; x++)
{
cout << endl;
for (int y = 0; y < size; y++)
cout << grid[x][y] << " ";
}
//Freeing the resources allocated from heap by dynamic memory allocation
for (int i = 0; i < size; i++)
delete[] grid[i];
delete[] grid;
for (int i = 0; i < size; i++)
delete[] gridc[i];
delete[] gridc;
return 0;
}
==================================================================
list.txt has 5 words inside
list.txt
apple pear orange grape tomato
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
