0% found this document useful (0 votes)
32 views4 pages

#Include #Include #Include #Include #Include #Include #Include Using Namespace Const

This C++ program implements a crossword puzzle game with different grid sizes (5x5, 10x10, 15x15) for beginner, intermediate, and advanced levels. The program uses templates, arrays, maps, and sets to generate random letter grids, check the grids for words from a dictionary file, and count the number of occurrences of each word found. The main function displays a menu, gets the player's choice of level, generates the appropriate sized grid, checks it for words, and displays the results before looping back to the menu.

Uploaded by

David foster
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views4 pages

#Include #Include #Include #Include #Include #Include #Include Using Namespace Const

This C++ program implements a crossword puzzle game with different grid sizes (5x5, 10x10, 15x15) for beginner, intermediate, and advanced levels. The program uses templates, arrays, maps, and sets to generate random letter grids, check the grids for words from a dictionary file, and count the number of occurrences of each word found. The main function displays a menu, gets the player's choice of level, generates the appropriate sized grid, checks it for words, and displays the results before looping back to the menu.

Uploaded by

David foster
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <string>

#include <map>
#include <set>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

const string dictionary = R"(c:\Users\Napster\Documents\Visual Studio


2017\Projects\Project\Project\Napster.txt)"s;

string reverse ( const string& str )


{
string rev;

for ( auto s = str.rbegin() ; s != str.rend() ; ++s )


rev += *s;

return rev;
}

template < size_t a, size_t b >

void random_letter(char(&arrays)[a][b]) {

for (size_t c = 0; c < a; ++c)

for (size_t d = 0; d < b; ++d)

arrays[c][d] = (rand() % 26) + 'A';

template < size_t a, size_t b >

void display_array ( char(&arr)[a][b] )


{
for ( size_t r = 0; r < a; ++r ) {

for (size_t c = 0; c < b; ++c)

cout << arr[r][c] << " ";

cout << endl;


}

cout << endl;


}

template < size_t a, size_t b >

void getwords ( const char(&arr)[a][b], map<string, int>& words, const set <string>& dict
)
{
words.clear();
//Do rows
for (size_t sr = 0; sr < a; ++sr)

for (size_t sc = 0; sc < b; ++sc)

for (size_t c = sc; c < b; ++c) {

string word, rv;

for (size_t cc = 0; cc + c < b; ++cc)

word += arr[sr][cc + sc];

if ( word.size() > 2 ) {

if (dict.count(word))

words[word]++;

if (dict.count (rv = reverse(word)) )

words[rv]++;
}
}

//Do cols
for (size_t sc = 0; sc < b; ++sc)

for (size_t sr = 0; sr < a; ++sr)

for (size_t r = sr; r < a; ++r) {

string word, rv;

for (size_t rr = 0; rr + r < a; ++rr)

word += arr[sr + rr][sc];

if (word.size() > 2) {

if (dict.count(word))

words[word]++;

if (dict.count(rv = reverse(word)))

words[rv]++;
}
}
}

bool dict ( set<string>& dicset )


{
dicset.clear();

ifstream dicts ( dictionary );

if ( !dicts.is_open() )
return false;

string read;

while ( getline(dicts , read) )

dicset.insert (read);

return true;
}

int main()
{
set <string> dictset;

srand( (unsigned int) time(0) );

int choice;

map<string, int> words;

do {

cout << "\nWelcome to our online Cross Word Game!\n"


<< "--------------------------------------\n"
<< "\nPlease choose an option from the menu below :\n\n"
<< "1-Beginner : A 5x5 grid.\n"
<< "2-Intermediate : A 10x10 grid.\n"
<< "3-Advanced : A 15x15 grid.\n"
<< "4-Or exit.\n"
<< "\nYour choice --> ";

choice = 0;
cin >> choice;

if (!cin) {
cin.clear();
cin.ignore(1000, '\n');
}

switch (choice) {
case 1: {
char arr[5][5];

random_letter(arr);
display_array(arr);
getwords(arr, words, dictset);
break;
}

case 2: {
char arr[10][10];

random_letter(arr);
display_array(arr);
getwords(arr, words, dictset);
break;
}

case 3: {
char arr[15][15];

random_letter(arr);
display_array(arr);
getwords(arr, words, dictset);
break;
}

case 4:
break;

default:
cout << "\nInvalid input!\n" << endl;
}

if (choice && choice < 4) {

if (words.size()) {

cout << "\nThe found words are : " << endl;

for (const auto& word : words)

cout << word.first << " " << word.second << endl;
}

else
cout << "\nNo words were found." << endl;
}

} while (choice != 4);

cout << "\n";


system("pause");
return 0;
}

You might also like