crossword puzzle

Pages: 123... 6
Which loop? You have several in your code, and I don't see any of them which looks like it has any checks for errors.

Also, you would help yourself enormously - and also us - if you used a sensible, consistent indentation style.
Last edited on
I have tested your program (with some minor changes) with https://github.com/dwyl/english-words/, and I got no runtime error.

And, what do you mean by "... but I can't get out of the loop in case of error and the program displays an error window in Windows when it does not find a solution."
The program is purely console-based, so I don't understand. If it's an error message (like a mismatched array position access), please post the error message here.
All I made were minor changes like formatting the code with help of a code formatter and adding command-line support for specifying a dictionary file.

Here the formatted code with its minor changes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <iostream>
#include <string>
#include <algorithm>
#include <bits/stdc++.h>
#include <stdlib.h>
#include <sstream>

// Used in randomization
#include <ctime>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/random/variate_generator.hpp>


using namespace std;
using namespace boost;

int current_time_nanoseconds() {
    struct timespec tm;
    clock_gettime(CLOCK_REALTIME, &tm);
    return tm.tv_nsec;
}

std::string   find_intersection(std::string & first, std::string & second)
{
    std::sort(first.begin(),first.end());
    std::sort(second.begin(), second.end());
    int length = std::min(first.length(), second.length());
    std::string result(length,' ');
    std::set_intersection(first.begin(),first.end(),second.begin(), second.end(),result.begin());
    return result;
}

