On the way to Scrabble Solver

Hello all,
I've been trying to make a nice scrabble solver code,but indeed it ain't going as smooth as i expected.
There is a problem in the word_worth function. Don't really know where it is, but I guess it's in the first 20 lines of the function. Instead of the reckoned value of the word(like 20 or 34), I get numbers like 1375000 or something.
The function is for returning the value of a specific word at a specific place on the scrabble board. I'm following the rules of the game, so i have the board and the letters with values.
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
#include<iostream>
#include<fstream>
#include<queue>
#include<iomanip>
using namespace std;
char grid[15][15],tmp[15][15];
short board[15][15];
short i,j,x,y;
bool direction;
string word;
short word_worth(string word,short board[15][15],short x, short y, bool direction)
{
    short bonus[10];
    int value[10];
    int sum=0;
    if(direction==0)
    {
        i=0;
        for(j=y; j<word.size()+y; j++)
        {
            bonus[i]=board[x][j];
            i++;
        }
    }
    else
    {
        j=0;
        for(i=x; i<word.size()+x; i++)
        {
            bonus[i]=board[i][y];
            j++;
        }
    }
    for(i=0; i<word.size(); i++)
    {
        if(bonus[i]==0 )
        {
            if(word[i]=='q' or word[i]=='z' )value[i]=10;
            else if(word[i]=='j' or word[i]=='x')value[i]=8;
            else if(word[i]=='k')value[i]=5;
            else if(word[i]=='f' or word[i]=='h' or word[i]=='w' or word[i]=='v' or word[i]=='y')value[i]=4;
            else if(word[i]=='b' or word[i]=='c' or word[i]=='m' or word[i]=='p')value[i]=3;
            else if(word[i]=='d' or word[i]=='g')value[i]=2;
            else
                value[i]=1;
        }
        else if(bonus[i]==3)
        {
            if(word[i]=='q' or word[i]=='z' )value[i]=10*3;
            else if(word[i]=='j' or word[i]=='x')value[i]=8*3;
            else if(word[i]=='k')value[i]=5*3;
            else if(word[i]=='f' or word[i]=='h' or word[i]=='w' or word[i]=='v' or word[i]=='y')value[i]=4*3;
            else if(word[i]=='b' or word[i]=='c' or word[i]=='m' or word[i]=='p')value[i]=3*3;
            else if(word[i]=='d' or word[i]=='g')value[i]=2*3;
            else value[i]=1*3;
        }
        else if(bonus[i]==4)
        {
            if(word[i]=='q' or word[i]=='z' )value[i]=10*2;
            else if(word[i]=='j' or word[i]=='x')value[i]=8*2;
            else if(word[i]=='k')value[i]=5*2;
            else if(word[i]=='f' or word[i]=='h' or word[i]=='w' or word[i]=='v' or word[i]=='y')value[i]=4*2;
            else if(word[i]=='b' or word[i]=='c' or word[i]=='m' or word[i]=='p')value[i]=3*2;
            else if(word[i]=='d' or word[i]=='g')value[i]=2*2;
            else value[i]=1*2;
        }
    }
    for(i=0; i<word.size(); i++)
    {
        sum+=value[i];
        
    }
    for(i=0; i<word.size(); i++)
    {
        if(bonus[i]==1)
        {
            sum*=2;
        }
        if(bonus[i]==2)
        {
            sum*=3;
        }
    }
    return sum;
}
void draw_board()
{
    ///setting the board to 0
    for(i=0; i<15; i++)
    {
        for(j=0; j<15; j++)
        {
            board[i][j]=0;
        }
    }
    ///setting the two diagonals to 1 ->DW
    for(i=0; i<15; i++)
    {
        for(j=0; j<15; j++)
        {
            if(i==j or i==15-j-1)
            {
                board[i][j]=1;
            }
        }
    }
    ///setting the TW to 2
    for(i=0; i<15; i+=7)
    {
        for(j=0; j<15; j+=7)
        {
            board[i][j]=2;
        }
    }
    board[7][7]=1;
///setting the TL to 3
    for(i=1; i<6; i+=4)
    {
        for(j=5; j<10; j+=4)
        {
            board[i][j]=3;
            board[j][i]=3;
        }
    }
    for(i=9; i<5+9; i+=4)
    {
        for(j=5; j<5+10; j+=4)
        {
            board[i][j]=3;
            board[j][i]=3;
        }
    }
///setting the DL to 4
    for(i=0; i<15; i+=7)
    {
        for(j=3; j<12; j+=8)
        {
            board[i][j]=4;
            board[j][i]=4;
        }
    }
    for(i=2; i<7; i+=4)
    {
        for(j=6; j<9; j+=2)
        {
            board[i][j]=4;
            board[j][i]=4;
        }
    }
    for(i=2+6; i<6+7; i+=4)
    {
        for(j=6; j<9; j+=2)
        {
            board[i][j]=4;
            board[j][i]=4;
        }
    }
}
bool does_the_word_exist(string word1)
{
    ifstream myFile("dictionary.txt");
    string word2;
    bool br=0;
    while(!myFile.eof())
    {
        myFile>>word2;
        if(word1==word2)
        {
            br++;
            break;
        }
    }
    myFile.close();
    if(br)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

void undo()
{
    for(i=0; i<15; i++)
    {
        for(j=0; j<15; j++)
        {
            grid[i][j]=tmp[i][j];
        }
    }
}
void save()
{
    for(i=0; i<15; i++)
    {
        for(j=0; j<15; j++)
        {
            tmp[i][j]=grid[i][j];
        }
    }
}
void show_grid()
{
    for(i=0; i<15; i++)
    {
        for(j=0; j<15; j++)
        {
            cout<<setw(2)<<grid[i][j];
        }
        cout<<endl;
    }
}
void show_board()
{
    for(i=0; i<15; i++)
    {
        for(j=0; j<15; j++)
        {
            cout<<setw(2)<<board[i][j];
        }
        cout<<endl;
    }
}

int main()
{
    ///setting the grid to '-'
    for(i=0; i<15; i++)
    {
        for(j=0; j<15; j++)
        {
            grid[i][j]='-';
        }
    }
    draw_board();
    show_board();
    cout<<"Enter X, Y and the DIRECTION:";
    cin>>x>>y>>direction;
    cout<<"Enter word:";
    cin>>word;
    if(does_the_word_exist(word))
    {
        save();
        if(direction==0)
        {
            i=0;
            for(j=y; j<word.size()+y; j++)
            {
                grid[x][j]=word[i];
                i++;
            }
        }
        else
        {
            j=0;
            for(i=x; i<word.size()+x; i++)
            {
                grid[i][y]=word[j];
                j++;
            }
        }
        show_grid();
        cout<<word_worth(word,board,x,y,direction);
    }
}


Last edited on
for that function i think you are going out of bound with the array, therefore, producing those numbers. for(j=y; j<word.size()+y; j++) in line 19.
Last edited on
Topic archived. No new replies allowed.