int main( int argc, char * argv[] )
{
    string temp;
    string temp2;
    string temp3;
    string temp4;
    string temp5;
    int x_temp;
    int x_temp2;
    int x_temp3;
    int x_temp2_3;
    int x_temp4;
    int x_temp3_4;
    int x_temp5;
    int x_temp4_5;
    char chr2;
    char chr1;
    char chr3;
    char chr4;
    int x_chr;
    int y_chr;
    int y_dep_chr;
    int x_chr2;
    int x_dep_chr2;
    int y_chr2;

    int x_chr3;
    int y_chr3;
    int y_dep_chr3;
    int x_chr4;
    int y_chr4;
    char grid[25][25] = {
        {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
        {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
        {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
        {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
        {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
        {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
        {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
        {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
        {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
        {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
        {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
        {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
        {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
        {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
        {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
        {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
        {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
        {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
        {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
        {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
        {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
        {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
        {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
        {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
        {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.'}
    };

    int LINE;


    random::mt19937 rng(current_time_nanoseconds());
    random::uniform_int_distribution<> sept(1000,10000);

    // command-line file input support
    std::string ifileName = "dictionary.txt";
    if (argc > 1 ) 
        ifileName = argv[1];

  
    std::ifstream f(ifileName);
    
    if ( !f ) std::cerr << "File '" << ifileName << "' couldn't opened!\n";
    
    std::string s;

    for (int i = 1; i <= sept(rng); i++)
    {
        std::getline(f, s);
        temp = s;

    }
    for (int i = 1; i <= sept(rng); i++)
    {
        std::getline(f, s);
        temp2 = s;

    }
    for (int i = 1; i <= sept(rng); i++)
    {
        std::getline(f, s);
        temp3 = s;

    }
    for (int i = 1; i <= sept(rng); i++)
    {
        std::getline(f, s);
        temp4 = s;

    }
    for (int i = 1; i <= sept(rng); i++)
    {
        std::getline(f, s);
        temp5 = s;

    }
    cout<<temp<<" temp"<<"\n";
    cout<<temp2<<" temp2"<<"\n";
    cout<<temp3<<" temp3"<<"\n";
    cout<<temp4<<" temp4"<<"\n";
    cout<<temp5<<" temp5"<<"\n";


    char arr1[temp.length()];
    for (int h = 0; h < sizeof(arr1); h++)
    {
        arr1[h] = temp[h];
    }


    char arr2[temp2.length()];

    for (int i = 0; i < sizeof(arr2); i++)
    {
        arr2[i] = temp2[i];
    }


//temp2

    std::string intersec = find_intersection(temp, temp2);
    char arr[intersec.length()];

    for (int j = 0; j < sizeof(arr); j++)
    {
        arr[j] = intersec[j];
    }

    if (sizeof(arr)>0)
    {
        chr1 = arr[0];
    }
    if (sizeof(arr) <=0)
    {
        chr1 = arr[0];
    }

    for (int k = 0; k < sizeof(arr1); k++)
    {
        if (arr1[k] == chr1)
        {
            x_temp = k;
        }
    }



    for (int l = 0; l < sizeof(arr2); l++)
    {
        if (arr2[l] == chr1)
        {
            x_temp2 = l;
        }
    }

    for (int kk = 0; kk < sizeof(arr1); kk++) {
        if (grid[12+kk][12] == '.' ||
                grid[12+kk][12]== arr1[kk]) {
            grid[12+kk][12] = arr1[kk];
        }


    }


    for (int i =0; i < 25; i ++)
    {
        for (int j =0; j < 25; j++)
        {
            if (grid[i][j] == chr1 )
            {
                x_chr = i;
                y_chr = j;
            }


        }
    }



    for (int l = 0; l < sizeof(arr2); l++) {
        y_dep_chr = y_chr-x_temp2;
        if (grid[x_chr][y_dep_chr+l] == '.' ||
                grid[x_chr][y_dep_chr+l]== arr2[l]) {
            grid[x_chr][y_dep_chr+l] = arr2[l];
        }


    }
2nd part:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180


//temp3
    char arr3[temp3.length()];
    for (int g = 0; g < sizeof(arr3); g++)
    {
        arr3[g] = temp3[g];
    }

    std::string intersec_temp3 = find_intersection(temp2, temp3);
    char arr4[intersec_temp3.length()];
    for (int f = 0; f < sizeof(arr4); f++)
    {
        arr4[f] = intersec_temp3[f];
    }
    if (sizeof(arr4)>0)
    {
        chr2 = arr4[1];
    }
    if (sizeof(arr4) <=0)
    {
        chr2 = arr4[1];
    }

    for (int l = 0; l < sizeof(arr2); l++)
    {
        if (arr2[l] == chr2)
        {
            x_temp2_3 = l;
        }
    }


    for (int k = 0; k < sizeof(arr3); k++)
    {
        if (arr3[k] == chr2)
        {
            x_temp3 = k;
        }
    }

    for (int l = 0; l < sizeof(arr2); l++)
    {
        if (grid[x_chr][y_dep_chr+l] == chr2)
        {
            x_chr2 = x_chr;
            y_chr2 = y_dep_chr+l;


        }



    }

    for (int k = 0; k < sizeof(arr3); k++) {
        x_dep_chr2 = x_chr2-x_temp3;
        if (grid[x_dep_chr2+k][(y_chr2)] == '.' ||
                grid[x_dep_chr2+k][(y_chr2)]== arr3[k]) {
            grid[x_dep_chr2+k][(y_chr2)] = arr3[k];
        }


    }
//temp4
    char arr6[temp4.length()];
    for (int g = 0; g < sizeof(arr6); g++)
    {
        arr6[g] = temp4[g];
    }

    std::string intersec_temp4 = find_intersection(temp3, temp4);
    char arr7[intersec_temp4.length()];
    for (int f = 0; f < sizeof(arr7); f++)
    {
        arr7[f] = intersec_temp4[f];
    }
    if (sizeof(arr7)>0)
    {
        chr3 = arr7[1];
    }
    if (sizeof(arr7) <=0)
    {
        chr3 = arr7[0];
    }


    for (int l = 0; l < sizeof(arr6); l++)
    {
        if (arr6[l] == chr3)
        {
            x_temp4 = l;
        }
    }

    for (int l = 0; l < sizeof(arr3); l++)
    {
        if(grid[(x_chr2-x_temp3)+l][y_chr2] == chr3)
        {
            x_chr3 = (x_chr2-x_temp3)+l;
            y_chr3 = y_chr2;
        }

    }


    for (int l = 0; l < sizeof(arr6); l++) {
        y_dep_chr3 = y_chr3-x_temp4;
        if (	grid[x_chr3][(y_dep_chr3)+l] == '.' ||
                grid[x_chr3][(y_dep_chr3)+l]== arr6[l]) {
            grid[x_chr3][(y_dep_chr3)+l] = arr6[l];
        }


    }

    //temp5

    char arr9[temp5.length()];
    for (int g = 0; g < sizeof(arr9); g++)
    {
        arr9[g] = temp5[g];
    }

    std::string intersec_temp5 = find_intersection(temp4, temp5);
    char arr10[intersec_temp4.length()];
    for (int f = 0; f < sizeof(arr10); f++)
    {
        arr10[f] = intersec_temp5[f];
    }
    if (sizeof(arr10)>0)
    {
        chr4 = arr10[1];
    }
    if (sizeof(arr10) <=0)
    {
        chr4 = arr10[0];
    }


    for (int l = 0; l < sizeof(arr9); l++)
    {
        if (arr9[l] == chr4)
        {
            x_temp5 = l;
        }
    }

    for (int l = 0; l < sizeof(arr4)+1; l++)
    {
        if(grid[x_chr3][(y_chr3-x_temp4+l)] == chr4)
        {
            x_chr4 = x_chr3;
            y_chr4 = (y_chr3-x_temp4+l);
        }

    }



    for (int l = 0; l < sizeof(arr9); l++) {

        if (	grid[(x_chr4-x_temp5)+l][(y_chr4)] == '.' ||
                grid[(x_chr4-x_temp5)+l][(y_chr4)]== arr9[l]) {
            grid[(x_chr4-x_temp5)+l][(y_chr4)] = arr9[l];
        }


    }
    // Draw the grid once
    for (int i =0; i < 25; i ++)
    {
        for (int j =0; j < 25; j++)
        {
            std::cout << grid[i][j];
            std::cout << " ";
        }
        std::cout << "\n";
    }
}
Is there a distinct reason that you need to stick with boost libraries for the random facilities?
Sorry, it's hard for me to follow your thoughts, especially at the code in your major code. Maybe that's the case because you used a plenty of non-meaningful variables.

Why not choose this approach:

1
2
3
4
5
6
7
8
9
10
Chose a randomly selected word, place it randomly at the grid.

WHILE

    Test a new (randomly chosen) word for all orthogonal positions on the grid, 
    count all matching intersections.

    Place the word at position where it has its most intersections. 

    BREAK IF Some words don't match anymore in the grid. 


With this approach you could emplace your words (after a match) directly within the grid (which makes it easier for testing further matches).

Merari, I've tried another approach for detecting intersections, if you want take a look, here it is:
http://www.cplusplus.com/forum/general/270142/#msg1163542
Commenting on the latest code:

you have using namespace std; but then you use std:: throughout the code. So why have using namespace std? My personal preference is to add "using" commands for the specific symbols that I'll use. Others prefer the std:: prefix throughout.

Seeding a random number generator with the current nanosecond timer may be bad. Most OS's don't have nanosecond resolution, tm-tv_nsec is very likely to end in a whole lot of zeros. I'd seed with time(0).

- find_intersection() usually returns a string with trailing spaces. Is that your intention?

- Function names beginning with "return_" don't add any semantic value and just take up space, at least in my opinion. From here on I'll refer to the names without the return_.

intersect_letter(): variable length arrays are not standard C++. In fact, I struggle to see why you have the arr array at all.

And what's with the loop. Why hunt for j== indice when you can use it directly?
1
2
3
4
int intersect_letter(string word1, string word2, int indice) {
  std::string intersec = find_intersection(word1, word2);
  return intersec[indice];
}


Better still, why not compute the intersection once in the calling code and get the i'th letter there. I would save the cost of recomputing the intersection each time.

In intersect_pos_in_word, you find the position of chr1, create a new string from the beginning of word to that position, and the return the length of the string. Instead, you could simply return word.find(chr1). In fact, you could replace calls to intersection_pos_in_word() with word.find(chr1).
1
2
3
4
    string word1;
    string word2;
    string word3;
    string word4;

Any time you find yourself using variable names like this, replace them with an array or vector.

In main():

You assume that word1 and word2 have common letters.

grid[x_word1][y_init + j] = word1[j]; What if the word doesn't fit here?
1
2
3
4
5
6
7
8
    // put word1 in grid
    for (unsigned i = 0; i != word1.length(); i++) {
        for (unsigned j = 0; j != word1.length(); j++) {
            grid[x_word1][y_init + j] = word1[j];

        }

    }

What purpose does the outer loop serve? It seems that you copy the word multiple times to the same place.

Same comment for placing word 2.

I think you should create a function to place a word in the grid. Something like:

1
2
3
// Place the word in the grid, starting at position grid[row][col].
// Tthe second char goes at grid[row+deltax][col+deltay], etc.
placeWord(string&word, unsigned row, unsigned col, int deltax, int deltay);


Once you figure out how to combine two words onto the grid, write a function that will add a new word to the grid. Right now it's hard coded for 4 words.
Hello Merari, it's a pity that you closed your account just because you didn't got an answer in relatively close time. The members here are not obliged to answer to anything here. And most have a life beside loitering here in the forum. I'm for myself have the luck having relatively much free time. But in the last two days, I had some trouble with my computer so I was not able to follow your thread. And so I wasn't able to read your private message and answer in a short time. If I wouldn't think that your behavior hints to a fickle mentality, I would invite you, coming back here.
I'm not sure what you want to obtain in your question. Is your ask about to deleting a letter in std::string, or from a plain char?

Concerning to the former, your statement in line 132 is correct. If your question is about erasing a word from a plain char array, then you need to copy step by step all of the chars which following the to erasing to their preceding position.
Hello, there is still a lot of error, I think that the loop that alternates (even number and odd number) vertical and horizontal placement does not totally solve the recursivity problem. I think that the common letter detection is not done correctly. Then the placement loop does not check the grid cells correctly and makes the errors persist despite the if loop. If you could help me that would be nice. Thanks again.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <iostream>
#include <string>
#include <algorithm>
#include <bits/stdc++.h>
#include <stdlib.h>
#include <sstream>
#include <ctime>
#include <vector>
using namespace std;
std::vector<double> coord_x;
std::vector<double> coord_y;
std::vector<std::string > words_hori;
std::vector<std::string > words_verti;
std::vector<std::string > words;

char grid[26][26] = {
		{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' }
};

int random(int from, int to)
{
	return rand() % (to - from + 1) + from;
}

std::string find_intersection(std::string &first, std::string &second)
{
	std::sort(first.begin(), first.end());
	std::sort(second.begin(), second.end());
	int length = std::min(first.length(), second.length());
	std::string result(length, ' ');
	std::set_intersection(first.begin(), first.end(), second.begin(), second.end(), result.begin());
	return result;
}

int PlaceHorizontal(int x, int y, std::string &word)
{
	coord_x.push_back(x);
	coord_y.push_back(y);
	int taille = word.length();
	for (int j = 0; j != taille; j++)
	{
		if (grid[x][y + j] == '.' || grid[x][y + j] == word[j])
		{
			grid[x][y + j] = word[j];
		}
		else {}
	}
}

int PlaceVertical(int x, int y, std::string &word)
{
	coord_x.push_back(x);
	coord_y.push_back(y);
	int taille = word.length();
	for (int j = 0; j != taille; j++)
	{
		if (grid[x + j][y] == '.' || grid[x + j][y] == word[j])
		{
			grid[x + j][y] = word[j];
		}
		else {}
	}
}

std::string randomword(int randomline)
{
	std::string word1;
	std::string ifileName = "dictionary.txt";
	std::ifstream f(ifileName);

	if (!f) std::cerr << "File '" << ifileName << "' couldn't opened!\n";

	std::string s;

	for (int i = 1; i <= randomline; i++)
	{
		std::getline(f, s);
		word1 = s;

	}

	return word1;
}

int main(int argc, char *argv[])
{
	string word1;
	string word2;
	string word3;

	std::string temp1;
	std::string temp2;
	std::string temp3;
	int x_word1_intersection;
	int x_word2_intersection;
	int x_word23_intersection;
	int x_word32_intersection;

	srand(time(0));
	int x = 8;
	int y = 8;
	int indice1 = 0;
	int indice2 = 0;

	std::string intersec1;
	std::string intersec;
	std::string replace;

	int n;
	std::cout << "Number of words to be placed in the grid : " << "\n";
	std::cin >> n;
	int number;

	for (int i = 0; i < n; i++)
	{
		int rand1 = random(2000, 4000);
		word1 = randomword(rand1);
		words.push_back(word1);

	}

	PlaceVertical(x, y, words[0]);

	for (int i = 1; i < words.size(); i++)
	{
		if (i % 2 == 0)
		{
			temp1 = words[i - 1];
			temp2 = words[i];
			word2 = words[i];

			intersec = find_intersection(temp1, temp2);
			if (intersec.empty())
			{
				int rand2 = random(2000, 4000);
				replace = randomword(rand2);
				word2 = replace;
				std::replace(words.begin(), words.end(), temp2, replace);
				intersec = find_intersection(temp1, temp2);
			}

			words_hori.push_back(word2);
			if (i < 2)
			{
				indice2 = 0;
			}
			else
			{
				if (intersec[0] = intersec1[0] && intersec.length() > 0)
				{
					if (indice1 += 1 <= intersec.length())
					{
						indice2 += 1;
					}
				}
				else
				{
					indice2 = 0;
				}
			}

			x_word1_intersection = word1.find_last_of(intersec[indice2]);
			x_word2_intersection = word2.find_last_of(intersec[indice2]);

			PlaceHorizontal(coord_x[i - 1] + x_word2_intersection, coord_y[i - 1] - x_word1_intersection, word2);
		}

		if (i % 2 != 0)
		{
			temp2 = words[i - 1];
			temp3 = words[i];
			word3 = words[i];
			words_verti.push_back(word3);

			intersec1 = find_intersection(temp2, temp3);
			if (intersec1.empty())
			{
				int rand3 = random(2000, 4000);
				replace = randomword(rand3);
				word3 = replace;
				std::replace(words.begin(), words.end(), temp3, replace);
				intersec1 = find_intersection(temp2, temp3);
			}

			if (intersec[0] = intersec1[0] && intersec1.length() > 0)
			{
				if (indice1 += 1 <= intersec1.length())
				{
					indice1 += 1;
				}
			}
			else
			{
				indice1 = 0;
			}

			x_word23_intersection = word2.find_last_of(intersec1[indice1]);
			x_word32_intersection = word3.find_last_of(intersec1[indice1]);

			PlaceVertical(coord_x[i - 1] - x_word23_intersection, coord_y[i - 1] + x_word32_intersection, word3);
		}
	}

	for (int i = 0; i < 26; i++)
	{
		for (int j = 0; j < 26; j++)
		{
			std::cout << grid[i][j];
			std::cout << " ";
		}

		std::cout << "\n";
	}

	words.clear();
}
I haven't looked through all your code because its hard to read with all the plenty of defined variables and the lack of sufficient comments. But what came me at first to my eyes is, that you in your randomword() function open (and implicitly close) the file just for reading a single word, and later you use this function repeatedly for reading some words! I suggest, open and close your file only once and read the words to std::vector<std::string>>.

Also, you mix camelCase, PascalCase, snake_case or putting the words just together. I suggest sticking by one of them.

And, you use namespace std, but some of your stuff from std you prefix with std:: and some not. I suggest ommitin 'using namespace std' and force you, always prefix std:: where it will be needed.

And what do you aiming with line 266?

1
2
3
4
				if (indice1 += 1 <= intersec1.length())
				{
					indice1 += 1;
				}

You're incrementing indice1 and then comparing its value, then possibly incrementing it again. I assume you just want indice1 + 1 in the comparison, or something like that.

if (intersec[0] = intersec1[0] && intersec.length() > 0)
Did you mean == here instead of = (assignment)? And what's so special about the first index [0]?
Last edited on
For line 266 I aim to clean up the vector where I store the words to avoid having words stored in it at the next run. I don't know the variables mix camelCase, PascalCase, snake_case. Then for the using namespace I already tried to remove it but the program doesn't work without it anymore. For the index yes indeed I want to make index1 + 1 to allow the program to change the letter in the common letters found in the variable intersec or intersec1. For the loop, it is there to check that the common letter between word1 and word2 is not the same between word2 and the next word.
Last edited on
I made some corrections and added some comments. Indeed there was a problem with the indexes.
first part of the code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <string>
#include <algorithm>
#include <bits/stdc++.h>
#include <stdlib.h>
#include <sstream>
#include <ctime>
#include <vector>
using namespace std;
std::vector<double> coord_x;
std::vector<double> coord_y;
std::vector<std::string > words;

char grid[26][26] = {
		{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
	{
		'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' }
};
//Random number generating function
int random(int from, int to)
{
	return rand() % (to - from + 1) + from;
}

//Function searching for letters common to two words
std::string find_intersection(std::string &first, std::string &second)
{
	std::sort(first.begin(), first.end());
	std::sort(second.begin(), second.end());
	int length = std::min(first.length(), second.length());
	std::string result(length, ' ');
	std::set_intersection(first.begin(), first.end(), second.begin(), second.end(), result.begin());
	return result;
}

//Horizontal word placement function
int PlaceHorizontal(int x, int y, std::string &word)
{
	coord_x.push_back(x);
	coord_y.push_back(y);
	int taille = word.length();
	for (int j = 0; j != taille; j++)
	{
		if (grid[x][y + j] == '.' || grid[x][y + j] == word[j])
		{
			grid[x][y + j] = word[j];
		}
		else {}
	}
}

//Function placing words vertically
int PlaceVertical(int x, int y, std::string &word)
{
	coord_x.push_back(x);
	coord_y.push_back(y);
	int taille = word.length();
	for (int j = 0; j != taille; j++)
	{
		if (grid[x + j][y] == '.' || grid[x + j][y] == word[j])
		{
			grid[x + j][y] = word[j];
		}
		else {}
	}
}

//Function randomly retrieving words from the dictionary
std::string randomword(int randomline)
{
	std::string word1;
	std::string ifileName = "dictionary.txt";
	std::ifstream f(ifileName);

	if (!f) std::cerr << "File '" << ifileName << "' couldn't opened!\n";

	std::string s;

	for (int i = 1; i <= randomline; i++)
	{
		std::getline(f, s);
                std::transform(s.begin(), s.end(), s.begin(),[](unsigned char c){ return std::tolower(c); });
		word1 = s;

	}

	return word1;
}
Last edited on
second part of the code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
int main(int argc, char *argv[])
{
	string word1;
	string word2;
	string word3;

	std::string temp1;
	std::string temp2;
	std::string temp3;
	int x_word1_intersection;
	int x_word2_intersection;
	int x_word23_intersection;
	int x_word32_intersection;

	srand(time(0));
	int x = 8;
	int y = 8;
	int indice1 = 0;
	int indice2 = 0;

	std::string intersec1;
	std::string intersec;
	std::string replace;

	int n;
	std::cout << "Number of words to be placed in the grid : " << "\n";
	std::cin >> n;
	int number;
	//random word retrieval and addition to vector 
	for (int i = 0; i < n; i++)
	{
		int rand1 = random(2000, 4000);
		word1 = randomword(rand1);
		words.push_back(word1);

	}

	//first word placement 
	PlaceVertical(x, y, words[0]);
	intersec1 = "";

	for (int i = 1; i < words.size(); i++)
	{
		//alternation of word placement with even and odd numbers
		if (i % 2 == 0)
		{
			temp1 = words[i - 1];
			temp2 = words[i];
			word2 = words[i];
			//if there is no common letter between the two words running to look for a new word
			intersec = find_intersection(temp1, temp2);
			if (intersec.empty())
			{
				int rand2 = random(2000, 4000);
				replace = randomword(rand2);
				word2 = replace;
				std::replace(words.begin(), words.end(), temp2, replace);
				intersec = find_intersection(temp1, temp2);
			}

			if (i < 2)
			{
				indice2 = 0;
			}
			else
			{
				//if the letters common to the two previous words are the same as for the two current words change the common letter if possible

				if (intersec[0] == intersec1[0] && intersec.length() > 0)
				{
					if (indice2 + 1 <= intersec.length())
					{
						indice2 = indice2 + 1;
					}
				}
				else
				{
					indice2 = 0;
				}
			}

			x_word1_intersection = word1.find_last_of(intersec[indice2]);
			x_word2_intersection = word2.find_last_of(intersec[indice2]);

			PlaceHorizontal(coord_x[i - 1] + x_word2_intersection, coord_y[i - 1] - x_word1_intersection, word2);
		}
                //second part of the loop with the same processes
		if (i % 2 != 0)
		{
			temp2 = words[i - 1];
			temp3 = words[i];
			word3 = words[i];

			intersec1 = find_intersection(temp2, temp3);
			if (intersec1.empty())
			{
				int rand3 = random(2000, 4000);
				replace = randomword(rand3);
				word3 = replace;
				std::replace(words.begin(), words.end(), temp3, replace);
				intersec1 = find_intersection(temp2, temp3);
			}

			if (intersec[0] == intersec1[0] && intersec1.length() > 0)
			{
				if (indice1 + 1 <= intersec1.length())
				{
					indice1 = indice1 + 1;
				}
			}
			else
			{
				indice1 = 0;
			}

			x_word23_intersection = word2.find_last_of(intersec1[indice1]);
			x_word32_intersection = word3.find_last_of(intersec1[indice1]);

			PlaceVertical(coord_x[i - 1] - x_word23_intersection, coord_y[i - 1] + x_word32_intersection, word3);
		}
	}

	//grid view
	for (int i = 0; i < 26; i++)
	{
		for (int j = 0; j < 26; j++)
		{
			std::cout << grid[i][j];
			std::cout << " ";
		}

		std::cout << "\n";
	}

	words.clear();
}
Last edited on
I added a line to avoid capital letters because it disturbs the search for common letters :

std::transform(s.begin(), s.end(), s.begin(),[](unsigned char c){ return std::tolower(c); });
Try this (but it is untested)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <vector>
#include <string>

using Grid = std::vector<std::vector<char>>;

constexpr char empty = '.';

enum class Direction { hor, vert };

struct Pos { int x, y; };

/* Returns the matching position of a word in the grid. If there is no matching position
 * available or if the matching letters within the grid are smaller than
 * needed_matching_letters, the function returns a at pos.x a value of -1.
 */
Pos can_place_word
(
    const std::string & word
  , const int needed_matching_letters
  , const Direction dir
  , const Grid & grid
)
{
    bool can_place = true;
    int matching_letters = 0;

    Pos pos;

    if( dir == Direction::hor )
    {
        for( int offset_y = 0; offset_y < grid.size(); ++offset_y)
        {
            for( int offset_x = 0; offset_x < grid[0].size() + word.length(); ++offset_x)
            {
                for( int word_idx = 0; word_idx < word.length(); ++word_idx)
                {
                    char letter_in_grid = grid[ offset_y ][ offset_x + word_idx ];
                    if( letter_in_grid != empty && letter_in_grid != word[ word_idx ])
                    {
                        can_place = false;
                        break;
                    }
                    else if( letter_in_grid == word[ word_idx ] )
                        ++matching_letters;
                }
                if( can_place == true )
                {
                    pos.x = offset_x;
                    pos.y = offset_y;
                }
            }
        }
    }
    else  // direction must be vertical
    {
    //.... // similar as above
    }

    if( can_place == false || matching_letters < needed_matching_letters )
        pos.x = -1;
    return pos;
}
Last edited on
Hello to replace this part of my code? or replacing the whole and recoding everything? Can you explain the idea to me, please?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
for (int i = 1; i < words.size(); i++)
	{
		//alternation of word placement with even and odd numbers
		if (i % 2 == 0)
		{
			temp1 = words[i - 1];
			temp2 = words[i];
			word2 = words[i];
			//if there is no common letter between the two words running to look for a new word
			intersec = find_intersection(temp1, temp2);
			if (intersec.empty())
			{
				int rand2 = random(2000, 4000);
				replace = randomword(rand2);
				word2 = replace;
				std::replace(words.begin(), words.end(), temp2, replace);
				intersec = find_intersection(temp1, temp2);
			}

			if (i < 2)
			{
				indice2 = 0;
			}
			else
			{
				//if the letters common to the two previous words are the same as for the two current words change the common letter if possible

				if (intersec[0] == intersec1[0] && intersec.length() > 0)
				{
					if (indice2 + 1 <= intersec.length())
					{
						indice2 = indice2 + 1;
					}
				}
				else
				{
					indice2 = 0;
				}
			}

			x_word1_intersection = word1.find_last_of(intersec[indice2]);
			x_word2_intersection = word2.find_last_of(intersec[indice2]);

			PlaceHorizontal(coord_x[i - 1] + x_word2_intersection, coord_y[i - 1] - x_word1_intersection, word2);
		}
                //second part of the loop with the same processes
		if (i % 2 != 0)
		{
			temp2 = words[i - 1];
			temp3 = words[i];
			word3 = words[i];

			intersec1 = find_intersection(temp2, temp3);
			if (intersec1.empty())
			{
				int rand3 = random(2000, 4000);
				replace = randomword(rand3);
				word3 = replace;
				std::replace(words.begin(), words.end(), temp3, replace);
				intersec1 = find_intersection(temp2, temp3);
			}

			if (intersec[0] == intersec1[0] && intersec1.length() > 0)
			{
				if (indice1 + 1 <= intersec1.length())
				{
					indice1 = indice1 + 1;
				}
			}
			else
			{
				indice1 = 0;
			}

			x_word23_intersection = word2.find_last_of(intersec1[indice1]);
			x_word32_intersection = word3.find_last_of(intersec1[indice1]);

			PlaceVertical(coord_x[i - 1] - x_word23_intersection, coord_y[i - 1] + x_word32_intersection, word3);
		}
	}


thank you
Pages: 123... 6